001 package org.apache.ojb.odmg.locking;
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 org.apache.ojb.broker.PersistenceBrokerException;
019 import org.apache.ojb.broker.core.proxy.ProxyHelper;
020 import org.apache.ojb.broker.locking.IsolationLevels;
021 import org.apache.ojb.broker.metadata.ClassDescriptor;
022 import org.apache.ojb.broker.util.logging.LoggerFactory;
023 import org.apache.ojb.odmg.TransactionImpl;
024 import org.apache.ojb.odmg.TxManagerFactory;
025
026 /**
027 * Factory class used to obtain the proper LockingStrategy for an Object.
028 * @author Thomas Mahler & David Dixon-Peugh
029 * @deprecated
030 * @version $Id: LockStrategyFactory.java,v 1.1 2007-08-24 22:17:28 ewestfal Exp $
031 */
032 public class LockStrategyFactory
033 {
034
035 /**
036 * private constructor: use static methods only.
037 *
038 */
039 private LockStrategyFactory()
040 {
041 }
042
043 private static LockStrategy readUncommitedStrategy = new ReadUncommittedStrategy();
044 private static LockStrategy readCommitedStrategy = new ReadCommittedStrategy();
045 private static LockStrategy readRepeatableStrategy = new RepeatableReadStrategy();
046 private static LockStrategy serializableStrategy = new SerializableStrategy();
047 private static LockStrategy noopStrategy = new NOOPStrategy();
048
049
050 /**
051 * obtains a LockStrategy for Object obj. The Strategy to be used is
052 * selected by evaluating the ClassDescriptor of obj.getClass().
053 *
054 * @return LockStrategy
055 */
056 public static LockStrategy getStrategyFor(Object obj)
057 {
058 int isolationLevel = getIsolationLevel(obj);
059 switch (isolationLevel)
060 {
061 case IsolationLevels.IL_READ_UNCOMMITTED:
062 return readUncommitedStrategy;
063 case IsolationLevels.IL_READ_COMMITTED:
064 return readCommitedStrategy;
065 case IsolationLevels.IL_REPEATABLE_READ:
066 return readRepeatableStrategy;
067 case IsolationLevels.IL_SERIALIZABLE:
068 return serializableStrategy;
069 case IsolationLevels.IL_OPTIMISTIC:
070 return noopStrategy;
071 case IsolationLevels.IL_NONE:
072 return noopStrategy;
073 default:
074 return readUncommitedStrategy;
075 }
076 }
077
078 /**
079 * determines the isolationlevel of class c by evaluating
080 * the ClassDescriptor of obj.getClass().
081 *
082 * @return int the isolationlevel
083 */
084 public static int getIsolationLevel(Object obj)
085 {
086 Class c = ProxyHelper.getRealClass(obj);
087 int isolationLevel = IsolationLevels.IL_READ_UNCOMMITTED;
088
089 try
090 {
091 ClassDescriptor cld = TxManagerFactory.instance().getCurrentTransaction().getBroker().getClassDescriptor(c);
092 isolationLevel = cld.getIsolationLevel();
093 }
094 catch (PersistenceBrokerException e)
095 {
096 LoggerFactory.getDefaultLogger().error("[LockStrategyFactory] Can't detect locking isolation level", e);
097 }
098 return isolationLevel;
099 }
100
101 static class NOOPStrategy implements LockStrategy
102 {
103 public boolean readLock(TransactionImpl tx, Object obj)
104 {
105 return true;
106 }
107
108 public boolean writeLock(TransactionImpl tx, Object obj)
109 {
110 return true;
111 }
112
113 public boolean upgradeLock(TransactionImpl tx, Object obj)
114 {
115 return true;
116 }
117
118 public boolean releaseLock(TransactionImpl tx, Object obj)
119 {
120 return false;
121 }
122
123 public boolean checkRead(TransactionImpl tx, Object obj)
124 {
125 return false;
126 }
127
128 public boolean checkWrite(TransactionImpl tx, Object obj)
129 {
130 return false;
131 }
132 }
133 }