1 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 37 public abstract class ClassProxyHandler extends FeaturedHandler implements RefClass { 38 39 40 41 42 43 protected ClassProxyHandler(StorableClass storable) { 44 super(storable); 45 } 46 47 48 49 50 51 private StorableClass getClassDelegate() { 52 return (StorableClass) _getDelegate(); 53 } 54 55 56 57 58 59 protected final Object _preCreate(Object 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 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 [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 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 _preEnum(String enumName, String literal) { 114 _lock(false); 115 return null; 116 } 117 118 protected final RefEnum _handleEnum(String enumName, String literal) { 119 return EnumResolver.resolveEnum(getClassDelegate().getDatatypeDesc(enumName).getIfcName(), literal); 120 } 121 122 protected final void _postEnum(RefEnum result, Object extraInfo, boolean fail) { 123 _unlock(); 124 } 125 126 protected final Object _preStruct(String structName, Object fieldValues[]) { 127 _lock(false); 128 return null; 129 } 130 131 protected final RefStruct _handleStruct(String structName, Object fieldValues[]) { 132 return StructImpl.newInstance(getClassDelegate().getDatatypeDesc(structName), fieldValues); 133 } 134 135 protected final void _postStruct(RefStruct result, Object extraInfo, boolean fail) { 136 _unlock(); 137 } 138 139 protected final Object _handleGet(String attrName) { 140 Object 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 attrName, Object 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 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 params[] = (args == null ? null : args.toArray()); 199 return _createInstance(params); 200 } 201 202 public final RefEnum refGetEnum(String enumName, java.lang.String name) { 203 boolean fail = true; 204 Object 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 e) { 212 throw new InvalidNameException(enumName); 213 } finally { 214 _postEnum(result, extraInfo, fail); 215 } 216 } 217 218 public final RefEnum refGetEnum(RefObject en, String name) { 219 EnumerationType et; 220 try { 221 et = (EnumerationType) en; 222 } catch (ClassCastException 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 structName, java.util.List params) { 233 Object 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 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 253 254 255 public RefObject _createInstance(Object [] params) { 256 throw new InvalidCallException(this, null, "Cannot create instance of an abstract class."); 257 } 258 259 public abstract RefStruct _createStruct(String structName, Object [] params); 260 261 262 263 264 265 269 public void addListener(MDRChangeListener listener) { 270 addListener(listener, MDRChangeEvent.EVENTMASK_ALL); 271 } 272 273 277 public void addListener(MDRChangeListener listener, int mask) { 278 _getMdrStorage().getEventNotifier().CLASS.addListener(listener, mask, this); 279 } 280 281 284 public void removeListener(MDRChangeListener listener) { 285 _getMdrStorage().getEventNotifier().CLASS.removeListener(listener, this); 286 } 287 288 292 public void removeListener(MDRChangeListener listener, int mask) { 293 _getMdrStorage().getEventNotifier().CLASS.removeListener(listener, mask, this); 294 } 295 296 297 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 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 |