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.Identity;
019 import org.apache.ojb.broker.locking.IsolationLevels;
020 import org.apache.ojb.broker.core.proxy.ProxyHelper;
021 import org.apache.ojb.broker.metadata.ClassDescriptor;
022 import org.apache.ojb.odmg.TransactionImpl;
023
024 /**
025 * The odmg lock manager implementation of the {@link LockManager} interface. This class
026 * using the OJB kernel locking api {@link org.apache.ojb.broker.locking.LockManager}
027 * to manage object locking.
028 *
029 * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
030 * @version $Id: LockManagerOdmgImpl.java,v 1.1 2007-08-24 22:17:28 ewestfal Exp $
031 */
032 public class LockManagerOdmgImpl implements LockManager
033 {
034 private org.apache.ojb.broker.locking.LockManager lm;
035
036 public LockManagerOdmgImpl(org.apache.ojb.broker.locking.LockManager lm)
037 {
038 this.lm = lm;
039 }
040
041 private boolean ignore(int isolationLevel)
042 {
043 return isolationLevel == IsolationLevels.IL_OPTIMISTIC || isolationLevel == IsolationLevels.IL_NONE;
044 }
045
046 public boolean readLock(TransactionImpl tx, Object obj)
047 {
048 Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
049 return readLock(tx, oid, obj);
050 }
051
052 public boolean readLock(TransactionImpl tx, Identity oid, Object obj)
053 {
054 ClassDescriptor cld = tx.getBroker().getClassDescriptor(ProxyHelper.getRealClass(obj));
055 int isolationLevel = cld.getIsolationLevel();
056 return ignore(isolationLevel) ? true : lm.readLock(tx.getGUID(), oid, isolationLevel);
057 }
058
059 public boolean writeLock(TransactionImpl tx, Object obj)
060 {
061 Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
062 return writeLock(tx, oid, obj);
063 }
064
065 public boolean writeLock(TransactionImpl tx, Identity oid, Object obj)
066 {
067 ClassDescriptor cld = tx.getBroker().getClassDescriptor(ProxyHelper.getRealClass(obj));
068 int isolationLevel = cld.getIsolationLevel();
069 return ignore(isolationLevel) ? true : lm.writeLock(tx.getGUID(), oid, isolationLevel);
070 }
071
072 public boolean upgradeLock(TransactionImpl tx, Object obj)
073 {
074 Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
075 return upgradeLock(tx, oid, obj);
076 }
077
078 public boolean upgradeLock(TransactionImpl tx, Identity oid, Object obj)
079 {
080 ClassDescriptor cld = tx.getBroker().getClassDescriptor(ProxyHelper.getRealClass(obj));
081 int isolationLevel = cld.getIsolationLevel();
082 return ignore(isolationLevel) ? true : lm.upgradeLock(tx.getGUID(), oid, isolationLevel);
083 }
084
085 public boolean releaseLock(TransactionImpl tx, Object obj)
086 {
087 Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
088 return releaseLock(tx, oid, obj);
089 }
090
091 public boolean releaseLock(TransactionImpl tx, Identity oid, Object obj)
092 {
093 return lm.releaseLock(tx.getGUID(), oid);
094 }
095
096 public boolean checkRead(TransactionImpl tx, Object obj)
097 {
098 Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
099 return checkRead(tx, oid, obj);
100 }
101
102 public boolean checkRead(TransactionImpl tx, Identity oid, Object obj)
103 {
104 return lm.hasRead(tx.getGUID(), oid);
105 }
106
107 public boolean checkWrite(TransactionImpl tx, Object obj)
108 {
109 Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
110 return checkWrite(tx, oid, obj);
111 }
112
113 public boolean checkWrite(TransactionImpl tx, Identity oid, Object obj)
114 {
115 return lm.hasWrite(tx.getGUID(), oid);
116 }
117 }