KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 public abstract class FeaturedHandler extends BaseObjectHandler implements RefFeatured {
38
39     /* -------------------------------------------------------------------- */
40     /* -- Constructor ----------------------------------------------------- */
41     /* -------------------------------------------------------------------- */
42
43     /** Creates new FeaturedHandler */
44     protected FeaturedHandler(StorableFeatured storable) {
45         super(storable);
46     }
47
48     /* -------------------------------------------------------------------- */
49     /* -- Helper methods -------------------------------------------------- */
50     /* -------------------------------------------------------------------- */
51     
52     private StorableFeatured getFeaturedDelegate() {
53         return (StorableFeatured) _getDelegate();
54     }
55
56     /* -------------------------------------------------------------------- */
57     /* -- Methods to be called by generated handlers (_pre, _handle, _post) */
58     /* -------------------------------------------------------------------- */
59     
60     protected final Object JavaDoc _preGet(String JavaDoc featureName) {
61         _lock(false);
62         return featureName;
63     }
64     
65     protected final void _postGet(Object JavaDoc result, Object JavaDoc extraInfo, boolean fail) {
66         try {
67             if (result instanceof AttrCollWrapper) {
68                 ((AttrCollWrapper) result).setAttrName((String JavaDoc) extraInfo);
69             }
70         } finally {
71             _unlock();
72         }
73     }
74     
75     protected final Object JavaDoc _preSet(String JavaDoc featureName, Object JavaDoc newValue) {
76         boolean fail = true;
77         _lock(true);
78         try {
79             if (_getMdrStorage().eventsEnabled()) {
80                 Object JavaDoc oldValue = _getAttribute(featureName);
81         // if (newValue == oldValue || (newValue != null && newValue.equals(oldValue))) {
82
// return null;
83
// } else {
84
AttributeEvent event = new AttributeEvent(
85                         this,
86                         AttributeEvent.EVENT_ATTRIBUTE_SET,
87                         featureName,
88                         oldValue,
89                         newValue,
90                         AttributeEvent.POSITION_NONE
91                     );
92                     if (this instanceof RefObject) {
93                         _getMdrStorage().getEventNotifier().INSTANCE.firePlannedChange(this, event);
94                     } else {
95                         _getMdrStorage().getEventNotifier().CLASS.firePlannedChange(this, event);
96                     }
97                     fail = false;
98                     return event;
99             }
100             fail = false;
101             return null;
102         } finally {
103             if (fail) _unlock(true);
104         }
105 // }
106
}
107     
108     protected final void _postSet(Object JavaDoc extraInfo, boolean fail) {
109         _unlock(fail);
110     }
111     
112
113     // RefFeatured implementation ------------------------------------------------
114

115     public final Object JavaDoc refInvokeOperation(String JavaDoc operationName, List args) throws RefException {
116         return _invokeOperation(operationName, args.toArray());
117     }
118
119     public final Object JavaDoc refInvokeOperation(RefObject requestedOperation, List args) throws RefException {
120         return _invokeOperation(((Operation) requestedOperation).getName(), args.toArray());
121     }
122
123     public void refSetValue(String JavaDoc featureName, java.lang.Object JavaDoc value) {
124         _lock(true);
125         try {
126             _setAttribute(featureName, value);
127         } finally {
128             _unlock();
129         }
130     }
131     
132     public void refSetValue(RefObject feature, Object JavaDoc value) {
133         _lock(true);
134         try {
135 /* StorableClass cls = getFeaturedDelegate().getClassProxy();
136             try {
137                 StorableClass.AttributeDescriptor desc = cls.getAttrDesc(((Attribute) feature).getName());
138                 if (!desc.getMofId().equals(feature.refMofId())) throw new InvalidCallException(null, feature);
139             } catch (DebugException e) {
140                 throw new InvalidCallException(null, feature);
141             }
142 */

143             _setAttribute(((Attribute) feature).getName(), value);
144         } catch (InvalidNameException e) {
145             throw new InvalidCallException(null, feature);
146         } catch (ClassCastException JavaDoc e) {
147             throw new InvalidCallException(null, feature);
148 /* } catch (StorageException e) {
149             throw Logger.getDefault().annotate(new DebugException(), e);
150 */
} finally {
151             _unlock();
152         }
153     }
154
155     public Object JavaDoc refGetValue(String JavaDoc featureName) {
156         _lock(false);
157         try {
158             return _getAttribute(featureName);
159         } finally {
160             _unlock();
161         }
162     }
163     
164     public Object JavaDoc refGetValue(RefObject feature) {
165         _lock(false);
166         try {
167             /*
168             StorableClass cls = getFeaturedDelegate().getClassProxy();
169             try {
170                 StorableClass.AttributeDescriptor desc = cls.getAttrDesc(((Attribute) feature).getName());
171                 if (!desc.getMofId().equals(feature.refMofId())) throw new InvalidCallException(null, feature);
172             } catch (DebugException e) {
173                 throw new InvalidCallException(null, feature);
174             }
175             */

176             return _getAttribute(((Attribute) feature).getName());
177         } catch (InvalidNameException e) {
178             throw new InvalidCallException(null, feature);
179         } catch (ClassCastException JavaDoc e) {
180             throw new InvalidCallException(null, feature);
181         /*
182         } catch (StorageException e) {
183             throw Logger.getDefault().annotate(new DebugException(), e);
184          */

185         } finally {
186             _unlock();
187         }
188     }
189
190     /* -------------------------------------------------------------------- */
191     /* -- abstract methods to be implemented by generated handlers -------- */
192     /* -------------------------------------------------------------------- */
193
194     public abstract void _setAttribute(String JavaDoc attrName, Object JavaDoc newValue);
195     public abstract Object JavaDoc _getAttribute(String JavaDoc attrName);
196     
197     public abstract Object JavaDoc _invokeOperation(String JavaDoc operName, Object JavaDoc[] params);
198 }
199
Popular Tags