KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
22 import javax.jmi.model.*;
23 import javax.jmi.reflect.*;
24
25 import org.netbeans.api.mdr.*;
26 import org.netbeans.api.mdr.events.*;
27
28 import org.netbeans.mdr.util.*;
29 import org.netbeans.mdr.storagemodel.*;
30 import org.netbeans.mdr.persistence.StorageException;
31
32 /** Invocation handler for Associations
33  *
34  * @author Petr Hrebejk, Martin Matula
35  * @version
36  */

37 public abstract class AssociationHandler extends BaseObjectHandler implements RefAssociation {
38
39     /* -------------------------------------------------------------------- */
40     /* -- Constructor ----------------------------------------------------- */
41     /* -------------------------------------------------------------------- */
42
43     /** Creates new AssociationProxy */
44     protected AssociationHandler(StorableAssociation storable) {
45         super(storable);
46     }
47
48     /* -------------------------------------------------------------------- */
49     /* -- Helper methods -------------------------------------------------- */
50     /* -------------------------------------------------------------------- */
51
52     private StorableAssociation getAssociationDelegate() {
53         return (StorableAssociation) _getDelegate();
54     }
55
56     /* -------------------------------------------------------------------- */
57     /* -- Methods to be called by generated handlers (_pre, _handle, _post) */
58     /* -------------------------------------------------------------------- */
59
60     /**
61      * Executed before allLinks operation of the underlying object gets called.
62      * @return additional information which will be passed to the _postAllLinks method (can be <code>null</code>)
63      */

64     protected final Object JavaDoc _preAllLinks() {
65         _lock(false);
66         return null;
67     }
68     
69     protected final java.util.Collection JavaDoc _handleAllLinks() {
70         try {
71             return new IndexSetWrapper(_getMdrStorage(), getAssociationDelegate().getAllLinks());
72         } catch (StorageException e) {
73             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
74         }
75     }
76     
77     /** Executed after the allLinks operation was called.
78      * @param result Result of allLinks operation
79      * @param extraInfo additional information created by {@link #_preAllLinks} method
80      */

81     protected final void _postAllLinks(java.util.Collection JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
82         _unlock();
83     }
84     
85     protected final Object JavaDoc _preExists(RefObject associationEnd1, RefObject associationEnd2) {
86         _lock(false);
87         return null;
88     }
89     
90     protected final Boolean JavaDoc _handleExists(RefObject associationEnd1, RefObject associationEnd2) {
91         try {
92             return getAssociationDelegate().linkExists(((BaseObjectHandler)associationEnd1)._getDelegate().getMofId(), ((BaseObjectHandler)associationEnd2)._getDelegate().getMofId()) ? Boolean.TRUE : Boolean.FALSE;
93         } catch (StorageException e) {
94             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
95         }
96     }
97     
98     protected final void _postExists(Boolean JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
99         _unlock();
100     }
101     
102     protected final Object JavaDoc _preQuery(String JavaDoc endName, RefObject query) {
103         _lock(false);
104         return null;
105     }
106
107     public final Object JavaDoc _handleQuery(String JavaDoc endName, RefObject query) {
108         Object JavaDoc result;
109         
110         try {
111             result = getAssociationDelegate().queryObjects(endName, query == null ? null : ((BaseObjectHandler)query)._getDelegate().getMofId());
112         } catch (StorageException e) {
113             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
114         }
115         
116         if (result instanceof AssocEndIndexUList) {
117             return new AEIndexUListWrapper(this, query, endName, (AssocEndIndexUList) result, getAssociationDelegate().getEnd1Name().equals(endName));
118         } else if (result instanceof AssocEndIndexSet) {
119             return new AEIndexSetWrapper(this, query, endName, (AssocEndIndexSet) result, getAssociationDelegate().getEnd1Name().equals(endName));
120         } else {
121             return _getRepository().getHandler((StorableBaseObject) result);
122         }
123     }
124     
125     protected final void _postQuery(Object JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
126         _unlock();
127     }
128     
129     protected final Object JavaDoc _preAdd(RefObject associationEnd1, RefObject associationEnd2) {
130         boolean fail = true;
131         _lock(true);
132         try {
133             if (_getMdrStorage().eventsEnabled()) {
134                 AssociationEvent event = new AssociationEvent(
135                 this,
136                 AssociationEvent.EVENT_ASSOCIATION_ADD,
137                 associationEnd1,
138                 getAssociationDelegate().getEnd1Name(),
139                 null,
140                 associationEnd2,
141                 AssociationEvent.POSITION_NONE
142                 );
143                 _getMdrStorage().getEventNotifier().ASSOCIATION.firePlannedChange(this, event);
144                 fail = false;
145                 return event;
146             }
147             fail = false;
148             return null;
149         } finally {
150             if (fail) _unlock(true);
151         }
152     }
153     
154     protected final Boolean JavaDoc _handleAdd(RefObject associationEnd1, RefObject associationEnd2) {
155         try {
156             Boolean JavaDoc result = _handleLocalAdd(associationEnd1, associationEnd2);
157             if (!associationEnd1.refOutermostPackage().equals(associationEnd2.refOutermostPackage())) {
158                 _addExternalLink(this, associationEnd1, associationEnd2);
159             }
160             return result;
161         } catch (StorageException e) {
162             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
163         }
164     }
165     
166     protected final Boolean JavaDoc _handleLocalAdd(RefObject associationEnd1, RefObject associationEnd2) throws StorageException {
167         return getAssociationDelegate().addLink(((BaseObjectHandler)associationEnd1)._getDelegate().getMofId(), ((BaseObjectHandler)associationEnd2)._getDelegate().getMofId()) ? Boolean.TRUE : Boolean.FALSE;
168     }
169     
170     protected final void _postAdd(Boolean JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
171         _unlock(fail);
172     }
173     
174     protected final Object JavaDoc _preRemove(RefObject associationEnd1, RefObject associationEnd2) {
175         boolean fail = true;
176         _lock(true);
177         try {
178             if (_getMdrStorage().eventsEnabled()) {
179                 AssociationEvent event = new AssociationEvent(
180                 this,
181                 AssociationEvent.EVENT_ASSOCIATION_REMOVE,
182                 associationEnd1,
183                 getAssociationDelegate().getEnd1Name(),
184                 associationEnd2,
185                 null,
186                 AssociationEvent.POSITION_NONE
187                 );
188                 _getMdrStorage().getEventNotifier().ASSOCIATION.firePlannedChange(this, event);
189                 fail = false;
190                 return event;
191             }
192             fail = false;
193             return null;
194         } finally {
195             if (fail) _unlock(true);
196         }
197     }
198     
199     protected final Boolean JavaDoc _handleRemove(RefObject associationEnd1, RefObject associationEnd2) {
200         try {
201             Boolean JavaDoc result = _handleLocalRemove(associationEnd1, associationEnd2);
202             if (!associationEnd1.refOutermostPackage().equals (associationEnd2.refOutermostPackage())) {
203                 _removeExternalLink(this, associationEnd1, associationEnd2);
204             }
205             return result;
206         } catch (StorageException e) {
207             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
208         }
209     }
210     
211     protected final Boolean JavaDoc _handleLocalRemove(RefObject associationEnd1, RefObject associationEnd2) throws StorageException {
212         return getAssociationDelegate().removeLink(((BaseObjectHandler)associationEnd1)._getDelegate().getMofId(), ((BaseObjectHandler)associationEnd2)._getDelegate().getMofId()) ? Boolean.TRUE : Boolean.FALSE;
213     }
214     
215     protected final void _postRemove(Boolean JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
216         _unlock(fail);
217     }
218
219     /* -------------------------------------------------------------------- */
220     /* -- Implementation of javax.jmi.reflect.RefAssociation -------------- */
221     /* -------------------------------------------------------------------- */
222
223     public final boolean refRemoveLink(RefObject firstEnd, RefObject secondEnd) {
224         try {
225             return _remove(firstEnd, secondEnd);
226         } catch (ClassCastException JavaDoc e) {
227             // this will throw TypeMismatchException or DebugException if the mismatch is not found
228
getAssociationDelegate().checkType(firstEnd, secondEnd);
229             // this is here only to make the compiler happy
230
return false;
231         }
232     }
233     
234     public final boolean refAddLink(RefObject firstEnd, RefObject secondEnd) {
235         try {
236             return _add(firstEnd, secondEnd);
237         } catch (ClassCastException JavaDoc e) {
238             // this will throw TypeMismatchException or DebugException if the mismatch is not found
239
getAssociationDelegate().checkType(firstEnd, secondEnd);
240             // this is here only to make the compiler happy
241
return false;
242         }
243     }
244     
245     public final Collection refQuery(RefObject queryEnd, RefObject queryObject) {
246         try {
247             return refQuery(((AssociationEnd) queryEnd).getName(), queryObject);
248         } catch (InvalidNameException e) {
249             throw new InvalidCallException(null, queryEnd);
250         } catch (ClassCastException JavaDoc e) {
251             throw new InvalidCallException(null, queryEnd);
252         }
253     }
254     
255     public final Collection refQuery(String JavaDoc endName, RefObject queryObject) {
256         Object JavaDoc result;
257         try {
258             result = _query(endName, queryObject);
259         } catch (ClassCastException JavaDoc e) {
260             // this will throw TypeMismatchException or DebugException if the mismatch is not found
261
if (getAssociationDelegate().getEnd1Name().equals(endName)) {
262                 getAssociationDelegate().checkType(queryObject, null);
263             } else {
264                 getAssociationDelegate().checkType(null, queryObject);
265             }
266             // this is here only to make the compiler happy
267
return null;
268         }
269         Collection col;
270         if (result instanceof Collection) {
271             return (Collection) result;
272         }
273         if (result == null) {
274             return new ArrayList(0);
275         }
276         col = new ArrayList(1);
277         col.add(result);
278         return col;
279     }
280     
281     public final boolean refLinkExists(RefObject firstEnd, RefObject secondEnd) {
282         try {
283             return _exists(firstEnd, secondEnd);
284         } catch (ClassCastException JavaDoc e) {
285             // this will throw TypeMismatchException or DebugException if the mismatch is not found
286
getAssociationDelegate().checkType(firstEnd, secondEnd);
287             // this is here only to make the compiler happy
288
return false;
289         }
290     }
291
292     /* -------------------------------------------------------------------- */
293     /* -- abstract methods to be implemented by generated handlers -------- */
294     /* -------------------------------------------------------------------- */
295
296     public abstract boolean _add(RefObject end1, RefObject end2);
297     public abstract boolean _remove(RefObject end1, RefObject end2);
298     public abstract Object JavaDoc _query(String JavaDoc endName, RefObject end);
299     public abstract boolean _exists(RefObject end1, RefObject end2);
300     public abstract Collection _all();
301     
302     // derived associations must override this method if one of the association ends
303
// is single-valued!
304
/*
305     public void replace(RefObject oldEnd1, RefObject oldEnd2, RefObject newEnd1, RefObject newEnd2) {
306     }
307
308     /* -------------------------------------------------------------------- */

309     /* -- Implementation of org.netbeans.api.mdr.events.MDRChangeSource --- */
310     /* -------------------------------------------------------------------- */
311     
312     /** Registers a listener for receiving all event notifications
313      * fired on this object.
314      * @param listener Object that implements {@link MDRChangeListener} interface.
315      */

316     public void addListener(MDRChangeListener listener) {
317         addListener(listener, MDRChangeEvent.EVENTMASK_ALL);
318     }
319     
320     /** Registers a listener for receiving event notifications.
321      * @param listener Object that implements {@link MDRChangeListener} interface.
322      * @param mask bitmask to filter types of events the listener listens on
323      */

324     public void addListener(MDRChangeListener listener, int mask) {
325         _getMdrStorage().getEventNotifier().ASSOCIATION.addListener(listener, mask, this);
326     }
327     
328     /** Removes listener from the list of objects registered for event notifications.
329      * @param listener Object that implements {@link MDRChangeListener} interface.
330      */

331     public void removeListener(MDRChangeListener listener) {
332         _getMdrStorage().getEventNotifier().ASSOCIATION.removeListener(listener, this);
333     }
334     
335     /** Removes listener from the list of objects registered for event notifications.
336      * @param listener Object that implements {@link MDRChangeListener} interface.
337      * @param mask determines type of the events the listeners stops to listen on
338      */

339     public void removeListener(MDRChangeListener listener, int mask) {
340         _getMdrStorage().getEventNotifier().ASSOCIATION.removeListener(listener, mask, this);
341     }
342
343     /* --------------------------------------------------------------------- */
344     /* -- -- */
345     /* --------------------------------------------------------------------- */
346     
347     static void _removeExternalLink(AssociationHandler associationHandler, RefObject thisEnd, RefObject otherEnd) {
348         if (MdrStorage.isTransientMofId (((BaseObjectHandler)thisEnd)._getDelegate().getMofId ()) || MdrStorage.isTransientMofId (((BaseObjectHandler)otherEnd)._getDelegate().getMofId ()))
349             return;
350         RefPackage oeip = null;
351         if (thisEnd.refOutermostPackage().equals(associationHandler.refOutermostPackage()))
352             oeip = otherEnd.refOutermostPackage();
353         else
354             oeip = thisEnd.refOutermostPackage();
355         
356         String JavaDoc name = ((javax.jmi.model.Association)associationHandler.refMetaObject()).getName();
357         RefAssociation oea = _findAssociation(oeip, name);
358         if (oea instanceof AssociationHandler) {
359             boolean failed = true;
360             Boolean JavaDoc result = null;
361             try {
362                 ((AssociationHandler)oea)._preRemove (thisEnd, otherEnd);
363                 result = ((AssociationHandler)oea)._handleLocalRemove(thisEnd, otherEnd);
364                 failed = false;
365             }catch (Exception JavaDoc e) {
366             }
367             finally {
368                 ((AssociationHandler)oea)._postRemove (result, null, failed);
369             }
370         }
371     }
372     
373     static void _addExternalLink(AssociationHandler associationHandler, RefObject thisEnd, RefObject otherEnd) {
374         if (MdrStorage.isTransientMofId (((BaseObjectHandler)thisEnd)._getDelegate().getMofId ()) || MdrStorage.isTransientMofId (((BaseObjectHandler)otherEnd)._getDelegate().getMofId ()))
375             return;
376         RefPackage oeip = null;
377         if (thisEnd.refOutermostPackage().equals(associationHandler.refOutermostPackage()))
378             oeip = otherEnd.refOutermostPackage();
379         else
380             oeip = thisEnd.refOutermostPackage ();
381         String JavaDoc name = ((javax.jmi.model.Association)associationHandler.refMetaObject()).getName();
382         RefAssociation oea = _findAssociation (oeip, name);
383         if (oea instanceof AssociationHandler) {
384             boolean failed = true;
385             Boolean JavaDoc result = null;
386             try {
387                 ((AssociationHandler)oea)._preAdd (thisEnd, otherEnd);
388                 result = ((AssociationHandler)oea)._handleLocalAdd(thisEnd, otherEnd);
389                 failed = false;
390             }catch (Exception JavaDoc e) {}
391             finally {
392                 ((AssociationHandler)oea)._postAdd (result, null, failed);
393             }
394         }
395     }
396     
397     // [PENDING] (MaM) This is wrong! Method needs to use FQN to find the association
398
private static RefAssociation _findAssociation (RefPackage opkg, String JavaDoc name) {
399         ArrayList list = new ArrayList ();
400         list.add (opkg);
401         while (!list.isEmpty()) {
402             RefPackage pkg = (RefPackage) list.remove (0);
403             try {
404                 RefAssociation oea = pkg.refAssociation (name);
405                 return oea;
406             } catch (javax.jmi.reflect.InvalidNameException invalidName) {
407             }
408             Object JavaDoc innerPackages = pkg.refAllPackages();
409             if (innerPackages instanceof RefPackage) {
410                 list.add (innerPackages);
411             }
412             else {
413                 list.addAll ((Collection)innerPackages);
414             }
415         }
416         return null;
417     }
418     
419     /* ---------------------------------------------------------------- */
420     /* -- Implementation of abstract methods from BaseObjectHandler --- */
421     /* ---------------------------------------------------------------- */
422
423     protected final Collection _recursiveVerify(Collection violations, Set visited) {
424         _lock(false);
425         try {
426             _verify(violations);
427             visited.add(this);
428             return violations;
429         } finally {
430             _unlock();
431         }
432     }
433
434     protected Collection _verify(Collection violations) {
435         _lock(false);
436         try {
437             getAssociationDelegate().verify(violations);
438             return violations;
439         } catch (StorageException e) {
440             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
441         } finally {
442             _unlock();
443         }
444     }
445     
446     /* -------------------------------------------------------------------- */
447     /* -- AssociationHandler.RefAssociationLinkImpl (inner class) --------- */
448     /* -------------------------------------------------------------------- */
449     
450     static class RefAssociationLinkImpl implements RefAssociationLink {
451         private final RefObject firstEnd;
452         private final RefObject secondEnd;
453         
454         RefAssociationLinkImpl(RefObject firstEnd, RefObject secondEnd) {
455             this.firstEnd = firstEnd;
456             this.secondEnd = secondEnd;
457         }
458         
459         public RefObject refFirstEnd() {
460             return firstEnd;
461         }
462         
463         public RefObject refSecondEnd() {
464             return secondEnd;
465         }
466     }
467 }
468
Popular Tags