KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > InstanceHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.handlers;
20
21 import org.netbeans.api.mdr.events.*;
22
23 import javax.jmi.model.*;
24 import javax.jmi.reflect.*;
25
26 import java.util.*;
27
28 import org.netbeans.mdr.storagemodel.*;
29 import org.netbeans.mdr.util.DebugException;
30 import org.netbeans.mdr.persistence.StorageException;
31 import org.netbeans.mdr.util.Logger;
32
33 /** Invocation handler for Instances
34  *
35  * @author Petr Hrebejk, Martin Matula
36  * @version 2.0
37  */

38 public abstract class InstanceHandler extends FeaturedHandler implements RefObject {
39
40     /* -------------------------------------------------------------------- */
41     /* -- Constructor ----------------------------------------------------- */
42     /* -------------------------------------------------------------------- */
43
44    /** Creates new ClassProxy */
45     protected InstanceHandler(StorableObject storable) {
46         super(storable);
47     }
48
49     /* -------------------------------------------------------------------- */
50     /* -- Helper methods -------------------------------------------------- */
51     /* -------------------------------------------------------------------- */
52
53     private StorableObject getInstanceDelegate() {
54         return (StorableObject) _getDelegate();
55     }
56
57     private void _setReference(String JavaDoc name, Object JavaDoc value) {
58         Object JavaDoc extraInfo = null;
59         boolean fail = true;
60         extraInfo = _preSetR(name, value);
61         try {
62             _handleSetR(name, value);
63             fail = false;
64         } catch (NullPointerException JavaDoc e) {
65             // reference descriptor was not found
66
throw new InvalidNameException(name);
67         } finally {
68             _postSetR(extraInfo, fail);
69         }
70     }
71     
72     private Object JavaDoc _getReference(String JavaDoc name) {
73         Object JavaDoc extraInfo = null;
74         Object JavaDoc result = null;
75         boolean fail = true;
76         extraInfo = _preGetR(name);
77         try {
78             result = _handleGetR(name);
79             fail = false;
80             return result;
81         } catch (NullPointerException JavaDoc e) {
82             // reference descriptor was not found
83
throw new InvalidNameException(name);
84         } finally {
85             _postGetR(result, extraInfo, fail);
86         }
87     }
88     
89     private boolean _isReference(String JavaDoc name) {
90         try {
91             return getInstanceDelegate().getClassProxy().getReferenceDescriptor(name) != null;
92         } catch (StorageException e) {
93             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
94         }
95     }
96
97     /* -------------------------------------------------------------------- */
98     /* -- Methods to be called by generated handlers (_pre, _handle, _post) */
99     /* -------------------------------------------------------------------- */
100
101     protected final Object JavaDoc _preGetR(String JavaDoc featureName) {
102         _lock(false);
103         return null;
104     }
105     
106     protected final Object JavaDoc _handleGetR(String JavaDoc featureName) {
107         StorableClass.ReferenceDescriptor reference = null;
108         try {
109             reference = getInstanceDelegate().getClassProxy().getReferenceDescriptor(featureName);
110             AssociationHandler assoc = (AssociationHandler) _getRepository().getHandler(reference.getAssociation());
111             //Logger.getDefault().log("getting reference " + featureName + " for end: " + reference.getEndName());
112
return assoc._query(reference.getEndName(), (RefObject) this);
113         } catch (ClassCastException JavaDoc e) {
114             // this will throw TypeMismatchException or DebugException if the mismatch is not found
115
if (reference.isFirstEnd()) {
116                 reference.getAssociation().checkType(null, this);
117             } else {
118                 reference.getAssociation().checkType(this, null);
119             }
120             return null;
121         } catch (StorageException e) {
122             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
123         }
124     }
125     
126     protected final void _postGetR(Object JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
127         _unlock();
128     }
129
130     protected final Object JavaDoc _preSetR(String JavaDoc featureName, Object JavaDoc newValue) {
131         _lock(true);
132         // events firing will be handled in the association proxy
133
return null;
134     }
135     
136     protected final void _handleSetR(String JavaDoc featureName, Object JavaDoc newValue) {
137         try {
138             StorableClass.ReferenceDescriptor reference = getInstanceDelegate().getClassProxy().getReferenceDescriptor(featureName);
139             AssociationHandler assoc = (AssociationHandler) _getRepository().getHandler(reference.getAssociation());
140             
141             // get old value of the reference
142
Object JavaDoc oldValue = _getReference(featureName);
143
144             // remove old link and add the new one
145
if (reference.isFirstEnd()) {
146                 if (oldValue != null) try {
147                     assoc._remove((RefObject) oldValue, (RefObject) this);
148                 } catch (ClassCastException JavaDoc e) {
149                     // this will throw TypeMismatchException or DebugException if the mismatch is not found
150
((StorableAssociation) assoc._getDelegate()).checkType(oldValue, this);
151                     // this is here only to make the compiler happy
152
return;
153                 }
154                 if (newValue != null) try {
155                     assoc._add((RefObject) newValue, (RefObject) this);
156                 } catch (ClassCastException JavaDoc e) {
157                     // this will throw TypeMismatchException or DebugException if the mismatch is not found
158
((StorableAssociation) assoc._getDelegate()).checkType(newValue, this);
159                     // this is here only to make the compiler happy
160
return;
161                 }
162             } else {
163                 if (oldValue != null) try {
164                     assoc._remove((RefObject) this, (RefObject) oldValue);
165                 } catch (ClassCastException JavaDoc e) {
166                     // this will throw TypeMismatchException or DebugException if the mismatch is not found
167
((StorableAssociation) assoc._getDelegate()).checkType(this, oldValue);
168                     // this is here only to make the compiler happy
169
return;
170                 }
171                 if (newValue != null) try {
172                     assoc._add((RefObject) this, (RefObject) newValue);
173                 } catch (ClassCastException JavaDoc e) {
174                     // this will throw TypeMismatchException or DebugException if the mismatch is not found
175
((StorableAssociation) assoc._getDelegate()).checkType(this, newValue);
176                     // this is here only to make the compiler happy
177
return;
178                 }
179             }
180             
181             // [PENDING] should add replace operation into an association
182
// and call it in case when both oldValue and newValue are not null so that
183
// the operation will be atomic and replace event can be thrown instead
184
// of add and remove events
185
} catch (StorageException e) {
186             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
187         }
188     }
189     
190     protected final void _postSetR(Object JavaDoc extraInfo, boolean fail) {
191         // events firing was handled in the association proxy
192
_unlock();
193     }
194
195     protected final Object JavaDoc _handleGet(int attrIndex) {
196         Object JavaDoc result;
197         
198         try {
199             result = getInstanceDelegate().getAttribute(attrIndex);
200             if (result instanceof Collection) {
201                 if (result instanceof AttrList) {
202                     result = new AttrListWrapper(this, attrIndex, null);
203                 } else if (result instanceof AttrCollection) {
204                     result = new AttrCollWrapper(this, attrIndex, null);
205                 } else if (result instanceof AttrImmutList) {
206                     result = new AttrImmutListWrapper(_getMdrStorage(), this, attrIndex, null);
207                 }
208             }
209             return result;
210         } catch (StorageException e) {
211             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
212         }
213     }
214     
215     protected final void _handleSet(int attrIndex, Object JavaDoc newValue) {
216         try {
217             getInstanceDelegate().setAttribute(attrIndex, newValue);
218         } catch (StorageException e) {
219             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
220         }
221     }
222
223     // MOF specific Handling methods ---------------------------------------------
224

225     /* -------------------------------------------------------------------- */
226     /* -- Implementation of javax.jmi.reflect.RefObject ------------------- */
227     /* -------------------------------------------------------------------- */
228
229     public final RefFeatured refOutermostComposite() {
230         _lock(false);
231         try {
232             return _outermostComposite();
233         } finally {
234             _unlock();
235         }
236     }
237     
238     protected RefFeatured _outermostComposite() {
239         try {
240             return (RefFeatured) _getRepository().getHandler(getInstanceDelegate().getOutermostComposite());
241         } catch ( StorageException e ) {
242             throw new DebugException("Storage exception: " + e);
243         }
244     }
245
246     public final RefFeatured refImmediateComposite() {
247         _lock(false);
248         try {
249             return _immediateComposite();
250         } finally {
251             _unlock();
252         }
253     }
254
255     protected RefFeatured _immediateComposite() {
256         try {
257             return (RefFeatured) _getRepository().getHandler(getInstanceDelegate().getImmediateComposite());
258         } catch ( StorageException e ) {
259             throw new DebugException("Storage exception: " + e);
260         }
261     }
262     
263     public final boolean refIsInstanceOf(RefObject objType, boolean considerSubtypes) {
264         _lock(false);
265         try {
266             return isInstanceOf((GeneralizableElement) refMetaObject(), objType, considerSubtypes);
267         } finally {
268             _unlock();
269         }
270     }
271
272     private final boolean isInstanceOf(GeneralizableElement metaObject, RefObject objType, boolean considerSubtypes) {
273         if (metaObject.equals(objType)) return true;
274         if (considerSubtypes) {
275             boolean isInstance;
276             Iterator it = metaObject.getSupertypes().iterator();
277             while (it.hasNext()) {
278                 if (isInstanceOf(((GeneralizableElement) it.next()), objType, true)) {
279                     return true;
280                 }
281             }
282         }
283         return false;
284     }
285
286     public final RefClass refClass() {
287         _lock(false);
288         try {
289             return (RefClass) _getRepository().getHandler(getInstanceDelegate().getClassProxy());
290         } catch (StorageException e) {
291             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
292         } finally {
293             _unlock();
294         }
295     }
296
297     public final void refDelete() {
298         boolean fail = true;
299         InstanceEvent event = null;
300         _lock(true);
301         try {
302             if (_getMdrStorage().eventsEnabled()) {
303                 event = new InstanceEvent(
304                     this,
305                     InstanceEvent.EVENT_INSTANCE_DELETE,
306                     null,
307                     this
308                 );
309
310                 _getMdrStorage().getEventNotifier().INSTANCE.firePlannedChange(this, event);
311             }
312             _delete();
313             Exception JavaDoc deletionStackTrace = new Exception JavaDoc("mofId: " + refMofId() + ", class: " + getClass().getName());
314             BaseObjectHandler.deletedInstances.put(this, deletionStackTrace);
315             fail = false;
316         } finally {
317             _unlock(fail);
318         }
319         _getRepository().removeHandler(_getMofId());
320     }
321     
322     protected void _delete() {
323         try {
324             getInstanceDelegate().delete();
325         } catch (StorageException e) {
326             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
327         }
328     };
329
330     public final void refSetValue(String JavaDoc featureName, java.lang.Object JavaDoc value) {
331         _lock(true);
332         try {
333             if (_isReference(featureName)) {
334                 _setReference(featureName, value);
335             } else {
336                 super.refSetValue(featureName, value);
337             }
338         } finally {
339             _unlock();
340         }
341     }
342     
343     public final void refSetValue(RefObject feature, Object JavaDoc value) {
344         if (feature instanceof Attribute) {
345             super.refSetValue(feature, value);
346         } else {
347             _lock(true);
348             try {
349                 StorableClass cls = getInstanceDelegate().getClassProxy();
350                 try {
351                     StorableClass.ReferenceDescriptor desc = cls.getReferenceDescriptor(((Reference) feature).getName());
352                     if (!desc.getMofId().equals(((BaseObjectHandler)feature)._getDelegate().getMofId())) throw new InvalidCallException(null, feature);
353                 } catch (DebugException e) {
354                     throw new InvalidCallException(null, feature);
355                 }
356                 _setReference(((Reference) feature).getName(), value);
357             } catch (InvalidNameException e) {
358                 throw new InvalidCallException(null, feature);
359             } catch (ClassCastException JavaDoc e) {
360                 throw new InvalidCallException(null, feature);
361             } catch (StorageException e) {
362                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
363             } finally {
364                 _unlock();
365             }
366         }
367     }
368
369     public final Object JavaDoc refGetValue(String JavaDoc featureName) {
370         _lock(false);
371         try {
372             if (_isReference(featureName)) {
373                 return _getReference(featureName);
374             } else {
375                 return super.refGetValue(featureName);
376             }
377         } finally {
378             _unlock();
379         }
380     }
381     
382     public final Object JavaDoc refGetValue(RefObject feature) {
383         if (feature instanceof Attribute) {
384             return super.refGetValue(feature);
385         } else {
386             _lock(false);
387             try {
388                 StorableClass cls = getInstanceDelegate().getClassProxy();
389                 try {
390                     StorableClass.ReferenceDescriptor desc = cls.getReferenceDescriptor(((Reference) feature).getName());
391                     if (desc == null || !desc.getMofId().equals(((BaseObjectHandler)feature)._getDelegate().getMofId())) throw new InvalidCallException(null, feature);
392                 } catch (DebugException e) {
393                     throw new InvalidCallException(null, feature);
394                 }
395                 return _getReference(((Reference) feature).getName());
396             } catch (InvalidNameException e) {
397                 throw new InvalidCallException(null, feature);
398             } catch (ClassCastException JavaDoc e) {
399                 throw new InvalidCallException(null, feature);
400             } catch (StorageException e) {
401                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
402             } finally {
403                 _unlock();
404             }
405         }
406     }
407
408     /* -------------------------------------------------------------------- */
409     /* -- Implementation of org.netbeans.api.mdr.events.MDRChangeSource --- */
410     /* -------------------------------------------------------------------- */
411     
412     /** Registers a listener for receiving all event notifications
413      * fired on this object.
414      * @param listener Object that implements {@link MDRChangeListener} interface.
415      */

416     public void addListener(MDRChangeListener listener) {
417         addListener(listener, MDRChangeEvent.EVENTMASK_ALL);
418     }
419     
420     /** Registers a listener for receiving event notifications.
421      * @param listener Object that implements {@link MDRChangeListener} interface.
422      * @param mask bitmask to filter types of events the listener listens on
423      */

424     public void addListener(MDRChangeListener listener, int mask) {
425         _getMdrStorage().getEventNotifier().INSTANCE.addListener(listener, mask, this);
426     }
427     
428     /** Removes listener from the list of objects registered for event notifications.
429      * @param listener Object that implements {@link MDRChangeListener} interface.
430      */

431     public void removeListener(MDRChangeListener listener) {
432         _getMdrStorage().getEventNotifier().INSTANCE.removeListener(listener, this);
433     }
434     
435     /** Removes listener from the list of objects registered for event notifications.
436      * @param listener Object that implements {@link MDRChangeListener} interface.
437      * @param mask determines type of the events the listeners stops to listen on
438      */

439     public void removeListener(MDRChangeListener listener, int mask) {
440         _getMdrStorage().getEventNotifier().INSTANCE.removeListener(listener, mask, this);
441     }
442
443     /** Returns all listeners registered for event notification on this object.
444      * @return collection of registered listeners
445      */

446     public Collection getListeners() {
447         return _getMdrStorage().getEventNotifier().INSTANCE.getListeners(this);
448     }
449         
450     /* ---------------------------------------------------------------- */
451     /* -- Implementation of abstract methods from BaseObjectHandler --- */
452     /* ---------------------------------------------------------------- */
453
454     protected final Collection _recursiveVerify(Collection violations, Set visited) {
455         _lock(false);
456         try {
457             _verify(violations);
458             visited.add(this);
459             // [PENDING] should verify all contained objects
460
return violations;
461         } finally {
462             _unlock();
463         }
464     }
465
466     protected Collection _verify(Collection violations) {
467         _lock(false);
468         try {
469             getInstanceDelegate().verify(violations);
470             return violations;
471         } catch (StorageException e) {
472             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
473         } finally {
474             _unlock();
475         }
476     }
477 }
478
Popular Tags