KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
22
23 import org.netbeans.mdr.persistence.*;
24 import org.netbeans.mdr.util.*;
25
26 /** Root class for all storable MOF repository objects.
27  *
28  * @author Martin Matula, Pavel Buzek
29  * @version 0.2
30  */

31 public abstract class StorableBaseObject implements Streamable, StorageClient {
32
33     /** MOFID of this object */
34     private org.netbeans.mdr.persistence.MOFID id;
35     /** MOFID of this object's metaobject */
36     protected org.netbeans.mdr.persistence.MOFID meta; // serialization of this value is handled by subclasses
37
/** MOFID of this object's immediate package */
38     protected org.netbeans.mdr.persistence.MOFID immediatePackage; //(de)serialization of this value handled by subclasses
39
/** MOFID of this object's outermost package */
40     private transient org.netbeans.mdr.persistence.MOFID outermostPackage = null; // value inited lazy on @link #getOutermostPackage method first call
41

42     private transient StorablePackage immediatePackageObj = null;
43     private transient StorablePackage outermostPackageObj = null;
44     
45     /** Storage reference */
46     private transient Storage storage;
47     
48     /** parent MdrStorage */
49     private transient MdrStorage mdrStorage;
50     
51     // semaphore to avoid calls to "objectStateChanged" before the object is added to the primary index
52
protected boolean initFinished;
53     
54     // slots for storing handler's private information
55
private Object JavaDoc slot1 = null;
56     private Object JavaDoc slot2 = null;
57     private Object JavaDoc slot3 = null;
58     private Object JavaDoc slot4 = null;
59     private Map propertiesSlot = null;
60     // number of used slots (to optimize storage)
61
private int slotsCount = 0;
62
63     /** Creates new StorableBaseObject.
64      * This default constructor is to be called only when deserializing
65      * object from the storage. It is immediatelly followed by call to <code>read</code> method.
66      */

67     public StorableBaseObject() {
68         super();
69         initFinished = true;
70     }
71
72     StorableBaseObject (MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta) throws StorageException {
73         this(mdrStorage, immediatePackage, meta, null);
74     }
75
76     StorableBaseObject (MdrStorage mdrStorage, org.netbeans.mdr.persistence.MOFID immediatePackage, org.netbeans.mdr.persistence.MOFID meta, String JavaDoc storageId) throws StorageException {
77         initFinished = false;
78         if (storageId == null) {
79             this.id = mdrStorage.generateMOFID (immediatePackage);
80         }
81         else {
82             this.id = mdrStorage.generateMOFID (storageId);
83         }
84         this.meta = meta;
85         this.immediatePackage = immediatePackage;
86         this.mdrStorage = mdrStorage;
87         //mdrStorage.addObject(this);
88
if (immediatePackage != null) {
89             org.netbeans.mdr.persistence.MOFID parentOutermost = mdrStorage.getObject(immediatePackage).getOutermostPackageId();
90             if (parentOutermost == null) {
91                 this.outermostPackage = immediatePackage;
92             } else {
93                 this.outermostPackage = parentOutermost;
94             }
95         } else {
96             this.outermostPackage = this.id;
97         }
98     }
99     
100     /** Returns metaobject of this object.
101      * @throws StorageException problem in storage
102      * @return metaobject
103      */

104     public StorableObject getMetaObject() throws StorageException {
105         return (StorableObject) getMdrStorage().getObject(meta);
106     }
107     
108     /** Returns immediate package of this object.
109      * @throws StorageException problem in storage
110      * @return immediate package
111      */

112     public StorablePackage getImmediatePackage() throws StorageException {
113         if (immediatePackageObj == null) {
114             immediatePackageObj = (StorablePackage) getMdrStorage().getObject(immediatePackage);
115         }
116         return immediatePackageObj;
117     }
118     
119     /** Returns MOFID of immediate package of this object.
120      * @return MOFID of immediate package
121      */

122     public org.netbeans.mdr.persistence.MOFID getImmediatePackageId() {
123         return immediatePackage;
124     }
125     
126     /** Returns MOFID of metaobject of this object.
127      * @return MOFID of metaobject
128      */

129     public org.netbeans.mdr.persistence.MOFID getMetaObjectId() {
130         return meta;
131     }
132     
133     /** Returns MOFID of outermost package of this object.
134      * @return MOFID of outermost package
135      */

136     public org.netbeans.mdr.persistence.MOFID getOutermostPackageId() {
137         if (outermostPackage == null) {
138             // value has to be inited
139
if (immediatePackage == null) {
140                 outermostPackage = getMofId(); // the object is proxy of an outermost package
141
} else {
142                 StorableBaseObject temp, pkg;
143                 try {
144                     pkg = mdrStorage.getObject(immediatePackage);
145                     do {
146                         temp = pkg.getImmediatePackage();
147                         if (temp != null) pkg = temp;
148                         else break;
149                     } while (temp != null);
150                     outermostPackage = pkg.getMofId();
151                 } catch (StorageException e) {
152                     throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
153                 }
154             } // if
155
} // if
156
return outermostPackage;
157     }
158
159     /** Returns outermost package of this object.
160      * @throws StorageException problem in storage
161      * @return outermost package
162      */

163     public StorablePackage getOutermostPackage() throws StorageException {
164         if (outermostPackageObj == null) {
165             outermostPackageObj = (StorablePackage) getMdrStorage().getObject(getOutermostPackageId());
166         }
167         return outermostPackageObj;
168     }
169
170     /** Returns MOFID of this object.
171      * @return MOFID
172      */

173     public org.netbeans.mdr.persistence.MOFID getMofId() {
174         return id;
175     }
176
177     /** Returns string representation of this object.
178      * @return string representation of this object
179      */

180     public String JavaDoc toString () {
181         return getClass().getName() + "("+id.toString()+")";
182     }
183
184     /** Compares this object with another object.
185      * @param o object to compare
186      * @return <code>true</code> if the objects represent the same repository object */

187     public boolean equals(Object JavaDoc o) {
188         return (o instanceof StorableBaseObject) && (((StorableBaseObject)o).getMofId().equals(this.getMofId()));
189     }
190     
191     /** Returns object's hashcode
192      * @return hashcode
193      */

194     public int hashCode() {
195         return id.hashCode();
196     }
197     
198     protected void deleteRecursive() throws StorageException {
199         getMdrStorage().removeObject(this);
200     }
201
202     /** Writes this object to an output stream.
203      * @param outputStream outputstream to be written to
204      */

205     public void write (java.io.OutputStream JavaDoc outputStream) {
206 // if (id == null || meta == null || storageID == null || context == null) throw new NullPointerException();
207
try {
208             IOUtils.writeMOFID (outputStream, id,this.getMdrStorage(), this.id);
209             outputStream.write(slotsCount);
210             switch (slotsCount) {
211                 case 5: IOUtils.write(outputStream, this.propertiesSlot, this);
212                 case 4: IOUtils.write(outputStream, slot4, this);
213                 case 3: IOUtils.write(outputStream, slot3, this);
214                 case 2: IOUtils.write(outputStream, slot2, this);
215                 case 1: IOUtils.write(outputStream, slot1, this);
216             }
217         } catch (java.io.IOException JavaDoc e) {
218             throw (DebugException) Logger.getDefault().annotate(new RuntimeException JavaDoc(), e);
219         }
220     }
221     
222     /** Reads this object from the passed input stream.
223      * @param inputStream inputstream to be read from
224      */

225     public void read (java.io.InputStream JavaDoc inputStream) {
226         try {
227             id = IOUtils.readMOFID (inputStream, storage);
228             slotsCount = inputStream.read();
229             switch (slotsCount) {
230                 case 5: this.propertiesSlot = (Map) IOUtils.read (inputStream, this, null);
231                 case 4: slot4 = IOUtils.read(inputStream, this, null);
232                 case 3: slot3 = IOUtils.read(inputStream, this, null);
233                 case 2: slot2 = IOUtils.read(inputStream, this, null);
234                 case 1: slot1 = IOUtils.read(inputStream, this, null);
235             }
236             outermostPackage = null; // not inited
237
} catch (java.io.IOException JavaDoc e) {
238             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
239         }
240     }
241     
242     public void setStorage(Storage storage) {
243         this.storage = storage;
244         mdrStorage = MdrStorage.getInstance(storage);
245     }
246     
247     /** Returns reference to parent MdrStorage.
248      * @return parent MdrStorage
249      */

250     public MdrStorage getMdrStorage() {
251         return mdrStorage;
252     }
253     
254     /** Adds a user specified property into the map of handler properties
255      * @param Object key, the key must be storable by IOUtils.write method
256      * @param Object value, the value must be storable by IOUtils.erite value
257      * @exception IllegalArgumentException is thrown when key or value is null
258      * or is not serializable by IOUtils.write method.
259      */

260     public void putProperty (Object JavaDoc key, Object JavaDoc value) {
261         // commented out - we cannot afford to do such an expensive checks
262
if (key == null || value == null /* || !IOUtils.checkObjectValidity (key) || !IOUtils.checkObjectValidity (value)*/)
263             throw new IllegalArgumentException JavaDoc ();
264         this.objectWillChange();
265         if (this.propertiesSlot == null) {
266             this.propertiesSlot = new HashMap ();
267             slotsCount = 5;
268         }
269         boolean failed = true;
270         this.mdrStorage.getRepository().beginTrans(true);
271         try {
272             this.propertiesSlot.put (key, value);
273             this.objectChanged ();
274             failed = false;
275         } finally {
276             this.mdrStorage.getRepository().endTrans(failed);
277         }
278     }
279     
280     /** Removes a user specified property from the map of handler properties
281      * @param Object key
282      * @return associated value or null, if no value is associated with given key
283      */

284     public Object JavaDoc removeProperty (Object JavaDoc key) {
285         if (this.propertiesSlot == null)
286             return null;
287         boolean failed = true;
288         this.mdrStorage.getRepository().beginTrans(true);
289         try {
290             this.objectWillChange();
291             Object JavaDoc result = this.propertiesSlot.remove (key);
292             if (result != null) {
293                 this.objectChanged ();
294             }
295             if (this.propertiesSlot.size () == 0)
296                 this.propertiesSlot = null;
297             failed = false;
298             return result;
299         } finally {
300             this.mdrStorage.getRepository().endTrans(failed);
301         }
302     }
303     
304     /** Returns the value which corresponds to key in the map of handler properties
305      * @param Object key
306      * @return associated value or null, if no value is associated with given key
307      */

308     public Object JavaDoc getProperty (Object JavaDoc key) {
309         if (this.propertiesSlot == null)
310             return null;
311         else
312             return this.propertiesSlot.get (key);
313     }
314     
315     public Object JavaDoc setSlot1(Object JavaDoc value) {
316         Object JavaDoc result = slot1;
317         objectWillChange();
318         slot1 = value;
319         if (slotsCount < 1) slotsCount = 1;
320         objectChanged();
321         return result;
322     }
323     
324     public Object JavaDoc getSlot1() {
325         return slot1;
326     }
327     
328     public Object JavaDoc setSlot2(Object JavaDoc value) {
329         Object JavaDoc result = slot2;
330         objectWillChange();
331         slot2 = value;
332         if (slotsCount < 2) slotsCount = 2;
333         objectChanged();
334         return result;
335     }
336     
337     public Object JavaDoc getSlot2() {
338         return slot2;
339     }
340     
341     public Object JavaDoc setSlot3(Object JavaDoc value) {
342         Object JavaDoc result = slot3;
343         objectWillChange();
344         slot3 = value;
345         if (slotsCount < 3) slotsCount = 3;
346         objectChanged();
347         return result;
348     }
349     
350     public Object JavaDoc getSlot3() {
351         return slot3;
352     }
353     
354     public Object JavaDoc setSlot4(Object JavaDoc value) {
355         Object JavaDoc result = slot4;
356         objectWillChange();
357         slot4 = value;
358         if (slotsCount < 4) slotsCount = 4;
359         objectChanged();
360         return result;
361     }
362     
363     public Object JavaDoc getSlot4() {
364         return slot4;
365     }
366     
367     /** Replaces MOFIDs using the passed map. (This method is used when rebuilding metaobjects.)
368      * @param table Map of old MOFIDs to new MOFIDs.
369      */

370     protected void replaceValues(Map table) {
371         meta = (org.netbeans.mdr.persistence.MOFID) table.get(meta);
372     }
373
374     /** Signals to the storage that this object will be changed.
375      */

376     public void objectWillChange() {
377         if (initFinished)
378             try {
379                 getMdrStorage().objectStateWillChange(id);
380             } catch (StorageException e) {
381                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
382             }
383     }
384     
385     /** Signals to the storage that this object has been changed.
386      */

387     public void objectChanged() {
388         if (initFinished)
389             try {
390                 getMdrStorage().objectStateChanged(id);
391             } catch (StorageException e) {
392                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
393             }
394     }
395
396 }
397
Popular Tags