1 19 package org.netbeans.mdr.handlers; 20 21 import java.util.*; 22 import javax.jmi.model.*; 23 import javax.jmi.reflect.*; 24 25 import org.netbeans.api.mdr.*; 26 import org.netbeans.api.mdr.events.*; 27 28 import org.netbeans.mdr.util.*; 29 import org.netbeans.mdr.storagemodel.*; 30 import org.netbeans.mdr.persistence.StorageException; 31 32 37 public abstract class AssociationHandler extends BaseObjectHandler implements RefAssociation { 38 39 40 41 42 43 44 protected AssociationHandler(StorableAssociation storable) { 45 super(storable); 46 } 47 48 49 50 51 52 private StorableAssociation getAssociationDelegate() { 53 return (StorableAssociation) _getDelegate(); 54 } 55 56 57 58 59 60 64 protected final Object _preAllLinks() { 65 _lock(false); 66 return null; 67 } 68 69 protected final java.util.Collection _handleAllLinks() { 70 try { 71 return new IndexSetWrapper(_getMdrStorage(), getAssociationDelegate().getAllLinks()); 72 } catch (StorageException e) { 73 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 74 } 75 } 76 77 81 protected final void _postAllLinks(java.util.Collection result, Object extraInfo, boolean fail) { 82 _unlock(); 83 } 84 85 protected final Object _preExists(RefObject associationEnd1, RefObject associationEnd2) { 86 _lock(false); 87 return null; 88 } 89 90 protected final Boolean _handleExists(RefObject associationEnd1, RefObject associationEnd2) { 91 try { 92 return getAssociationDelegate().linkExists(((BaseObjectHandler)associationEnd1)._getDelegate().getMofId(), ((BaseObjectHandler)associationEnd2)._getDelegate().getMofId()) ? Boolean.TRUE : Boolean.FALSE; 93 } catch (StorageException e) { 94 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 95 } 96 } 97 98 protected final void _postExists(Boolean result, Object extraInfo, boolean fail) { 99 _unlock(); 100 } 101 102 protected final Object _preQuery(String endName, RefObject query) { 103 _lock(false); 104 return null; 105 } 106 107 public final Object _handleQuery(String endName, RefObject query) { 108 Object result; 109 110 try { 111 result = getAssociationDelegate().queryObjects(endName, query == null ? null : ((BaseObjectHandler)query)._getDelegate().getMofId()); 112 } catch (StorageException e) { 113 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 114 } 115 116 if (result instanceof AssocEndIndexUList) { 117 return new AEIndexUListWrapper(this, query, endName, (AssocEndIndexUList) result, getAssociationDelegate().getEnd1Name().equals(endName)); 118 } else if (result instanceof AssocEndIndexSet) { 119 return new AEIndexSetWrapper(this, query, endName, (AssocEndIndexSet) result, getAssociationDelegate().getEnd1Name().equals(endName)); 120 } else { 121 return _getRepository().getHandler((StorableBaseObject) result); 122 } 123 } 124 125 protected final void _postQuery(Object result, Object extraInfo, boolean fail) { 126 _unlock(); 127 } 128 129 protected final Object _preAdd(RefObject associationEnd1, RefObject associationEnd2) { 130 boolean fail = true; 131 _lock(true); 132 try { 133 if (_getMdrStorage().eventsEnabled()) { 134 AssociationEvent event = new AssociationEvent( 135 this, 136 AssociationEvent.EVENT_ASSOCIATION_ADD, 137 associationEnd1, 138 getAssociationDelegate().getEnd1Name(), 139 null, 140 associationEnd2, 141 AssociationEvent.POSITION_NONE 142 ); 143 _getMdrStorage().getEventNotifier().ASSOCIATION.firePlannedChange(this, event); 144 fail = false; 145 return event; 146 } 147 fail = false; 148 return null; 149 } finally { 150 if (fail) _unlock(true); 151 } 152 } 153 154 protected final Boolean _handleAdd(RefObject associationEnd1, RefObject associationEnd2) { 155 try { 156 Boolean result = _handleLocalAdd(associationEnd1, associationEnd2); 157 if (!associationEnd1.refOutermostPackage().equals(associationEnd2.refOutermostPackage())) { 158 _addExternalLink(this, associationEnd1, associationEnd2); 159 } 160 return result; 161 } catch (StorageException e) { 162 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 163 } 164 } 165 166 protected final Boolean _handleLocalAdd(RefObject associationEnd1, RefObject associationEnd2) throws StorageException { 167 return getAssociationDelegate().addLink(((BaseObjectHandler)associationEnd1)._getDelegate().getMofId(), ((BaseObjectHandler)associationEnd2)._getDelegate().getMofId()) ? Boolean.TRUE : Boolean.FALSE; 168 } 169 170 protected final void _postAdd(Boolean result, Object extraInfo, boolean fail) { 171 _unlock(fail); 172 } 173 174 protected final Object _preRemove(RefObject associationEnd1, RefObject associationEnd2) { 175 boolean fail = true; 176 _lock(true); 177 try { 178 if (_getMdrStorage().eventsEnabled()) { 179 AssociationEvent event = new AssociationEvent( 180 this, 181 AssociationEvent.EVENT_ASSOCIATION_REMOVE, 182 associationEnd1, 183 getAssociationDelegate().getEnd1Name(), 184 associationEnd2, 185 null, 186 AssociationEvent.POSITION_NONE 187 ); 188 _getMdrStorage().getEventNotifier().ASSOCIATION.firePlannedChange(this, event); 189 fail = false; 190 return event; 191 } 192 fail = false; 193 return null; 194 } finally { 195 if (fail) _unlock(true); 196 } 197 } 198 199 protected final Boolean _handleRemove(RefObject associationEnd1, RefObject associationEnd2) { 200 try { 201 Boolean result = _handleLocalRemove(associationEnd1, associationEnd2); 202 if (!associationEnd1.refOutermostPackage().equals (associationEnd2.refOutermostPackage())) { 203 _removeExternalLink(this, associationEnd1, associationEnd2); 204 } 205 return result; 206 } catch (StorageException e) { 207 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 208 } 209 } 210 211 protected final Boolean _handleLocalRemove(RefObject associationEnd1, RefObject associationEnd2) throws StorageException { 212 return getAssociationDelegate().removeLink(((BaseObjectHandler)associationEnd1)._getDelegate().getMofId(), ((BaseObjectHandler)associationEnd2)._getDelegate().getMofId()) ? Boolean.TRUE : Boolean.FALSE; 213 } 214 215 protected final void _postRemove(Boolean result, Object extraInfo, boolean fail) { 216 _unlock(fail); 217 } 218 219 220 221 222 223 public final boolean refRemoveLink(RefObject firstEnd, RefObject secondEnd) { 224 try { 225 return _remove(firstEnd, secondEnd); 226 } catch (ClassCastException e) { 227 getAssociationDelegate().checkType(firstEnd, secondEnd); 229 return false; 231 } 232 } 233 234 public final boolean refAddLink(RefObject firstEnd, RefObject secondEnd) { 235 try { 236 return _add(firstEnd, secondEnd); 237 } catch (ClassCastException e) { 238 getAssociationDelegate().checkType(firstEnd, secondEnd); 240 return false; 242 } 243 } 244 245 public final Collection refQuery(RefObject queryEnd, RefObject queryObject) { 246 try { 247 return refQuery(((AssociationEnd) queryEnd).getName(), queryObject); 248 } catch (InvalidNameException e) { 249 throw new InvalidCallException(null, queryEnd); 250 } catch (ClassCastException e) { 251 throw new InvalidCallException(null, queryEnd); 252 } 253 } 254 255 public final Collection refQuery(String endName, RefObject queryObject) { 256 Object result; 257 try { 258 result = _query(endName, queryObject); 259 } catch (ClassCastException e) { 260 if (getAssociationDelegate().getEnd1Name().equals(endName)) { 262 getAssociationDelegate().checkType(queryObject, null); 263 } else { 264 getAssociationDelegate().checkType(null, queryObject); 265 } 266 return null; 268 } 269 Collection col; 270 if (result instanceof Collection) { 271 return (Collection) result; 272 } 273 if (result == null) { 274 return new ArrayList(0); 275 } 276 col = new ArrayList(1); 277 col.add(result); 278 return col; 279 } 280 281 public final boolean refLinkExists(RefObject firstEnd, RefObject secondEnd) { 282 try { 283 return _exists(firstEnd, secondEnd); 284 } catch (ClassCastException e) { 285 getAssociationDelegate().checkType(firstEnd, secondEnd); 287 return false; 289 } 290 } 291 292 293 294 295 296 public abstract boolean _add(RefObject end1, RefObject end2); 297 public abstract boolean _remove(RefObject end1, RefObject end2); 298 public abstract Object _query(String endName, RefObject end); 299 public abstract boolean _exists(RefObject end1, RefObject end2); 300 public abstract Collection _all(); 301 302 309 310 311 312 316 public void addListener(MDRChangeListener listener) { 317 addListener(listener, MDRChangeEvent.EVENTMASK_ALL); 318 } 319 320 324 public void addListener(MDRChangeListener listener, int mask) { 325 _getMdrStorage().getEventNotifier().ASSOCIATION.addListener(listener, mask, this); 326 } 327 328 331 public void removeListener(MDRChangeListener listener) { 332 _getMdrStorage().getEventNotifier().ASSOCIATION.removeListener(listener, this); 333 } 334 335 339 public void removeListener(MDRChangeListener listener, int mask) { 340 _getMdrStorage().getEventNotifier().ASSOCIATION.removeListener(listener, mask, this); 341 } 342 343 344 345 346 347 static void _removeExternalLink(AssociationHandler associationHandler, RefObject thisEnd, RefObject otherEnd) { 348 if (MdrStorage.isTransientMofId (((BaseObjectHandler)thisEnd)._getDelegate().getMofId ()) || MdrStorage.isTransientMofId (((BaseObjectHandler)otherEnd)._getDelegate().getMofId ())) 349 return; 350 RefPackage oeip = null; 351 if (thisEnd.refOutermostPackage().equals(associationHandler.refOutermostPackage())) 352 oeip = otherEnd.refOutermostPackage(); 353 else 354 oeip = thisEnd.refOutermostPackage(); 355 356 String name = ((javax.jmi.model.Association)associationHandler.refMetaObject()).getName(); 357 RefAssociation oea = _findAssociation(oeip, name); 358 if (oea instanceof AssociationHandler) { 359 boolean failed = true; 360 Boolean result = null; 361 try { 362 ((AssociationHandler)oea)._preRemove (thisEnd, otherEnd); 363 result = ((AssociationHandler)oea)._handleLocalRemove(thisEnd, otherEnd); 364 failed = false; 365 }catch (Exception e) { 366 } 367 finally { 368 ((AssociationHandler)oea)._postRemove (result, null, failed); 369 } 370 } 371 } 372 373 static void _addExternalLink(AssociationHandler associationHandler, RefObject thisEnd, RefObject otherEnd) { 374 if (MdrStorage.isTransientMofId (((BaseObjectHandler)thisEnd)._getDelegate().getMofId ()) || MdrStorage.isTransientMofId (((BaseObjectHandler)otherEnd)._getDelegate().getMofId ())) 375 return; 376 RefPackage oeip = null; 377 if (thisEnd.refOutermostPackage().equals(associationHandler.refOutermostPackage())) 378 oeip = otherEnd.refOutermostPackage(); 379 else 380 oeip = thisEnd.refOutermostPackage (); 381 String name = ((javax.jmi.model.Association)associationHandler.refMetaObject()).getName(); 382 RefAssociation oea = _findAssociation (oeip, name); 383 if (oea instanceof AssociationHandler) { 384 boolean failed = true; 385 Boolean result = null; 386 try { 387 ((AssociationHandler)oea)._preAdd (thisEnd, otherEnd); 388 result = ((AssociationHandler)oea)._handleLocalAdd(thisEnd, otherEnd); 389 failed = false; 390 }catch (Exception e) {} 391 finally { 392 ((AssociationHandler)oea)._postAdd (result, null, failed); 393 } 394 } 395 } 396 397 private static RefAssociation _findAssociation (RefPackage opkg, String name) { 399 ArrayList list = new ArrayList (); 400 list.add (opkg); 401 while (!list.isEmpty()) { 402 RefPackage pkg = (RefPackage) list.remove (0); 403 try { 404 RefAssociation oea = pkg.refAssociation (name); 405 return oea; 406 } catch (javax.jmi.reflect.InvalidNameException invalidName) { 407 } 408 Object innerPackages = pkg.refAllPackages(); 409 if (innerPackages instanceof RefPackage) { 410 list.add (innerPackages); 411 } 412 else { 413 list.addAll ((Collection)innerPackages); 414 } 415 } 416 return null; 417 } 418 419 420 421 422 423 protected final Collection _recursiveVerify(Collection violations, Set visited) { 424 _lock(false); 425 try { 426 _verify(violations); 427 visited.add(this); 428 return violations; 429 } finally { 430 _unlock(); 431 } 432 } 433 434 protected Collection _verify(Collection violations) { 435 _lock(false); 436 try { 437 getAssociationDelegate().verify(violations); 438 return violations; 439 } catch (StorageException e) { 440 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 441 } finally { 442 _unlock(); 443 } 444 } 445 446 447 448 449 450 static class RefAssociationLinkImpl implements RefAssociationLink { 451 private final RefObject firstEnd; 452 private final RefObject secondEnd; 453 454 RefAssociationLinkImpl(RefObject firstEnd, RefObject secondEnd) { 455 this.firstEnd = firstEnd; 456 this.secondEnd = secondEnd; 457 } 458 459 public RefObject refFirstEnd() { 460 return firstEnd; 461 } 462 463 public RefObject refSecondEnd() { 464 return secondEnd; 465 } 466 } 467 } 468 | Popular Tags |