1 19 20 package org.netbeans.mdr.handlers; 21 22 import java.util.*; 23 24 import org.netbeans.api.mdr.events.*; 25 26 import javax.jmi.model.*; 27 import javax.jmi.reflect.*; 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 PackageProxyHandler extends BaseObjectHandler implements RefPackage { 38 39 40 41 42 43 private HashMap clusteredPackages = null; 44 45 46 47 48 49 51 protected PackageProxyHandler(StorablePackage storable) { 52 super(storable); 53 } 54 55 56 57 58 59 private void checkClustered() { 61 if (clusteredPackages == null) { 62 MofPackage metaPkg = (MofPackage) refMetaObject(); 63 clusteredPackages = new HashMap(); 64 ModelElement element; 65 for (Iterator it = metaPkg.allSupertypes().iterator(); it.hasNext();) { 66 for (Iterator it2 = ((GeneralizableElement) it.next()).getContents().iterator(); it2.hasNext();) { 67 element = (ModelElement) it2.next(); 68 if ((element instanceof Import) && ((Import) element).isClustered()) { 69 clusteredPackages.put(((Import) element).getImportedNamespace().refMofId(), element.getName()); 70 } 71 } 72 } 73 } 74 } 75 76 private StorablePackage getPackageDelegate() { 77 return (StorablePackage) _getDelegate(); 78 } 79 80 81 82 83 84 protected final Object _preGetPackageProxy(String proxyName) { 85 _lock(false); 86 return null; 87 } 88 89 protected final RefPackage _handleGetPackageProxy(String proxyName) { 90 try { 91 return (RefPackage) _getRepository().getHandler(getPackageDelegate().getPackage(proxyName)); 92 } catch (StorageException e) { 93 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 94 } 95 } 96 97 protected final void _postGetPackageProxy(RefPackage result, Object extraInfo, boolean fail) { 98 _unlock(); 99 } 100 101 protected final Object _preGetClassProxy(String proxyName) { 102 _lock(false); 103 return null; 104 } 105 106 protected final RefClass _handleGetClassProxy(String proxyName) { 107 try { 108 return (RefClass) _getRepository().getHandler(getPackageDelegate().getClassProxy(proxyName)); 109 } catch (StorageException e) { 110 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 111 } 112 } 113 114 protected final void _postGetClassProxy(RefClass result, Object extraInfo, boolean fail) { 115 _unlock(); 116 } 117 118 protected final Object _preGetAssociationProxy(String proxyName) { 119 _lock(false); 120 return null; 121 } 122 123 protected final RefAssociation _handleGetAssociationProxy(String proxyName) { 124 try { 125 return (RefAssociation) _getRepository().getHandler(getPackageDelegate().getAssociation(proxyName)); 126 } catch (StorageException e) { 127 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 128 } 129 } 130 131 protected final void _postGetAssociationProxy(RefAssociation result, Object extraInfo, boolean fail) { 132 _unlock(); 133 } 134 135 protected final Object _preEnum(String enumName, String literal) { 136 _lock(false); 137 return null; 138 } 139 140 protected final RefEnum _handleEnum(String enumName, String literal) { 141 return EnumResolver.resolveEnum(getPackageDelegate().getDatatypeDesc(enumName).getIfcName(), literal); 142 } 143 144 protected final void _postEnum(RefEnum result, Object extraInfo, boolean fail) { 145 _unlock(); 146 } 147 148 protected final Object _preStruct(String structName, Object fieldValues[]) { 149 _lock(false); 150 return null; 151 } 152 153 protected final RefStruct _handleStruct(String structName, Object fieldValues[]) { 154 return StructImpl.newInstance(getPackageDelegate().getDatatypeDesc(structName), fieldValues); 155 } 156 157 protected final void _postStruct(RefStruct result, Object extraInfo, boolean fail) { 158 _unlock(); 159 } 160 161 162 163 164 165 public final Collection refAllAssociations() { 166 _lock(false); 167 try { 168 return new IndexSetWrapper(_getMdrStorage(), getPackageDelegate().getAllAssociations()); 169 } catch (StorageException e) { 170 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 171 } finally { 172 _unlock(); 173 } 174 } 175 176 public final Collection refAllClasses() { 177 _lock(false); 178 try { 179 return new IndexSetWrapper(_getMdrStorage(), getPackageDelegate().getAllClasses()); 180 } catch (StorageException e) { 181 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 182 } finally { 183 _unlock(); 184 } 185 } 186 187 public final Collection refAllPackages() { 188 _lock(false); 189 try { 190 return new IndexSetWrapper(_getMdrStorage(), getPackageDelegate().getAllPackages()); 191 } catch (StorageException e) { 192 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 193 } finally { 194 _unlock(); 195 } 196 } 197 198 public final RefAssociation refAssociation(String assocName) { 199 return _getAssociation(assocName); 200 } 201 202 public final RefAssociation refAssociation(RefObject assoc) { 203 Association a; 204 try { 205 a = (Association) assoc; 206 } catch (ClassCastException e) { 207 throw new InvalidCallException(this, assoc, "Invalid association designator: " + assoc); 208 } 209 try { 210 return refAssociation(a.getName()); 211 } catch (InvalidNameException e) { 212 throw new InvalidCallException(this, assoc); 213 } 214 } 215 216 public final RefPackage refPackage(String name) { 217 return _getPackage(name); 218 } 219 220 public final RefPackage refPackage(RefObject nestedPackage) { 221 MofPackage pkg; 222 try { 223 pkg = (MofPackage) nestedPackage; 224 } catch (ClassCastException e) { 225 throw new InvalidCallException(this, nestedPackage, "Invalid package designator: " + nestedPackage); 226 } 227 _lock(false); 228 try { 229 checkClustered(); 230 String name = (String ) clusteredPackages.get(pkg.refMofId()); 231 232 if (name == null) { 233 name = pkg.getName(); 234 } 235 236 return refPackage(name); 237 } catch (InvalidNameException e) { 238 throw new InvalidCallException(this, nestedPackage); 239 } finally { 240 _unlock(); 241 } 242 } 243 244 public final RefClass refClass(String name) { 245 return _getClass(name); 246 } 247 248 public final RefClass refClass(RefObject type) { 249 MofClass cls; 250 try { 251 cls = (MofClass) type; 252 } catch (ClassCastException e) { 253 throw new InvalidCallException(this, type, "Invalid class designator: " + type); 254 } 255 try { 256 return refClass(cls.getName()); 257 } catch (InvalidNameException e) { 258 throw new InvalidCallException(this, type); 259 } 260 } 261 262 public final RefEnum refGetEnum(String enumName, java.lang.String name) { 263 boolean fail = true; 264 Object extraInfo = null; 265 RefEnum result = null; 266 try { 267 extraInfo = _preEnum(enumName, name); 268 result = _handleEnum(enumName, name); 269 fail = false; 270 return result; 271 } catch (NullPointerException e) { 272 throw new InvalidNameException(enumName); 273 } finally { 274 _postEnum(result, extraInfo, fail); 275 } 276 } 277 278 public final RefEnum refGetEnum(RefObject en, String name) { 279 EnumerationType et; 280 try { 281 et = (EnumerationType) en; 282 } catch (ClassCastException e) { 283 throw new InvalidCallException(this, en, "Invalid enumeration designator: " + en); 284 } 285 try { 286 return refGetEnum(et.getName(), name); 287 } catch (InvalidNameException e) { 288 throw new InvalidCallException(this, en); 289 } 290 } 291 292 public final RefStruct refCreateStruct(String structName, java.util.List params) { 293 Object args[] = params.toArray(); 294 return _createStruct(structName, args); 295 } 296 297 public final RefStruct refCreateStruct(RefObject struct, List params) { 298 StructureType st; 299 try { 300 st = (StructureType) struct; 301 } catch (ClassCastException e) { 302 throw new InvalidCallException(this, struct, "Invalid structure designator: " + struct); 303 } 304 try { 305 return refCreateStruct(st.getName(), params); 306 } catch (InvalidNameException e) { 307 throw new InvalidCallException(this, struct); 308 } 309 } 310 311 public void refDelete() { 312 ExtentEvent event = null; 313 boolean fail = true; 314 _lock(true); 315 try { 316 if (_getMdrStorage().eventsEnabled()) { 317 event = new ExtentEvent( 318 this, 319 ExtentEvent.EVENT_EXTENT_DELETE, 320 getPackageDelegate().getContext(), 321 refMetaObject(), 322 null 323 ); 324 _getMdrStorage().getEventNotifier().PACKAGE.firePlannedChange(this, event); 325 } 326 try { 327 getPackageDelegate().delete(); 328 } catch (StorageException e) { 329 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 330 } 331 fail = false; 332 } finally { 333 _unlock(fail); 334 } 335 _getRepository().removeHandler(_getMofId()); 336 } 337 338 339 340 341 342 public abstract RefClass _getClass(String className); 343 public abstract RefAssociation _getAssociation(String associationName); 344 public abstract RefPackage _getPackage(String packageName); 345 public abstract RefStruct _createStruct(String structName, Object [] params); 346 347 348 349 350 351 355 public void addListener(MDRChangeListener listener) { 356 addListener(listener, MDRChangeEvent.EVENTMASK_ALL); 357 } 358 359 363 public void addListener(MDRChangeListener listener, int mask) { 364 _getMdrStorage().getEventNotifier().PACKAGE.addListener(listener, mask, this); 365 } 366 367 370 public void removeListener(MDRChangeListener listener) { 371 _getMdrStorage().getEventNotifier().PACKAGE.removeListener(listener, this); 372 } 373 374 378 public void removeListener(MDRChangeListener listener, int mask) { 379 _getMdrStorage().getEventNotifier().PACKAGE.removeListener(listener, mask, this); 380 } 381 382 383 384 385 386 protected final Collection _recursiveVerify(Collection violations, Set visited) { 387 _lock(false); 388 try { 389 _verify(violations); 390 visited.add(this); 391 for (Iterator it = refAllPackages().iterator(); it.hasNext();) { 392 PackageProxyHandler pkg = (PackageProxyHandler) it.next(); 393 if (!visited.contains(pkg)) pkg._recursiveVerify(violations, visited); 394 } 395 for (Iterator it = refAllAssociations().iterator(); it.hasNext();) { 396 AssociationHandler assoc = (AssociationHandler) it.next(); 397 if (!visited.contains(assoc)) assoc._recursiveVerify(violations, visited); 398 } 399 for (Iterator it = refAllClasses().iterator(); it.hasNext();) { 400 ClassProxyHandler cls = (ClassProxyHandler) it.next(); 401 if (!visited.contains(cls)) cls._recursiveVerify(violations, visited); 402 } 403 return violations; 404 } finally { 405 _unlock(); 406 } 407 } 408 409 protected Collection _verify(Collection violations) { 410 return violations; 411 } 412 } 413 | Popular Tags |