001: package org.apache.ojb.otm.kit;
002:
003: /* Copyright 2003-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 org.apache.ojb.broker.Identity;
019: import org.apache.ojb.otm.OTMKit;
020: import org.apache.ojb.otm.copy.*;
021: import org.apache.ojb.otm.lock.map.InMemoryLockMap;
022: import org.apache.ojb.otm.lock.map.LockMap;
023: import org.apache.ojb.otm.lock.wait.LockWaitStrategy;
024: import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
025: import org.apache.ojb.otm.swizzle.CopySwizzling;
026: import org.apache.ojb.otm.swizzle.Swizzling;
027: import org.apache.ojb.otm.transaction.LocalTransactionFactory;
028: import org.apache.ojb.otm.transaction.TransactionFactory;
029:
030: import java.io.Serializable;
031:
032: /**
033: * A base implementation of an OTMKit using local transactions, an
034: * in-memory lock map, and metadata based object copying for
035: * object swizzling in transactional contexts.
036: *
037: * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
038: */
039: public class SimpleKit extends OTMKit {
040:
041: private static SimpleKit _instance;
042:
043: protected TransactionFactory _txFactory;
044: protected Swizzling _swizzlingStrategy;
045: protected LockWaitStrategy _lockWaitStrategy;
046: protected LockMap _lockMap;
047: protected ObjectCopyStrategy _noOpCopyStrategy;
048: protected ObjectCopyStrategy _defaultCopyStrategy;
049: protected ObjectCopyStrategy _cloneableCopyStrategy;
050:
051: /**
052: * Constructor for SimpleKit.
053: */
054: protected SimpleKit() {
055: super ();
056: _txFactory = new LocalTransactionFactory();
057: _swizzlingStrategy = new CopySwizzling();
058: _lockWaitStrategy = new TimeoutStrategy();
059: _lockMap = new InMemoryLockMap();
060: _noOpCopyStrategy = new NoOpObjectCopyStrategy();
061: //_defaultCopyStrategy = new ReflectiveObjectCopyStrategy();
062: //_defaultCopyStrategy = new SerializeObjectCopyStrategy();
063: _defaultCopyStrategy = new MetadataObjectCopyStrategy();
064: _cloneableCopyStrategy = new CloneableObjectCopyStrategy();
065: }
066:
067: /**
068: * Obtain the single instance of SimpleKit
069: */
070: public static SimpleKit getInstance() {
071: if (_instance == null) {
072: _instance = new SimpleKit();
073: }
074: return _instance;
075: }
076:
077: ///////////////////////////////////////
078: // OTMKit Protocol
079: ///////////////////////////////////////
080:
081: /**
082: * @see org.apache.ojb.otm.OTMKit#getTransactionFactory()
083: */
084: protected TransactionFactory getTransactionFactory() {
085: return _txFactory;
086: }
087:
088: /**
089: * @see org.apache.ojb.otm.OTMKit#getSwizzlingStrategy()
090: */
091: public Swizzling getSwizzlingStrategy() {
092: return _swizzlingStrategy;
093: }
094:
095: /**
096: * @see org.apache.ojb.otm.OTMKit#getLockWaitStrategy()
097: */
098: public LockWaitStrategy getLockWaitStrategy() {
099: return _lockWaitStrategy;
100: }
101:
102: /**
103: * @see org.apache.ojb.otm.OTMKit#getLockMap()
104: */
105: public LockMap getLockMap() {
106: return _lockMap;
107: }
108:
109: /**
110: * @see org.apache.ojb.otm.OTMKit#getCopyStrategy(Identity)
111: */
112: public ObjectCopyStrategy getCopyStrategy(Identity oid) {
113: Class clazz = oid.getClass();
114:
115: if (OjbCloneable.class.isAssignableFrom(clazz)) {
116: return _cloneableCopyStrategy;
117: } else if (Serializable.class.isAssignableFrom(clazz)) {
118: return _defaultCopyStrategy;
119: } else {
120: return _noOpCopyStrategy;
121: }
122: }
123:
124: /**
125: * Should OTM implicitely lock all objects that are reachable
126: * from the explicitely locked object? The updates to the locked
127: * objects are automatically stored to the database at the end
128: * of transaction.
129: **/
130: public boolean isImplicitLockingUsed() {
131: return true;
132: }
133:
134: /**
135: * Should OTM verify each inserted object for presence in the database?
136: **/
137: public boolean isInsertVerified() {
138: return false;
139: }
140:
141: /**
142: * Should OTM perform INSERTs for the given object eagerly or during commit?
143: **/
144: public boolean isEagerInsert(Object obj) {
145: return false;
146: }
147: }
|