KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdori > sql > OjbStoreManager


1 package org.apache.ojb.jdori.sql;
2 /* Copyright 2002-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.util.BitSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.jdo.Extent;
21 import javax.jdo.JDOFatalInternalException;
22 import javax.jdo.JDOUserException;
23 import javax.jdo.spi.PersistenceCapable;
24
25 import org.apache.ojb.broker.Identity;
26 import org.apache.ojb.broker.PersistenceBroker;
27 import org.apache.ojb.broker.util.ObjectModificationDefaultImpl;
28 import org.apache.ojb.broker.util.logging.Logger;
29 import org.apache.ojb.broker.util.logging.LoggerFactory;
30
31 import com.sun.jdori.Connector;
32 import com.sun.jdori.FieldManager;
33 import com.sun.jdori.PersistenceManagerInternal;
34 import com.sun.jdori.StateManagerInternal;
35 import com.sun.jdori.StoreManager;
36 import com.sun.jdori.common.query.BasicQueryResult;
37 import com.sun.jdori.model.jdo.JDOClass;
38 import com.sun.jdori.query.QueryResult;
39 import com.sun.jdori.query.QueryResultHelper;
40
41
42 /**
43  * StoreManager represents the datastore to the rest of the JDO components.
44  * It provides the means to write and read instances, to get the extent of
45  * classes, and to get the object id for a persistence capable object.
46  *
47  * @author Thomas Mahler
48  */

