View Javadoc

1   package org.apache.ojb.otm.kit;
2   
3   /* Copyright 2003-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import org.apache.ojb.broker.Identity;
19  import org.apache.ojb.otm.OTMKit;
20  import org.apache.ojb.otm.copy.*;
21  import org.apache.ojb.otm.lock.map.InMemoryLockMap;
22  import org.apache.ojb.otm.lock.map.LockMap;
23  import org.apache.ojb.otm.lock.wait.LockWaitStrategy;
24  import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
25  import org.apache.ojb.otm.swizzle.CopySwizzling;
26  import org.apache.ojb.otm.swizzle.Swizzling;
27  import org.apache.ojb.otm.transaction.LocalTransactionFactory;
28  import org.apache.ojb.otm.transaction.TransactionFactory;
29  
30  import java.io.Serializable;
31  
32  /**
33   * A base implementation of an OTMKit using local transactions, an
34   * in-memory lock map, and metadata based object copying for
35   * object swizzling in transactional contexts.
36   *
37   * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
38   */
39  public class SimpleKit extends OTMKit
40  {
41  
42      private static SimpleKit _instance;
43  
44      protected TransactionFactory _txFactory;
45      protected Swizzling _swizzlingStrategy;
46      protected LockWaitStrategy _lockWaitStrategy;
47      protected LockMap _lockMap;
48      protected ObjectCopyStrategy _noOpCopyStrategy;
49      protected ObjectCopyStrategy _defaultCopyStrategy;
50      protected ObjectCopyStrategy _cloneableCopyStrategy;
51  
52      /**
53       * Constructor for SimpleKit.
54       */
55      protected SimpleKit()
56      {
57          super();
58          _txFactory = new LocalTransactionFactory();
59          _swizzlingStrategy = new CopySwizzling();
60          _lockWaitStrategy = new TimeoutStrategy();
61          _lockMap = new InMemoryLockMap();
62          _noOpCopyStrategy = new NoOpObjectCopyStrategy();
63          //_defaultCopyStrategy = new ReflectiveObjectCopyStrategy();
64          //_defaultCopyStrategy = new SerializeObjectCopyStrategy();
65          _defaultCopyStrategy = new MetadataObjectCopyStrategy();
66          _cloneableCopyStrategy = new CloneableObjectCopyStrategy();
67      }
68  
69      /**
70       * Obtain the single instance of SimpleKit
71       */
72      public static SimpleKit getInstance()
73      {
74          if (_instance == null)
75          {
76              _instance = new SimpleKit();
77          }
78          return _instance;
79      }
80  
81      ///////////////////////////////////////
82      // OTMKit Protocol
83      ///////////////////////////////////////
84  
85      /**
86       * @see org.apache.ojb.otm.OTMKit#getTransactionFactory()
87       */
88      protected TransactionFactory getTransactionFactory()
89      {
90          return _txFactory;
91      }
92  
93      /**
94       * @see org.apache.ojb.otm.OTMKit#getSwizzlingStrategy()
95       */
96      public Swizzling getSwizzlingStrategy()
97      {
98          return _swizzlingStrategy;
99      }
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 }