KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > transientimpl > TransientStorage


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
20 package org.netbeans.mdr.storagemodel.transientimpl;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Stack JavaDoc;
26 import org.netbeans.mdr.persistence.*;
27 import org.netbeans.mdr.storagemodel.MdrStorage;
28 import org.netbeans.mdr.storagemodel.TransientStorableObject;
29
30 /**
31  *
32  * @author Tomas Zezula
33  */

34 public class TransientStorage implements Storage {
35
36     public static final String JavaDoc STORAGE_ID = "GC"; // NOI18N
37

38     private MdrStorage mdrStorage;
39     private long sequenceNumber;
40     private String JavaDoc name;
41     private Stack JavaDoc indexTxLog;
42     private Stack JavaDoc attrTxLog;
43     
44     private HashMap JavaDoc indexes;
45     
46     /** Creates a new instance of TransientStorage */
47     public TransientStorage (String JavaDoc name) {
48         this.name = name;
49         this.sequenceNumber = 1;
50         this.indexes = new HashMap JavaDoc ();
51         this.indexTxLog = new Stack JavaDoc ();
52         this.attrTxLog = new Stack JavaDoc ();
53     }
54     
55     public String JavaDoc getName() {
56         return this.name;
57     }
58     
59     public String JavaDoc getStorageId () {
60         return STORAGE_ID;
61     }
62     
63     public synchronized long getSerialNumber () {
64         return this.sequenceNumber++;
65     }
66     
67     /** This method is not used by transient storage,
68      * the data are not externalized due to its transient behaviour.
69      */

70     public MOFID readMOFID (java.io.InputStream JavaDoc inputStream) throws StorageException {
71         throw new UnsupportedOperationException JavaDoc ();
72     }
73     
74     /** This method is not used by transient storage,
75      * the data are not externalized due to its transient behaviour.
76      */

77     public void writeMOFID (java.io.OutputStream JavaDoc outputStream, MOFID modId) throws StorageException {
78         throw new UnsupportedOperationException JavaDoc ();
79     }
80     
81     public MOFID resolveMOFID (String JavaDoc sMofId) {
82         if (sMofId == null)
83             return null;
84         if (!sMofId.startsWith (STORAGE_ID))
85             return null;
86         try {
87             long serialNumber = Long.parseLong (sMofId.substring(STORAGE_ID.length()+1),16);
88             return new MOFID (serialNumber, STORAGE_ID);
89         }catch (NumberFormatException JavaDoc nfe) {
90             return null;
91         }
92     }
93
94     /** Returns the primary index in this Storage. There is exactly one primary
95      * index in every storage, primary index has valueType STREAMABLE.
96      */

97     public synchronized SinglevaluedIndex getPrimaryIndex() throws StorageException {
98         SinglevaluedIndex primaryIndex = (SinglevaluedIndex) this.indexes.get (TransientObjectResolverIndex.NAME);
99         if ( primaryIndex == null) {
100             primaryIndex = new TransientObjectResolverIndex ();
101             this.indexes.put (TransientObjectResolverIndex.NAME, primaryIndex);
102             this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, primaryIndex));
103         }
104         return primaryIndex;
105     }
106     
107     /* Returns true if the Storage of the given name exists */
108     public boolean exists() throws StorageException {
109         return true;
110     }
111     
112     /** Delete any persistent resources associated with the Storage.
113      * @return true if and only if the Storage is succesfully deleted, false otherwise
114      */

115     public synchronized boolean delete() throws StorageException {
116         for (Iterator JavaDoc it = this.indexes.keySet ().iterator (); it.hasNext(); ) {
117             this.dropIndex ((String JavaDoc) it.next());
118         }
119         return true;
120     }
121     
122     /* Create a new Storage instance. For btree, this creates a new btree repository.
123      * For an RDBMS implementation, this might create a new database, or new tables and
124      * indexes within an existing database. For an implementation which uses a JMI
125      * service, it might create a top-level package containing all MDR pacakges.
126      * After the repository is created, it is opened.
127      * If the repository already exists, and "replace" is false, an exception is
128      * thrown. If replace is true, the existing repository is deleted and a new one created.
129      * // PENDING: should return primary index or nothing ?
130      */

131     public void create (boolean replace, ObjectResolver resolver) throws StorageException {
132         this.mdrStorage = (MdrStorage) resolver;
133     }
134     
135     /* Open an existing repository. If createOnNoExist is true, and the Storage
136      * doesn't exist, create it and then open it.
137      * // PENDING: should return primary index or nothing ?
138      */

139     public void open(boolean createOnNoExist, ObjectResolver resolver) throws StorageException {
140         throw new UnsupportedOperationException JavaDoc ();
141     }
142     
143     /* Close the Storage */
144     public void close() throws StorageException {
145         this.commitChanges ();
146         this.delete ();
147     }
148
149     /** Create index that holds exactly one value for each key.
150      * @return created index
151      * @param name name of the index
152      * @param keyType type of keys in the index
153      * @param valueType type of values in the index (any type except STREAMABLE)
154      */

155     public synchronized SinglevaluedIndex createSinglevaluedIndex(String JavaDoc name, EntryType keyType, EntryType valueType) throws StorageException {
156         if (this.indexes.get (name) != null)
157             throw new StorageBadRequestException ("Index already exists.");
158         TransientSinglevaluedIndex tsi = new TransientSinglevaluedIndex (this.mdrStorage, name, keyType, valueType);
159         this.indexes.put (name, tsi);
160         this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, tsi));
161         return tsi;
162     }
163     /** Create index that holds sorted set of values for each key.
164      * @return created index
165      * @param name name of the index
166      * @param keyType type of keys in the index
167      * @param valueType type of values in the index (any type except STREAMABLE)
168      * @param unique true if values associated with one key do not contain duplicates
169      */

