KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > StorablePackage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
31  * @author Petr Hrebejk, Martin Matula
32  * @version 0.2
33  */

34 public class StorablePackage extends StorableBaseObject implements Streamable {
35     private String JavaDoc context;
36     private Map packageProxies;
37     private Map assocProxies;
38     private Map classProxies;
39     private Map datatypes;
40     private Class JavaDoc packageSuperclass = null;
41     
42     /** Replaces MOFIDs using the passed map. (This method is used when rebuilding metaobjects.)
43      * @param table Map of old MOFIDs to new MOFIDs.
44      */

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 JavaDoc mofContext, Map datatypes, String JavaDoc 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     /** Creates new StorablePackage */
76     public StorablePackage(MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID packageId, org.netbeans.mdr.persistence.MOFID metaId,
77         String JavaDoc mofContext, Map datatypes) throws StorageException {
78         this(mdrStorage, packageId, metaId, mofContext, datatypes, null);
79     }
80     
81     public synchronized Class JavaDoc getPackageSuperclass() throws StorageException, ClassNotFoundException JavaDoc {
82         objectWillChange();
83         packageSuperclass = resolvePackageSuperclass();
84         objectChanged();
85
86         return packageSuperclass;
87     }
88     
89     private Class JavaDoc resolvePackageSuperclass() throws StorageException, ClassNotFoundException JavaDoc {
90         try {
91             return BaseObjectHandler.resolveImplementation(TagSupport.getImplFullName(getMetaObject(), TagSupport.PACKAGE));
92         } catch (ClassNotFoundException JavaDoc e) {
93             return PackageProxyHandler.class;
94         }
95     }
96
97     public Class JavaDoc getPackageCustomImpl() {
98         try {
99             Class JavaDoc sup = getPackageSuperclass();
100             return sup == PackageProxyHandler.class ? null : sup;
101         } catch (Exception JavaDoc e) {}
102         return null;
103     }
104
105     /**
106      * @param pkgMeta
107      * @param clustered
108      * @throws StorageException
109      */

110     public void clusterPackage(String JavaDoc clusteredMetaName, org.netbeans.mdr.persistence.MOFID clusteredId) throws StorageException {
111         addPackage(clusteredMetaName, clusteredId);
112     }
113     
114     public void addPackage(String JavaDoc metaName, org.netbeans.mdr.persistence.MOFID mofId) {
115         objectWillChange();
116         packageProxies.put(metaName, mofId);
117         objectChanged();
118     }
119
120     public void addClass(String JavaDoc metaName, org.netbeans.mdr.persistence.MOFID mofId) {
121         objectWillChange();
122         classProxies.put(metaName, mofId);
123         objectChanged();
124     }
125
126     public void addAssociation(String JavaDoc metaName, org.netbeans.mdr.persistence.MOFID mofId) {
127         objectWillChange();
128         assocProxies.put(metaName, mofId);
129         objectChanged();
130     }
131
132     /**
133      * @param meta
134      * @throws StorageException
135      * @return
136      */

137     public StorablePackage getPackage(String JavaDoc metaName) throws StorageException {
138         return (StorablePackage) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) packageProxies.get(metaName));
139     }
140
141     /**
142      * @param meta
143      * @throws StorageException
144      * @return
145      */

146     public StorableClass getClassProxy(String JavaDoc metaName) throws StorageException {
147         return (StorableClass) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) classProxies.get(metaName));
148     }
149
150     /**
151      * @param meta
152      * @throws StorageException
153      * @return
154      */

155     public StorableAssociation getAssociation(String JavaDoc metaName) throws StorageException {
156         return (StorableAssociation) getMdrStorage().getObject((org.netbeans.mdr.persistence.MOFID) assocProxies.get(metaName));
157     }
158
159     /**
160      * @throws StorageException
161      * @return
162      */

163     public Collection getAllPackages() throws StorageException {
164         return new CachedCollection(getMdrStorage(), packageProxies.values());
165     }
166
167     /**
168      * @throws StorageException
169      * @return
170      */

171     public Collection getAllClasses () throws StorageException {
172         return new CachedCollection(getMdrStorage(), classProxies.values());
173     }
174
175     /**
176      * @throws StorageException
177      * @return
178      */

179     public Collection getAllAssociations () throws StorageException {
180         return new CachedCollection(getMdrStorage(), assocProxies.values());
181     }
182     
183     public String JavaDoc getContext() {
184         return context;
185     }
186     
187     public DatatypeDescriptor getDatatypeDesc(String JavaDoc name) {
188         return (DatatypeDescriptor) datatypes.get(name);
189     }
190
191     /**
192      * @throws StorageException
193      */

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     /**
233      * @param outputStream
234      */

235     public void write(java.io.OutputStream JavaDoc outputStream) {
236 // if (packageProxies == null || classProxies == null || assocProxies == null) throw new NullPointerException();
237

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 JavaDoc name = (String JavaDoc) it.next();
251                 IOUtils.writeString(outputStream, name);
252                 ((DatatypeDescriptor) datatypes.get(name)).write(outputStream);
253             }
254         } catch (java.io.IOException JavaDoc e) {
255             Logger.getDefault().notify(Logger.INFORMATIONAL, e);
256         }
257     }
258
259     /**
260      * @param inputStream
261      */

262     public void read(java.io.InputStream JavaDoc 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 JavaDoc 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