KickJava   Java API By Example, From Geeks To Geeks.

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


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;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import org.netbeans.mdr.persistence.MOFID;
26 import org.netbeans.mdr.persistence.StorageException;
27 import org.netbeans.mdr.storagemodel.transientimpl.TransientStorage;
28 import org.netbeans.mdr.util.DebugException;
29 import org.netbeans.mdr.util.Logger;
30
31 /**
32  *
33  * @author Tomas Zezula
34  */

35 public class TransientStorableObject extends StorableObject implements Transient {
36
37     private ArrayList JavaDoc referentQueue;
38     private HashMap JavaDoc attrTxLog;
39     
40     /** Creates a new instance of TransientStorableObject */
41     public TransientStorableObject() {
42         super ();
43         this.attrTxLog = new HashMap JavaDoc ();
44     }
45     
46     public TransientStorableObject (MdrStorage mdrStorage, MOFID immediatePackage, MOFID meta, MOFID classProxy) throws StorageException {
47         this (mdrStorage, immediatePackage, meta, classProxy, null);
48     }
49     
50     public TransientStorableObject (MdrStorage mdrStorage, MOFID immediatePackage, MOFID meta, MOFID classProxy, Object JavaDoc[] params) throws StorageException {
51         super (mdrStorage, immediatePackage, meta, classProxy, params, TransientStorage.STORAGE_ID);
52         this.attrTxLog = new HashMap JavaDoc ();
53     }
54     
55     
56     public void delete () throws StorageException {
57         this.deleteRecursive ();
58     }
59     
60     public void write(java.io.OutputStream JavaDoc outputStream) {
61         throw new DebugException ("Trying to write tranient object");
62     }
63     
64     public void read (java.io.InputStream JavaDoc inputStream) {
65         throw new DebugException ("Trying to read transient object");
66     }
67     
68     public void addReferent (TransientStorableObject referent) {
69         if (this.referentQueue == null)
70             this.referentQueue = new ArrayList JavaDoc ();
71         this.referentQueue.add (referent);
72     }
73     
74     public void removeReferent (TransientStorableObject referent) {
75         if (this.referentQueue == null)
76             return;
77         this.referentQueue.remove (referent);
78     }
79    
80     /** Transactional support
81      * the setAttribute is overloaded to allow more efficient transaction
82      * handling
83      */

84     public void setAttribute (int index, Object JavaDoc value) throws StorageException {
85         check ();
86         Object JavaDoc oldValue = values[index];
87         super.setAttribute (index, value);
88         Integer JavaDoc key = new Integer JavaDoc (index);
89         if (!attrTxLog.containsKey (key)) {
90             this.attrTxLog.put (key, oldValue);
91         }
92     }
93     
94      /** Transaction support
95       * commits transaction, clears the compensation transactions log
96       */

97     public void commit () {
98         this.attrTxLog.clear ();
99     }
100     
101     /** Transaction support
102       * rollbacks transaction, performs the compensation transactions log
103       */

104     public void rollBack () {
105         for (Iterator JavaDoc it = this.attrTxLog.keySet ().iterator (); it.hasNext ();) {
106             Integer JavaDoc key = (Integer JavaDoc) it.next ();
107             Object JavaDoc value = this.attrTxLog.get (key);
108             it.remove ();
109             try {
110                 super.setAttribute (key.intValue (), value);
111             } catch (StorageException se) {
112                 Logger.getDefault().notify(Logger.INFORMATIONAL, se);
113             }
114         }
115     }
116    
117     protected void deleteRecursive () throws StorageException {
118         this.getMdrStorage().removeInstance (this);
119     }
120     
121     /** Additional indexing is not supported on transient metadata
122      * the methods are implemented as noops
123      */

124     
125     protected void modifyIndex (int attrIdx, Object JavaDoc oldValue, Object JavaDoc newValue) throws StorageException {
126     }
127     
128     void addToIndex (String JavaDoc proxyId, String JavaDoc endId) throws StorageException {
129     }
130     
131     void removeFromIndex (String JavaDoc proxyId, String JavaDoc endId) throws StorageException {
132     }
133     
134 }
135
Popular Tags