170     public synchronized MultivaluedOrderedIndex createMultivaluedOrderedIndex(String JavaDoc name, EntryType keyType, EntryType valueType, boolean unique) throws StorageException {
171         if (this.indexes.get (name) != null)
172             throw new StorageBadRequestException ("Index already exists.");
173         MultivaluedOrderedIndex tmoi = new TransientMultivaluedOrderedIndex (this.mdrStorage, name, keyType, valueType, unique);
174         this.indexes.put (name, tmoi);
175         this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, tmoi));
176         return tmoi;
177     }
178     /** Create index that hold a set of values for each key. Elements in one Multivalued are
179      * not sorted. Set does not contain duplicate values.
180      * @return created index
181      * @param name name of the index
182      * @param keyType type of keys in the index
183      * @param valueType type of values in the index (any type except STREAMABLE)
184      * @param unique true if values associated with one key do not contain duplicates
185      */

186     public synchronized MultivaluedIndex createMultivaluedIndex(String JavaDoc name, EntryType keyType, EntryType valueType, boolean unique) throws StorageException {
187         if (this.indexes.get (name) != null)
188             throw new StorageBadRequestException ("Index already exists.");
189         MultivaluedIndex tmi = new TransientMultivaluedIndex (this.mdrStorage, name, keyType, valueType, unique);
190         this.indexes.put (name, tmi);
191         this.indexTxLog.push ( new CompensatingTransaction.CreateIndexCTx (name, tmi));
192         return tmi;
193     }
194
195     /** Retrieve index by name.
196      * @param name name of the index
197      * @return index of the specified name
198      */

199     public synchronized Index getIndex(String JavaDoc name) throws StorageException {
200         return (Index) this.indexes.get (name);
201     }
202     /** Retrieve index by name.
203      * @param name name of the index
204      * @return index of the specified name and type
205      */

206     public synchronized SinglevaluedIndex getSinglevaluedIndex(String JavaDoc name) throws StorageException {
207         return (SinglevaluedIndex) this.getIndex (name);
208     }
209     /** Retrieve index by name.
210      * @param name name of the index
211      * @return index of the specified name and type
212      */

213     public synchronized MultivaluedIndex getMultivaluedIndex(String JavaDoc name) throws StorageException {
214         return (MultivaluedIndex) this.getIndex (name);
215     }
216     /** Retrieve index by name.
217      * @param name name of the index
218      * @return index of the specified name and type
219      */

220     public synchronized MultivaluedOrderedIndex getMultivaluedOrderedIndex(String JavaDoc name) throws StorageException {
221         return (MultivaluedOrderedIndex) this.getIndex (name);
222     }
223     /** Delete index.
224      * @param name name of the index
225      */

226     public synchronized void dropIndex(String JavaDoc name) throws StorageException {
227         Object JavaDoc index = this.indexes.remove (name);
228         this.indexTxLog.push ( new CompensatingTransaction.DropIndexCTx (name, index));
229     }
230     
231     public void objectStateWillChange (Object JavaDoc key) throws StorageException {
232         // do nothing
233
}
234     
235     /** Notify the Storage that state of the object associated with this key
236      * was changed. This must be called after the change is made to ensure
237      * that the changed state is comitted correctly.
238      * @param key key of the object that was changed and must be saved
239      */

240     public void objectStateChanged (Object JavaDoc key) throws StorageException {
241         this.attrTxLog.push (key);
242     }
243
244     /** Save all objects changed since this method was last call.
245      * This operation implements transactions on the storage.
246      * It must either whole complete or whole fail.
247      */

248     public void commitChanges() throws StorageException {
249         this.indexTxLog.clear ();
250         Iterator JavaDoc it = null;
251         synchronized (this) {
252             it = ((HashMap JavaDoc)this.indexes.clone ()).values ().iterator ();
253         }
254         while (it.hasNext ()) {
255             ((TransactionalIndex)it.next()).commit ();
256         }
257         while (!this.attrTxLog.empty ()) {
258             MOFID id = (MOFID) this.attrTxLog.pop ();
259             SinglevaluedIndex pi = this.getPrimaryIndex ();
260             TransientStorableObject tso = (TransientStorableObject) pi.getIfExists(id);
261             if (tso != null)
262                 tso.commit ();
263         }
264     }
265
266     /** Discard all changes since commitChanges() method was last call.
267      * This operation implements transactions on the storage.
268      * It must either whole complete or whole fail.
269      * Note that, after this method completes, the persistent MDR and
270      * any modified objects in memory are inconsistent, so it should
271      * be followed shortly by program exit.
272      */

273     public void rollBackChanges () throws StorageException {
274         while (!this.indexTxLog.empty ()) {
275             CompensatingTransaction ctx = (CompensatingTransaction) this.indexTxLog.pop ();
276             ctx.perform (this.indexes);
277         }
278         Iterator JavaDoc it = null;
279         synchronized (this) {
280             it = ((HashMap JavaDoc)this.indexes.clone ()).values ().iterator ();
281         }
282         while (it.hasNext ()) {
283             ((TransactionalIndex)it.next ()).rollBack ();
284         }
285         while (!this.attrTxLog.empty ()) {
286             MOFID id = (MOFID) this.attrTxLog.pop ();
287             SinglevaluedIndex pi = this.getPrimaryIndex ();
288             TransientStorableObject tso = (TransientStorableObject) pi.getIfExists(id);
289             if (tso != null)
290                 tso.rollBack ();
291         }
292     }
293     
294     /**
295      * Performs operations needed on exit.
296      */

297     public void shutDown() throws StorageException {
298     }
299     
300     
301     
302 }
303
Popular Tags