File: SimpleBean.java

package info (click to toggle)
libdb-je-java 3.3.62-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 12,832 kB
  • ctags: 18,708
  • sloc: java: 149,906; xml: 1,980; makefile: 14; sh: 12
file content (208 lines) | stat: -rw-r--r-- 5,496 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package jca.simple;

import java.rmi.RemoteException;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.SecondaryDatabase;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.SecondaryConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.SecondaryKeyCreator;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.jca.ra.JEConnection;
import com.sleepycat.je.jca.ra.JEConnectionFactory;

public class SimpleBean implements SessionBean {

    /*
     * Set this to something appropriate for your environment.  Make sure it
     * matches the ra.xml.
     */
    private final String JE_ENV = "/tmp/je_store";
    private final boolean TRANSACTIONAL = true;

    private SessionContext sessionCtx;

    public void ejbCreate() {
    }

    public void ejbRemove() {
    }

    public void setSessionContext(SessionContext context) {
	sessionCtx = context;
    }

    public void unsetSessionContext() {
	sessionCtx = null;
    }

    public void ejbActivate() {
    }

    public void ejbPassivate() {
    }

    public void put(String key, String data)
	throws RemoteException {

	try {
	    @SuppressWarnings("unused")
            Environment env = null;
	    @SuppressWarnings("unused")
            Transaction txn = null;
	    Database db = null;
	    @SuppressWarnings("unused")
            SecondaryDatabase secDb = null;
	    Cursor cursor = null;
	    JEConnection dc = null;
	    try {
		dc = getConnection(JE_ENV);

		env = dc.getEnvironment();
		DatabaseConfig dbConfig = new DatabaseConfig();
		SecondaryConfig secDbConfig = new SecondaryConfig();
		dbConfig.setAllowCreate(true);
		dbConfig.setTransactional(TRANSACTIONAL);
		secDbConfig.setAllowCreate(true);
		secDbConfig.setTransactional(TRANSACTIONAL);
		secDbConfig.setKeyCreator(new MyKeyCreator());

		/*
		 * Use JEConnection.openDatabase() to obtain a cached Database
		 * handle.  Do not call close() on Database handles obtained
		 * using this method.
		 */
		db = dc.openDatabase("db", dbConfig);
		secDb = dc.openSecondaryDatabase("secDb", db, secDbConfig);
		cursor = db.openCursor(null, null);
		cursor.put(new DatabaseEntry(key.getBytes("UTF-8")),
			   new DatabaseEntry(data.getBytes("UTF-8")));
	    } finally {
		if (cursor != null) {
		    cursor.close();
		}
		if (dc != null) {
		    dc.close();
		}
	    }
	} catch (Exception e) {
	    System.err.println("Failure in put" + e);
	}
    }

    public void removeDatabase()
	throws RemoteException {

	try {
	    JEConnection dc = null;
	    try {
		dc = getConnection(JE_ENV);

		DatabaseConfig dbConfig = new DatabaseConfig();
		dbConfig.setAllowCreate(true);
		dbConfig.setTransactional(TRANSACTIONAL);

		/*
		 * Once you have removed a database from the environment,
		 * do not try to open it anymore.
		 */
		dc.removeDatabase("db");
	    } finally {
		if (dc != null) {
		    dc.close();
		}
	    }
	} catch (Exception e) {
	    System.err.println("Failure in remove " + e);
	    e.printStackTrace();
	}
    }

    public String get(String key)
	throws RemoteException {

	try {
	    @SuppressWarnings("unused")
            Environment env = null;
	    @SuppressWarnings("unused")
            Transaction txn = null;
	    Database db = null;
	    Cursor cursor = null;
	    JEConnection dc = null;
	    try {
		dc = getConnection(JE_ENV);

		env = dc.getEnvironment();
		DatabaseConfig dbConfig = new DatabaseConfig();
		dbConfig.setAllowCreate(true);
		dbConfig.setTransactional(TRANSACTIONAL);

		/*
		 * Use JEConnection.openDatabase() to obtain a cached Database
		 * handle.  Do not call close() on Database handles obtained
		 * using this method.
		 */
		db = dc.openDatabase("db", dbConfig);
		cursor = db.openCursor(null, null);
		DatabaseEntry data = new DatabaseEntry();
		cursor.getSearchKey(new DatabaseEntry(key.getBytes("UTF-8")),
				    data,
				    null);
		return new String(data.getData(), "UTF-8");
	    } finally {
		if (cursor != null) {
		    cursor.close();
		}
		if (dc != null) {
		    dc.close();
		}
	    }
	} catch (Exception e) {
	    System.err.println("Failure in get" + e);
	    e.printStackTrace();
	}
	return null;
    }

    private JEConnection getConnection(String envDir) {
	try {
	    EnvironmentConfig envConfig = new EnvironmentConfig();
	    envConfig.setTransactional(true);
	    envConfig.setAllowCreate(true);
	    InitialContext iniCtx = new InitialContext();
	    Context enc = (Context) iniCtx.lookup("java:comp/env");
	    Object ref = enc.lookup("ra/JEConnectionFactory");
	    JEConnectionFactory dcf = (JEConnectionFactory) ref;
	    JEConnection dc = dcf.getConnection(envDir, envConfig);
	    return dc;
	} catch(Exception e) {
	    System.err.println("Failure in getConnection " + e);
	}
	return null;
    }

    private static class MyKeyCreator implements SecondaryKeyCreator {

        MyKeyCreator() {
        }

        public boolean createSecondaryKey(SecondaryDatabase secondaryDb,
                                          DatabaseEntry keyEntry,
                                          DatabaseEntry dataEntry,
                                          DatabaseEntry resultEntry)
            throws DatabaseException {

	    return false;
        }
    }
}