00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package db;
00011
00012 import com.sleepycat.db.*;
00013 import java.io.File;
00014 import java.io.FileNotFoundException;
00015 import java.io.InputStreamReader;
00016 import java.io.IOException;
00017 import java.io.PrintStream;
00018 import java.util.Vector;
00019
00020
00021
00022
00023 class LockExample {
00024 private static final String progname = "LockExample";
00025 private static final File LOCK_HOME = new File("TESTDIR");
00026 Environment dbenv;
00027
00028 public LockExample(File home, int maxlocks, boolean do_unlink)
00029 throws DatabaseException, FileNotFoundException {
00030
00031 if (do_unlink) {
00032 Environment.remove(home, true, null);
00033 }
00034
00035 EnvironmentConfig config = new EnvironmentConfig();
00036 config.setErrorStream(System.err);
00037 config.setErrorPrefix("LockExample");
00038 config.setMaxLocks(maxlocks);
00039 config.setAllowCreate(true);
00040 config.setInitializeLocking(true);
00041 dbenv = new Environment(home, config);
00042 }
00043
00044 public void close() throws DatabaseException {
00045 dbenv.close();
00046 }
00047
00048
00049
00050
00051 public static String askForLine(InputStreamReader reader,
00052 PrintStream out, String prompt) {
00053 String result = "";
00054 while (result != null && result.length() == 0) {
00055 out.print(prompt);
00056 out.flush();
00057 result = getLine(reader);
00058 }
00059 return result;
00060 }
00061
00062
00063
00064
00065
00066
00067 public static String getLine(InputStreamReader reader) {
00068 StringBuffer b = new StringBuffer();
00069 int c;
00070 try {
00071 while ((c = reader.read()) != -1 && c != '\n')
00072 if (c != '\r')
00073 b.append((char)c);
00074 } catch (IOException ioe) {
00075 c = -1;
00076 }
00077
00078 if (c == -1 && b.length() == 0)
00079 return null;
00080 else
00081 return b.toString();
00082 }
00083
00084 public void run() throws DatabaseException {
00085 long held;
00086 int len = 0, locker;
00087 int ret;
00088 boolean did_get = false;
00089 int lockid = 0;
00090 InputStreamReader in = new InputStreamReader(System.in);
00091 Vector locks = new Vector();
00092
00093
00094
00095
00096 locker = dbenv.createLockerID();
00097 for (held = 0;;) {
00098 String opbuf = askForLine(in, System.out,
00099 "Operation get/release [get]> ");
00100 if (opbuf == null)
00101 break;
00102
00103 try {
00104 if (opbuf.equals("get")) {
00105
00106 String objbuf = askForLine(in, System.out,
00107 "input object (text string) to lock> ");
00108 if (objbuf == null)
00109 break;
00110
00111 String lockbuf;
00112 do {
00113 lockbuf = askForLine(in, System.out,
00114 "lock type read/write [read]> ");
00115 if (lockbuf == null)
00116 break;
00117 len = lockbuf.length();
00118 } while (len >= 1 &&
00119 !lockbuf.equals("read") &&
00120 !lockbuf.equals("write"));
00121
00122 LockRequestMode lock_type;
00123 if (len <= 1 || lockbuf.equals("read"))
00124 lock_type = LockRequestMode.READ;
00125 else
00126 lock_type = LockRequestMode.WRITE;
00127
00128 DatabaseEntry entry = new DatabaseEntry(objbuf.getBytes());
00129
00130 Lock lock;
00131 did_get = true;
00132 lock = dbenv.getLock(locker, true, entry, lock_type);
00133 lockid = locks.size();
00134 locks.addElement(lock);
00135 } else {
00136
00137 String objbuf;
00138 objbuf = askForLine(in, System.out,
00139 "input lock to release> ");
00140 if (objbuf == null)
00141 break;
00142
00143 lockid = Integer.parseInt(objbuf, 16);
00144 if (lockid < 0 || lockid >= locks.size()) {
00145 System.out.println("Lock #" + lockid + " out of range");
00146 continue;
00147 }
00148 did_get = false;
00149 Lock lock = (Lock)locks.elementAt(lockid);
00150 dbenv.putLock(lock);
00151 }
00152 System.out.println("Lock #" + lockid + " " +
00153 (did_get ? "granted" : "released"));
00154 held += did_get ? 1 : -1;
00155 } catch (LockNotGrantedException lnge) {
00156 System.err.println("Lock not granted");
00157 } catch (DeadlockException de) {
00158 System.err.println("LockExample: lock_" +
00159 (did_get ? "get" : "put") +
00160 ": returned DEADLOCK");
00161 } catch (DatabaseException dbe) {
00162 System.err.println("LockExample: lock_get: " + dbe.toString());
00163 }
00164 }
00165 System.out.println();
00166 System.out.println("Closing lock region " + String.valueOf(held) +
00167 " locks held");
00168 }
00169
00170 private static void usage() {
00171 System.err.println("usage: LockExample [-u] [-h home] [-m maxlocks]");
00172 System.exit(1);
00173 }
00174
00175 public static void main(String[] argv) {
00176 File home = LOCK_HOME;
00177 boolean do_unlink = false;
00178 int maxlocks = 0;
00179
00180 for (int i = 0; i < argv.length; ++i) {
00181 if (argv[i].equals("-h")) {
00182 if (++i >= argv.length)
00183 usage();
00184 home = new File(argv[i]);
00185 } else if (argv[i].equals("-m")) {
00186 if (++i >= argv.length)
00187 usage();
00188
00189 try {
00190 maxlocks = Integer.parseInt(argv[i]);
00191 } catch (NumberFormatException nfe) {
00192 usage();
00193 }
00194 } else if (argv[i].equals("-u")) {
00195 do_unlink = true;
00196 } else {
00197 usage();
00198 }
00199 }
00200
00201 try {
00202 LockExample app = new LockExample(home, maxlocks, do_unlink);
00203 app.run();
00204 app.close();
00205 } catch (DatabaseException dbe) {
00206 System.err.println(progname + ": " + dbe.toString());
00207 } catch (Throwable t) {
00208 System.err.println(progname + ": " + t.toString());
00209 }
00210 System.out.println("LockExample completed");
00211 }
00212 }