KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public abstract class PackageProxyHandler extends BaseObjectHandler implements RefPackage {
38
39     /* -------------------------------------------------------------------- */
40     /* -- Private attributes ---------------------------------------------- */
41     /* -------------------------------------------------------------------- */
42
43     private HashMap clusteredPackages = null;
44
45     /* -------------------------------------------------------------------- */
46     /* -- Constructor ----------------------------------------------------- */
47     /* -------------------------------------------------------------------- */
48     
49     /** Creates new PackageProxy
50     */

51     protected PackageProxyHandler(StorablePackage storable) {
52         super(storable);
53     }
54
55     /* -------------------------------------------------------------------- */
56     /* -- Helper methods -------------------------------------------------- */
57     /* -------------------------------------------------------------------- */
58
59     // Handler specific methods --------------------------------------------------
60
private void checkClustered() {
61         if (clusteredPackages == null) {
62             MofPackage metaPkg = (MofPackage) refMetaObject();
63             clusteredPackages = new HashMap();
64             ModelElement element;
65             for (Iterator it = metaPkg.allSupertypes().iterator(); it.hasNext();) {
66                 for (Iterator it2 = ((GeneralizableElement) it.next()).getContents().iterator(); it2.hasNext();) {
67                     element = (ModelElement) it2.next();
68                     if ((element instanceof Import) && ((Import) element).isClustered()) {
69                         clusteredPackages.put(((Import) element).getImportedNamespace().refMofId(), element.getName());
70                     }
71                 }
72             }
73         }
74     }
75
76     private StorablePackage getPackageDelegate() {
77         return (StorablePackage) _getDelegate();
78     }
79
80     /* -------------------------------------------------------------------- */
81     /* -- Methods to be called by generated handlers (_pre, _handle, _post) */
82     /* -------------------------------------------------------------------- */
83
84     protected final Object JavaDoc _preGetPackageProxy(String JavaDoc proxyName) {
85         _lock(false);
86         return null;
87     }
88
89     protected final RefPackage _handleGetPackageProxy(String JavaDoc proxyName) {
90         try {
91             return (RefPackage) _getRepository().getHandler(getPackageDelegate().getPackage(proxyName));
92         } catch (StorageException e) {
93             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
94         }
95     }
96     
97     protected final void _postGetPackageProxy(RefPackage result, Object JavaDoc extraInfo, boolean fail) {
98         _unlock();
99     }
100
101     protected final Object JavaDoc _preGetClassProxy(String JavaDoc proxyName) {
102         _lock(false);
103         return null;
104     }
105
106     protected final RefClass _handleGetClassProxy(String JavaDoc proxyName) {
107         try {
108             return (RefClass) _getRepository().getHandler(getPackageDelegate().getClassProxy(proxyName));
109         } catch (StorageException e) {
110             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
111         }
112     }
113
114     protected final void _postGetClassProxy(RefClass result, Object JavaDoc extraInfo, boolean fail) {
115         _unlock();
116     }
117
118     protected final Object JavaDoc _preGetAssociationProxy(String JavaDoc proxyName) {
119         _lock(false);
120         return null;
121     }
122
123     protected final RefAssociation _handleGetAssociationProxy(String JavaDoc proxyName) {
124         try {
125             return (RefAssociation) _getRepository().getHandler(getPackageDelegate().getAssociation(proxyName));
126         } catch (StorageException e) {
127             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
128         }
129     }
130
131     protected final void _postGetAssociationProxy(RefAssociation result, Object JavaDoc extraInfo, boolean fail) {
132         _unlock();
133     }
134
135     protected final Object JavaDoc _preEnum(String JavaDoc enumName, String JavaDoc literal) {
136         _lock(false);
137         return null;
138     }
139     
140     protected final RefEnum _handleEnum(String JavaDoc enumName, String JavaDoc literal) {
141         return EnumResolver.resolveEnum(getPackageDelegate().getDatatypeDesc(enumName).getIfcName(), literal);
142     }
143     
144     protected final void _postEnum(RefEnum result, Object JavaDoc extraInfo, boolean fail) {
145         _unlock();
146     }
147     
148     protected final Object JavaDoc _preStruct(String JavaDoc structName, Object JavaDoc fieldValues[]) {
149         _lock(false);
150         return null;
151     }
152
153     protected final RefStruct _handleStruct(String JavaDoc structName, Object JavaDoc fieldValues[]) {
154         return StructImpl.newInstance(getPackageDelegate().getDatatypeDesc(structName), fieldValues);
155     }
156
157     protected final void _postStruct(RefStruct result, Object JavaDoc extraInfo, boolean fail) {
158         _unlock();
159     }
160
161     /* -------------------------------------------------------------------- */
162     /* -- Implementation of javax.jmi.reflect.RefPackage ------------------ */
163     /* -------------------------------------------------------------------- */
164
165     public final Collection refAllAssociations() {
166         _lock(false);
167         try {
168             return new IndexSetWrapper(_getMdrStorage(), getPackageDelegate().getAllAssociations());
169         } catch (StorageException e) {
170             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
171         } finally {
172             _unlock();
173         }
174     }
175
176     public final Collection refAllClasses() {
177         _lock(false);
178         try {
179             return new IndexSetWrapper(_getMdrStorage(), getPackageDelegate().getAllClasses());
180         } catch (StorageException e) {
181             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
182         } finally {
183             _unlock();
184         }
185     }
186
187     public final Collection refAllPackages() {
188         _lock(false);
189         try {
190             return new IndexSetWrapper(_getMdrStorage(), getPackageDelegate().getAllPackages());
191         } catch (StorageException e) {
192             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
193         } finally {
194             _unlock();
195         }
196     }
197
198     public final RefAssociation refAssociation(String JavaDoc assocName) {
199         return _getAssociation(assocName);
200     }
201     
202     public final RefAssociation refAssociation(RefObject assoc) {
203         Association a;
204         try {
205             a = (Association) assoc;
206         } catch (ClassCastException JavaDoc e) {
207             throw new InvalidCallException(this, assoc, "Invalid association designator: " + assoc);
208         }
209         try {
210             return refAssociation(a.getName());
211         } catch (InvalidNameException e) {
212             throw new InvalidCallException(this, assoc);
213         }
214     }
215
216     public final RefPackage refPackage(String JavaDoc name) {
217         return _getPackage(name);
218     }
219
220     public final RefPackage refPackage(RefObject nestedPackage) {
221         MofPackage pkg;
222         try {
223             pkg = (MofPackage) nestedPackage;
224         } catch (ClassCastException JavaDoc e) {
225             throw new InvalidCallException(this, nestedPackage, "Invalid package designator: " + nestedPackage);
226         }
227         _lock(false);
228         try {
229             checkClustered();
230             String JavaDoc name = (String JavaDoc) clusteredPackages.get(pkg.refMofId());
231         
232             if (name == null) {
233                 name = pkg.getName();
234             }
235
236             return refPackage(name);
237         } catch (InvalidNameException e) {
238             throw new InvalidCallException(this, nestedPackage);
239         } finally {
240             _unlock();
241         }
242     }
243
244     public final RefClass refClass(String JavaDoc name) {
245         return _getClass(name);
246     }
247     
248     public final RefClass refClass(RefObject type) {
249         MofClass cls;
250         try {
251             cls = (MofClass) type;
252         } catch (ClassCastException JavaDoc e) {
253             throw new InvalidCallException(this, type, "Invalid class designator: " + type);
254         }
255         try {
256             return refClass(cls.getName());
257         } catch (InvalidNameException e) {
258             throw new InvalidCallException(this, type);
259         }
260     }
261
262     public final RefEnum refGetEnum(String JavaDoc enumName, java.lang.String JavaDoc name) {
263         boolean fail = true;
264         Object JavaDoc extraInfo = null;
265         RefEnum result = null;
266         try {
267             extraInfo = _preEnum(enumName, name);
268             result = _handleEnum(enumName, name);
269             fail = false;
270             return result;
271         } catch (NullPointerException JavaDoc e) {
272             throw new InvalidNameException(enumName);
273         } finally {
274             _postEnum(result, extraInfo, fail);
275         }
276     }
277     
278     public final RefEnum refGetEnum(RefObject en, String JavaDoc name) {
279         EnumerationType et;
280         try {
281             et = (EnumerationType) en;
282         } catch (ClassCastException JavaDoc e) {
283             throw new InvalidCallException(this, en, "Invalid enumeration designator: " + en);
284         }
285         try {
286             return refGetEnum(et.getName(), name);
287         } catch (InvalidNameException e) {
288             throw new InvalidCallException(this, en);
289         }
290     }
291
292     public final RefStruct refCreateStruct(String JavaDoc structName, java.util.List JavaDoc params) {
293         Object JavaDoc args[] = params.toArray();
294         return _createStruct(structName, args);
295     }
296     
297     public final RefStruct refCreateStruct(RefObject struct, List params) {
298         StructureType st;
299         try {
300             st = (StructureType) struct;
301         } catch (ClassCastException JavaDoc e) {
302             throw new InvalidCallException(this, struct, "Invalid structure designator: " + struct);
303         }
304         try {
305             return refCreateStruct(st.getName(), params);
306         } catch (InvalidNameException e) {
307             throw new InvalidCallException(this, struct);
308         }
309     }
310     
311     public void refDelete() {
312         ExtentEvent event = null;
313         boolean fail = true;
314         _lock(true);
315         try {
316             if (_getMdrStorage().eventsEnabled()) {
317                 event = new ExtentEvent(
318                     this,
319                     ExtentEvent.EVENT_EXTENT_DELETE,
320                     getPackageDelegate().getContext(),
321                     refMetaObject(),
322                     null
323                 );
324                 _getMdrStorage().getEventNotifier().PACKAGE.firePlannedChange(this, event);
325             }
326             try {
327                 getPackageDelegate().delete();
328             } catch (StorageException e) {
329                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
330             }
331             fail = false;
332         } finally {
333             _unlock(fail);
334         }
335         _getRepository().removeHandler(_getMofId());
336     }
337
338     /* -------------------------------------------------------------------- */
339     /* -- abstract methods to be implemented by generated handlers -------- */
340     /* -------------------------------------------------------------------- */
341
342     public abstract RefClass _getClass(String JavaDoc className);
343     public abstract RefAssociation _getAssociation(String JavaDoc associationName);
344     public abstract RefPackage _getPackage(String JavaDoc packageName);
345     public abstract RefStruct _createStruct(String JavaDoc structName, Object JavaDoc[] params);
346
347     /* -------------------------------------------------------------------- */
348     /* -- Implementation of org.netbeans.api.mdr.events.MDRChangeSource --- */
349     /* -------------------------------------------------------------------- */
350     
351     /** Registers a listener for receiving all event notifications
352      * fired on this object.
353      * @param listener Object that implements {@link MDRChangeListener} interface.
354      */

355     public void addListener(MDRChangeListener listener) {
356         addListener(listener, MDRChangeEvent.EVENTMASK_ALL);
357     }
358     
359     /** Registers a listener for receiving event notifications.
360      * @param listener Object that implements {@link MDRChangeListener} interface.
361      * @param mask bitmask to filter types of events the listener listens on
362      */

363     public void addListener(MDRChangeListener listener, int mask) {
364         _getMdrStorage().getEventNotifier().PACKAGE.addListener(listener, mask, this);
365     }
366     
367     /** Removes listener from the list of objects registered for event notifications.
368      * @param listener Object that implements {@link MDRChangeListener} interface.
369      */

370     public void removeListener(MDRChangeListener listener) {
371         _getMdrStorage().getEventNotifier().PACKAGE.removeListener(listener, this);
372     }
373     
374     /** Removes listener from the list of objects registered for event notifications.
375      * @param listener Object that implements {@link MDRChangeListener} interface.
376      * @param mask determines type of the events the listeners stops to listen on
377      */

378     public void removeListener(MDRChangeListener listener, int mask) {
379         _getMdrStorage().getEventNotifier().PACKAGE.removeListener(listener, mask, this);
380     }
381
382     /* ---------------------------------------------------------------- */
383     /* -- Implementation of abstract methods from BaseObjectHandler --- */
384     /* ---------------------------------------------------------------- */
385
386     protected final Collection _recursiveVerify(Collection violations, Set visited) {
387         _lock(false);
388         try {
389             _verify(violations);
390             visited.add(this);
391             for (Iterator it = refAllPackages().iterator(); it.hasNext();) {
392                 PackageProxyHandler pkg = (PackageProxyHandler) it.next();
393                 if (!visited.contains(pkg)) pkg._recursiveVerify(violations, visited);
394             }
395             for (Iterator it = refAllAssociations().iterator(); it.hasNext();) {
396                 AssociationHandler assoc = (AssociationHandler) it.next();
397                 if (!visited.contains(assoc)) assoc._recursiveVerify(violations, visited);
398             }
399             for (Iterator it = refAllClasses().iterator(); it.hasNext();) {
400                 ClassProxyHandler cls = (ClassProxyHandler) it.next();
401                 if (!visited.contains(cls)) cls._recursiveVerify(violations, visited);
402             }
403             return violations;
404         } finally {
405             _unlock();
406         }
407     }
408     
409     protected Collection _verify(Collection violations) {
410         return violations;
411     }
412 }
413
Popular Tags