KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jmi.model.*;
25 import javax.jmi.reflect.*;
26
27 import org.netbeans.api.mdr.events.*;
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 ClassProxies
34  *
35  * @author Petr Hrebejk, Martin Matula
36  */

37 public abstract class ClassProxyHandler extends FeaturedHandler implements RefClass {
38
39     /* -------------------------------------------------------------------- */
40     /* -- Constructor ----------------------------------------------------- */
41     /* -------------------------------------------------------------------- */
42
43     protected ClassProxyHandler(StorableClass storable) {
44         super(storable);
45     }
46
47     /* -------------------------------------------------------------------- */
48     /* -- Helper methods -------------------------------------------------- */
49     /* -------------------------------------------------------------------- */
50
51     private StorableClass getClassDelegate() {
52         return (StorableClass) _getDelegate();
53     }
54     
55     /* -------------------------------------------------------------------- */
56     /* -- Methods to be called by generated handlers (_pre, _handle, _post) */
57     /* -------------------------------------------------------------------- */
58     
59     protected final Object JavaDoc _preCreate(Object JavaDoc params[]) {
60         boolean fail = true;
61         _lock(true);
62         try {
63             if (_getMdrStorage().eventsEnabled()) {
64                 CreateInstanceEvent event = new CreateInstanceEvent(
65                     params == null ? null : new ImmutableList(params)
66                 );
67                 _getMdrStorage().getEventNotifier().CLASS.firePlannedChange(this, event);
68                 fail = false;
69                 return event;
70             }
71             fail = false;
72             return null;
73         } finally {
74             if (fail) _unlock(true);
75         }
76     }
77
78     protected final RefObject _handleCreate(Object JavaDoc params[]) {
79         try {
80             if (getClassDelegate().isSingleton()) {
81                 Iterator it = refAllOfClass().iterator();
82                 if (it.hasNext()) {
83                     throw new AlreadyExistsException((RefObject) it.next());
84                 }
85             }
86             if (params == null) {
87                 params = new Object JavaDoc[getClassDelegate().getAttrCount()];
88             }
89             StorableObject storableObject;
90             if (getClassDelegate ().isTransient ()) {
91                 storableObject = new TransientStorableObject (getClassDelegate().getMdrStorage(), _getDelegate().getImmediatePackageId(), _getDelegate().getMetaObjectId(), _getDelegate().getMofId(), params);
92             }
93             else {
94                 storableObject = new StorableObject(getClassDelegate().getMdrStorage(), _getDelegate().getImmediatePackageId(), _getDelegate().getMetaObjectId(), _getDelegate().getMofId(), params);
95             }
96             return (RefObject) _getRepository().getHandler (storableObject);
97         } catch (StorageException e) {
98             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
99         }
100     }
101     
102     protected final void _postCreate(RefObject result, Object JavaDoc extraInfo, boolean fail) {
103         try {
104             if (extraInfo != null) {
105                 CreateInstanceEvent event = (CreateInstanceEvent) extraInfo;
106                 event.setInstance(result);
107             }
108         } finally {
109             _unlock(fail);
110         }
111     }
112     
113     protected final Object JavaDoc _preEnum(String JavaDoc enumName, String JavaDoc literal) {
114         _lock(false);
115         return null;
116     }
117     
118     protected final RefEnum _handleEnum(String JavaDoc enumName, String JavaDoc literal) {
119         return EnumResolver.resolveEnum(getClassDelegate().getDatatypeDesc(enumName).getIfcName(), literal);
120     }
121     
122     protected final void _postEnum(RefEnum result, Object JavaDoc extraInfo, boolean fail) {
123         _unlock();
124     }
125     
126     protected final Object JavaDoc _preStruct(String JavaDoc structName, Object JavaDoc fieldValues[]) {
127         _lock(false);
128         return null;
129     }
130
131     protected final RefStruct _handleStruct(String JavaDoc structName, Object JavaDoc fieldValues[]) {
132         return StructImpl.newInstance(getClassDelegate().getDatatypeDesc(structName), fieldValues);
133     }
134
135     protected final void _postStruct(RefStruct result, Object JavaDoc extraInfo, boolean fail) {
136         _unlock();
137     }
138     
139     protected final Object JavaDoc _handleGet(String JavaDoc attrName) {
140         Object JavaDoc result;
141         
142         try {
143             result = getClassDelegate().getAttribute(attrName);
144             if (result instanceof Collection) {
145                 if (result instanceof AttrList) {
146                     result = new AttrListWrapper(this, 0, attrName);
147                 } else if (result instanceof AttrCollection) {
148                     result = new AttrCollWrapper(this, 0, attrName);
149                 } else if (result instanceof AttrImmutList) {
150                     result = new AttrImmutListWrapper(_getMdrStorage(), this, 0, attrName);
151                 }
152             }
153             return result;
154         } catch (StorageException e) {
155             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
156         }
157     }
158     
159     protected final void _handleSet(String JavaDoc attrName, Object JavaDoc newValue) {
160         try {
161             getClassDelegate().setAttribute(attrName, newValue);
162         } catch (StorageException e) {
163             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
164         }
165     }
166
167     /* -------------------------------------------------------------------- */
168     /* -- Implementation of javax.jmi.reflect.RefClass -------------------- */
169     /* -------------------------------------------------------------------- */
170
171     public final Collection refAllOfType() {
172         try {
173             _lock(false);
174             return _allOfClass(true);
175         } finally {
176             _unlock();
177         }
178     }
179     
180     public final Collection refAllOfClass() {
181         try {
182             _lock(false);
183             return _allOfClass(false);
184         } finally {
185             _unlock();
186         }
187     }
188     
189     protected Collection _allOfClass(boolean includeSubtypes) {
190         try {
191             return new IndexSetWrapper(_getMdrStorage(), getClassDelegate().allObjects(includeSubtypes));
192         } catch (StorageException e) {
193             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
194         }
195     }
196
197     public final RefObject refCreateInstance(List args) {
198         Object JavaDoc params[] = (args == null ? null : args.toArray());
199         return _createInstance(params);
200     }
201
202     public final RefEnum refGetEnum(String JavaDoc enumName, java.lang.String JavaDoc name) {
203         boolean fail = true;
204         Object JavaDoc extraInfo = null;
205         RefEnum result = null;
206         try {
207             extraInfo = _preEnum(enumName, name);
208             result = _handleEnum(enumName, name);
209             fail = false;
210             return result;
211         } catch (NullPointerException JavaDoc e) {
212             throw new InvalidNameException(enumName);
213         } finally {
214             _postEnum(result, extraInfo, fail);
215         }
216     }
217     
218     public final RefEnum refGetEnum(RefObject en, String JavaDoc name) {
219         EnumerationType et;
220         try {
221             et = (EnumerationType) en;
222         } catch (ClassCastException JavaDoc e) {
223             throw new InvalidCallException(this, en, "Invalid enumeration designator: " + en);
224         }
225         try {
226             return refGetEnum(et.getName(), name);
227         } catch (InvalidNameException e) {
228             throw new InvalidCallException(this, en);
229         }
230     }
231
232     public final RefStruct refCreateStruct(String JavaDoc structName, java.util.List JavaDoc params) {
233         Object JavaDoc args[] = params.toArray();
234         return _createStruct(structName, args);
235     }
236     
237     public final RefStruct refCreateStruct(RefObject struct, List params) {
238         StructureType st;
239         try {
240             st = (StructureType) struct;
241         } catch (ClassCastException JavaDoc e) {
242             throw new InvalidCallException(this, struct, "Invalid structure designator: " + struct);
243         }
244         try {
245             return refCreateStruct(st.getName(), params);
246         } catch (InvalidNameException e) {
247             throw new InvalidCallException(this, struct);
248         }
249     }
250     
251     /* ----------------------------------------------------------- */
252     /* -- methods to be implemented by generated handlers -------- */
253     /* ----------------------------------------------------------- */
254
255     public RefObject _createInstance(Object JavaDoc[] params) {
256         throw new InvalidCallException(this, null, "Cannot create instance of an abstract class.");
257     }
258
259     public abstract RefStruct _createStruct(String JavaDoc structName, Object JavaDoc[] params);
260     
261     /* -------------------------------------------------------------------- */
262     /* -- Implementation of org.netbeans.api.mdr.events.MDRChangeSource --- */
263     /* -------------------------------------------------------------------- */
264     
265     /** Registers a listener for receiving all event notifications
266      * fired on this object.
267      * @param listener Object that implements {@link MDRChangeListener} interface.
268      */

269     public void addListener(MDRChangeListener listener) {
270         addListener(listener, MDRChangeEvent.EVENTMASK_ALL);
271     }
272     
273     /** Registers a listener for receiving event notifications.
274      * @param listener Object that implements {@link MDRChangeListener} interface.
275      * @param mask bitmask to filter types of events the listener listens on
276      */

277     public void addListener(MDRChangeListener listener, int mask) {
278         _getMdrStorage().getEventNotifier().CLASS.addListener(listener, mask, this);
279     }
280     
281     /** Removes listener from the list of objects registered for event notifications.
282      * @param listener Object that implements {@link MDRChangeListener} interface.
283      */

284     public void removeListener(MDRChangeListener listener) {
285         _getMdrStorage().getEventNotifier().CLASS.removeListener(listener, this);
286     }
287     
288     /** Removes listener from the list of objects registered for event notifications.
289      * @param listener Object that implements {@link MDRChangeListener} interface.
290      * @param mask determines type of the events the listeners stops to listen on
291      */

292     public void removeListener(MDRChangeListener listener, int mask) {
293         _getMdrStorage().getEventNotifier().CLASS.removeListener(listener, mask, this);
294     }
295     
296     /* -------------------------------------------------------------------- */
297     /* -- ClassProxyHandler.CreateInstanceEvent (inner class) ------------- */
298     /* -------------------------------------------------------------------- */
299     
300     private class CreateInstanceEvent extends InstanceEvent {
301         CreateInstanceEvent(List params) {
302             super(ClassProxyHandler.this, EVENT_INSTANCE_CREATE, params, null);
303         }
304         
305         void setInstance(RefObject instance) {
306             this.instance = instance;
307         }
308     }
309
310     /* ---------------------------------------------------------------- */
311     /* -- Implementation of abstract methods from BaseObjectHandler --- */
312     /* ---------------------------------------------------------------- */
313
314     protected final Collection _recursiveVerify(Collection violations, Set visited) {
315         _lock(false);
316         try {
317             _verify(violations);
318             visited.add(this);
319             for (Iterator it = refAllOfClass().iterator(); it.hasNext();) {
320                 InstanceHandler obj = (InstanceHandler) it.next();
321                 if (!visited.contains(obj)) obj._recursiveVerify(violations, visited);
322             }
323             return violations;
324         } finally {
325             _unlock();
326         }
327     }
328
329     protected Collection _verify(Collection violations) {
330         _lock(false);
331         try {
332             getClassDelegate().verify(violations);
333             return violations;
334         } finally {
335             _unlock();
336         }
337     }
338 }
339
Popular Tags