001: package org.apache.ojb.jdori.sql;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.BitSet;
019: import java.util.Iterator;
020:
021: import javax.jdo.Extent;
022: import javax.jdo.JDOFatalInternalException;
023: import javax.jdo.JDOUserException;
024: import javax.jdo.spi.PersistenceCapable;
025:
026: import org.apache.ojb.broker.Identity;
027: import org.apache.ojb.broker.PersistenceBroker;
028: import org.apache.ojb.broker.util.ObjectModificationDefaultImpl;
029: import org.apache.ojb.broker.util.logging.Logger;
030: import org.apache.ojb.broker.util.logging.LoggerFactory;
031:
032: import com.sun.jdori.Connector;
033: import com.sun.jdori.FieldManager;
034: import com.sun.jdori.PersistenceManagerInternal;
035: import com.sun.jdori.StateManagerInternal;
036: import com.sun.jdori.StoreManager;
037: import com.sun.jdori.common.query.BasicQueryResult;
038: import com.sun.jdori.model.jdo.JDOClass;
039: import com.sun.jdori.query.QueryResult;
040: import com.sun.jdori.query.QueryResultHelper;
041:
042: /**
043: * StoreManager represents the datastore to the rest of the JDO components.
044: * It provides the means to write and read instances, to get the extent of
045: * classes, and to get the object id for a persistence capable object.
046: *
047: * @author Thomas Mahler
048: */
049: class OjbStoreManager implements StoreManager {
050: private final OjbStorePMF pmf;
051: private final OjbStoreConnector connector;
052:
053: private boolean optimistic;
054:
055: /** the logger used for debugging*/
056: private Logger logger = LoggerFactory.getLogger("JDO");
057:
058: OjbStoreManager(OjbStorePMF pmf) {
059: this .pmf = pmf;
060: this .connector = new OjbStoreConnector(pmf);
061: }
062:
063: /**
064: * @see com.sun.jdori.StoreManager#getConnector
065: */
066: public Connector getConnector() {
067: return connector;
068: }
069:
070: /**
071: * @see com.sun.jdori.StoreManager#getConnector(String userid,
072: * String password)
073: */
074: public Connector getConnector(String userid, String password) {
075: throw new JDOUserException("Not implemented");
076: }
077:
078: /**
079: * @see com.sun.jdori.StoreManager#insert(BitSet, BitSet, StateManagerInternal)
080: */
081: public synchronized int insert(BitSet loadedFields,
082: BitSet dirtyFields, StateManagerInternal sm) {
083:
084: try {
085: logger.debug("OjbStoreManager.insert");
086: PersistenceBroker broker = connector.getBroker();
087: Object instance = sm.getObject();
088: broker
089: .store(instance,
090: ObjectModificationDefaultImpl.INSERT);
091: } catch (Exception ex) {
092: throw new OjbStoreFatalInternalException(getClass(),
093: "insert", ex);
094: }
095: dirtyFields.xor(dirtyFields);
096: return StateManagerInternal.FLUSHED_COMPLETE;
097: }
098:
099: /**
100: * @see com.sun.jdori.StoreManager#update(BitSet, BitSet, StateManagerInternal)
101: */
102: public synchronized int update(BitSet loadedFields,
103: BitSet dirtyFields, StateManagerInternal sm) {
104:
105: try {
106: logger.debug("OjbStoreManager.update");
107: PersistenceBroker broker = connector.getBroker();
108: fetch(sm, null);
109: Object instance = sm.getObject();
110: broker
111: .store(instance,
112: ObjectModificationDefaultImpl.UPDATE);
113: } catch (Exception ex) {
114: throw new OjbStoreFatalInternalException(getClass(),
115: "update", ex);
116: }
117: dirtyFields.xor(dirtyFields);
118: return StateManagerInternal.FLUSHED_COMPLETE;
119: }
120:
121: /**
122: * @see com.sun.jdori.StoreManager#verifyFields(BitSet, BitSet, StateManagerInternal)
123: */
124: public synchronized int verifyFields(BitSet ignoredFields,
125: BitSet fieldsToVerify, StateManagerInternal sm) {
126: fieldsToVerify.xor(fieldsToVerify);
127: return StateManagerInternal.FLUSHED_COMPLETE;
128: }
129:
130: /**
131: * @see com.sun.jdori.StoreManager#delete(BitSet, BitSet, StateManagerInternal)
132: */
133: public synchronized int delete(BitSet loadedFields,
134: BitSet dirtyFields, StateManagerInternal sm) {
135: Identity oid = (Identity) sm.getInternalObjectId();
136: logger.debug("OjbStoreManager.delete(" + oid + ")");
137: try {
138: fetch(sm, null);
139: connector.getBroker().delete(sm.getObject());
140: } catch (Exception ex) {
141: throw new OjbStoreFatalInternalException(getClass(),
142: "delete", ex);
143: }
144: dirtyFields.xor(dirtyFields);
145: return StateManagerInternal.FLUSHED_COMPLETE;
146: }
147:
148: /**
149: * @see com.sun.jdori.StoreManager#fetch
150: */
151: public synchronized void fetch(StateManagerInternal sm,
152: int fieldNums[]) {
153: PersistenceBroker broker = connector.getBroker();
154: try {
155: Object instance = sm.getObject();
156: Identity oid = (Identity) sm.getInternalObjectId();
157: if (oid == null) {
158: oid = new Identity(instance, broker);
159: }
160: broker.removeFromCache(instance);
161: PersistenceCapable pc = (PersistenceCapable) broker
162: .getObjectByIdentity(oid);
163:
164: JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
165: if (fieldNums == null) {
166: fieldNums = jdoClass.getManagedFieldNumbers();
167: }
168:
169: FieldManager fm = new OjbFieldManager(pc, broker);
170: sm.replaceFields(fieldNums, fm);
171:
172: getConnector().flush();
173: }
174:
175: catch (Exception ex) {
176: throw new OjbStoreFatalInternalException(getClass(),
177: "fetch", ex);
178: }
179: }
180:
181: /**
182: * @see com.sun.jdori.StoreManager#getExtent
183: */
184: public synchronized Extent getExtent(Class pcClass,
185: boolean subclasses, PersistenceManagerInternal pm) {
186: PersistenceBroker broker = connector.getBroker();
187: return new OjbExtent(pcClass, broker, pm);
188: }
189:
190: /**
191: * @see com.sun.jdori.StoreManager#createObjectId
192: */
193: public synchronized Object createObjectId(StateManagerInternal sm,
194: PersistenceManagerInternal pm) {
195: PersistenceCapable obj = sm.getObject();
196: Identity oid = new Identity(obj, connector.getBroker());
197: return oid;
198: }
199:
200: /**
201: * @see com.sun.jdori.StoreManager#createObjectId
202: */
203: public synchronized Object createInternalObjectId(
204: StateManagerInternal sm, PersistenceCapable pc, Object oid,
205: Class cls, PersistenceManagerInternal pm) {
206: return new Identity(pc, connector.getBroker());
207: }
208:
209: /**
210: * @see com.sun.jdori.StoreManager#getExternalObjectId(Object oid,
211: * PersistenceCapable pc)
212: */
213: public synchronized Object getExternalObjectId(Object objectId,
214: PersistenceCapable pc) {
215: return new Identity(pc, connector.getBroker());
216: }
217:
218: /**
219: * @see com.sun.jdori.StoreManager#copyKeyFieldsFromObjectId
220: */
221: public void copyKeyFieldsFromObjectId(StateManagerInternal sm,
222: Class pcClass) {
223: new Identity(sm.getObject(), connector.getBroker());
224: }
225:
226: /**
227: * @see com.sun.jdori.StoreManager#hasActualPCClass
228: */
229: public boolean hasActualPCClass(Object objectId) {
230: boolean rc = true;
231: return rc;
232: }
233:
234: /**
235: * @see com.sun.jdori.StoreManager#getInternalObjectId
236: */
237: public synchronized Object getInternalObjectId(Object objectId,
238: PersistenceManagerInternal pm) {
239: return objectId;
240: }
241:
242: /**
243: * @see com.sun.jdori.StoreManager#getPCClassForOid
244: */
245: public synchronized Class getPCClassForOid(Object objectId,
246: PersistenceManagerInternal pm) {
247: return ((Identity) objectId).getObjectsTopLevelClass();
248: }
249:
250: /**
251: * @see com.sun.jdori.StoreManager#newObjectIdInstance
252: */
253: public Object newObjectIdInstance(Class pcClass, String str) {
254: return Identity.fromByteArray(str.getBytes());
255: }
256:
257: /**
258: * @see com.sun.jdori.StoreManager#flush
259: */
260: public void flush(Iterator it, PersistenceManagerInternal pm) {
261: this .optimistic = pm.currentTransaction().getOptimistic();
262: boolean err = false;
263:
264: while (it.hasNext()) {
265: StateManagerInternal sm = (StateManagerInternal) it.next();
266: logger.debug("OjbStoreManager.flush: "
267: + sm.getInternalObjectId() + ", "
268: + Helper.getLCState(sm));
269: sm.preStore();
270: sm.replaceSCOFields();
271: sm.flush(this );
272: if (!sm.isFlushed()) {
273: err = true;
274: break;
275: }
276: }
277:
278: logger.debug("OjbStoreManager.flush: end, err=" + err);
279:
280: if (err) {
281: throw new JDOFatalInternalException("Error in flush");
282: }
283: }
284:
285: /**
286: * @see com.sun.jdori.StoreManager#newObjectIdInstance
287: */
288: public QueryResult newQueryResult(
289: QueryResultHelper queryResultHelper) {
290: return new BasicQueryResult(queryResultHelper);
291: }
292:
293: }
|