1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 import javax.jmi.reflect.ConstraintViolationException; 23 import org.netbeans.jmi.javamodel.JavaModelPackage; 24 import org.netbeans.jmi.javamodel.JavaPackage; 25 import org.netbeans.jmi.javamodel.JavaPackageClass; 26 import org.netbeans.mdr.handlers.ClassProxyHandler; 27 import org.netbeans.mdr.persistence.MOFID; 28 import org.netbeans.mdr.persistence.StorageException; 29 import org.netbeans.mdr.storagemodel.StorableBaseObject; 30 import org.netbeans.mdr.storagemodel.StorableClass; 31 import org.netbeans.mdr.storagemodel.StorableObject; 32 import org.netbeans.mdr.util.DebugException; 33 import org.netbeans.modules.javacore.JMManager; 34 import org.openide.ErrorManager; 35 36 40 public abstract class JavaPackageClassImpl extends ClassProxyHandler implements JavaPackageClass { 41 private static final String MOFID_PREFIX = "package:"; private static final HashMap allInstances = new HashMap(); 43 44 private static JavaPackageClassImpl mainProxy = null; 45 46 private static JavaPackageClassImpl getMainProxy() { 47 if (mainProxy == null) { 51 JavaModelPackage pkg = org.netbeans.modules.javacore.internalapi.JavaMetamodel.getManager().getDefaultExtent(); 52 mainProxy = (JavaPackageClassImpl) pkg.getJavaPackage(); 53 } 54 return mainProxy; 55 } 56 57 static void removePackage(JavaPackage pkg) { 58 allInstances.remove(pkg.getName()); 59 } 60 61 62 public JavaPackageClassImpl(StorableClass s) { 63 super(s); 64 } 65 66 public JavaPackage createJavaPackage(String name, List annotations) { 67 throw new UnsupportedOperationException ("Cannot explicitly create a package."); } 69 70 protected abstract JavaPackage super_createJavaPackage(String name, List annotations); 71 72 public JavaPackage createJavaPackage() { 73 throw new ConstraintViolationException(this, refMetaObject(), "Cannot create package without a name."); } 75 76 public JavaPackage resolvePackage(String name, boolean forceInclude) { 77 if (getMainProxy() != this) { 79 return getMainProxy().resolvePackage(name, forceInclude); 80 } 81 JavaPackageImpl result = (JavaPackageImpl) allInstances.get(name); 82 return (forceInclude || (result != null && result.shouldInclude())) ? result : null; 83 } 84 85 public JavaPackage resolvePackage(String name) { 86 repository().beginTrans(false); 87 try { 88 boolean force = false; 89 if (getMainProxy() != this) { 90 force = (findRealPackage(name) != null); 91 } 92 return resolvePackage(name, force); 93 } finally { 94 repository().endTrans(); 95 } 96 } 97 98 JavaPackageImpl resolveOrCreatePackage(String name) { 99 JavaPackageImpl result = (JavaPackageImpl) allInstances.get(name); 100 if (result == null) { 101 try { 102 JavaPackageImpl parent = null; 103 if (name.length() > 0) { 104 int index = name.lastIndexOf('.'); 105 String parentName = index > 0 ? name.substring(0, index) : ""; 106 parent = resolveOrCreatePackage(parentName); 107 } 108 StorableBaseObject s = _getDelegate(); 109 MOFID mofId = new MOFID(name.hashCode(), MOFID_PREFIX + name); 110 DeferredObject o = new DeferredObject(mofId, s.getMdrStorage(), s.getImmediatePackageId(), s.getOutermostPackageId(), s.getMetaObject(), (StorableClass) s, parent == null ? null : (StorableObject) parent._getDelegate()); 111 result = (JavaPackageImpl) _getRepository().getHandler(o); 112 result.name = name; 113 allInstances.put(name, result); 114 if (parent != null) { 115 parent.addSubPackage(result); 116 } 117 } catch (StorageException e) { 120 throw new DebugException(); 121 } 122 } 123 return result; 124 } 125 126 JavaPackage resolveRealPackage(String name) { 127 if (name == null) return null; 128 JavaPackageImpl result = findRealPackage(name); 129 if (result == null) { 130 result = (JavaPackageImpl) super_createJavaPackage(name, null); 131 if (name.length() > 0) { 132 int index = name.lastIndexOf('.'); 133 String parentName = index > 0 ? name.substring(0, index) : ""; 134 JavaPackageImpl parent = (JavaPackageImpl) resolveRealPackage(parentName); 135 if (parent == null) { 136 throw new IllegalStateException ("parent package for " + name + " not found."); } 138 parent.addSubPackage(result); 139 } 140 addRealPackage(result); 141 } 142 return result; 143 } 144 145 public JavaPackageImpl findRealPackage(String name) { 146 StorableBaseObject storable = _getDelegate(); 147 Collection col = storable.getMdrStorage().getObjectsFromAdditionalIndex(storable.getImmediatePackageId(), "Package.name", name); return col == null || col.isEmpty() ? null : (JavaPackageImpl) _getRepository().getHandler((StorableBaseObject) col.iterator().next()); 149 } 150 151 void removeRealPackage(JavaPackageImpl realPackage) { 152 JavaPackageImpl result = (JavaPackageImpl) resolvePackage(realPackage.getName(), true); 153 if (result != null) { 154 result.removeRealPackage(realPackage); 155 } else { 156 JMManager.getLog().log(ErrorManager.WARNING, "proxy package for package " + realPackage.getName() + " not found."); 157 } 158 } 159 160 void addRealPackage(JavaPackageImpl realPackage) { 161 if (getMainProxy() != this) { 162 getMainProxy().addRealPackage(realPackage); 163 return; 164 } 165 String name = realPackage.getName(); 166 JavaPackageImpl result = resolveOrCreatePackage(name); 167 result.addRealPackage(realPackage); 168 } 169 170 public void addAllRealPackages() { 171 for (Iterator it = super._allOfClass(false).iterator(); it.hasNext();) { 172 addRealPackage((JavaPackageImpl) it.next()); 173 } 174 } 175 176 public void removeAllRealPackages() { 177 for (Iterator it = super._allOfClass(false).iterator(); it.hasNext();) { 178 removeRealPackage((JavaPackageImpl) it.next()); 179 } 180 } 181 182 protected Collection _allOfClass(boolean recursive) { 183 return Collections.unmodifiableCollection(allInstances.values()); 184 } 185 } 186 | Popular Tags |