View Javadoc

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.Identity;
19  import org.apache.ojb.broker.locking.IsolationLevels;
20  import org.apache.ojb.broker.core.proxy.ProxyHelper;
21  import org.apache.ojb.broker.metadata.ClassDescriptor;
22  import org.apache.ojb.odmg.TransactionImpl;
23  
24  /**
25   * The odmg lock manager implementation of the {@link LockManager} interface. This class
26   * using the OJB kernel locking api {@link org.apache.ojb.broker.locking.LockManager}
27   * to manage object locking.
28   *
29   * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
30   * @version $Id: LockManagerOdmgImpl.java,v 1.1 2007-08-24 22:17:28 ewestfal Exp $
31   */
32  public class LockManagerOdmgImpl implements LockManager
33  {
34      private org.apache.ojb.broker.locking.LockManager lm;
35  
36      public LockManagerOdmgImpl(org.apache.ojb.broker.locking.LockManager lm)
37      {
38          this.lm = lm;
39      }
40  
41      private boolean ignore(int isolationLevel)
42      {
43          return isolationLevel == IsolationLevels.IL_OPTIMISTIC || isolationLevel == IsolationLevels.IL_NONE;
44      }
45  
46      public boolean readLock(TransactionImpl tx, Object obj)
47      {
48          Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
49          return readLock(tx, oid, obj);
50      }
51  
52      public boolean readLock(TransactionImpl tx, Identity oid, Object obj)
53      {
54          ClassDescriptor cld = tx.getBroker().getClassDescriptor(ProxyHelper.getRealClass(obj));
55          int isolationLevel = cld.getIsolationLevel();
56          return ignore(isolationLevel) ? true : lm.readLock(tx.getGUID(), oid, isolationLevel);
57      }
58  
59      public boolean writeLock(TransactionImpl tx, Object obj)
60      {
61          Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
62          return writeLock(tx, oid, obj);
63      }
64  
65      public boolean writeLock(TransactionImpl tx, Identity oid, Object obj)
66      {
67          ClassDescriptor cld = tx.getBroker().getClassDescriptor(ProxyHelper.getRealClass(obj));
68          int isolationLevel = cld.getIsolationLevel();
69          return ignore(isolationLevel) ? true : lm.writeLock(tx.getGUID(), oid, isolationLevel);
70      }
71  
72      public boolean upgradeLock(TransactionImpl tx, Object obj)
73      {
74          Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
75          return upgradeLock(tx, oid, obj);
76      }
77  
78      public boolean upgradeLock(TransactionImpl tx, Identity oid, Object obj)
79      {
80          ClassDescriptor cld = tx.getBroker().getClassDescriptor(ProxyHelper.getRealClass(obj));
81          int isolationLevel = cld.getIsolationLevel();
82          return ignore(isolationLevel) ? true : lm.upgradeLock(tx.getGUID(), oid, isolationLevel);
83      }
84  
85      public boolean releaseLock(TransactionImpl tx, Object obj)
86      {
87          Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
88          return releaseLock(tx, oid, obj);
89      }
90  
91      public boolean releaseLock(TransactionImpl tx, Identity oid, Object obj)
92      {
93          return lm.releaseLock(tx.getGUID(), oid);
94      }
95  
96      public boolean checkRead(TransactionImpl tx, Object obj)
97      {
98          Identity oid = tx.getBroker().serviceIdentity().buildIdentity(obj);
99          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 }