~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/java/src/com/sleepycat/examples/LockExample.java

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1997-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id$
8
 
 */
9
 
 
10
 
package com.sleepycat.examples;
11
 
 
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;
19
 
 
20
 
//
21
 
// An example of a program using DbLock and related classes.
22
 
//
23
 
class LockExample extends DbEnv
24
 
{
25
 
    private static final String progname = "LockExample";
26
 
    private static final String LOCK_HOME = "TESTDIR";
27
 
 
28
 
    public LockExample(String home, int maxlocks, boolean do_unlink)
29
 
         throws DbException, FileNotFoundException
30
 
    {
31
 
        super(0);
32
 
        if (do_unlink) {
33
 
            remove(home, Db.DB_FORCE);
34
 
        }
35
 
        else {
36
 
            set_error_stream(System.err);
37
 
            set_errpfx("LockExample");
38
 
            if (maxlocks != 0)
39
 
                set_lk_max_locks(maxlocks);
40
 
            open(home, Db.DB_CREATE|Db.DB_INIT_LOCK, 0);
41
 
        }
42
 
    }
43
 
 
44
 
    // Prompts for a line, and keeps prompting until a non blank
45
 
    // line is returned.  Returns null on error.
46
 
    //
47
 
    static public String askForLine(InputStreamReader reader,
48
 
                                    PrintStream out, String prompt)
49
 
    {
50
 
        String result = "";
51
 
        while (result != null && result.length() == 0) {
52
 
            out.print(prompt);
53
 
            out.flush();
54
 
            result = getLine(reader);
55
 
        }
56
 
        return result;
57
 
    }
58
 
 
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.
63
 
    //
64
 
    static public String getLine(InputStreamReader reader)
65
 
    {
66
 
        StringBuffer b = new StringBuffer();
67
 
        int c;
68
 
        try {
69
 
            while ((c = reader.read()) != -1 && c != '\n') {
70
 
                if (c != '\r')
71
 
                    b.append((char)c);
72
 
            }
73
 
        }
74
 
        catch (IOException ioe) {
75
 
            c = -1;
76
 
        }
77
 
 
78
 
        if (c == -1 && b.length() == 0)
79
 
            return null;
80
 
        else
81
 
            return b.toString();
82
 
    }
83
 
 
84
 
    public void run()
85
 
         throws DbException
86
 
    {
87
 
        long held;
88
 
        int len = 0, locker;
89
 
        int ret;
90
 
        boolean did_get = false;
91
 
        int lockid = 0;
92
 
        InputStreamReader in = new InputStreamReader(System.in);
93
 
        Vector locks = new Vector();
94
 
 
95
 
        //
96
 
        // Accept lock requests.
97
 
        //
98
 
        locker = lock_id();
99
 
        for (held = 0;;) {
100
 
            String opbuf = askForLine(in, System.out,
101
 
                                      "Operation get/release [get]> ");
102
 
            if (opbuf == null)
103
 
                break;
104
 
 
105
 
            try {
106
 
                if (opbuf.equals("get")) {
107
 
                    // Acquire a lock.
108
 
                    String objbuf = askForLine(in, System.out,
109
 
                                   "input object (text string) to lock> ");
110
 
                    if (objbuf == null)
111
 
                        break;
112
 
 
113
 
                    String lockbuf;
114
 
                    do {
115
 
                        lockbuf = askForLine(in, System.out,
116
 
                                             "lock type read/write [read]> ");
117
 
                        if (lockbuf == null)
118
 
                            break;
119
 
                        len = lockbuf.length();
120
 
                    } while (len >= 1 &&
121
 
                             !lockbuf.equals("read") &&
122
 
                             !lockbuf.equals("write"));
123
 
 
124
 
                    int lock_type;
125
 
                    if (len <= 1 || lockbuf.equals("read"))
126
 
                        lock_type = Db.DB_LOCK_READ;
127
 
                    else
128
 
                        lock_type = Db.DB_LOCK_WRITE;
129
 
 
130
 
                    Dbt dbt = new Dbt(objbuf.getBytes());
131
 
 
132
 
                    DbLock lock;
133
 
                    did_get = true;
134
 
                    lock = lock_get(locker, Db.DB_LOCK_NOWAIT,
135
 
                                    dbt, lock_type);
136
 
                    lockid = locks.size();
137
 
                    locks.addElement(lock);
138
 
                } else {
139
 
                    // Release a lock.
140
 
                    String objbuf;
141
 
                    objbuf = askForLine(in, System.out,
142
 
                                        "input lock to release> ");
143
 
                    if (objbuf == null)
144
 
                        break;
145
 
 
146
 
                    lockid = Integer.parseInt(objbuf, 16);
147
 
                    if (lockid < 0 || lockid >= locks.size()) {
148
 
                        System.out.println("Lock #" + lockid + " out of range");
149
 
                        continue;
150
 
                    }
151
 
                    did_get = false;
152
 
                    DbLock lock = (DbLock)locks.elementAt(lockid);
153
 
                    lock_put(lock);
154
 
                }
155
 
                System.out.println("Lock #" + lockid + " " +
156
 
                                   (did_get ? "granted" : "released"));
157
 
                held += did_get ? 1 : -1;
158
 
            }
159
 
            catch (DbException dbe) {
160
 
                switch (dbe.get_errno()) {
161
 
                case Db.DB_LOCK_NOTGRANTED:
162
 
                    System.out.println("Lock not granted");
163
 
                    break;
164
 
                case Db.DB_LOCK_DEADLOCK:
165
 
                    System.err.println("LockExample: lock_" +
166
 
                                       (did_get ? "get" : "put") +
167
 
                                       ": returned DEADLOCK");
168
 
                    break;
169
 
                default:
170
 
                    System.err.println("LockExample: lock_get: " + dbe.toString());
171
 
                }
172
 
            }
173
 
        }
174
 
        System.out.println();
175
 
        System.out.println("Closing lock region " + String.valueOf(held) +
176
 
                           " locks held");
177
 
    }
178
 
 
179
 
    private static void usage()
180
 
    {
181
 
        System.err.println("usage: LockExample [-u] [-h home] [-m maxlocks]");
182
 
        System.exit(1);
183
 
    }
184
 
 
185
 
    public static void main(String argv[])
186
 
    {
187
 
        String home = LOCK_HOME;
188
 
        boolean do_unlink = false;
189
 
        int maxlocks = 0;
190
 
 
191
 
        for (int i = 0; i < argv.length; ++i) {
192
 
            if (argv[i].equals("-h")) {
193
 
                if (++i >= argv.length)
194
 
                    usage();
195
 
                home = argv[i];
196
 
            }
197
 
            else if (argv[i].equals("-m")) {
198
 
                if (++i >= argv.length)
199
 
                    usage();
200
 
 
201
 
                try {
202
 
                    maxlocks = Integer.parseInt(argv[i]);
203
 
                }
204
 
                catch (NumberFormatException nfe) {
205
 
                    usage();
206
 
                }
207
 
            }
208
 
            else if (argv[i].equals("-u")) {
209
 
                do_unlink = true;
210
 
            }
211
 
            else {
212
 
                usage();
213
 
            }
214
 
        }
215
 
 
216
 
        try {
217
 
            if (do_unlink) {
218
 
                // Create an environment that immediately
219
 
                // removes all files.
220
 
                LockExample tmp = new LockExample(home, maxlocks, do_unlink);
221
 
            }
222
 
 
223
 
            LockExample app = new LockExample(home, maxlocks, do_unlink);
224
 
            app.run();
225
 
            app.close(0);
226
 
        }
227
 
        catch (DbException dbe) {
228
 
            System.err.println(progname + ": " + dbe.toString());
229
 
        }
230
 
        catch (Throwable t) {
231
 
            System.err.println(progname + ": " + t.toString());
232
 
        }
233
 
        System.out.println("LockExample completed");
234
 
    }
235
 
}