2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
package com.sleepycat.examples;
12
import com.sleepycat.db.*;
13
import java.io.FileNotFoundException;
14
import java.io.InputStream;
15
import java.io.InputStreamReader;
16
import java.io.IOException;
17
import java.io.PrintStream;
18
import java.util.Vector;
21
// An example of a program using DbLock and related classes.
23
class LockExample extends DbEnv
25
private static final String progname = "LockExample";
26
private static final String LOCK_HOME = "TESTDIR";
28
public LockExample(String home, int maxlocks, boolean do_unlink)
29
throws DbException, FileNotFoundException
33
remove(home, Db.DB_FORCE);
36
set_error_stream(System.err);
37
set_errpfx("LockExample");
39
set_lk_max_locks(maxlocks);
40
open(home, Db.DB_CREATE|Db.DB_INIT_LOCK, 0);
44
// Prompts for a line, and keeps prompting until a non blank
45
// line is returned. Returns null on error.
47
static public String askForLine(InputStreamReader reader,
48
PrintStream out, String prompt)
51
while (result != null && result.length() == 0) {
54
result = getLine(reader);
59
// Not terribly efficient, but does the job.
60
// Works for reading a line from stdin or a file.
61
// Returns null on EOF. If EOF appears in the middle
62
// of a line, returns that line, then null on next call.
64
static public String getLine(InputStreamReader reader)
66
StringBuffer b = new StringBuffer();
69
while ((c = reader.read()) != -1 && c != '\n') {
74
catch (IOException ioe) {
78
if (c == -1 && b.length() == 0)
90
boolean did_get = false;
92
InputStreamReader in = new InputStreamReader(System.in);
93
Vector locks = new Vector();
96
// Accept lock requests.
100
String opbuf = askForLine(in, System.out,
101
"Operation get/release [get]> ");
106
if (opbuf.equals("get")) {
108
String objbuf = askForLine(in, System.out,
109
"input object (text string) to lock> ");
115
lockbuf = askForLine(in, System.out,
116
"lock type read/write [read]> ");
119
len = lockbuf.length();
121
!lockbuf.equals("read") &&
122
!lockbuf.equals("write"));
125
if (len <= 1 || lockbuf.equals("read"))
126
lock_type = Db.DB_LOCK_READ;
128
lock_type = Db.DB_LOCK_WRITE;
130
Dbt dbt = new Dbt(objbuf.getBytes());
134
lock = lock_get(locker, Db.DB_LOCK_NOWAIT,
136
lockid = locks.size();
137
locks.addElement(lock);
141
objbuf = askForLine(in, System.out,
142
"input lock to release> ");
146
lockid = Integer.parseInt(objbuf, 16);
147
if (lockid < 0 || lockid >= locks.size()) {
148
System.out.println("Lock #" + lockid + " out of range");
152
DbLock lock = (DbLock)locks.elementAt(lockid);
155
System.out.println("Lock #" + lockid + " " +
156
(did_get ? "granted" : "released"));
157
held += did_get ? 1 : -1;
159
catch (DbException dbe) {
160
switch (dbe.get_errno()) {
161
case Db.DB_LOCK_NOTGRANTED:
162
System.out.println("Lock not granted");
164
case Db.DB_LOCK_DEADLOCK:
165
System.err.println("LockExample: lock_" +
166
(did_get ? "get" : "put") +
167
": returned DEADLOCK");
170
System.err.println("LockExample: lock_get: " + dbe.toString());
174
System.out.println();
175
System.out.println("Closing lock region " + String.valueOf(held) +
179
private static void usage()
181
System.err.println("usage: LockExample [-u] [-h home] [-m maxlocks]");
185
public static void main(String argv[])
187
String home = LOCK_HOME;
188
boolean do_unlink = false;
191
for (int i = 0; i < argv.length; ++i) {
192
if (argv[i].equals("-h")) {
193
if (++i >= argv.length)
197
else if (argv[i].equals("-m")) {
198
if (++i >= argv.length)
202
maxlocks = Integer.parseInt(argv[i]);
204
catch (NumberFormatException nfe) {
208
else if (argv[i].equals("-u")) {
218
// Create an environment that immediately
219
// removes all files.
220
LockExample tmp = new LockExample(home, maxlocks, do_unlink);
223
LockExample app = new LockExample(home, maxlocks, do_unlink);
227
catch (DbException dbe) {
228
System.err.println(progname + ": " + dbe.toString());
230
catch (Throwable t) {
231
System.err.println(progname + ": " + t.toString());
233
System.out.println("LockExample completed");