KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > locking > LockStrategyFactory


1 package org.apache.ojb.odmg.locking;
2
3 /* Copyright 2002-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.PersistenceBrokerException;
19 import org.apache.ojb.broker.core.proxy.ProxyHelper;
20 import org.apache.ojb.broker.locking.IsolationLevels;
21 import org.apache.ojb.broker.metadata.ClassDescriptor;
22 import org.apache.ojb.broker.util.logging.LoggerFactory;
23 import org.apache.ojb.odmg.TransactionImpl;
24 import org.apache.ojb.odmg.TxManagerFactory;
25
26 /**
27  * Factory class used to obtain the proper LockingStrategy for an Object.
28  * @author Thomas Mahler & David Dixon-Peugh
29  * @deprecated
30  * @version $Id: LockStrategyFactory.java,v 1.13.2.4 2005/12/21 22:29:51 tomdz Exp $
31  */

32 public class LockStrategyFactory
33 {
34
35     /**
36      * private constructor: use static methods only.
37      *
38      */

39     private LockStrategyFactory()
40     {
41     }
42
43     private static LockStrategy readUncommitedStrategy = new ReadUncommittedStrategy();
44     private static LockStrategy readCommitedStrategy = new ReadCommittedStrategy();
45     private static LockStrategy readRepeatableStrategy = new RepeatableReadStrategy();
46     private static LockStrategy serializableStrategy = new SerializableStrategy();
47     private static LockStrategy noopStrategy = new NOOPStrategy();
48
49
50     /**
51      * obtains a LockStrategy for Object obj. The Strategy to be used is
52      * selected by evaluating the ClassDescriptor of obj.getClass().
53      *
54      * @return LockStrategy
55      */

56     public static LockStrategy getStrategyFor(Object JavaDoc obj)
57     {
58         int isolationLevel = getIsolationLevel(obj);
59         switch (isolationLevel)
60         {
61             case IsolationLevels.IL_READ_UNCOMMITTED:
62                 return readUncommitedStrategy;
63             case IsolationLevels.IL_READ_COMMITTED:
64                 return readCommitedStrategy;
65             case IsolationLevels.IL_REPEATABLE_READ:
66                 return readRepeatableStrategy;
67             case IsolationLevels.IL_SERIALIZABLE:
68                 return serializableStrategy;
69             case IsolationLevels.IL_OPTIMISTIC:
70                 return noopStrategy;
71             case IsolationLevels.IL_NONE:
72                 return noopStrategy;
73             default:
74                 return readUncommitedStrategy;
75         }
76     }
77
78     /**
79      * determines the isolationlevel of class c by evaluating
80      * the ClassDescriptor of obj.getClass().
81      *
82      * @return int the isolationlevel
83      */

84     public static int getIsolationLevel(Object JavaDoc obj)
85     {
86         Class JavaDoc c = ProxyHelper.getRealClass(obj);
87         int isolationLevel = IsolationLevels.IL_READ_UNCOMMITTED;
88
89         try
90         {
91             ClassDescriptor cld = TxManagerFactory.instance().getCurrentTransaction().getBroker().getClassDescriptor(c);
92             isolationLevel = cld.getIsolationLevel();
93         }
94         catch (PersistenceBrokerException e)
95         {
96             LoggerFactory.getDefaultLogger().error("[LockStrategyFactory] Can't detect locking isolation level", e);
97         }
98         return isolationLevel;
99     }
100
101     static class NOOPStrategy implements LockStrategy
102     {
103         public boolean readLock(TransactionImpl tx, Object JavaDoc obj)
104         {
105             return true;
106         }
107
108         public boolean writeLock(TransactionImpl tx, Object JavaDoc obj)
109         {
110             return true;
111         }
112
113         public boolean upgradeLock(TransactionImpl tx, Object JavaDoc obj)
114         {
115             return true;
116         }
117
118         public boolean releaseLock(TransactionImpl tx, Object JavaDoc obj)
119         {
120             return false;
121         }
122
123         public boolean checkRead(TransactionImpl tx, Object JavaDoc obj)
124         {
125             return false;
126         }
127
128         public boolean checkWrite(TransactionImpl tx, Object JavaDoc obj)
129         {
130             return false;
131         }
132     }
133 }
134
Popular Tags