KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > multi > InTransactionBaseImageMultiImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.multi;
25
26 import org.objectweb.jalisto.se.api.*;
27 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess;
28 import org.objectweb.jalisto.se.api.internal.InTransactionBaseImage;
29 import org.objectweb.jalisto.se.api.internal.JalistoObject;
30 import org.objectweb.jalisto.se.exception.JalistoException;
31 import org.objectweb.jalisto.se.impl.InFileAddress;
32 import org.objectweb.jalisto.se.impl.server.InTransactionBaseImageImpl;
33 import org.objectweb.jalisto.se.impl.trace.Trace;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 public class InTransactionBaseImageMultiImpl extends InTransactionBaseImageImpl {
39
40     protected InTransactionBaseImageMultiImpl(PluggablePhysicalFileAccess physicalAccess, JalistoProperties properties) {
41         super(physicalAccess, properties);
42         this.isWorking = false;
43         trace.println(Trace.DEBUG, "Uses InTransactionBaseImageMultiImpl");
44     }
45
46     /**
47      * ***************************************************************************************************
48      */

49
50     public synchronized void commit() {
51         isWorking = true;
52         physicalAccess.startCommit();
53         try {
54             Iterator JavaDoc values = deletedObjects.values().iterator();
55             while (values.hasNext()) {
56                 physicalAccess.deleteFileObject((InFileAddress) values.next());
57             }
58             values = newObjects.values().iterator();
59             while (values.hasNext()) {
60                 JalistoObject fo = (JalistoObject) values.next();
61                 physicalAccess.insertFileObject(fo.getIfa(), fo);
62             }
63             values = modifiedObjects.values().iterator();
64             while (values.hasNext()) {
65                 JalistoObject fo = (JalistoObject) values.next();
66                 physicalAccess.updateFileObject(fo.getIfa(), fo);
67             }
68             finishCommit();
69             physicalAccess.commit();
70         } finally {
71             isWorking = false;
72         }
73     }
74
75     public synchronized void rollback() {
76         isWorking = true;
77         try {
78             clear();
79             readObjects.clear();
80             physicalAccess.rollback();
81         } finally {
82             isWorking = false;
83         }
84     }
85
86
87     public synchronized void insertFileObject(InFileAddress ifa, JalistoObject fo) {
88         isWorking = true;
89         try {
90             String JavaDoc address = ifa.getAddress();
91             if (modifiedObjects.containsKey(address) ||
92                 newObjects.containsKey(address) ||
93                 readObjects.containsKey(address)) {
94                 isWorking = false;
95                 throw new JalistoException("you could not insert a object already in base : " + ifa + ", " + fo);
96             }
97             if (deletedObjects.containsKey(address)) {
98                 deletedObjects.remove(address);
99                 modifiedObjects.put(address, fo);
100             } else {
101                 newObjects.put(address, fo);
102             }
103         } finally {
104             isWorking = false;
105         }
106     }
107
108     public synchronized JalistoObject readFileObjectAt(InFileAddress ifa) {
109         isWorking = true;
110         try {
111             String JavaDoc address = ifa.getAddress();
112             if (deletedObjects.containsKey(address)) {
113                 throw new JalistoException("object has been removed from base : " + ifa);
114             }
115             JalistoObject result = (JalistoObject) readObjects.get(address);
116             if (result != null)
117                 return result;
118             result = (JalistoObject) modifiedObjects.get(address);
119             if (result != null)
120                 return result;
121             result = (JalistoObject) newObjects.get(address);
122             if (result != null)
123                 return result;
124             result = physicalAccess.readFileObjectAt(ifa);
125             readObjects.put(address, result);
126             return result;
127         } finally {
128             isWorking = false;
129         }
130
131     }
132
133     public synchronized void updateFileObject(InFileAddress ifa, JalistoObject fo) {
134         isWorking = true;
135         try {
136             String JavaDoc address = ifa.getAddress();
137             if (deletedObjects.containsKey(address)) {
138                 isWorking = false;
139                 throw new JalistoException("object has been removed from base : " + ifa + ", " + fo);
140             }
141             if (newObjects.containsKey(address)) {
142                 newObjects.put(address, fo);
143             } else {
144                 modifiedObjects.put(address, fo);
145             }
146             readObjects.remove(address);
147         } finally {
148             isWorking = false;
149         }
150     }
151
152     public synchronized void deleteFileObject(InFileAddress ifa) {
153         isWorking = true;
154         try {
155             String JavaDoc address = ifa.getAddress();
156             if (deletedObjects.containsKey(address)) {
157                 isWorking = false;
158                 throw new JalistoException("object has already been removed from base : " + ifa);
159             }
160             if (newObjects.remove(address) == null) {
161                 modifiedObjects.remove(address);
162             }
163             readObjects.remove(address);
164             deletedObjects.put(address, ifa);
165         } finally {
166             isWorking = false;
167         }
168     }
169
170
171     public boolean isWorking() {
172         return isWorking;
173     }
174
175     public synchronized boolean startWorking() {
176         if (isWorking) {
177             return false;
178         }
179         isWorking = true;
180         return true;
181     }
182
183     public synchronized void stopWorking() {
184         isWorking = false;
185     }
186
187     public synchronized Collection JavaDoc getKeysStartingWith(String JavaDoc filter, boolean withOrder) {
188         return super.getKeysStartingWith(filter, withOrder);
189     }
190
191     /**
192      * *************************** INTERNAL *****************************************************
193      */

194
195     protected void finishCommit() {
196         if (keepInMemory) {
197             readObjects.putAll(modifiedObjects);
198             readObjects.putAll(newObjects);
199         }
200         clear();
201     }
202
203     protected void clear() {
204         if (!keepInMemory) {
205             readObjects.clear();
206         }
207         modifiedObjects.clear();
208         newObjects.clear();
209         deletedObjects.clear();
210     }
211
212
213     private boolean isWorking;
214
215
216     /**
217      * ******************************* STATIC CONSTRUCTOR ******************************************
218      */

219
220     public static InTransactionBaseImage getInTransactionBaseImageInstance(
221             PluggablePhysicalFileAccess physicalAccess, JalistoProperties properties) {
222         return new InTransactionBaseImageMultiImpl(physicalAccess, properties);
223     }
224 }
225
Popular Tags