|
Lines 15-20
Link Here
|
| 15 |
import org.eclipse.emf.cdo.common.id.CDOID; |
15 |
import org.eclipse.emf.cdo.common.id.CDOID; |
| 16 |
import org.eclipse.emf.cdo.common.model.CDOModelUtil; |
16 |
import org.eclipse.emf.cdo.common.model.CDOModelUtil; |
| 17 |
import org.eclipse.emf.cdo.common.model.CDOPackageRegistry; |
17 |
import org.eclipse.emf.cdo.common.model.CDOPackageRegistry; |
|
|
18 |
import org.eclipse.emf.cdo.common.model.CDOType; |
| 18 |
import org.eclipse.emf.cdo.common.protocol.CDOProtocolConstants; |
19 |
import org.eclipse.emf.cdo.common.protocol.CDOProtocolConstants; |
| 19 |
import org.eclipse.emf.cdo.common.revision.CDORevision; |
20 |
import org.eclipse.emf.cdo.common.revision.CDORevision; |
| 20 |
import org.eclipse.emf.cdo.eresource.CDOResource; |
21 |
import org.eclipse.emf.cdo.eresource.CDOResource; |
|
Lines 28-36
Link Here
|
| 28 |
import org.eclipse.net4j.util.om.trace.ContextTracer; |
29 |
import org.eclipse.net4j.util.om.trace.ContextTracer; |
| 29 |
|
30 |
|
| 30 |
import org.eclipse.emf.common.notify.Adapter; |
31 |
import org.eclipse.emf.common.notify.Adapter; |
| 31 |
import org.eclipse.emf.common.notify.Notification; |
|
|
| 32 |
import org.eclipse.emf.common.notify.impl.NotifyingListImpl; |
32 |
import org.eclipse.emf.common.notify.impl.NotifyingListImpl; |
| 33 |
import org.eclipse.emf.common.util.URI; |
33 |
import org.eclipse.emf.common.util.URI; |
|
|
34 |
import org.eclipse.emf.ecore.EAttribute; |
| 34 |
import org.eclipse.emf.ecore.EClass; |
35 |
import org.eclipse.emf.ecore.EClass; |
| 35 |
import org.eclipse.emf.ecore.EClassifier; |
36 |
import org.eclipse.emf.ecore.EClassifier; |
| 36 |
import org.eclipse.emf.ecore.EObject; |
37 |
import org.eclipse.emf.ecore.EObject; |
|
Lines 46-51
Link Here
|
| 46 |
import java.lang.reflect.InvocationHandler; |
47 |
import java.lang.reflect.InvocationHandler; |
| 47 |
import java.lang.reflect.Method; |
48 |
import java.lang.reflect.Method; |
| 48 |
import java.lang.reflect.Proxy; |
49 |
import java.lang.reflect.Proxy; |
|
|
50 |
import java.util.HashMap; |
| 51 |
import java.util.Map; |
| 49 |
|
52 |
|
| 50 |
/** |
53 |
/** |
| 51 |
* @author Eike Stepper |
54 |
* @author Eike Stepper |
|
Lines 53-588
Link Here
|
| 53 |
*/ |
56 |
*/ |
| 54 |
public abstract class CDOLegacyWrapper extends CDOObjectWrapper |
57 |
public abstract class CDOLegacyWrapper extends CDOObjectWrapper |
| 55 |
{ |
58 |
{ |
| 56 |
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_OBJECT, CDOLegacyWrapper.class); |
59 |
//private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_OBJECT, CDOLegacyWrapper.class); |
| 57 |
|
60 |
|
| 58 |
protected CDOState state; |
61 |
//TODO Martin: with OM.DEBUG_VIEW I managed activating traces. Should ask Eike how the tracing works in general. |
|
|
62 |
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG_VIEW, CDOLegacyWrapper.class); |
| 59 |
|
63 |
|
| 60 |
protected InternalCDORevision revision; |
64 |
protected CDOState state; |
| 61 |
|
65 |
|
| 62 |
public CDOLegacyWrapper(InternalEObject instance) |
66 |
protected InternalCDORevision revision; |
| 63 |
{ |
67 |
|
| 64 |
this.instance = instance; |
68 |
/** |
| 65 |
state = CDOState.TRANSIENT; |
69 |
* @since 3.0 |
| 66 |
} |
70 |
* @description It could happen the while <i>revisionToInstance()</i> is |
| 67 |
|
71 |
* executed externally the <i>internalPostLoad()</i> method will |
| 68 |
public CDOState cdoState() |
72 |
* be called. This happens for example if |
| 69 |
{ |
73 |
* <i>internalPostInvalidate()</i> is called. The leads to |
| 70 |
return state; |
74 |
* another <i>revisionToInstance()</i> call while the first call |
| 71 |
} |
75 |
* has not finished. This is certainly not so cool. That's why |
| 72 |
|
76 |
* <b>underConstruction</b> will flag that |
| 73 |
public InternalCDORevision cdoRevision() |
77 |
* <i>revisionToInstance()</i> is still running and avoid the |
| 74 |
{ |
78 |
* second call. |
| 75 |
return revision; |
79 |
*/ |
| 76 |
} |
80 |
private boolean underConstruction = false; |
| 77 |
|
81 |
|
| 78 |
public void cdoReload() |
82 |
/** |
| 79 |
{ |
83 |
* This local ThreadMap stores all pre-registered objects. This avoids a |
| 80 |
CDOStateMachine.INSTANCE.reload(this); |
84 |
* neverending loop when setting the container for the object |
| 81 |
} |
85 |
*/ |
| 82 |
|
86 |
private static ThreadLocal<Map<CDOID, EObject>> localThread = new InheritableThreadLocal<Map<CDOID, EObject>>() |
| 83 |
public CDOState cdoInternalSetState(CDOState state) |
87 |
{ |
| 84 |
{ |
88 |
@Override |
| 85 |
if (this.state != state) |
89 |
protected synchronized Map<CDOID, EObject> initialValue() |
| 86 |
{ |
90 |
{ |
| 87 |
if (TRACER.isEnabled()) |
91 |
return new HashMap<CDOID, EObject>(); |
| 88 |
{ |
92 |
} |
| 89 |
TRACER.format("Setting state {0} for {1}", state, this); //$NON-NLS-1$ |
93 |
}; |
| 90 |
} |
94 |
|
| 91 |
|
95 |
//TODO Martin: threadCount could be removed if clearing the ThreadMap is not necessary |
| 92 |
CDOState oldState = this.state; |
96 |
private static Integer threadCount = 0; |
| 93 |
this.state = state; |
97 |
|
| 94 |
adjustEProxy(); |
98 |
public CDOLegacyWrapper(InternalEObject instance) |
| 95 |
if (view != null) |
99 |
{ |
| 96 |
{ |
100 |
this.instance = instance; |
| 97 |
view.handleObjectStateChanged(this, oldState, state); |
101 |
state = CDOState.TRANSIENT; |
| 98 |
} |
102 |
|
| 99 |
|
103 |
} |
| 100 |
return oldState; |
104 |
|
| 101 |
} |
105 |
public CDOState cdoState() |
| 102 |
|
106 |
{ |
| 103 |
return null; |
107 |
return state; |
| 104 |
} |
108 |
} |
| 105 |
|
109 |
|
| 106 |
public void cdoInternalSetRevision(CDORevision revision) |
110 |
public InternalCDORevision cdoRevision() |
| 107 |
{ |
111 |
{ |
| 108 |
if (TRACER.isEnabled()) |
112 |
return revision; |
| 109 |
{ |
113 |
} |
| 110 |
TRACER.format("Setting revision: {0}", revision); //$NON-NLS-1$ |
114 |
|
| 111 |
} |
115 |
public void cdoReload() |
| 112 |
|
116 |
{ |
| 113 |
this.revision = (InternalCDORevision)revision; |
117 |
CDOStateMachine.INSTANCE.reload(this); |
| 114 |
} |
118 |
} |
| 115 |
|
119 |
|
| 116 |
public void cdoInternalPostAttach() |
120 |
public CDOState cdoInternalSetState(CDOState state) |
| 117 |
{ |
121 |
{ |
| 118 |
instanceToRevision(); |
122 |
if (this.state != state) |
| 119 |
|
123 |
{ |
| 120 |
// TODO Avoid if no adapters in list (eBasicAdapters?) |
124 |
if (TRACER.isEnabled()) |
| 121 |
// TODO LEGACY Clarify how to intercept adapter addition in the legacy instance |
125 |
{ |
| 122 |
for (Adapter adapter : eAdapters()) |
126 |
TRACER.format("Setting state {0} for {1}", state, this); //$NON-NLS-1$ |
| 123 |
{ |
127 |
} |
| 124 |
view.subscribe(this, adapter); |
128 |
|
| 125 |
} |
129 |
CDOState oldState = this.state; |
| 126 |
} |
130 |
this.state = state; |
| 127 |
|
131 |
adjustEProxy(); |
| 128 |
public void cdoInternalPostDetach(boolean remote) |
132 |
if (view != null) |
| 129 |
{ |
133 |
{ |
| 130 |
// Do nothing |
134 |
view.handleObjectStateChanged(this, oldState, state); |
| 131 |
} |
135 |
} |
| 132 |
|
136 |
|
| 133 |
public void cdoInternalPreCommit() |
137 |
return oldState; |
| 134 |
{ |
138 |
} |
| 135 |
// instanceToRevision(); |
139 |
|
| 136 |
// if (cdoState() == CDOState.DIRTY) // NEW is handled in PrepareTransition |
140 |
return null; |
| 137 |
// { |
141 |
} |
| 138 |
// CDORevisionManagerImpl revisionManager = (CDORevisionManagerImpl)cdoView().getSession().getRevisionManager(); |
142 |
|
| 139 |
// InternalCDORevision originRevision = revisionManager.getRevisionByVersion(revision.getID(), |
143 |
public void cdoInternalSetRevision(CDORevision revision) |
| 140 |
// CDORevision.UNCHUNKED, revision.getVersion() - 1, false); |
144 |
{ |
| 141 |
// CDORevisionDelta delta = revision.compare(originRevision); |
145 |
|
| 142 |
// |
146 |
trace("Setting revision: " + revision + ""); |
| 143 |
// // TODO LEGACY Consider to gather the deltas on the fly with noremal EMF change notifications |
147 |
this.revision = (InternalCDORevision) revision; |
| 144 |
// cdoView().toTransaction().registerRevisionDelta(delta); |
148 |
} |
| 145 |
// } |
149 |
|
| 146 |
} |
150 |
public void cdoInternalPostAttach() |
| 147 |
|
151 |
{ |
| 148 |
public void cdoInternalPreLoad() |
152 |
instanceToRevision(); |
| 149 |
{ |
153 |
|
| 150 |
} |
154 |
// TODO Avoid if no adapters in list (eBasicAdapters?) |
| 151 |
|
155 |
// TODO LEGACY Clarify how to intercept adapter addition in the legacy instance |
| 152 |
public void cdoInternalPostLoad() |
156 |
for (Adapter adapter : eAdapters()) |
| 153 |
{ |
157 |
{ |
| 154 |
// TODO Consider not remembering the revisin after copying it to the instance (spare 1/2 of the space) |
158 |
view.subscribe(this, adapter); |
| 155 |
revisionToInstance(); |
159 |
} |
| 156 |
} |
160 |
} |
| 157 |
|
161 |
|
| 158 |
public void cdoInternalPostInvalidate() |
162 |
public void cdoInternalPostDetach(boolean remote) |
| 159 |
{ |
163 |
{ |
| 160 |
} |
164 |
|
| 161 |
|
165 |
} |
| 162 |
public void cdoInternalCleanup() |
166 |
|
| 163 |
{ |
167 |
public void cdoInternalPreCommit() |
| 164 |
} |
168 |
{ |
| 165 |
|
169 |
// instanceToRevision(); |
| 166 |
@Override |
170 |
// if (cdoState() == CDOState.DIRTY) // NEW is handled in PrepareTransition |
| 167 |
public boolean equals(Object obj) |
171 |
// { |
| 168 |
{ |
172 |
// CDORevisionManagerImpl revisionManager = (CDORevisionManagerImpl)cdoView().getSession().getRevisionManager(); |
| 169 |
return obj == this || obj == instance; |
173 |
// InternalCDORevision originRevision = revisionManager.getRevisionByVersion(revision.getID(), |
| 170 |
} |
174 |
// CDORevision.UNCHUNKED, revision.getVersion() - 1, false); |
| 171 |
|
175 |
// CDORevisionDelta delta = revision.compare(originRevision); |
| 172 |
@Override |
176 |
// |
| 173 |
public int hashCode() |
177 |
// // TODO LEGACY Consider to gather the deltas on the fly with noremal EMF change notifications |
| 174 |
{ |
178 |
// cdoView().toTransaction().registerRevisionDelta(delta); |
| 175 |
if (instance != null) |
179 |
// } |
| 176 |
{ |
180 |
} |
| 177 |
return instance.hashCode(); |
181 |
|
| 178 |
} |
182 |
public void cdoInternalPreLoad() |
| 179 |
|
183 |
{ |
| 180 |
return super.hashCode(); |
184 |
|
| 181 |
} |
185 |
} |
| 182 |
|
186 |
|
| 183 |
@Override |
187 |
public void cdoInternalPostLoad() |
| 184 |
public String toString() |
188 |
{ |
| 185 |
{ |
189 |
|
| 186 |
return "CDOLegacyWrapper[" + id + "]"; //$NON-NLS-1$ //$NON-NLS-2$ |
190 |
// TODO Consider not remembering the revisin after copying it to the instance (spare 1/2 of the space) |
| 187 |
} |
191 |
revisionToInstance(); |
| 188 |
|
192 |
|
| 189 |
protected void instanceToRevision() |
193 |
} |
| 190 |
{ |
194 |
|
| 191 |
if (TRACER.isEnabled()) |
195 |
public void cdoInternalPostInvalidate() |
| 192 |
{ |
196 |
{ |
| 193 |
TRACER.format("Transfering instance to revision: {0} --> {1}", instance, revision); //$NON-NLS-1$ |
197 |
|
| 194 |
} |
198 |
InternalCDORevision revision = cdoView().getRevision(cdoID(), true); |
| 195 |
|
199 |
cdoInternalSetRevision(revision); |
| 196 |
// Handle containment |
200 |
revisionToInstance(); |
| 197 |
instanceToRevisionContainment(); |
201 |
cdoInternalSetState(CDOState.CLEAN); |
| 198 |
|
202 |
|
| 199 |
// Handle values |
203 |
} |
| 200 |
CDOPackageRegistry packageRegistry = cdoView().getSession().getPackageRegistry(); |
204 |
|
| 201 |
EClass eClass = revision.getEClass(); |
205 |
public void cdoInternalCleanup() |
| 202 |
for (EStructuralFeature feature : CDOModelUtil.getAllPersistentFeatures(eClass)) |
206 |
{ |
| 203 |
{ |
207 |
|
| 204 |
instanceToRevisionFeature(feature, packageRegistry); |
208 |
} |
| 205 |
} |
209 |
|
| 206 |
} |
210 |
@Override |
| 207 |
|
211 |
public boolean equals(Object obj) |
| 208 |
protected void instanceToRevisionContainment() |
212 |
{ |
| 209 |
{ |
213 |
return obj == this || obj == instance; |
| 210 |
CDOResource resource = (CDOResource)getInstanceResource(instance); |
214 |
} |
| 211 |
revision.setResourceID(resource == null ? CDOID.NULL : resource.cdoID()); |
215 |
|
| 212 |
|
216 |
@Override |
| 213 |
InternalEObject eContainer = getInstanceContainer(instance); |
217 |
public int hashCode() |
| 214 |
if (eContainer == null) |
218 |
{ |
| 215 |
{ |
219 |
if (instance != null) |
| 216 |
revision.setContainerID(CDOID.NULL); |
220 |
{ |
| 217 |
revision.setContainingFeatureID(0); |
221 |
return instance.hashCode(); |
| 218 |
} |
222 |
} |
| 219 |
else |
223 |
|
| 220 |
{ |
224 |
return super.hashCode(); |
| 221 |
CDOObject cdoContainer = FSMUtil.adapt(eContainer, view); |
225 |
} |
| 222 |
revision.setContainerID(cdoContainer.cdoID()); |
226 |
|
| 223 |
revision.setContainingFeatureID(getInstanceContainerFeatureID(instance)); |
227 |
@Override |
| 224 |
} |
228 |
public String toString() |
| 225 |
} |
229 |
{ |
| 226 |
|
230 |
return "CDOLegacyWrapper[" + id + "]"; |
| 227 |
protected void instanceToRevisionFeature(EStructuralFeature feature, CDOPackageRegistry packageRegistry) |
231 |
} |
| 228 |
{ |
232 |
|
| 229 |
Object instanceValue = getInstanceValue(instance, feature, packageRegistry); |
233 |
protected void instanceToRevision() |
| 230 |
CDOObjectImpl.instanceToRevisionFeature(view, this, feature, instanceValue); |
234 |
{ |
| 231 |
} |
235 |
if (TRACER.isEnabled()) |
| 232 |
|
236 |
{ |
| 233 |
/** |
237 |
TRACER.format("Transfering instance to revision: {0} --> {1}", instance, revision); |
| 234 |
* TODO Simon: Fix this whole mess ;-) |
238 |
} |
| 235 |
*/ |
239 |
|
| 236 |
protected void revisionToInstance() |
240 |
// Handle containment |
| 237 |
{ |
241 |
instanceToRevisionContainment(); |
| 238 |
if (TRACER.isEnabled()) |
242 |
|
| 239 |
{ |
243 |
// Handle values |
| 240 |
TRACER.format("Transfering revision to instance: {0} --> {1}", revision, instance); //$NON-NLS-1$ |
244 |
CDOPackageRegistry packageRegistry = cdoView().getSession().getPackageRegistry(); |
| 241 |
} |
245 |
EClass eClass = revision.getEClass(); |
| 242 |
|
246 |
for (EStructuralFeature feature : CDOModelUtil.getAllPersistentFeatures(eClass)) |
| 243 |
boolean deliver = instance.eDeliver(); |
247 |
{ |
| 244 |
if (deliver) |
248 |
instanceToRevisionFeature(feature, packageRegistry); |
| 245 |
{ |
249 |
} |
| 246 |
instance.eSetDeliver(false); |
250 |
} |
| 247 |
} |
251 |
|
| 248 |
|
252 |
protected void instanceToRevisionContainment() |
| 249 |
try |
253 |
{ |
| 250 |
{ |
254 |
CDOResource resource = (CDOResource) getInstanceResource(instance); |
| 251 |
// Handle containment |
255 |
revision.setResourceID(resource == null ? CDOID.NULL : resource.cdoID()); |
| 252 |
revisionToInstanceContainment(); |
256 |
|
| 253 |
|
257 |
InternalEObject eContainer = getInstanceContainer(instance); |
| 254 |
// Handle values |
258 |
if (eContainer == null) |
| 255 |
CDOPackageRegistry packageRegistry = cdoView().getSession().getPackageRegistry(); |
259 |
{ |
| 256 |
EClass eClass = revision.getEClass(); |
260 |
revision.setContainerID(CDOID.NULL); |
| 257 |
for (EStructuralFeature feature : CDOModelUtil.getAllPersistentFeatures(eClass)) |
261 |
revision.setContainingFeatureID(0); |
| 258 |
{ |
262 |
} |
| 259 |
revisionToInstanceFeature(feature, packageRegistry); |
263 |
else |
| 260 |
} |
264 |
{ |
| 261 |
} |
265 |
CDOObject cdoContainer = FSMUtil.adapt(eContainer, view); |
| 262 |
finally |
266 |
revision.setContainerID(cdoContainer.cdoID()); |
| 263 |
{ |
267 |
revision.setContainingFeatureID(getInstanceContainerFeatureID(instance)); |
| 264 |
if (deliver) |
268 |
} |
| 265 |
{ |
269 |
} |
| 266 |
instance.eSetDeliver(true); |
270 |
|
| 267 |
} |
271 |
protected void instanceToRevisionFeature(EStructuralFeature feature, CDOPackageRegistry packageRegistry) |
| 268 |
} |
272 |
{ |
| 269 |
} |
273 |
|
| 270 |
|
274 |
Object instanceValue = getInstanceValue(instance, feature, packageRegistry); |
| 271 |
protected void revisionToInstanceContainment() |
275 |
CDOObjectImpl.instanceToRevisionFeature(view, this, feature, instanceValue); |
| 272 |
{ |
276 |
} |
| 273 |
CDOID resourceID = revision.getResourceID(); |
277 |
|
| 274 |
InternalEObject resource = getEObjectFromPotentialID(view, null, resourceID); |
278 |
/** |
| 275 |
setInstanceResource((Resource.Internal)resource); |
279 |
* TODO Simon: Fix this whole mess ;-) |
| 276 |
|
280 |
*/ |
| 277 |
Object containerID = revision.getContainerID(); |
281 |
protected void revisionToInstance() |
| 278 |
InternalEObject container = getEObjectFromPotentialID(view, null, containerID); |
282 |
{ |
| 279 |
setInstanceContainer(container, revision.getContainingFeatureID()); |
283 |
|
| 280 |
} |
284 |
if (isUnderConstruction()) //return if revisionToInstance was called befor to avoid doubled calls |
| 281 |
|
285 |
{ |
| 282 |
protected void revisionToInstanceFeature(EStructuralFeature feature, CDOPackageRegistry packageRegistry) |
286 |
return; |
| 283 |
{ |
287 |
} |
| 284 |
// Attempt 4 |
288 |
setUnderConstruction(true); |
| 285 |
Object value = revision.getValue(feature); |
289 |
|
| 286 |
view.getStore().set(instance, feature, Notification.NO_INDEX, value); |
290 |
if (TRACER.isEnabled()) |
| 287 |
} |
291 |
{ |
| 288 |
|
292 |
TRACER.format("Transfering revision to instance: {0} --> {1}", revision, instance); |
| 289 |
protected Resource.Internal getInstanceResource(InternalEObject instance) |
293 |
} |
| 290 |
{ |
294 |
boolean deliver = instance.eDeliver(); |
| 291 |
return instance.eDirectResource(); |
295 |
if (deliver) |
| 292 |
} |
296 |
{ |
| 293 |
|
297 |
instance.eSetDeliver(false); |
| 294 |
protected InternalEObject getInstanceContainer(InternalEObject instance) |
298 |
} |
| 295 |
{ |
299 |
|
| 296 |
return instance.eInternalContainer(); |
300 |
try |
| 297 |
} |
301 |
{ |
| 298 |
|
302 |
// Handle containment |
| 299 |
protected int getInstanceContainerFeatureID(InternalEObject instance) |
303 |
|
| 300 |
{ |
304 |
preRegisterObject(this); |
| 301 |
return instance.eContainerFeatureID(); |
305 |
incrementThreadCount(); |
| 302 |
} |
306 |
|
| 303 |
|
307 |
trace("Thread " + localThread + " MAP: (" + instance + ") {" + getPreRegisteredObjects() + "}"); |
| 304 |
protected Object getInstanceValue(InternalEObject instance, EStructuralFeature feature, |
308 |
revisionToInstanceContainment(); |
| 305 |
CDOPackageRegistry packageRegistry) |
309 |
|
| 306 |
{ |
310 |
// Handle values |
| 307 |
return instance.eGet(feature); |
311 |
//CDOPackageRegistry packageRegistry = cdoView().getSession().getPackageRegistry(); |
| 308 |
} |
312 |
EClass eClass = revision.getEClass(); |
| 309 |
|
313 |
for (EStructuralFeature feature : CDOModelUtil.getAllPersistentFeatures(eClass)) |
| 310 |
protected void setInstanceResource(Resource.Internal resource) |
314 |
{ |
| 311 |
{ |
315 |
revisionToInstanceFeature(feature); |
| 312 |
Method method = ReflectUtil.getMethod(instance.getClass(), "eSetDirectResource", Resource.Internal.class); //$NON-NLS-1$ |
316 |
} |
| 313 |
ReflectUtil.invokeMethod(method, instance, resource); |
317 |
} |
| 314 |
} |
318 |
catch (Exception e) |
| 315 |
|
319 |
{ |
| 316 |
protected void setInstanceContainer(InternalEObject container, int containerFeatureID) |
320 |
e.printStackTrace(); |
| 317 |
{ |
321 |
} |
| 318 |
Method method = ReflectUtil.getMethod(instance.getClass(), "eBasicSetContainer", InternalEObject.class, int.class); //$NON-NLS-1$ |
322 |
finally |
| 319 |
ReflectUtil.invokeMethod(method, instance, container, containerFeatureID); |
323 |
{ |
| 320 |
} |
324 |
if (deliver) |
| 321 |
|
325 |
{ |
| 322 |
protected void setInstanceValue(InternalEObject instance, EStructuralFeature feature, Object value) |
326 |
instance.eSetDeliver(true); |
| 323 |
{ |
327 |
} |
| 324 |
instance.eSet(feature, value); |
328 |
decrementThreadCount(); |
| 325 |
} |
329 |
if (getPreRegisteredObjects() != null) |
| 326 |
|
330 |
{ |
| 327 |
/** |
331 |
if (getThreadCount() == 0) |
| 328 |
* @param feature |
332 |
{ |
| 329 |
* in case that a proxy has to be created the feature that will determine the interface type of the proxy and |
333 |
|
| 330 |
* that will be used later to resolve the proxy. <code>null</code> indicates that proxy creation will be |
334 |
//localThread.remove(); // TODO Martin: check why new objects will be created if this list is cleared |
| 331 |
* avoided! |
335 |
|
| 332 |
*/ |
336 |
} |
| 333 |
protected InternalEObject getEObjectFromPotentialID(InternalCDOView view, EStructuralFeature feature, |
337 |
} |
| 334 |
Object potentialID) |
338 |
setUnderConstruction(false); |
| 335 |
{ |
339 |
} |
| 336 |
if (potentialID instanceof CDOID) |
340 |
|
| 337 |
{ |
341 |
} |
| 338 |
CDOID id = (CDOID)potentialID; |
342 |
|
| 339 |
if (id.isNull()) |
343 |
private void decrementThreadCount() |
| 340 |
{ |
344 |
{ |
| 341 |
return null; |
345 |
threadCount--; |
| 342 |
} |
346 |
} |
| 343 |
|
347 |
|
| 344 |
boolean loadOnDemand = feature == null; |
348 |
private void incrementThreadCount() |
| 345 |
potentialID = view.getObject(id, loadOnDemand); |
349 |
{ |
| 346 |
if (potentialID == null && !loadOnDemand) |
350 |
threadCount++; |
| 347 |
{ |
351 |
} |
| 348 |
return createProxy(view, feature, id); |
352 |
|
| 349 |
} |
353 |
private Integer getThreadCount() |
| 350 |
} |
354 |
{ |
| 351 |
|
355 |
return threadCount; |
| 352 |
if (potentialID instanceof InternalCDOObject) |
356 |
} |
| 353 |
{ |
357 |
|
| 354 |
return ((InternalCDOObject)potentialID).cdoInternalInstance(); |
358 |
/** |
| 355 |
} |
359 |
* adds an object to the pre-registered objects list which hold all created |
| 356 |
|
360 |
* objects even if they are not registered in the view |
| 357 |
return (InternalEObject)potentialID; |
361 |
*/ |
| 358 |
} |
362 |
private void preRegisterObject(CDOLegacyWrapper wrapper) |
| 359 |
|
363 |
{ |
| 360 |
/** |
364 |
getPreRegisteredObjects().put(wrapper.cdoID(), wrapper); |
| 361 |
* Creates and returns a <em>proxy</em> object. The usage of a proxy object is strongly limited. The only guarantee |
365 |
} |
| 362 |
* that can be made is that the following methods are callable and will behave in the expected way: |
366 |
|
| 363 |
* <ul> |
367 |
/** |
| 364 |
* <li>{@link CDOObject#cdoID()} will return the {@link CDOID} of the target object |
368 |
* |
| 365 |
* <li>{@link CDOObject#cdoState()} will return {@link CDOState#PROXY PROXY} |
369 |
* |
| 366 |
* <li>{@link InternalEObject#eIsProxy()} will return <code>true</code> |
370 |
*/ |
| 367 |
* <li>{@link InternalEObject#eProxyURI()} will return the EMF proxy URI of the target object |
371 |
protected void revisionToInstanceContainment() |
| 368 |
* </ul> |
372 |
{ |
| 369 |
* Calling any other method on the proxy object will result in an {@link UnsupportedOperationException} being thrown |
373 |
|
| 370 |
* at runtime. Note also that the proxy object might even not be cast to the concrete type of the target object. The |
374 |
CDOID resourceID = revision.getResourceID(); |
| 371 |
* proxy can only guaranteed to be of <em>any</em> concrete subtype of the declared type of the given feature. |
375 |
InternalEObject resource = getEObjectFromPotentialID(view, null, resourceID); |
| 372 |
* <p> |
376 |
setInstanceResource((Resource.Internal) resource); |
| 373 |
* TODO {@link InternalEObject#eResolveProxy(InternalEObject) |
377 |
|
| 374 |
*/ |
378 |
Object containerID = revision.getContainerID(); |
| 375 |
protected InternalEObject createProxy(InternalCDOView view, EStructuralFeature feature, CDOID id) |
379 |
InternalEObject container; |
| 376 |
{ |
380 |
|
| 377 |
EClassifier eType = feature.getEType(); |
381 |
container = getEObjectFromPotentialID(view, null, containerID); |
| 378 |
Class<?> instanceClass = eType.getInstanceClass(); |
382 |
|
| 379 |
|
383 |
setInstanceContainer(container, revision.getContainingFeatureID()); |
| 380 |
Class<?>[] interfaces = { instanceClass, InternalEObject.class, LegacyProxy.class }; |
384 |
|
| 381 |
ClassLoader classLoader = CDOLegacyWrapper.class.getClassLoader(); |
385 |
} |
| 382 |
LegacyProxyInvocationHandler handler = new LegacyProxyInvocationHandler(this, id); |
386 |
|
| 383 |
return (InternalEObject)Proxy.newProxyInstance(classLoader, interfaces, handler); |
387 |
private Map<CDOID, EObject> getPreRegisteredObjects() |
| 384 |
} |
388 |
{ |
| 385 |
|
389 |
return localThread.get(); |
| 386 |
/** |
390 |
} |
| 387 |
* TODO Ed: Fix whole mess ;-) |
391 |
|
| 388 |
*/ |
392 |
/** |
| 389 |
protected void clearEList(InternalEList<Object> list) |
393 |
* @since 3.0 |
| 390 |
{ |
394 |
*/ |
| 391 |
while (!list.isEmpty()) |
395 |
protected void revisionToInstanceFeature(EStructuralFeature feature) |
| 392 |
{ |
396 |
{ |
| 393 |
Object toBeRemoved = list.basicGet(0); |
397 |
|
| 394 |
list.basicRemove(toBeRemoved, null); |
398 |
if (feature.isMany()) |
| 395 |
} |
399 |
{ |
| 396 |
} |
400 |
int size = view.getStore().size(instance, feature); |
| 397 |
|
401 |
|
| 398 |
/** |
402 |
if (!state.equals(CDOState.CONFLICT)) //do not do anything with the list if state is conflict |
| 399 |
* TODO Consider using only EMF concepts for resolving proxies! |
403 |
{ |
| 400 |
*/ |
404 |
InternalEList list = (InternalEList) instance.eGet(feature); |
| 401 |
protected void resolveAllProxies() |
405 |
EStructuralFeature.Internal internalFeature = (EStructuralFeature.Internal) feature; |
| 402 |
{ |
406 |
//EReference oppositeReference = cdoID().isTemporary() ? null : internalFeature.getEOpposite(); |
| 403 |
// if (!allProxiesResolved) |
407 |
|
| 404 |
{ |
408 |
clearList(feature, list); |
| 405 |
CDOPackageRegistry packageRegistry = cdoView().getSession().getPackageRegistry(); |
409 |
|
| 406 |
EClass eClass = revision.getEClass(); |
410 |
for (int index = 0; index < size; index++) |
| 407 |
for (EStructuralFeature feature : CDOModelUtil.getAllPersistentFeatures(eClass)) |
411 |
{ |
| 408 |
{ |
412 |
|
| 409 |
if (feature instanceof EReference) |
413 |
Object object = getValueFromFeature(feature, index); |
| 410 |
{ |
414 |
try |
| 411 |
resolveProxies(feature, packageRegistry); |
415 |
{ |
| 412 |
} |
416 |
trace("Adding " + object + " to feature " + feature + "in instance " + instance); |
| 413 |
} |
417 |
instance.eInverseAdd((InternalEObject) object, feature.getFeatureID(), object.getClass(), null); |
| 414 |
|
418 |
|
| 415 |
// allProxiesResolved = true; |
419 |
} |
| 416 |
} |
420 |
//catch (ArrayIndexOutOfBoundsException e) //TODO: Martin fallback if eInverseAdd is not implemented (felegated to eDynmic... and resulted in an execption). maybe basicadd is enough |
| 417 |
} |
421 |
catch (Exception e) |
| 418 |
|
422 |
{ |
| 419 |
/* |
423 |
trace("Error: " + e.getMessage()); |
| 420 |
* IMPORTANT: Compile errors in this method might indicate an old version of EMF. Legacy support is only enabled for |
424 |
|
| 421 |
* EMF with fixed bug #247130. These compile errors do not affect native models! |
425 |
list.basicAdd(object, null);//this seems to be wrong |
| 422 |
*/ |
426 |
|
| 423 |
@SuppressWarnings("unchecked") |
427 |
} |
| 424 |
protected void resolveProxies(EStructuralFeature feature, CDOPackageRegistry packageRegistry) |
428 |
} |
| 425 |
{ |
429 |
} |
| 426 |
Object value = getInstanceValue(instance, feature, packageRegistry); |
430 |
|
| 427 |
if (value != null) |
431 |
} |
| 428 |
{ |
432 |
else |
| 429 |
if (feature.isMany()) |
433 |
{ |
| 430 |
{ |
434 |
Object object = getValueFromFeature(feature, 0); |
| 431 |
InternalEList<Object> list = (InternalEList<Object>)value; |
435 |
if (feature instanceof EAttribute) |
| 432 |
int size = list.size(); |
436 |
{ |
| 433 |
for (int i = 0; i < size; i++) |
437 |
|
| 434 |
{ |
438 |
trace("Setting attribute value " + object + " to feature " + feature + " in instance " + instance); |
| 435 |
Object element = list.get(i); |
439 |
eSet(feature, object); |
| 436 |
if (element instanceof LegacyProxy) |
440 |
} |
| 437 |
{ |
441 |
else |
| 438 |
CDOID id = ((LegacyProxy)element).getID(); |
442 |
{ |
| 439 |
InternalCDOObject resolved = (InternalCDOObject)view.getObject(id); |
443 |
|
| 440 |
InternalEObject instance = resolved.cdoInternalInstance(); |
444 |
if (instance != null) |
| 441 |
|
445 |
{ |
| 442 |
// TODO LEGACY |
446 |
if (object != null) |
| 443 |
// // TODO Is InternalEList.basicSet() needed??? |
447 |
{ |
| 444 |
// if (list instanceof org.eclipse.emf.ecore.util.DelegatingInternalEList) |
448 |
try |
| 445 |
// { |
449 |
{ |
| 446 |
// list = ((org.eclipse.emf.ecore.util.DelegatingInternalEList)list).getDelegateInternalEList(); |
450 |
trace("Adding object " + object + " to feature " + feature + " in instance " + instance); |
| 447 |
// } |
451 |
instance.eInverseAdd((InternalEObject) object, feature.getFeatureID(), ((EObject) object).getClass(), null); |
| 448 |
|
452 |
trace("Added object " + object + " to feature " + feature + " in instance " + instance); |
| 449 |
if (list instanceof NotifyingListImpl) |
453 |
} |
| 450 |
{ |
454 |
catch (Exception e) |
| 451 |
((NotifyingListImpl)list).basicSet(i, instance, null); |
455 |
{ |
| 452 |
} |
456 |
//e.printStackTrace(); |
| 453 |
else |
457 |
//TODO Martin: Should handle this exception more carefully |
| 454 |
{ |
458 |
trace("Error: " + e.getMessage() + " trying to use eSet"); |
| 455 |
list.set(i, instance); |
459 |
|
| 456 |
} |
460 |
instance.eSet(feature.getFeatureID(), object); |
| 457 |
} |
461 |
|
| 458 |
} |
462 |
} |
| 459 |
} |
463 |
|
| 460 |
else |
464 |
//TODO Martin: check if adjusting the opposite is needed here |
| 461 |
{ |
465 |
// EStructuralFeature.Internal internalFeature = (EStructuralFeature.Internal) feature; |
| 462 |
if (value instanceof LegacyProxy) |
466 |
// EReference oppositeReference = cdoID().isTemporary() ? null : internalFeature.getEOpposite(); |
| 463 |
{ |
467 |
// if (oppositeReference != null && object != null) |
| 464 |
CDOID id = ((LegacyProxy)value).getID(); |
468 |
// { |
| 465 |
InternalCDOObject resolved = (InternalCDOObject)view.getObject(id); |
469 |
// adjustOppositeReference(instance, (InternalEObject) object, oppositeReference); |
| 466 |
InternalEObject instance = resolved.cdoInternalInstance(); |
470 |
// } |
| 467 |
setInstanceValue(instance, feature, instance); |
471 |
} |
| 468 |
} |
472 |
else |
| 469 |
} |
473 |
{ |
| 470 |
} |
474 |
try |
| 471 |
} |
475 |
{ |
| 472 |
|
476 |
trace("Setting feature " + feature + " to null in instance " + instance); |
| 473 |
protected void adjustEProxy() |
477 |
instance.eInverseAdd((InternalEObject) object, feature.getFeatureID(), null, null); |
| 474 |
{ |
478 |
} |
| 475 |
// Setting eProxyURI is necessary to prevent content adapters from |
479 |
catch (Exception e) |
| 476 |
// loading the whole content tree. |
480 |
{ |
| 477 |
// TODO Does not have the desired effect ;-( see CDOEditor.createModel() |
481 |
//e.printStackTrace(); |
| 478 |
if (state == CDOState.PROXY) |
482 |
//TODO Martin: Should handle this exception more carefully |
| 479 |
{ |
483 |
instance.eSet(feature.getFeatureID(), null); |
| 480 |
if (!instance.eIsProxy()) |
484 |
trace(e.getMessage()); |
| 481 |
{ |
485 |
} |
| 482 |
URI uri = URI.createURI(CDOProtocolConstants.PROTOCOL_NAME + ":proxy#" + id); //$NON-NLS-1$ |
486 |
} |
| 483 |
if (TRACER.isEnabled()) |
487 |
} |
| 484 |
{ |
488 |
} |
| 485 |
TRACER.format("Setting proxyURI {0} for {1}", uri, instance); //$NON-NLS-1$ |
489 |
} |
| 486 |
} |
490 |
} |
| 487 |
|
491 |
|
| 488 |
instance.eSetProxyURI(uri); |
492 |
private void clearList(EStructuralFeature feature, InternalEList list) |
| 489 |
} |
493 |
{ |
| 490 |
} |
494 |
|
| 491 |
else |
495 |
//attempt 2 |
| 492 |
{ |
496 |
int size = list.size(); |
| 493 |
if (instance.eIsProxy()) |
497 |
for (int i = 0; i < size; i++) |
| 494 |
{ |
498 |
{ |
| 495 |
if (TRACER.isEnabled()) |
499 |
InternalEObject obj = (InternalEObject) list.get(0); |
| 496 |
{ |
500 |
instance.eInverseRemove(obj, feature.getFeatureID(), obj.getClass(), null); |
| 497 |
TRACER.format("Unsetting proxyURI for {0}", instance); //$NON-NLS-1$ |
501 |
|
| 498 |
} |
502 |
} |
| 499 |
|
503 |
} |
| 500 |
instance.eSetProxyURI(null); |
504 |
|
| 501 |
} |
505 |
/** |
| 502 |
} |
506 |
* This method retrieves the value from the feature at the given index. It |
| 503 |
} |
507 |
* retrieves the value either from the views's store or the internal |
| 504 |
|
508 |
* pre-registration Map. |
| 505 |
protected static int getEFlagMask(Class<?> instanceClass, String flagName) |
509 |
* |
| 506 |
{ |
510 |
* @param feature the feature to retireive the value from |
| 507 |
Field field = ReflectUtil.getField(instanceClass, flagName); |
511 |
* @param index the given index of the object in the feature |
| 508 |
if (!field.isAccessible()) |
512 |
* @return the value from the feature at the given index |
| 509 |
{ |
513 |
*/ |
| 510 |
field.setAccessible(true); |
514 |
private Object getValueFromFeature(EStructuralFeature feature, int index) |
| 511 |
} |
515 |
{ |
| 512 |
|
516 |
|
| 513 |
try |
517 |
Object object = null; |
| 514 |
{ |
518 |
|
| 515 |
return (Integer)field.get(null); |
519 |
object = revision.get(feature, index); |
| 516 |
} |
520 |
|
| 517 |
catch (IllegalAccessException ex) |
521 |
if (object == null) |
| 518 |
{ |
522 |
{ |
| 519 |
throw new ImplementationError(ex); |
523 |
return null; //TODO Martin: chekc if this is correct |
| 520 |
} |
524 |
} |
| 521 |
} |
525 |
|
| 522 |
|
526 |
CDOType type = CDOModelUtil.getType(feature.getEType()); |
| 523 |
public static boolean isLegacyProxy(Object object) |
527 |
if (type != null) |
| 524 |
{ |
528 |
{ |
| 525 |
return object instanceof LegacyProxy; |
529 |
object = type.convertToEMF(feature.getEType(), object); |
| 526 |
} |
530 |
if (object instanceof CDOID) |
| 527 |
|
531 |
{ |
| 528 |
/** |
532 |
if (getPreRegisteredObjects().get(object) != null) |
| 529 |
* @author Eike Stepper |
533 |
{ |
| 530 |
*/ |
534 |
object = ((CDOLegacyWrapper) getPreRegisteredObjects().get(object)).instance;//getEObjectFromPotentialID(view, feature, object); |
| 531 |
private static interface LegacyProxy |
535 |
} |
| 532 |
{ |
536 |
else |
| 533 |
public CDOID getID(); |
537 |
//if Object not preregistered get the object from the view which creates it or returns a still registered object |
| 534 |
} |
538 |
{ |
| 535 |
|
539 |
|
| 536 |
/** |
540 |
object = view.getStore().get(instance, feature, index); |
| 537 |
* @author Eike Stepper |
541 |
if (object != null) |
| 538 |
*/ |
542 |
{ |
| 539 |
private static final class LegacyProxyInvocationHandler implements InvocationHandler, LegacyProxy |
543 |
CDOLegacyWrapper wrapper = FSMUtil.getLegacyAdapter(((EObject) object).eAdapters()); |
| 540 |
{ |
544 |
if (wrapper == null) |
| 541 |
private static final Method getIDMethod = ReflectUtil.getMethod(LegacyProxy.class, "getID"); //$NON-NLS-1$ |
545 |
{ |
| 542 |
|
546 |
wrapper = (CDOLegacyWrapper) FSMUtil.adaptLegacy((InternalEObject) object); |
| 543 |
private static final Method eIsProxyMethod = ReflectUtil.getMethod(EObject.class, "eIsProxy"); //$NON-NLS-1$ |
547 |
} |
| 544 |
|
548 |
preRegisterObject(wrapper); |
| 545 |
private static final Method eProxyURIMethod = ReflectUtil.getMethod(InternalEObject.class, "eProxyURI"); //$NON-NLS-1$ |
549 |
// getPreRegisteredObjects().put(wrapper.cdoID(), wrapper); |
| 546 |
|
550 |
} |
| 547 |
private CDOLegacyWrapper wrapper; |
551 |
|
| 548 |
|
552 |
} |
| 549 |
private CDOID id; |
553 |
} |
| 550 |
|
554 |
} |
| 551 |
public LegacyProxyInvocationHandler(CDOLegacyWrapper wrapper, CDOID id) |
555 |
|
| 552 |
{ |
556 |
return object; |
| 553 |
this.wrapper = wrapper; |
557 |
} |
| 554 |
this.id = id; |
558 |
|
| 555 |
} |
559 |
private void adjustOppositeReference(InternalEObject instance, InternalEObject object, EReference oppositeReference) |
| 556 |
|
560 |
{ |
| 557 |
public CDOID getID() |
561 |
|
| 558 |
{ |
562 |
trace("Setting opposite: " + object + " in Feature " + oppositeReference + " in instance " + instance + ""); |
| 559 |
return id; |
563 |
boolean deliver = object.eDeliver(); //disable notifications |
| 560 |
} |
564 |
if (deliver) |
| 561 |
|
565 |
{ |
| 562 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
566 |
object.eSetDeliver(false); |
| 563 |
{ |
567 |
} |
| 564 |
if (method.equals(getIDMethod)) |
568 |
try |
| 565 |
{ |
569 |
{ |
| 566 |
return id; |
570 |
if (oppositeReference.isMany()) |
| 567 |
} |
571 |
{ |
| 568 |
|
572 |
|
| 569 |
if (method.equals(eIsProxyMethod)) |
573 |
//TODO Martin: ist this correct? Should I adjust opposite here? |
| 570 |
{ |
574 |
//((InternalEList<Object>) object.eGet(oppositeReference)).basicAdd(instance, null); |
| 571 |
return true; |
575 |
} |
| 572 |
} |
576 |
else |
| 573 |
|
577 |
{ |
| 574 |
if (method.equals(eProxyURIMethod)) |
578 |
|
| 575 |
{ |
579 |
//TODO Martin: this only increases performance if getter is cheaper than setter. Should discuss this. |
| 576 |
// Use the resource of the container because it's guaranteed to be in the same CDOView as the resource |
580 |
if (object.eGet(oppositeReference) != instance) |
| 577 |
// of the target! |
581 |
{ |
| 578 |
Resource resource = wrapper.eResource(); |
582 |
|
| 579 |
|
583 |
object.eInverseAdd(instance, oppositeReference.getFeatureID(), ((EObject) instance).getClass(), null); |
| 580 |
// TODO Consider using a "fake" Resource implementation. See Resource.getEObject(...) |
584 |
} |
| 581 |
return resource.getURI().appendFragment(id.toURIFragment()); |
585 |
} |
| 582 |
} |
586 |
} |
| 583 |
|
587 |
finally |
| 584 |
// A client must have invoked the proxy while being told not to do so! |
588 |
{ |
| 585 |
throw new UnsupportedOperationException(method.getName()); |
589 |
if (deliver) |
| 586 |
} |
590 |
{ |
| 587 |
} |
591 |
object.eSetDeliver(true); |
|
|
592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
} |
| 596 |
|
| 597 |
//TODO Martin: remove this uncommented block if sure that it is not needed anymore |
| 598 |
// private void convertAndSetList(EStructuralFeature feature, int size) |
| 599 |
// { |
| 600 |
// |
| 601 |
// EStructuralFeature.Internal internalFeature = (EStructuralFeature.Internal) feature; |
| 602 |
// EReference oppositeReference = cdoID().isTemporary() ? null : internalFeature.getEOpposite(); |
| 603 |
// |
| 604 |
// |
| 605 |
// List<Object> tmp = new ArrayList<Object>(); |
| 606 |
// for (int index = 0; index < size; index++) |
| 607 |
// { |
| 608 |
// //Object object = view.getStore().get(instance, feature, index); |
| 609 |
// Object object = getValueFromFeature(feature, index); |
| 610 |
// tmp.add(object); |
| 611 |
// |
| 612 |
// |
| 613 |
// // if (oppositeReference != null) |
| 614 |
// // { |
| 615 |
// // adjustOppositeReference(instance, (InternalEObject) object, oppositeReference); |
| 616 |
// // } |
| 617 |
// } |
| 618 |
// InternalEList list = (InternalEList) instance.eGet(feature); |
| 619 |
// internalSynchronizeLists(tmp, list, feature.getFeatureID(), oppositeReference); |
| 620 |
// |
| 621 |
// } |
| 622 |
// |
| 623 |
// private void internalSynchronizeLists(List<Object> tmp, InternalEList list, int featureId, EReference oppositeReference) |
| 624 |
// { |
| 625 |
// //remove |
| 626 |
// for (Object obj : list) |
| 627 |
// { |
| 628 |
// |
| 629 |
// if (!tmp.contains(obj))//remove if removed from list |
| 630 |
// { |
| 631 |
// |
| 632 |
// list.basicRemove(obj, null); |
| 633 |
// adjustOppositeReference(instance, (InternalEObject) obj, oppositeReference); |
| 634 |
// |
| 635 |
// //((BasicEObjectImpl) instance).eInverseRemove((InternalEObject) obj, featureId, null); |
| 636 |
// //((InternalEObject) obj).eBasicRemoveFromContainer(null); |
| 637 |
// // NotificationChain eInverseRemove = ((InternalEObject) obj).eInverseRemove(instance, featureId, instance.getClass(), null); |
| 638 |
// // NotificationChain basicRemove = list.basicRemove(obj, eInverseRemove); |
| 639 |
// // basicRemove.dispatch(); |
| 640 |
// //((InternalEObject) obj).eInternalContainer() |
| 641 |
// |
| 642 |
// //((InternalEObject) obj).eContainer().notify(); |
| 643 |
// // Method method = ReflectUtil.getMethod(obj.getClass(), "eBasicRemoveFromContainer", InternalEObject.class, int.class); //$NON-NLS-1$ |
| 644 |
// // ReflectUtil.invokeMethod(method, obj); |
| 645 |
// |
| 646 |
// |
| 647 |
// } |
| 648 |
// } |
| 649 |
// |
| 650 |
// for (Object obj : tmp) |
| 651 |
// { |
| 652 |
// if (!list.basicContains(obj))//remove if removed from list |
| 653 |
// { |
| 654 |
// |
| 655 |
// list.basicAdd(obj, null); |
| 656 |
// if (oppositeReference != null) |
| 657 |
// { |
| 658 |
// adjustOppositeReference(instance, (InternalEObject) obj, oppositeReference); |
| 659 |
// } |
| 660 |
// |
| 661 |
// //instance.eInverseAdd((InternalEObject) obj, featureId, ((EObject) obj).getClass(), null); |
| 662 |
// //((EObjectImpl) instance).eInverseAdd((InternalEObject) obj, featureId, null); |
| 663 |
// //((InternalEObject) obj).eBasicSetContainer(instance, ((InternalEObject) obj).eContainerFeatureID(), null); |
| 664 |
// |
| 665 |
// //list.add(obj); |
| 666 |
// } |
| 667 |
// } |
| 668 |
// } |
| 669 |
|
| 670 |
protected Resource.Internal getInstanceResource(InternalEObject instance) |
| 671 |
{ |
| 672 |
return instance.eDirectResource(); |
| 673 |
} |
| 674 |
|
| 675 |
protected InternalEObject getInstanceContainer(InternalEObject instance) |
| 676 |
{ |
| 677 |
return instance.eInternalContainer(); |
| 678 |
} |
| 679 |
|
| 680 |
protected int getInstanceContainerFeatureID(InternalEObject instance) |
| 681 |
{ |
| 682 |
return instance.eContainerFeatureID(); |
| 683 |
} |
| 684 |
|
| 685 |
protected Object getInstanceValue(InternalEObject instance, EStructuralFeature feature, CDOPackageRegistry packageRegistry) |
| 686 |
{ |
| 687 |
return instance.eGet(feature); |
| 688 |
} |
| 689 |
|
| 690 |
protected void setInstanceResource(Resource.Internal resource) |
| 691 |
{ |
| 692 |
Method method = ReflectUtil.getMethod(instance.getClass(), "eSetDirectResource", Resource.Internal.class); //$NON-NLS-1$ |
| 693 |
ReflectUtil.invokeMethod(method, instance, resource); |
| 694 |
} |
| 695 |
|
| 696 |
protected void setInstanceContainer(InternalEObject container, int containerFeatureID) |
| 697 |
{ |
| 698 |
Method method = ReflectUtil.getMethod(instance.getClass(), "eBasicSetContainer", InternalEObject.class, int.class); //$NON-NLS-1$ |
| 699 |
ReflectUtil.invokeMethod(method, instance, container, containerFeatureID); |
| 700 |
} |
| 701 |
|
| 702 |
protected void setInstanceValue(InternalEObject instance, EStructuralFeature feature, Object value) |
| 703 |
{ |
| 704 |
instance.eSet(feature, value); |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* @param feature in case that a proxy has to be created the feature that will |
| 709 |
* determine the interface type of the proxy and that will be used |
| 710 |
* later to resolve the proxy. <code>null</code> indicates that proxy |
| 711 |
* creation will be avoided! |
| 712 |
*/ |
| 713 |
protected InternalEObject getEObjectFromPotentialID(InternalCDOView view, EStructuralFeature feature, Object potentialID) |
| 714 |
{ |
| 715 |
|
| 716 |
if (getPreRegisteredObjects().get(potentialID) != null) |
| 717 |
{ |
| 718 |
|
| 719 |
potentialID = ((CDOLegacyWrapper) getPreRegisteredObjects().get(potentialID)).instance; |
| 720 |
|
| 721 |
trace("getting Object (" + potentialID + ") from localThread instead of the view"); |
| 722 |
|
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
|
| 727 |
if (potentialID instanceof CDOID) |
| 728 |
{ |
| 729 |
CDOID id = (CDOID) potentialID; |
| 730 |
if (id.isNull()) |
| 731 |
{ |
| 732 |
return null; |
| 733 |
} |
| 734 |
|
| 735 |
boolean loadOnDemand = feature == null; |
| 736 |
potentialID = view.getObject(id, loadOnDemand); |
| 737 |
if (potentialID == null && !loadOnDemand) |
| 738 |
{ |
| 739 |
return createProxy(view, feature, id); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
if (potentialID instanceof InternalCDOObject) |
| 744 |
{ |
| 745 |
return ((InternalCDOObject) potentialID).cdoInternalInstance(); |
| 746 |
} |
| 747 |
|
| 748 |
} |
| 749 |
return (InternalEObject) potentialID; |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
* Creates and returns a <em>proxy</em> object. The usage of a proxy object is |
| 754 |
* strongly limited. The only guarantee that can be made is that the following |
| 755 |
* methods are callable and will behave in the expected way: |
| 756 |
* <ul> |
| 757 |
* <li>{@link CDOObject#cdoID()} will return the {@link CDOID} of the target |
| 758 |
* object |
| 759 |
* <li>{@link CDOObject#cdoState()} will return {@link CDOState#PROXY PROXY} |
| 760 |
* <li>{@link InternalEObject#eIsProxy()} will return <code>true</code> |
| 761 |
* <li>{@link InternalEObject#eProxyURI()} will return the EMF proxy URI of |
| 762 |
* the target object |
| 763 |
* </ul> |
| 764 |
* Calling any other method on the proxy object will result in an |
| 765 |
* {@link UnsupportedOperationException} being thrown at runtime. Note also |
| 766 |
* that the proxy object might even not be cast to the concrete type of the |
| 767 |
* target object. The proxy can only guaranteed to be of <em>any</em> concrete |
| 768 |
* subtype of the declared type of the given feature. |
| 769 |
* <p> |
| 770 |
* TODO {@link InternalEObject#eResolveProxy(InternalEObject) |
| 771 |
*/ |
| 772 |
protected InternalEObject createProxy(InternalCDOView view, EStructuralFeature feature, CDOID id) |
| 773 |
{ |
| 774 |
EClassifier eType = feature.getEType(); |
| 775 |
Class<?> instanceClass = eType.getInstanceClass(); |
| 776 |
|
| 777 |
Class<?>[] interfaces = |
| 778 |
{ instanceClass, InternalEObject.class, LegacyProxy.class }; |
| 779 |
ClassLoader classLoader = CDOLegacyWrapper.class.getClassLoader(); |
| 780 |
LegacyProxyInvocationHandler handler = new LegacyProxyInvocationHandler(this, id); |
| 781 |
return (InternalEObject) Proxy.newProxyInstance(classLoader, interfaces, handler); |
| 782 |
} |
| 783 |
|
| 784 |
/** |
| 785 |
* TODO Ed: Fix whole mess ;-) |
| 786 |
*/ |
| 787 |
protected void clearEList(InternalEList<Object> list) |
| 788 |
{ |
| 789 |
while (!list.isEmpty()) |
| 790 |
{ |
| 791 |
Object toBeRemoved = list.basicGet(0); |
| 792 |
list.basicRemove(toBeRemoved, null); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
/** |
| 797 |
* TODO Consider using only EMF concepts for resolving proxies! |
| 798 |
*/ |
| 799 |
protected void resolveAllProxies() |
| 800 |
{ |
| 801 |
// if (!allProxiesResolved) |
| 802 |
{ |
| 803 |
CDOPackageRegistry packageRegistry = cdoView().getSession().getPackageRegistry(); |
| 804 |
EClass eClass = revision.getEClass(); |
| 805 |
for (EStructuralFeature feature : CDOModelUtil.getAllPersistentFeatures(eClass)) |
| 806 |
{ |
| 807 |
if (feature instanceof EReference) |
| 808 |
{ |
| 809 |
resolveProxies(feature, packageRegistry); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
// allProxiesResolved = true; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
/* |
| 818 |
* IMPORTANT: Compile errors in this method might indicate an old version of |
| 819 |
* EMF. Legacy support is only enabled for EMF with fixed bug #247130. These |
| 820 |
* compile errors do not affect native models! |
| 821 |
*/ |
| 822 |
@SuppressWarnings("unchecked") |
| 823 |
protected void resolveProxies(EStructuralFeature feature, CDOPackageRegistry packageRegistry) |
| 824 |
{ |
| 825 |
Object value = getInstanceValue(instance, feature, packageRegistry); |
| 826 |
if (value != null) |
| 827 |
{ |
| 828 |
if (feature.isMany()) |
| 829 |
{ |
| 830 |
InternalEList<Object> list = (InternalEList<Object>) value; |
| 831 |
int size = list.size(); |
| 832 |
for (int i = 0; i < size; i++) |
| 833 |
{ |
| 834 |
Object element = list.get(i); |
| 835 |
if (element instanceof LegacyProxy) |
| 836 |
{ |
| 837 |
CDOID id = ((LegacyProxy) element).getID(); |
| 838 |
InternalCDOObject resolved = (InternalCDOObject) view.getObject(id); |
| 839 |
InternalEObject instance = resolved.cdoInternalInstance(); |
| 840 |
|
| 841 |
// TODO LEGACY |
| 842 |
// // TODO Is InternalEList.basicSet() needed??? |
| 843 |
// if (list instanceof org.eclipse.emf.ecore.util.DelegatingInternalEList) |
| 844 |
// { |
| 845 |
// list = ((org.eclipse.emf.ecore.util.DelegatingInternalEList)list).getDelegateInternalEList(); |
| 846 |
// } |
| 847 |
|
| 848 |
if (list instanceof NotifyingListImpl) |
| 849 |
{ |
| 850 |
((NotifyingListImpl) list).basicSet(i, instance, null); |
| 851 |
} |
| 852 |
else |
| 853 |
{ |
| 854 |
list.set(i, instance); |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
else |
| 860 |
{ |
| 861 |
if (value instanceof LegacyProxy) |
| 862 |
{ |
| 863 |
CDOID id = ((LegacyProxy) value).getID(); |
| 864 |
InternalCDOObject resolved = (InternalCDOObject) view.getObject(id); |
| 865 |
InternalEObject instance = resolved.cdoInternalInstance(); |
| 866 |
setInstanceValue(instance, feature, instance); |
| 867 |
} |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
protected void adjustEProxy() |
| 873 |
{ |
| 874 |
// Setting eProxyURI is necessary to prevent content adapters from |
| 875 |
// loading the whole content tree. |
| 876 |
// TODO Does not have the desired effect ;-( see CDOEditor.createModel() |
| 877 |
if (state == CDOState.PROXY) |
| 878 |
{ |
| 879 |
if (!instance.eIsProxy()) |
| 880 |
{ |
| 881 |
URI uri = URI.createURI(CDOProtocolConstants.PROTOCOL_NAME + ":proxy#" + id); //$NON-NLS-1$ |
| 882 |
if (TRACER.isEnabled()) |
| 883 |
{ |
| 884 |
TRACER.format("Setting proxyURI {0} for {1}", uri, instance); |
| 885 |
} |
| 886 |
|
| 887 |
instance.eSetProxyURI(uri); |
| 888 |
} |
| 889 |
} |
| 890 |
else |
| 891 |
{ |
| 892 |
if (instance.eIsProxy()) |
| 893 |
{ |
| 894 |
if (TRACER.isEnabled()) |
| 895 |
{ |
| 896 |
TRACER.format("Unsetting proxyURI for {0}", instance); |
| 897 |
} |
| 898 |
|
| 899 |
instance.eSetProxyURI(null); |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
protected static int getEFlagMask(Class<?> instanceClass, String flagName) |
| 905 |
{ |
| 906 |
Field field = ReflectUtil.getField(instanceClass, flagName); |
| 907 |
if (!field.isAccessible()) |
| 908 |
{ |
| 909 |
field.setAccessible(true); |
| 910 |
} |
| 911 |
|
| 912 |
try |
| 913 |
{ |
| 914 |
return (Integer) field.get(null); |
| 915 |
} |
| 916 |
catch (IllegalAccessException ex) |
| 917 |
{ |
| 918 |
throw new ImplementationError(ex); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
public static boolean isLegacyProxy(Object object) |
| 923 |
{ |
| 924 |
return object instanceof LegacyProxy; |
| 925 |
} |
| 926 |
|
| 927 |
/** |
| 928 |
* @author Eike Stepper |
| 929 |
*/ |
| 930 |
private static interface LegacyProxy |
| 931 |
{ |
| 932 |
public CDOID getID(); |
| 933 |
} |
| 934 |
|
| 935 |
/** |
| 936 |
* @author Eike Stepper |
| 937 |
*/ |
| 938 |
private static final class LegacyProxyInvocationHandler implements InvocationHandler, LegacyProxy |
| 939 |
{ |
| 940 |
private static final Method getIDMethod = ReflectUtil.getMethod(LegacyProxy.class, "getID"); |
| 941 |
|
| 942 |
private static final Method eIsProxyMethod = ReflectUtil.getMethod(EObject.class, "eIsProxy"); |
| 943 |
|
| 944 |
private static final Method eProxyURIMethod = ReflectUtil.getMethod(InternalEObject.class, "eProxyURI"); |
| 945 |
|
| 946 |
private CDOLegacyWrapper wrapper; |
| 947 |
|
| 948 |
private CDOID id; |
| 949 |
|
| 950 |
public LegacyProxyInvocationHandler(CDOLegacyWrapper wrapper, CDOID id) |
| 951 |
{ |
| 952 |
this.wrapper = wrapper; |
| 953 |
this.id = id; |
| 954 |
} |
| 955 |
|
| 956 |
public CDOID getID() |
| 957 |
{ |
| 958 |
return id; |
| 959 |
} |
| 960 |
|
| 961 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable |
| 962 |
{ |
| 963 |
if (method.equals(getIDMethod)) |
| 964 |
{ |
| 965 |
return id; |
| 966 |
} |
| 967 |
|
| 968 |
if (method.equals(eIsProxyMethod)) |
| 969 |
{ |
| 970 |
return true; |
| 971 |
} |
| 972 |
|
| 973 |
if (method.equals(eProxyURIMethod)) |
| 974 |
{ |
| 975 |
// Use the resource of the container because it's guaranteed to be in the same CDOView as the resource |
| 976 |
// of the target! |
| 977 |
Resource resource = wrapper.eResource(); |
| 978 |
|
| 979 |
// TODO Consider using a "fake" Resource implementation. See Resource.getEObject(...) |
| 980 |
return resource.getURI().appendFragment(id.toURIFragment()); |
| 981 |
} |
| 982 |
|
| 983 |
// A client must have invoked the proxy while being told not to do so! |
| 984 |
throw new UnsupportedOperationException(method.getName()); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
private void trace(String trace) |
| 989 |
{ |
| 990 |
|
| 991 |
if (TRACER.isEnabled()) |
| 992 |
{ |
| 993 |
TRACER.format(trace); |
| 994 |
} |
| 995 |
//System.out.println(trace); |
| 996 |
} |
| 997 |
|
| 998 |
/** |
| 999 |
* @since 3.0 |
| 1000 |
*/ |
| 1001 |
public synchronized void setUnderConstruction(boolean underConstruction) |
| 1002 |
{ |
| 1003 |
this.underConstruction = underConstruction; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* @since 3.0 |
| 1008 |
*/ |
| 1009 |
public boolean isUnderConstruction() |
| 1010 |
{ |
| 1011 |
return underConstruction; |
| 1012 |
} |
| 588 |
} |
1013 |
} |