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.13.2.4 2005/12/21 22:29:51 tomdz Exp $
031: */
032: public class LockStrategyFactory {
033:
034: /**
035: * private constructor: use static methods only.
036: *
037: */
038: private LockStrategyFactory() {
039: }
040:
041: private static LockStrategy readUncommitedStrategy = new ReadUncommittedStrategy();
042: private static LockStrategy readCommitedStrategy = new ReadCommittedStrategy();
043: private static LockStrategy readRepeatableStrategy = new RepeatableReadStrategy();
044: private static LockStrategy serializableStrategy = new SerializableStrategy();
045: private static LockStrategy noopStrategy = new NOOPStrategy();
046:
047: /**
048: * obtains a LockStrategy for Object obj. The Strategy to be used is
049: * selected by evaluating the ClassDescriptor of obj.getClass().
050: *
051: * @return LockStrategy
052: */
053: public static LockStrategy getStrategyFor(Object obj) {
054: int isolationLevel = getIsolationLevel(obj);
055: switch (isolationLevel) {
056: case IsolationLevels.IL_READ_UNCOMMITTED:
057: return readUncommitedStrategy;
058: case IsolationLevels.IL_READ_COMMITTED:
059: return readCommitedStrategy;
060: case IsolationLevels.IL_REPEATABLE_READ:
061: return readRepeatableStrategy;
062: case IsolationLevels.IL_SERIALIZABLE:
063: return serializableStrategy;
064: case IsolationLevels.IL_OPTIMISTIC:
065: return noopStrategy;
066: case IsolationLevels.IL_NONE:
067: return noopStrategy;
068: default:
069: return readUncommitedStrategy;
070: }
071: }
072:
073: /**
074: * determines the isolationlevel of class c by evaluating
075: * the ClassDescriptor of obj.getClass().
076: *
077: * @return int the isolationlevel
078: */
079: public static int getIsolationLevel(Object obj) {
080: Class c = ProxyHelper.getRealClass(obj);
081: int isolationLevel = IsolationLevels.IL_READ_UNCOMMITTED;
082:
083: try {
084: ClassDescriptor cld = TxManagerFactory.instance()
085: .getCurrentTransaction().getBroker()
086: .getClassDescriptor(c);
087: isolationLevel = cld.getIsolationLevel();
088: } catch (PersistenceBrokerException e) {
089: LoggerFactory
090: .getDefaultLogger()
091: .error(
092: "[LockStrategyFactory] Can't detect locking isolation level",
093: e);
094: }
095: return isolationLevel;
096: }
097:
098: static class NOOPStrategy implements LockStrategy {
099: public boolean readLock(TransactionImpl tx, Object obj) {
100: return true;
101: }
102:
103: public boolean writeLock(TransactionImpl tx, Object obj) {
104: return true;
105: }
106:
107: public boolean upgradeLock(TransactionImpl tx, Object obj) {
108: return true;
109: }
110:
111: public boolean releaseLock(TransactionImpl tx, Object obj) {
112: return false;
113: }
114:
115: public boolean checkRead(TransactionImpl tx, Object obj) {
116: return false;
117: }
118:
119: public boolean checkWrite(TransactionImpl tx, Object obj) {
120: return false;
121: }
122: }
123: }
|