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    
042        private static SimpleKit _instance;
043    
044        protected TransactionFactory _txFactory;
045        protected Swizzling _swizzlingStrategy;
046        protected LockWaitStrategy _lockWaitStrategy;
047        protected LockMap _lockMap;
048        protected ObjectCopyStrategy _noOpCopyStrategy;
049        protected ObjectCopyStrategy _defaultCopyStrategy;
050        protected ObjectCopyStrategy _cloneableCopyStrategy;
051    
052        /**
053         * Constructor for SimpleKit.
054         */
055        protected SimpleKit()
056        {
057            super();
058            _txFactory = new LocalTransactionFactory();
059            _swizzlingStrategy = new CopySwizzling();
060            _lockWaitStrategy = new TimeoutStrategy();
061            _lockMap = new InMemoryLockMap();
062            _noOpCopyStrategy = new NoOpObjectCopyStrategy();
063            //_defaultCopyStrategy = new ReflectiveObjectCopyStrategy();
064            //_defaultCopyStrategy = new SerializeObjectCopyStrategy();
065            _defaultCopyStrategy = new MetadataObjectCopyStrategy();
066            _cloneableCopyStrategy = new CloneableObjectCopyStrategy();
067        }
068    
069        /**
070         * Obtain the single instance of SimpleKit
071         */
072        public static SimpleKit getInstance()
073        {
074            if (_instance == null)
075            {
076                _instance = new SimpleKit();
077            }
078            return _instance;
079        }
080    
081        ///////////////////////////////////////
082        // OTMKit Protocol
083        ///////////////////////////////////////
084    
085        /**
086         * @see org.apache.ojb.otm.OTMKit#getTransactionFactory()
087         */
088        protected TransactionFactory getTransactionFactory()
089        {
090            return _txFactory;
091        }
092    
093        /**
094         * @see org.apache.ojb.otm.OTMKit#getSwizzlingStrategy()
095         */
096        public Swizzling getSwizzlingStrategy()
097        {
098            return _swizzlingStrategy;
099        }
100    
101    
102        /**
103        * @see org.apache.ojb.otm.OTMKit#getLockWaitStrategy()
104        */
105        public LockWaitStrategy getLockWaitStrategy()
106        {
107            return _lockWaitStrategy;
108        }
109    
110        /**
111         * @see org.apache.ojb.otm.OTMKit#getLockMap()
112         */
113        public LockMap getLockMap()
114        {
115            return _lockMap;
116        }
117    
118        /**
119         * @see org.apache.ojb.otm.OTMKit#getCopyStrategy(Identity)
120         */
121        public ObjectCopyStrategy getCopyStrategy(Identity oid)
122        {
123            Class clazz = oid.getClass();
124    
125            if (OjbCloneable.class.isAssignableFrom(clazz))
126            {
127                return _cloneableCopyStrategy;
128            }
129            else if (Serializable.class.isAssignableFrom(clazz))
130            {
131                return _defaultCopyStrategy;
132            }
133            else
134            {
135                return _noOpCopyStrategy;
136            }
137        }
138    
139        /**
140         * Should OTM implicitely lock all objects that are reachable
141         * from the explicitely locked object? The updates to the locked
142         * objects are automatically stored to the database at the end
143         * of transaction.
144         **/
145        public boolean isImplicitLockingUsed()
146        {
147            return true;
148        }
149    
150    
151        /**
152         * Should OTM verify each inserted object for presence in the database?
153         **/
154        public boolean isInsertVerified()
155        {
156            return false;
157        }
158        
159        /**
160         * Should OTM perform INSERTs for the given object eagerly or during commit?
161         **/
162        public boolean isEagerInsert(Object obj)
163        {
164            return false;
165        }
166    }