1 19 package org.netbeans.mdr.handlers; 20 21 import org.netbeans.api.mdr.events.*; 22 23 import javax.jmi.model.*; 24 import javax.jmi.reflect.*; 25 26 import java.util.*; 27 28 import org.netbeans.mdr.storagemodel.*; 29 import org.netbeans.mdr.util.DebugException; 30 import org.netbeans.mdr.persistence.StorageException; 31 import org.netbeans.mdr.util.Logger; 32 33 38 public abstract class InstanceHandler extends FeaturedHandler implements RefObject { 39 40 41 42 43 44 45 protected InstanceHandler(StorableObject storable) { 46 super(storable); 47 } 48 49 50 51 52 53 private StorableObject getInstanceDelegate() { 54 return (StorableObject) _getDelegate(); 55 } 56 57 private void _setReference(String name, Object value) { 58 Object extraInfo = null; 59 boolean fail = true; 60 extraInfo = _preSetR(name, value); 61 try { 62 _handleSetR(name, value); 63 fail = false; 64 } catch (NullPointerException e) { 65 throw new InvalidNameException(name); 67 } finally { 68 _postSetR(extraInfo, fail); 69 } 70 } 71 72 private Object _getReference(String name) { 73 Object extraInfo = null; 74 Object result = null; 75 boolean fail = true; 76 extraInfo = _preGetR(name); 77 try { 78 result = _handleGetR(name); 79 fail = false; 80 return result; 81 } catch (NullPointerException e) { 82 throw new InvalidNameException(name); 84 } finally { 85 _postGetR(result, extraInfo, fail); 86 } 87 } 88 89 private boolean _isReference(String name) { 90 try { 91 return getInstanceDelegate().getClassProxy().getReferenceDescriptor(name) != null; 92 } catch (StorageException e) { 93 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 94 } 95 } 96 97 98 99 100 101 protected final Object _preGetR(String featureName) { 102 _lock(false); 103 return null; 104 } 105 106 protected final Object _handleGetR(String featureName) { 107 StorableClass.ReferenceDescriptor reference = null; 108 try { 109 reference = getInstanceDelegate().getClassProxy().getReferenceDescriptor(featureName); 110 AssociationHandler assoc = (AssociationHandler) _getRepository().getHandler(reference.getAssociation()); 111 return assoc._query(reference.getEndName(), (RefObject) this); 113 } catch (ClassCastException e) { 114 if (reference.isFirstEnd()) { 116 reference.getAssociation().checkType(null, this); 117 } else { 118 reference.getAssociation().checkType(this, null); 119 } 120 return null; 121 } catch (StorageException e) { 122 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 123 } 124 } 125 126 protected final void _postGetR(Object result, Object extraInfo, boolean fail) { 127 _unlock(); 128 } 129 130 protected final Object _preSetR(String featureName, Object newValue) { 131 _lock(true); 132 return null; 134 } 135 136 protected final void _handleSetR(String featureName, Object newValue) { 137 try { 138 StorableClass.ReferenceDescriptor reference = getInstanceDelegate().getClassProxy().getReferenceDescriptor(featureName); 139 AssociationHandler assoc = (AssociationHandler) _getRepository().getHandler(reference.getAssociation()); 140 141 Object oldValue = _getReference(featureName); 143 144 if (reference.isFirstEnd()) { 146 if (oldValue != null) try { 147 assoc._remove((RefObject) oldValue, (RefObject) this); 148 } catch (ClassCastException e) { 149 ((StorableAssociation) assoc._getDelegate()).checkType(oldValue, this); 151 return; 153 } 154 if (newValue != null) try { 155 assoc._add((RefObject) newValue, (RefObject) this); 156 } catch (ClassCastException e) { 157 ((StorableAssociation) assoc._getDelegate()).checkType(newValue, this); 159 return; 161 } 162 } else { 163 if (oldValue != null) try { 164 assoc._remove((RefObject) this, (RefObject) oldValue); 165 } catch (ClassCastException e) { 166 ((StorableAssociation) assoc._getDelegate()).checkType(this, oldValue); 168 return; 170 } 171 if (newValue != null) try { 172 assoc._add((RefObject) this, (RefObject) newValue); 173 } catch (ClassCastException e) { 174 ((StorableAssociation) assoc._getDelegate()).checkType(this, newValue); 176 return; 178 } 179 } 180 181 } catch (StorageException e) { 186 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 187 } 188 } 189 190 protected final void _postSetR(Object extraInfo, boolean fail) { 191 _unlock(); 193 } 194 195 protected final Object _handleGet(int attrIndex) { 196 Object result; 197 198 try { 199 result = getInstanceDelegate().getAttribute(attrIndex); 200 if (result instanceof Collection) { 201 if (result instanceof AttrList) { 202 result = new AttrListWrapper(this, attrIndex, null); 203 } else if (result instanceof AttrCollection) { 204 result = new AttrCollWrapper(this, attrIndex, null); 205 } else if (result instanceof AttrImmutList) { 206 result = new AttrImmutListWrapper(_getMdrStorage(), this, attrIndex, null); 207 } 208 } 209 return result; 210 } catch (StorageException e) { 211 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 212 } 213 } 214 215 protected final void _handleSet(int attrIndex, Object newValue) { 216 try { 217 getInstanceDelegate().setAttribute(attrIndex, newValue); 218 } catch (StorageException e) { 219 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 220 } 221 } 222 223 225 226 227 228 229 public final RefFeatured refOutermostComposite() { 230 _lock(false); 231 try { 232 return _outermostComposite(); 233 } finally { 234 _unlock(); 235 } 236 } 237 238 protected RefFeatured _outermostComposite() { 239 try { 240 return (RefFeatured) _getRepository().getHandler(getInstanceDelegate().getOutermostComposite()); 241 } catch ( StorageException e ) { 242 throw new DebugException("Storage exception: " + e); 243 } 244 } 245 246 public final RefFeatured refImmediateComposite() { 247 _lock(false); 248 try { 249 return _immediateComposite(); 250 } finally { 251 _unlock(); 252 } 253 } 254 255 protected RefFeatured _immediateComposite() { 256 try { 257 return (RefFeatured) _getRepository().getHandler(getInstanceDelegate().getImmediateComposite()); 258 } catch ( StorageException e ) { 259 throw new DebugException("Storage exception: " + e); 260 } 261 } 262 263 public final boolean refIsInstanceOf(RefObject objType, boolean considerSubtypes) { 264 _lock(false); 265 try { 266 return isInstanceOf((GeneralizableElement) refMetaObject(), objType, considerSubtypes); 267 } finally { 268 _unlock(); 269 } 270 } 271 272 private final boolean isInstanceOf(GeneralizableElement metaObject, RefObject objType, boolean considerSubtypes) { 273 if (metaObject.equals(objType)) return true; 274 if (considerSubtypes) { 275 boolean isInstance; 276 Iterator it = metaObject.getSupertypes().iterator(); 277 while (it.hasNext()) { 278 if (isInstanceOf(((GeneralizableElement) it.next()), objType, true)) { 279 return true; 280 } 281 } 282 } 283 return false; 284 } 285 286 public final RefClass refClass() { 287 _lock(false); 288 try { 289 return (RefClass) _getRepository().getHandler(getInstanceDelegate().getClassProxy()); 290 } catch (StorageException e) { 291 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 292 } finally { 293 _unlock(); 294 } 295 } 296 297 public final void refDelete() { 298 boolean fail = true; 299 InstanceEvent event = null; 300 _lock(true); 301 try { 302 if (_getMdrStorage().eventsEnabled()) { 303 event = new InstanceEvent( 304 this, 305 InstanceEvent.EVENT_INSTANCE_DELETE, 306 null, 307 this 308 ); 309 310 _getMdrStorage().getEventNotifier().INSTANCE.firePlannedChange(this, event); 311 } 312 _delete(); 313 Exception deletionStackTrace = new Exception ("mofId: " + refMofId() + ", class: " + getClass().getName()); 314 BaseObjectHandler.deletedInstances.put(this, deletionStackTrace); 315 fail = false; 316 } finally { 317 _unlock(fail); 318 } 319 _getRepository().removeHandler(_getMofId()); 320 } 321 322 protected void _delete() { 323 try { 324 getInstanceDelegate().delete(); 325 } catch (StorageException e) { 326 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 327 } 328 }; 329 330 public final void refSetValue(String featureName, java.lang.Object value) { 331 _lock(true); 332 try { 333 if (_isReference(featureName)) { 334 _setReference(featureName, value); 335 } else { 336 super.refSetValue(featureName, value); 337 } 338 } finally { 339 _unlock(); 340 } 341 } 342 343 public final void refSetValue(RefObject feature, Object value) { 344 if (feature instanceof Attribute) { 345 super.refSetValue(feature, value); 346 } else { 347 _lock(true); 348 try { 349 StorableClass cls = getInstanceDelegate().getClassProxy(); 350 try { 351 StorableClass.ReferenceDescriptor desc = cls.getReferenceDescriptor(((Reference) feature).getName()); 352 if (!desc.getMofId().equals(((BaseObjectHandler)feature)._getDelegate().getMofId())) throw new InvalidCallException(null, feature); 353 } catch (DebugException e) { 354 throw new InvalidCallException(null, feature); 355 } 356 _setReference(((Reference) feature).getName(), value); 357 } catch (InvalidNameException e) { 358 throw new InvalidCallException(null, feature); 359 } catch (ClassCastException e) { 360 throw new InvalidCallException(null, feature); 361 } catch (StorageException e) { 362 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 363 } finally { 364 _unlock(); 365 } 366 } 367 } 368 369 public final Object refGetValue(String featureName) { 370 _lock(false); 371 try { 372 if (_isReference(featureName)) { 373 return _getReference(featureName); 374 } else { 375 return super.refGetValue(featureName); 376 } 377 } finally { 378 _unlock(); 379 } 380 } 381 382 public final Object refGetValue(RefObject feature) { 383 if (feature instanceof Attribute) { 384 return super.refGetValue(feature); 385 } else { 386 _lock(false); 387 try { 388 StorableClass cls = getInstanceDelegate().getClassProxy(); 389 try { 390 StorableClass.ReferenceDescriptor desc = cls.getReferenceDescriptor(((Reference) feature).getName()); 391 if (desc == null || !desc.getMofId().equals(((BaseObjectHandler)feature)._getDelegate().getMofId())) throw new InvalidCallException(null, feature); 392 } catch (DebugException e) { 393 throw new InvalidCallException(null, feature); 394 } 395 return _getReference(((Reference) feature).getName()); 396 } catch (InvalidNameException e) { 397 throw new InvalidCallException(null, feature); 398 } catch (ClassCastException e) { 399 throw new InvalidCallException(null, feature); 400 } catch (StorageException e) { 401 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 402 } finally { 403 _unlock(); 404 } 405 } 406 } 407 408 409 410 411 412 416 public void addListener(MDRChangeListener listener) { 417 addListener(listener, MDRChangeEvent.EVENTMASK_ALL); 418 } 419 420 424 public void addListener(MDRChangeListener listener, int mask) { 425 _getMdrStorage().getEventNotifier().INSTANCE.addListener(listener, mask, this); 426 } 427 428 431 public void removeListener(MDRChangeListener listener) { 432 _getMdrStorage().getEventNotifier().INSTANCE.removeListener(listener, this); 433 } 434 435 439 public void removeListener(MDRChangeListener listener, int mask) { 440 _getMdrStorage().getEventNotifier().INSTANCE.removeListener(listener, mask, this); 441 } 442 443 446 public Collection getListeners() { 447 return _getMdrStorage().getEventNotifier().INSTANCE.getListeners(this); 448 } 449 450 451 452 453 454 protected final Collection _recursiveVerify(Collection violations, Set visited) { 455 _lock(false); 456 try { 457 _verify(violations); 458 visited.add(this); 459 return violations; 461 } finally { 462 _unlock(); 463 } 464 } 465 466 protected Collection _verify(Collection violations) { 467 _lock(false); 468 try { 469 getInstanceDelegate().verify(violations); 470 return violations; 471 } catch (StorageException e) { 472 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 473 } finally { 474 _unlock(); 475 } 476 } 477 } 478 | Popular Tags |