1 19 package org.netbeans.mdr.storagemodel; 20 21 import javax.jmi.reflect.*; 22 import java.util.*; 23 import java.io.*; 24 25 import org.netbeans.mdr.handlers.*; 26 import org.netbeans.mdr.persistence.*; 27 import org.netbeans.mdr.util.*; 28 import org.netbeans.mdr.handlers.gen.TagSupport; 29 30 34 public class StorablePackage extends StorableBaseObject implements Streamable { 35 private String context; 36 private Map packageProxies; 37 private Map assocProxies; 38 private Map classProxies; 39 private Map datatypes; 40 private Class packageSuperclass = null; 41 42 45 protected void replaceValues(Map table) { 46 objectWillChange(); 47 super.replaceValues(table); 48 objectChanged(); 49 } 50 51 public StorablePackage() { 52 super(); 53 } 54 55 public StorablePackage(MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID packageId, org.netbeans.mdr.persistence.MOFID metaId, 56 String mofContext, Map datatypes, String storageId) throws StorageException { 57 super (mdrStorage, packageId, metaId, storageId); 58 objectWillChange(); 59 this.packageProxies = new HashMap(); 60 this.assocProxies = new HashMap(); 61 this.classProxies = new HashMap(); 62 this.datatypes = datatypes; 63 this.getMdrStorage().addObject(this); 64 if (packageId == null && mofContext != null) { 65 this.context = mofContext; 66 this.getMdrStorage().createContext(mofContext, this.getMofId()); 67 } 68 else { 69 this.context = null; 70 } 71 this.initFinished = true; 72 objectChanged (); 73 } 74 75 76 public StorablePackage(MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID packageId, org.netbeans.mdr.persistence.MOFID metaId, 77 String mofContext, Map datatypes) throws StorageException { 78 this(mdrStorage, packageId, metaId, mofContext, datatypes, null); 79 } 80 81 public synchronized Class getPackageSuperclass() throws StorageException, ClassNotFoundException { 82 objectWillChange(); 83 packageSuperclass = resolvePackageSuperclass(); 84 objectChanged(); 85 86 return packageSuperclass; 87 } 88 89 private Class resolvePackageSuperclass() throws StorageException, ClassNotFoundException { 90 try { 91 return BaseObjectHandler.resolveImplementation(TagSupport.getImplFullName(getMetaObject(), TagSupport.PACKAGE)); 92 } catch (ClassNotFoundException e) { 93 return PackageProxyHandler.class; 94 } 95 } 96 97 public Class getPackageCustomImpl() { 98 try { 99 Class sup = getPackageSuperclass(); 100 return sup == PackageProxyHandler.class ? null : sup; 101 } catch (Exception e) {} 102 return null; 103 } 104 105 110 public void clusterPackage(String clusteredMetaName, org.netbeans.mdr.persistence.MOFID clusteredId) throws StorageException { 111 addPackage(clusteredMetaName, clusteredId); 112 } 113 114 public void addPackage(String metaName, org.netbeans.mdr.persistence.MOFID mofId) { 115 objectWillChange(); 116 packageProxies.put(metaName, mofId); 117 objectChanged(); 118 } 119 120 public void addClass(String metaName, org.netbeans.mdr.persistence.MOFID mofId) { 121 objectWillChange(); 122 classProxies.put(metaName, mofId); 123 objectChanged(); 124 } 125 126 public void addAssociation(String metaName, org.netbeans.mdr.persistence.MOFID mofId) { 127 objectWillChange(); 128 assocProxies.put(metaName, mofId); 129 objectChanged(); 130 } 131 132 137 public StorablePackage getPackage(String metaName) throws StorageException { 138 return (StorablePackage) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) packageProxies.get(metaName)); 139 } 140 141 146 public StorableClass getClassProxy(String metaName) throws StorageException { 147 return (StorableClass) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) classProxies.get(metaName)); 148 } 149 150 155 public StorableAssociation getAssociation(String metaName) throws StorageException { 156 return (StorableAssociation) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) assocProxies.get(metaName)); 157 } 158 159 163 public Collection getAllPackages() throws StorageException { 164 return new CachedCollection(getMdrStorage(), packageProxies.values()); 165 } 166 167 171 public Collection getAllClasses () throws StorageException { 172 return new CachedCollection(getMdrStorage(), classProxies.values()); 173 } 174 175 179 public Collection getAllAssociations () throws StorageException { 180 return new CachedCollection(getMdrStorage(), assocProxies.values()); 181 } 182 183 public String getContext() { 184 return context; 185 } 186 187 public DatatypeDescriptor getDatatypeDesc(String name) { 188 return (DatatypeDescriptor) datatypes.get(name); 189 } 190 191 194 public void delete() throws StorageException { 195 if (getImmediatePackageId() == null) { 196 getMdrStorage().dropContext(context, getMofId()); 197 deleteRecursive(); 198 } else { 199 throw new InvalidCallException(getMdrStorage().getRepository().getHandler(this), null, "Not an outermost package."); 200 } 201 } 202 203 protected void deleteRecursive() throws StorageException { 204 StorablePackage sp; 205 206 for (Iterator it = packageProxies.values().iterator(); it.hasNext();) { 207 try { 208 sp = (StorablePackage) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) it.next()); 209 } catch (StorageBadRequestException e) { 210 sp = null; 211 } 212 if (sp != null) sp.deleteRecursive(); 213 } 214 215 for (Iterator it = classProxies.values().iterator(); it.hasNext();) { 216 StorableClass sc = (StorableClass) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) it.next()); 217 sc.deleteRecursive(); 218 } 219 220 for (Iterator it = assocProxies.values().iterator(); it.hasNext();) { 221 StorableAssociation sa = (StorableAssociation) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) it.next()); 222 sa.deleteRecursive(); 223 } 224 225 packageProxies.clear(); 226 classProxies.clear(); 227 assocProxies.clear(); 228 229 super.deleteRecursive(); 230 } 231 232 235 public void write(java.io.OutputStream outputStream) { 236 238 super.write (outputStream); 239 240 try { 241 IOUtils.write (outputStream, meta, this); 242 IOUtils.write (outputStream, immediatePackage, this); 243 IOUtils.writeString(outputStream, context); 244 IOUtils.write(outputStream, packageProxies, this); 245 IOUtils.write(outputStream, classProxies, this); 246 IOUtils.write(outputStream, assocProxies, this); 247 248 IOUtils.writeInt(outputStream, datatypes.size()); 249 for (Iterator it = datatypes.keySet().iterator(); it.hasNext();) { 250 String name = (String ) it.next(); 251 IOUtils.writeString(outputStream, name); 252 ((DatatypeDescriptor) datatypes.get(name)).write(outputStream); 253 } 254 } catch (java.io.IOException e) { 255 Logger.getDefault().notify(Logger.INFORMATIONAL, e); 256 } 257 } 258 259 262 public void read(java.io.InputStream inputStream) { 263 super.read (inputStream); 264 265 try { 266 meta = (org.netbeans.mdr.persistence.MOFID) IOUtils.read (inputStream, this); 267 immediatePackage = (org.netbeans.mdr.persistence.MOFID) IOUtils.read (inputStream, this); 268 context = IOUtils.readString(inputStream); 269 packageProxies = (Map) IOUtils.read(inputStream, this); 270 classProxies = (Map) IOUtils.read(inputStream, this); 271 assocProxies = (Map) IOUtils.read(inputStream, this); 272 273 int count = IOUtils.readInt(inputStream); 274 datatypes = new HashMap(count, 1); 275 for (int i = 0; i < count; i++) { 276 String name = IOUtils.readString(inputStream); 277 datatypes.put(name, DatatypeDescriptor.readResolve(inputStream, this)); 278 } 279 } catch (IOException e) { 280 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 281 } 282 } 283 } 284 | Popular Tags |