49 class OjbStoreManager implements StoreManager
50 {
51     private final OjbStorePMF pmf;
52     private final OjbStoreConnector connector;
53
54     private boolean optimistic;
55
56     /** the logger used for debugging*/
57     private Logger logger = LoggerFactory.getLogger("JDO");
58     
59     OjbStoreManager(OjbStorePMF pmf)
60     {
61         this.pmf = pmf;
62         this.connector = new OjbStoreConnector(pmf);
63     }
64
65
66     /**
67      * @see com.sun.jdori.StoreManager#getConnector
68      */

69     public Connector getConnector()
70     {
71         return connector;
72     }
73
74     /**
75      * @see com.sun.jdori.StoreManager#getConnector(String userid,
76      * String password)
77      */

78     public Connector getConnector(String JavaDoc userid, String JavaDoc password)
79     {
80         throw new JDOUserException("Not implemented");
81     }
82
83     /**
84     * @see com.sun.jdori.StoreManager#insert(BitSet, BitSet, StateManagerInternal)
85     */

86     public synchronized int insert(
87         BitSet JavaDoc loadedFields,
88         BitSet JavaDoc dirtyFields,
89         StateManagerInternal sm)
90     {
91
92         try
93         {
94             logger.debug("OjbStoreManager.insert");
95             PersistenceBroker broker = connector.getBroker();
96             Object JavaDoc instance = sm.getObject();
97             broker.store(instance, ObjectModificationDefaultImpl.INSERT);
98         }
99         catch (Exception JavaDoc ex)
100         {
101             throw new OjbStoreFatalInternalException(getClass(), "insert", ex);
102         }
103         dirtyFields.xor(dirtyFields);
104         return StateManagerInternal.FLUSHED_COMPLETE;
105     }
106
107     /**
108     * @see com.sun.jdori.StoreManager#update(BitSet, BitSet, StateManagerInternal)
109     */

110     public synchronized int update(
111         BitSet JavaDoc loadedFields,
112         BitSet JavaDoc dirtyFields,
113         StateManagerInternal sm)
114     {
115
116         try
117         {
118             logger.debug("OjbStoreManager.update");
119             PersistenceBroker broker = connector.getBroker();
120             fetch(sm, null);
121             Object JavaDoc instance = sm.getObject();
122             broker.store(instance, ObjectModificationDefaultImpl.UPDATE);
123         }
124         catch (Exception JavaDoc ex)
125         {
126             throw new OjbStoreFatalInternalException(getClass(), "update", ex);
127         }
128         dirtyFields.xor(dirtyFields);
129         return StateManagerInternal.FLUSHED_COMPLETE;
130     }
131
132     /**
133      * @see com.sun.jdori.StoreManager#verifyFields(BitSet, BitSet, StateManagerInternal)
134      */

135     public synchronized int verifyFields(
136         BitSet JavaDoc ignoredFields,
137         BitSet JavaDoc fieldsToVerify,
138         StateManagerInternal sm)
139     {
140         fieldsToVerify.xor(fieldsToVerify);
141         return StateManagerInternal.FLUSHED_COMPLETE;
142     }
143
144
145     /**
146      * @see com.sun.jdori.StoreManager#delete(BitSet, BitSet, StateManagerInternal)
147      */

148     public synchronized int delete(
149         BitSet JavaDoc loadedFields,
150         BitSet JavaDoc dirtyFields,
151         StateManagerInternal sm)
152     {
153         Identity oid = (Identity)sm.getInternalObjectId();
154         logger.debug("OjbStoreManager.delete(" + oid + ")");
155         try
156         {
157             fetch(sm,null);
158             connector.getBroker().delete(sm.getObject());
159         }
160         catch (Exception JavaDoc ex)
161         {
162             throw new OjbStoreFatalInternalException(getClass(), "delete", ex);
163         }
164         dirtyFields.xor(dirtyFields);
165         return StateManagerInternal.FLUSHED_COMPLETE;
166     }
167
168     /**
169     * @see com.sun.jdori.StoreManager#fetch
170     */

171     public synchronized void fetch(StateManagerInternal sm, int fieldNums[])
172     {
173         PersistenceBroker broker = connector.getBroker();
174         try
175         {
176             Object JavaDoc instance = sm.getObject();
177             Identity oid = (Identity) sm.getInternalObjectId();
178             if (oid == null)
179             {
180                 oid = new Identity(instance,broker);
181             }
182             broker.removeFromCache(instance);
183             PersistenceCapable pc = (PersistenceCapable) broker.getObjectByIdentity(oid);
184
185             JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
186             if (fieldNums == null)
187             {
188                 fieldNums = jdoClass.getManagedFieldNumbers();
189             }
190
191             FieldManager fm = new OjbFieldManager(pc, broker);
192             sm.replaceFields(fieldNums, fm);
193
194             getConnector().flush();
195         }
196
197         catch (Exception JavaDoc ex)
198         {
199             throw new OjbStoreFatalInternalException(getClass(), "fetch", ex);
200         }
201     }
202
203     /**
204      * @see com.sun.jdori.StoreManager#getExtent
205      */

206     public synchronized Extent getExtent(
207         Class JavaDoc pcClass,
208         boolean subclasses,
209         PersistenceManagerInternal pm)
210     {
211         PersistenceBroker broker = connector.getBroker();
212         return new OjbExtent(pcClass, broker, pm);
213     }
214
215     /**
216      * @see com.sun.jdori.StoreManager#createObjectId
217      */

218     public synchronized Object JavaDoc createObjectId(
219         StateManagerInternal sm,
220         PersistenceManagerInternal pm)
221     {
222         PersistenceCapable obj = sm.getObject();
223         Identity oid = new Identity(obj, connector.getBroker());
224         return oid;
225     }
226
227     /**
228      * @see com.sun.jdori.StoreManager#createObjectId
229      */

230     public synchronized Object JavaDoc createInternalObjectId(
231         StateManagerInternal sm,
232         PersistenceCapable pc,
233         Object JavaDoc oid,
234         Class JavaDoc cls,
235         PersistenceManagerInternal pm)
236     {
237         return new Identity(pc, connector.getBroker());
238     }
239
240     /**
241      * @see com.sun.jdori.StoreManager#getExternalObjectId(Object oid,
242      * PersistenceCapable pc)
243      */

244     public synchronized Object JavaDoc getExternalObjectId(Object JavaDoc objectId, PersistenceCapable pc)
245     {
246         return new Identity(pc, connector.getBroker());
247     }
248
249     /**
250      * @see com.sun.jdori.StoreManager#copyKeyFieldsFromObjectId
251      */

252     public void copyKeyFieldsFromObjectId(StateManagerInternal sm, Class JavaDoc pcClass)
253     {
254         new Identity(sm.getObject(), connector.getBroker());
255     }
256
257     /**
258      * @see com.sun.jdori.StoreManager#hasActualPCClass
259      */

260     public boolean hasActualPCClass(Object JavaDoc objectId)
261     {
262         boolean rc = true;
263         return rc;
264     }
265
266     /**
267      * @see com.sun.jdori.StoreManager#getInternalObjectId
268      */

269     public synchronized Object JavaDoc getInternalObjectId(Object JavaDoc objectId, PersistenceManagerInternal pm)
270     {
271         return objectId;
272     }
273
274     /**
275      * @see com.sun.jdori.StoreManager#getPCClassForOid
276      */

277     public synchronized Class JavaDoc getPCClassForOid(Object JavaDoc objectId, PersistenceManagerInternal pm)
278     {
279         return ((Identity) objectId).getObjectsTopLevelClass();
280     }
281
282     /**
283      * @see com.sun.jdori.StoreManager#newObjectIdInstance
284      */

285     public Object JavaDoc newObjectIdInstance(Class JavaDoc pcClass, String JavaDoc str)
286     {
287         return Identity.fromByteArray(str.getBytes());
288     }
289
290     /**
291      * @see com.sun.jdori.StoreManager#flush
292      */

293     public void flush(Iterator JavaDoc it, PersistenceManagerInternal pm)
294     {
295         this.optimistic = pm.currentTransaction().getOptimistic();
296         boolean err = false;
297
298         while (it.hasNext())
299         {
300             StateManagerInternal sm = (StateManagerInternal) it.next();
301             logger.debug("OjbStoreManager.flush: " + sm.getInternalObjectId() + ", " + Helper.getLCState(sm));
302             sm.preStore();
303             sm.replaceSCOFields();
304             sm.flush(this);
305             if (!sm.isFlushed())
306             {
307                 err = true;
308                 break;
309             }
310         }
311
312         logger.debug("OjbStoreManager.flush: end, err=" + err);
313
314         if (err)
315         {
316             throw new JDOFatalInternalException("Error in flush");
317         }
318     }
319
320
321
322     /**
323      * @see com.sun.jdori.StoreManager#newObjectIdInstance
324      */

325     public QueryResult newQueryResult(QueryResultHelper queryResultHelper)
326     {
327         return new BasicQueryResult(queryResultHelper);
328     }
329
330 }
331
Popular Tags