• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/timemachine/db-4.7.25.NC/examples_java/src/db/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: LockExample.java,v 12.8 2008/01/08 20:58:32 bostic Exp $
7 */
8
9package db;
10
11import com.sleepycat.db.*;
12import java.io.File;
13import java.io.FileNotFoundException;
14import java.io.InputStreamReader;
15import java.io.IOException;
16import java.io.PrintStream;
17import java.util.Vector;
18
19//
20// An example of a program using Lock and related classes.
21//
22class LockExample {
23    private static final String progname = "LockExample";
24    private static final File LOCK_HOME = new File("TESTDIR");
25    Environment dbenv;
26
27    public LockExample(File home, int maxlocks, boolean do_unlink)
28        throws DatabaseException, FileNotFoundException {
29
30        if (do_unlink) {
31            Environment.remove(home, true, null);
32        }
33
34        EnvironmentConfig config = new EnvironmentConfig();
35        config.setErrorStream(System.err);
36        config.setErrorPrefix("LockExample");
37        config.setMaxLocks(maxlocks);
38        config.setAllowCreate(true);
39        config.setInitializeLocking(true);
40        dbenv = new Environment(home, config);
41    }
42
43    public void close() throws DatabaseException {
44        dbenv.close();
45    }
46
47    // Prompts for a line, and keeps prompting until a non blank
48    // line is returned.  Returns null on error.
49    //
50    public static String askForLine(InputStreamReader reader,
51                                    PrintStream out, String prompt) {
52        String result = "";
53        while (result != null && result.length() == 0) {
54            out.print(prompt);
55            out.flush();
56            result = getLine(reader);
57        }
58        return result;
59    }
60
61    // Not terribly efficient, but does the job.
62    // Works for reading a line from stdin or a file.
63    // Returns null on EOF.  If EOF appears in the middle
64    // of a line, returns that line, then null on next call.
65    //
66    public static String getLine(InputStreamReader reader) {
67        StringBuffer b = new StringBuffer();
68        int c;
69        try {
70            while ((c = reader.read()) != -1 && c != '\n')
71                if (c != '\r')
72                    b.append((char)c);
73        } catch (IOException ioe) {
74            c = -1;
75        }
76
77        if (c == -1 && b.length() == 0)
78            return null;
79        else
80            return b.toString();
81    }
82
83    public void run() throws DatabaseException {
84        long held;
85        int len = 0, locker;
86        int ret;
87        boolean did_get = false;
88        int lockid = 0;
89        InputStreamReader in = new InputStreamReader(System.in);
90        Vector locks = new Vector();
91
92        //
93        // Accept lock requests.
94        //
95        locker = dbenv.createLockerID();
96        for (held = 0;;) {
97            String opbuf = askForLine(in, System.out,
98                                      "Operation get/release [get]> ");
99            if (opbuf == null)
100                break;
101
102            try {
103                if (opbuf.equals("get")) {
104                    // Acquire a lock.
105                    String objbuf = askForLine(in, System.out,
106                           "input object (text string) to lock> ");
107                    if (objbuf == null)
108                        break;
109
110                    String lockbuf;
111                    do {
112                        lockbuf = askForLine(in, System.out,
113                                             "lock type read/write [read]> ");
114                        if (lockbuf == null)
115                            break;
116                        len = lockbuf.length();
117                    } while (len >= 1 &&
118                             !lockbuf.equals("read") &&
119                             !lockbuf.equals("write"));
120
121                    LockRequestMode lock_type;
122                    if (len <= 1 || lockbuf.equals("read"))
123                        lock_type = LockRequestMode.READ;
124                    else
125                        lock_type = LockRequestMode.WRITE;
126
127                    DatabaseEntry entry = new DatabaseEntry(objbuf.getBytes());
128
129                    Lock lock;
130                    did_get = true;
131                    lock = dbenv.getLock(locker, true, entry, lock_type);
132                    lockid = locks.size();
133                    locks.addElement(lock);
134                } else {
135                    // Release a lock.
136                    String objbuf;
137                    objbuf = askForLine(in, System.out,
138                                        "input lock to release> ");
139                    if (objbuf == null)
140                        break;
141
142                    lockid = Integer.parseInt(objbuf, 16);
143                    if (lockid < 0 || lockid >= locks.size()) {
144                        System.out.println("Lock #" + lockid + " out of range");
145                        continue;
146                    }
147                    did_get = false;
148                    Lock lock = (Lock)locks.elementAt(lockid);
149                    dbenv.putLock(lock);
150                }
151                System.out.println("Lock #" + lockid + " " +
152                                   (did_get ? "granted" : "released"));
153                held += did_get ? 1 : -1;
154            } catch (LockNotGrantedException lnge) {
155                System.err.println("Lock not granted");
156            } catch (DeadlockException de) {
157                System.err.println("LockExample: lock_" +
158                                   (did_get ? "get" : "put") +
159                                   ": returned DEADLOCK");
160            } catch (DatabaseException dbe) {
161                System.err.println("LockExample: lock_get: " + dbe.toString());
162            }
163        }
164        System.out.println();
165        System.out.println("Closing lock region " + String.valueOf(held) +
166                           " locks held");
167    }
168
169    private static void usage() {
170        System.err.println("usage: LockExample [-u] [-h home] [-m maxlocks]");
171        System.exit(1);
172    }
173
174    public static void main(String[] argv) {
175        File home = LOCK_HOME;
176        boolean do_unlink = false;
177        int maxlocks = 0;
178
179        for (int i = 0; i < argv.length; ++i) {
180            if (argv[i].equals("-h")) {
181                if (++i >= argv.length)
182                    usage();
183                home = new File(argv[i]);
184            } else if (argv[i].equals("-m")) {
185                if (++i >= argv.length)
186                    usage();
187
188                try {
189                    maxlocks = Integer.parseInt(argv[i]);
190                } catch (NumberFormatException nfe) {
191                    usage();
192                }
193            } else if (argv[i].equals("-u")) {
194                do_unlink = true;
195            } else {
196                usage();
197            }
198        }
199
200        try {
201            LockExample app = new LockExample(home, maxlocks, do_unlink);
202            app.run();
203            app.close();
204        } catch (DatabaseException dbe) {
205            System.err.println(progname + ": " + dbe.toString());
206        } catch (Throwable t) {
207            System.err.println(progname + ": " + t.toString());
208        }
209        System.out.println("LockExample completed");
210    }
211}
212