KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > Lock


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.terracotta.session.util;
6
7 import com.tc.object.bytecode.Manager;
8 import com.tc.object.bytecode.ManagerUtil;
9
10 public class Lock {
11
12   private final Timer unlockTimer;
13   private final Timer lockTimer;
14   private final String JavaDoc lockId;
15
16   private boolean isLocked = false;
17   private final int lockType;
18
19   // for non-synchronous-write tests
20
public Lock(final String JavaDoc lockId) {
21     this(lockId, Manager.LOCK_TYPE_WRITE);
22   }
23
24   public Lock(final String JavaDoc lockId, final int lockType) {
25     if (lockType != Manager.LOCK_TYPE_SYNCHRONOUS_WRITE && lockType != Manager.LOCK_TYPE_WRITE) { throw new AssertionError JavaDoc(
26                                                                                                                            "Trying to set lockType to "
27                                                                                                                                + lockType
28                                                                                                                                + " -- must be either write or synchronous-write"); }
29
30     this.lockType = lockType;
31     Assert.pre(lockId != null && lockId.length() > 0);
32     this.lockId = lockId;
33     lockTimer = new Timer(false);
34     unlockTimer = new Timer(false);
35   }
36
37   public void commitLock() {
38     unlockTimer.start();
39     ManagerUtil.commitLock(lockId);
40     isLocked = false;
41     unlockTimer.stop();
42   }
43
44   public void getWriteLock() {
45     lockTimer.start();
46     ManagerUtil.beginLock(lockId, lockType);
47     isLocked = true;
48     lockTimer.stop();
49   }
50
51   public boolean tryWriteLock() {
52     lockTimer.start();
53     isLocked = ManagerUtil.tryBeginLock(lockId, lockType);
54     lockTimer.stop();
55     return isLocked;
56   }
57
58   public Timer getLockTimer() {
59     return lockTimer;
60   }
61
62   public Timer getUnlockTimer() {
63     return unlockTimer;
64   }
65
66   public String JavaDoc getLockId() {
67     return lockId;
68   }
69
70   public boolean isLocked() {
71     return isLocked;
72   }
73 }
74
Popular Tags