001: package jca.simple;
002:
003: import java.rmi.RemoteException;
004:
005: import javax.ejb.SessionBean;
006: import javax.ejb.SessionContext;
007: import javax.naming.Context;
008: import javax.naming.InitialContext;
009:
010: import com.sleepycat.je.Cursor;
011: import com.sleepycat.je.Database;
012: import com.sleepycat.je.SecondaryDatabase;
013: import com.sleepycat.je.DatabaseConfig;
014: import com.sleepycat.je.DatabaseException;
015: import com.sleepycat.je.SecondaryConfig;
016: import com.sleepycat.je.DatabaseEntry;
017: import com.sleepycat.je.Environment;
018: import com.sleepycat.je.EnvironmentConfig;
019: import com.sleepycat.je.SecondaryKeyCreator;
020: import com.sleepycat.je.Transaction;
021: import com.sleepycat.je.jca.ra.JEConnection;
022: import com.sleepycat.je.jca.ra.JEConnectionFactory;
023:
024: public class SimpleBean implements SessionBean {
025:
026: /*
027: * Set this to something appropriate for your environment. Make sure it
028: * matches the ra.xml.
029: */
030: private final String JE_ENV = "/tmp/je_store";
031: private final boolean TRANSACTIONAL = true;
032:
033: private SessionContext sessionCtx;
034:
035: public void ejbCreate() {
036: }
037:
038: public void ejbRemove() {
039: }
040:
041: public void setSessionContext(SessionContext context) {
042: sessionCtx = context;
043: }
044:
045: public void unsetSessionContext() {
046: sessionCtx = null;
047: }
048:
049: public void ejbActivate() {
050: }
051:
052: public void ejbPassivate() {
053: }
054:
055: public void put(String key, String data) throws RemoteException {
056:
057: try {
058: Environment env = null;
059: Transaction txn = null;
060: Database db = null;
061: SecondaryDatabase secDb = null;
062: Cursor cursor = null;
063: JEConnection dc = null;
064: try {
065: dc = getConnection(JE_ENV);
066:
067: env = dc.getEnvironment();
068: DatabaseConfig dbConfig = new DatabaseConfig();
069: SecondaryConfig secDbConfig = new SecondaryConfig();
070: dbConfig.setAllowCreate(true);
071: dbConfig.setTransactional(TRANSACTIONAL);
072: secDbConfig.setAllowCreate(true);
073: secDbConfig.setTransactional(TRANSACTIONAL);
074: secDbConfig.setKeyCreator(new MyKeyCreator());
075:
076: /*
077: * Use JEConnection.openDatabase() to obtain a cached Database
078: * handle. Do not call close() on Database handles obtained
079: * using this method.
080: */
081: db = dc.openDatabase("db", dbConfig);
082: secDb = dc.openSecondaryDatabase("secDb", db,
083: secDbConfig);
084: cursor = db.openCursor(null, null);
085: cursor.put(new DatabaseEntry(key.getBytes("UTF-8")),
086: new DatabaseEntry(data.getBytes("UTF-8")));
087: } finally {
088: if (cursor != null) {
089: cursor.close();
090: }
091: if (dc != null) {
092: dc.close();
093: }
094: }
095: } catch (Exception e) {
096: System.err.println("Failure in put" + e);
097: }
098: }
099:
100: public void removeDatabase() throws RemoteException {
101:
102: try {
103: JEConnection dc = null;
104: try {
105: dc = getConnection(JE_ENV);
106:
107: DatabaseConfig dbConfig = new DatabaseConfig();
108: dbConfig.setAllowCreate(true);
109: dbConfig.setTransactional(TRANSACTIONAL);
110:
111: /*
112: * Once you have removed a database from the environment,
113: * do not try to open it anymore.
114: */
115: dc.removeDatabase("db");
116: } finally {
117: if (dc != null) {
118: dc.close();
119: }
120: }
121: } catch (Exception e) {
122: System.err.println("Failure in remove " + e);
123: e.printStackTrace();
124: }
125: }
126:
127: public String get(String key) throws RemoteException {
128:
129: try {
130: Environment env = null;
131: Transaction txn = null;
132: Database db = null;
133: Cursor cursor = null;
134: JEConnection dc = null;
135: try {
136: dc = getConnection(JE_ENV);
137:
138: env = dc.getEnvironment();
139: DatabaseConfig dbConfig = new DatabaseConfig();
140: dbConfig.setAllowCreate(true);
141: dbConfig.setTransactional(TRANSACTIONAL);
142:
143: /*
144: * Use JEConnection.openDatabase() to obtain a cached Database
145: * handle. Do not call close() on Database handles obtained
146: * using this method.
147: */
148: db = dc.openDatabase("db", dbConfig);
149: cursor = db.openCursor(null, null);
150: DatabaseEntry data = new DatabaseEntry();
151: cursor.getSearchKey(new DatabaseEntry(key
152: .getBytes("UTF-8")), data, null);
153: return new String(data.getData(), "UTF-8");
154: } finally {
155: if (cursor != null) {
156: cursor.close();
157: }
158: if (dc != null) {
159: dc.close();
160: }
161: }
162: } catch (Exception e) {
163: System.err.println("Failure in get" + e);
164: e.printStackTrace();
165: }
166: return null;
167: }
168:
169: private JEConnection getConnection(String envDir) {
170: try {
171: EnvironmentConfig envConfig = new EnvironmentConfig();
172: envConfig.setTransactional(true);
173: envConfig.setAllowCreate(true);
174: InitialContext iniCtx = new InitialContext();
175: Context enc = (Context) iniCtx.lookup("java:comp/env");
176: Object ref = enc.lookup("ra/JEConnectionFactory");
177: JEConnectionFactory dcf = (JEConnectionFactory) ref;
178: JEConnection dc = dcf.getConnection(envDir, envConfig);
179: return dc;
180: } catch (Exception e) {
181: System.err.println("Failure in getConnection " + e);
182: }
183: return null;
184: }
185:
186: private static class MyKeyCreator implements SecondaryKeyCreator {
187:
188: MyKeyCreator() {
189: }
190:
191: public boolean createSecondaryKey(
192: SecondaryDatabase secondaryDb, DatabaseEntry keyEntry,
193: DatabaseEntry dataEntry, DatabaseEntry resultEntry)
194: throws DatabaseException {
195:
196: return false;
197: }
198: }
199: }
|