~ubuntu-branches/ubuntu/precise/rpm/precise-proposed

« back to all changes in this revision

Viewing changes to db/test/scr016/TestXAServlet.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-06-25 18:57:20 UTC
  • mfrom: (1.1.5 upstream) (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090625185720-617sjskgtgmf09vf
Tags: 4.7.0-7ubuntu1
* Merge from debian unstable, remaining changes:
  - change build depends from libdwarf-dev -> libdw-dev
    (libdwarf-dev is in universe)

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-2004
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id: TestXAServlet.java,v 1.4 2004/01/28 03:36:34 bostic Exp $
8
 
 */
9
 
 
10
 
/*
11
 
 * Simple test of XA, using WebLogic.
12
 
 */
13
 
 
14
 
package com.sleepycat.test;
15
 
 
16
 
import com.sleepycat.db.*;
17
 
import com.sleepycat.db.xa.*;
18
 
import java.io.File;
19
 
import java.io.FileNotFoundException;
20
 
import java.io.IOException;
21
 
import java.io.PrintWriter;
22
 
import java.util.Hashtable;
23
 
import javax.servlet.*;
24
 
import javax.servlet.http.*;
25
 
import javax.transaction.*;
26
 
import javax.transaction.xa.*;
27
 
import javax.naming.Context;
28
 
import javax.naming.InitialContext;
29
 
import javax.naming.NamingException;
30
 
import weblogic.transaction.TxHelper;
31
 
import weblogic.transaction.TransactionManager;
32
 
 
33
 
public class TestXAServlet extends HttpServlet
34
 
{
35
 
    public static final String ENV_HOME = "TESTXADIR";
36
 
    public static final String DEFAULT_URL = "t3://localhost:7001";
37
 
    public static String filesep = System.getProperty("file.separator");
38
 
 
39
 
    private static TransactionManager tm;
40
 
    private static DbXAResource xaresource;
41
 
    private static boolean initialized = false;
42
 
 
43
 
    /**
44
 
     * Utility to remove files recursively.
45
 
     */
46
 
    public static void removeRecursive(File f)
47
 
    {
48
 
        if (f.isDirectory()) {
49
 
            String[] sub = f.list();
50
 
            for (int i=0; i<sub.length; i++)
51
 
                removeRecursive(new File(f.getName() + filesep + sub[i]));
52
 
        }
53
 
        f.delete();
54
 
    }
55
 
 
56
 
    /**
57
 
     * Typically done only once, unless shutdown is invoked.  This
58
 
     * sets up directories, and removes any work files from previous
59
 
     * runs.  Also establishes a transaction manager that we'll use
60
 
     * for various transactions.  Each call opens/creates a new DB
61
 
     * environment in our work directory.
62
 
     */
63
 
    public static synchronized void startup()
64
 
    {
65
 
        if (initialized)
66
 
            return;
67
 
 
68
 
        try {
69
 
            File dir = new File(ENV_HOME);
70
 
            removeRecursive(dir);
71
 
            dir.mkdirs();
72
 
 
73
 
            System.out.println("Getting context");
74
 
            InitialContext ic = getInitialContext(DEFAULT_URL);
75
 
            System.out.println("Creating XAResource");
76
 
            xaresource = new DbXAResource(ENV_HOME, 77, 0);
77
 
            System.out.println("Registering with transaction manager");
78
 
            tm = TxHelper.getTransactionManager();
79
 
            tm.registerStaticResource("DbXA", xaresource);
80
 
            initialized = true;
81
 
        }
82
 
        catch (Exception e) {
83
 
            System.err.println("Exception: " + e);
84
 
            e.printStackTrace();
85
 
        }
86
 
        initialized = true;
87
 
    }
88
 
 
89
 
    /**
90
 
     * Closes the XA resource manager.
91
 
     */
92
 
    public static synchronized void shutdown(PrintWriter out)
93
 
        throws XAException
94
 
    {
95
 
        if (!initialized)
96
 
            return;
97
 
 
98
 
        out.println("Closing the resource.");
99
 
        xaresource.close(0);
100
 
        out.println("Shutdown complete.");
101
 
        initialized = false;
102
 
     }
103
 
 
104
 
 
105
 
    /**
106
 
     * Should be called once per chunk of major activity.
107
 
     */
108
 
    public void initialize()
109
 
    {
110
 
        startup();
111
 
    }
112
 
 
113
 
    private static int count = 1;
114
 
    private static boolean debugInited = false;
115
 
    private Xid bogusXid;
116
 
 
117
 
    public static synchronized int incrCount()
118
 
    {
119
 
         return count++;
120
 
    }
121
 
 
122
 
    public void debugSetup(PrintWriter out)
123
 
        throws ServletException, IOException
124
 
    {
125
 
        try {
126
 
            Db.load_db();
127
 
        }
128
 
        catch (Exception e) {
129
 
            out.println("got exception during load: " + e);
130
 
            System.out.println("got exception during load: " + e);
131
 
        }
132
 
        out.println("The servlet has been restarted, and Berkeley DB is loaded");
133
 
        out.println("<p>If you're debugging, you should now start the debugger and set breakpoints.");
134
 
    }
135
 
 
136
 
    public void doXATransaction(PrintWriter out, String key, String value,
137
 
                                String operation)
138
 
        throws ServletException, IOException
139
 
    {
140
 
        try {
141
 
            int counter = incrCount();
142
 
            if (key == null || key.equals(""))
143
 
                    key = "key" + counter;
144
 
            if (value == null || value.equals(""))
145
 
                    value = "value" + counter;
146
 
 
147
 
            out.println("Adding (\"" + key + "\", \"" + value + "\")");
148
 
 
149
 
            System.out.println("XA transaction begin");
150
 
            tm.begin();
151
 
            System.out.println("getting XA transaction");
152
 
            DbXAResource.DbAttach attach = DbXAResource.xa_attach(null, null);
153
 
            DbTxn txn = attach.get_txn();
154
 
            DbEnv env = attach.get_env();
155
 
            Db db = new Db(env, 0);
156
 
            db.open(txn, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0644);
157
 
            System.out.println("DB put " + key);
158
 
            db.put(txn,
159
 
                   new Dbt(key.getBytes()),
160
 
                   new Dbt(value.getBytes()),
161
 
                   0);
162
 
 
163
 
            if (operation.equals("rollback")) {
164
 
                out.println("<p>ROLLBACK");
165
 
                System.out.println("XA transaction rollback");
166
 
                tm.rollback();
167
 
                System.out.println("XA rollback returned");
168
 
 
169
 
                // The old db is no good after the rollback
170
 
                // since the open was part of the transaction.
171
 
                // Get another db for the cursor dump
172
 
                //
173
 
                db = new Db(env, 0);
174
 
                db.open(null, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0644);
175
 
            }
176
 
            else {
177
 
                out.println("<p>COMMITTED");
178
 
                System.out.println("XA transaction commit");
179
 
                tm.commit();
180
 
            }
181
 
 
182
 
            // Show the current state of the database.
183
 
            Dbc dbc = db.cursor(null, 0);
184
 
            Dbt gotkey = new Dbt();
185
 
            Dbt gotdata = new Dbt();
186
 
 
187
 
            out.println("<p>Current database values:");
188
 
            while (dbc.get(gotkey, gotdata, Db.DB_NEXT) == 0) {
189
 
                out.println("<br>  " + getDbtString(gotkey) + " : "
190
 
                                   + getDbtString(gotdata));
191
 
            }
192
 
            dbc.close();
193
 
            db.close(0);
194
 
        }
195
 
        catch (DbException dbe) {
196
 
            System.err.println("Db Exception: " + dbe);
197
 
            out.println(" *** Exception received: " + dbe);
198
 
            dbe.printStackTrace();
199
 
        }
200
 
        catch (FileNotFoundException fnfe) {
201
 
            System.err.println("FileNotFoundException: " + fnfe);
202
 
            out.println(" *** Exception received: " + fnfe);
203
 
            fnfe.printStackTrace();
204
 
        }
205
 
        // Includes SystemException, NotSupportedException, RollbackException
206
 
        catch (Exception e) {
207
 
            System.err.println("Exception: " + e);
208
 
            out.println(" *** Exception received: " + e);
209
 
            e.printStackTrace();
210
 
        }
211
 
    }
212
 
 
213
 
    private static Xid getBogusXid()
214
 
        throws XAException
215
 
    {
216
 
        return new DbXid(1, "BOGUS_gtrid".getBytes(),
217
 
                         "BOGUS_bqual".getBytes());
218
 
    }
219
 
 
220
 
    private static String getDbtString(Dbt dbt)
221
 
    {
222
 
        return new String(dbt.get_data(), 0, dbt.get_size());
223
 
    }
224
 
 
225
 
    /**
226
 
     * doGet is called as a result of invoking the servlet.
227
 
     */
228
 
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
229
 
        throws ServletException, IOException
230
 
    {
231
 
        try {
232
 
            resp.setContentType("text/html");
233
 
            PrintWriter out = resp.getWriter();
234
 
 
235
 
            String key = req.getParameter("key");
236
 
            String value = req.getParameter("value");
237
 
            String operation = req.getParameter("operation");
238
 
 
239
 
            out.println("<HTML>");
240
 
            out.println("<HEAD>");
241
 
            out.println("<TITLE>Berkeley DB with XA</TITLE>");
242
 
            out.println("</HEAD><BODY>");
243
 
            out.println("<a href=\"TestXAServlet" +
244
 
                        "\">Database put and commit</a><br>");
245
 
            out.println("<a href=\"TestXAServlet?operation=rollback" +
246
 
                        "\">Database put and rollback</a><br>");
247
 
            out.println("<a href=\"TestXAServlet?operation=close" +
248
 
                        "\">Close the XA resource manager</a><br>");
249
 
            out.println("<a href=\"TestXAServlet?operation=forget" +
250
 
                        "\">Forget an operation (bypasses TM)</a><br>");
251
 
            out.println("<a href=\"TestXAServlet?operation=prepare" +
252
 
                        "\">Prepare an operation (bypasses TM)</a><br>");
253
 
            out.println("<br>");
254
 
 
255
 
            if (!debugInited) {
256
 
                // Don't initialize XA yet, give the user
257
 
                // a chance to attach a debugger if necessary.
258
 
                debugSetup(out);
259
 
                debugInited = true;
260
 
            }
261
 
            else {
262
 
                initialize();
263
 
                if (operation == null)
264
 
                    operation = "commit";
265
 
 
266
 
                if (operation.equals("close")) {
267
 
                    shutdown(out);
268
 
                }
269
 
                else if (operation.equals("forget")) {
270
 
                    // A bogus test, we just make sure the API is callable.
271
 
                    out.println("<p>FORGET");
272
 
                    System.out.println("XA forget bogus XID (bypass TM)");
273
 
                    xaresource.forget(getBogusXid());
274
 
                }
275
 
                else if (operation.equals("prepare")) {
276
 
                    // A bogus test, we just make sure the API is callable.
277
 
                    out.println("<p>PREPARE");
278
 
                    System.out.println("XA prepare bogus XID (bypass TM)");
279
 
                    xaresource.prepare(getBogusXid());
280
 
                }
281
 
                else {
282
 
                    // commit, rollback, prepare, forget
283
 
                    doXATransaction(out, key, value, operation);
284
 
                }
285
 
            }
286
 
            out.println("</BODY></HTML>");
287
 
 
288
 
            System.out.println("Finished.");
289
 
        }
290
 
        // Includes SystemException, NotSupportedException, RollbackException
291
 
        catch (Exception e) {
292
 
            System.err.println("Exception: " + e);
293
 
            e.printStackTrace();
294
 
        }
295
 
 
296
 
    }
297
 
 
298
 
 
299
 
    /**
300
 
     * From weblogic's sample code:
301
 
     *    samples/examples/jta/jmsjdbc/Client.java
302
 
     */
303
 
    private static InitialContext getInitialContext(String url)
304
 
        throws NamingException
305
 
    {
306
 
        Hashtable env = new Hashtable();
307
 
        env.put(Context.INITIAL_CONTEXT_FACTORY,
308
 
                "weblogic.jndi.WLInitialContextFactory");
309
 
        env.put(Context.PROVIDER_URL, url);
310
 
        return new InitialContext(env);
311
 
    }
312
 
 
313
 
}