KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > Lock


1 /**
2  * com.mckoi.database.Lock 11 May 1998
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.debug.*;
28
29 /**
30  * This is a lock on a table in the LockingMechanism class. A new instance
31  * of this class is created whenever a new lock for a table is made. A Lock
32  * may be either a READ lock or a WRITE lock. A lock is within a LockingQueue
33  * object.
34  * <p>
35  * @author Tobias Downer
36  */

37
38 public final class Lock {
39
40   /**
41    * These statics are used to define whether the lock is a READ or WRITE
42    * lock.
43    */

44   public static final int READ = 0;
45   public static final int WRITE = 1;
46
47   /**
48    * This stores the type of lock. It is either set to 'READ' or 'WRITE'
49    */

50   private int type;
51
52   /**
53    * The table queue this lock is 'inside'.
54    */

55   private LockingQueue queue;
56
57   /**
58    * This is set to true when the 'checkAccess' method is called on this
59    * lock.
60    */

61   private boolean was_checked;
62
63   /**
64    * The DebugLogger object that we log debug message to.
65    */

66   private final DebugLogger debug;
67
68   /**
69    * The Constructor. As well as setting up the state of this object, it
70    * also puts this lock into the table queue.
71    */

72   Lock(int type, LockingQueue queue, DebugLogger logger) {
73     this.debug = logger;
74     this.type = type;
75     this.queue = queue;
76     was_checked = false;
77     queue.addLock(this);
78   }
79
80   /**
81    * Returns the type of lock.
82    */

83   int getType() {
84     return type;
85   }
86
87   /**
88    * Returns the type of the lock as a string.
89    */

90   String JavaDoc getTypeAsString() {
91     int type = getType();
92     if (type == READ) {
93       return "READ";
94     }
95     else {
96       return "WRITE";
97     }
98   }
99
100   /**
101    * Returns the DataTable object this lock is locking
102    */

103   DataTable getTable() {
104     return queue.getTable();
105   }
106
107   /**
108    * Removes this lock from the queue. This is called when lock is released
109    * from the table queues.
110    * NOTE: This method does not need to be synchronized because synchronization
111    * is handled by the 'LockingMechanism.unlockTables' method.
112    */

113   void release() {
114     queue.removeLock(this);
115
116     if (!was_checked) {
117       // Prints out a warning if a lock was released from the table queue but
118
// never had 'checkAccess' called for it.
119
String JavaDoc table_name = queue.getTable().getTableName().toString();
120       debug.write(Lvl.ERROR, this,
121          "Lock on table '" + getTable().getTableName() +
122          "' was released but never checked. " + toString());
123       debug.writeException(new RuntimeException JavaDoc("Lock Error Dump"));
124     }
125 // else {
126
// // Notify table we released read/write lock
127
// getTable().notifyReleaseRWLock(type);
128
// }
129
}
130
131   /**
132    * Checks the access for this lock. This asks the queue that contains
133    * this lock if it is currently safe to access the table. If it is unsafe
134    * for the table to be accessed, then it blocks until it is safe. Therefore,
135    * when this method returns, it is safe to access the table for this lock.
136    * The 'access_type' variable contains either 'READ' or 'WRITE' and is set
137    * to the type of access that is currently being done to the table. If
138    * access_type == WRITE then this.type must be WRITE. If access_type ==
139    * READ then this.type may be either READ or WRITE.
140    * <p>
141    * NOTE: After the first call to this method, following calls will not
142    * block.
143    */

144   void checkAccess(int access_type) {
145     if (access_type == WRITE && this.type != WRITE) {
146       throw new Error JavaDoc(
147                  "Access error on Lock: Tried to write to a non write lock.");
148     }
149     if (was_checked == false) {
150       queue.checkAccess(this);
151       was_checked = true;
152 // // Notify table we are read/write locked
153
// getTable().notifyAddRWLock(type);
154
}
155   }
156
157   public String JavaDoc toString() {
158     return "[Lock] type: " + getTypeAsString() +
159            " was_checked: " + was_checked;
160   }
161
162 }
163
Popular Tags