KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jca > simple > SimpleBean


1 package jca.simple;
2
3 import java.rmi.RemoteException JavaDoc;
4
5 import javax.ejb.SessionBean JavaDoc;
6 import javax.ejb.SessionContext JavaDoc;
7 import javax.naming.Context JavaDoc;
8 import javax.naming.InitialContext JavaDoc;
9
10 import com.sleepycat.je.Cursor;
11 import com.sleepycat.je.Database;
12 import com.sleepycat.je.SecondaryDatabase;
13 import com.sleepycat.je.DatabaseConfig;
14 import com.sleepycat.je.DatabaseException;
15 import com.sleepycat.je.SecondaryConfig;
16 import com.sleepycat.je.DatabaseEntry;
17 import com.sleepycat.je.Environment;
18 import com.sleepycat.je.EnvironmentConfig;
19 import com.sleepycat.je.SecondaryKeyCreator;
20 import com.sleepycat.je.Transaction;
21 import com.sleepycat.je.jca.ra.JEConnection;
22 import com.sleepycat.je.jca.ra.JEConnectionFactory;
23
24 public class SimpleBean implements SessionBean JavaDoc {
25
26     /*
27      * Set this to something appropriate for your environment. Make sure it
28      * matches the ra.xml.
29      */

30     private final String JavaDoc JE_ENV = "/tmp/je_store";
31     private final boolean TRANSACTIONAL = true;
32
33     private SessionContext JavaDoc sessionCtx;
34
35     public void ejbCreate() {
36     }
37
38     public void ejbRemove() {
39     }
40
41     public void setSessionContext(SessionContext JavaDoc context) {
42     sessionCtx = context;
43     }
44
45     public void unsetSessionContext() {
46     sessionCtx = null;
47     }
48
49     public void ejbActivate() {
50     }
51
52     public void ejbPassivate() {
53     }
54
55     public void put(String JavaDoc key, String JavaDoc data)
56     throws RemoteException JavaDoc {
57
58     try {
59         Environment env = null;
60         Transaction txn = null;
61         Database db = null;
62         SecondaryDatabase secDb = null;
63         Cursor cursor = null;
64         JEConnection dc = null;
65         try {
66         dc = getConnection(JE_ENV);
67
68         env = dc.getEnvironment();
69         DatabaseConfig dbConfig = new DatabaseConfig();
70         SecondaryConfig secDbConfig = new SecondaryConfig();
71         dbConfig.setAllowCreate(true);
72         dbConfig.setTransactional(TRANSACTIONAL);
73         secDbConfig.setAllowCreate(true);
74         secDbConfig.setTransactional(TRANSACTIONAL);
75         secDbConfig.setKeyCreator(new MyKeyCreator());
76
77         /*
78          * Use JEConnection.openDatabase() to obtain a cached Database
79          * handle. Do not call close() on Database handles obtained
80          * using this method.
81          */

82         db = dc.openDatabase("db", dbConfig);
83         secDb = dc.openSecondaryDatabase("secDb", db, secDbConfig);
84         System.out.println("blort");
85         cursor = db.openCursor(null, null);
86         cursor.put(new DatabaseEntry(key.getBytes("UTF-8")),
87                new DatabaseEntry(data.getBytes("UTF-8")));
88         } finally {
89         if (cursor != null) {
90             cursor.close();
91         }
92         if (dc != null) {
93             dc.close();
94         }
95         }
96     } catch (Exception JavaDoc e) {
97         System.err.println("Failure in put" + e);
98     }
99     }
100
101     public void removeDatabase()
102     throws RemoteException JavaDoc {
103
104     try {
105         JEConnection dc = null;
106         try {
107         dc = getConnection(JE_ENV);
108
109         DatabaseConfig dbConfig = new DatabaseConfig();
110         dbConfig.setAllowCreate(true);
111         dbConfig.setTransactional(TRANSACTIONAL);
112
113         /*
114          * Once you have removed a database from the environment,
115          * do not try to open it anymore.
116          */

117         dc.removeDatabase("db");
118         } finally {
119         if (dc != null) {
120             dc.close();
121         }
122         }
123     } catch (Exception JavaDoc e) {
124         System.err.println("Failure in remove " + e);
125         e.printStackTrace();
126     }
127     }
128
129     public String JavaDoc get(String JavaDoc key)
130     throws RemoteException JavaDoc {
131
132     try {
133         Environment env = null;
134         Transaction txn = null;
135         Database db = null;
136         Cursor cursor = null;
137         JEConnection dc = null;
138         try {
139         dc = getConnection(JE_ENV);
140
141         env = dc.getEnvironment();
142         DatabaseConfig dbConfig = new DatabaseConfig();
143         dbConfig.setAllowCreate(true);
144         dbConfig.setTransactional(TRANSACTIONAL);
145
146         /*
147          * Use JEConnection.openDatabase() to obtain a cached Database
148          * handle. Do not call close() on Database handles obtained
149          * using this method.
150          */

151         db = dc.openDatabase("db", dbConfig);
152         cursor = db.openCursor(null, null);
153         DatabaseEntry data = new DatabaseEntry();
154         cursor.getSearchKey(new DatabaseEntry(key.getBytes("UTF-8")),
155                     data,
156                     null);
157         return new String JavaDoc(data.getData(), "UTF-8");
158         } finally {
159         if (cursor != null) {
160             cursor.close();
161         }
162         if (dc != null) {
163             dc.close();
164         }
165         }
166     } catch (Exception JavaDoc e) {
167         System.err.println("Failure in get" + e);
168         e.printStackTrace();
169     }
170     return null;
171     }
172
173     private JEConnection getConnection(String JavaDoc envDir) {
174     try {
175         EnvironmentConfig envConfig = new EnvironmentConfig();
176         envConfig.setTransactional(true);
177         envConfig.setAllowCreate(true);
178         InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
179         Context JavaDoc enc = (Context JavaDoc) iniCtx.lookup("java:comp/env");
180         Object JavaDoc ref = enc.lookup("ra/JEConnectionFactory");
181         JEConnectionFactory dcf = (JEConnectionFactory) ref;
182         JEConnection dc = dcf.getConnection(envDir, envConfig);
183         return dc;
184     } catch(Exception JavaDoc e) {
185         System.err.println("Failure in getConnection " + e);
186     }
187     return null;
188     }
189
190     private static class MyKeyCreator implements SecondaryKeyCreator {
191
192         MyKeyCreator() {
193         }
194
195         public boolean createSecondaryKey(SecondaryDatabase secondaryDb,
196                                           DatabaseEntry keyEntry,
197                                           DatabaseEntry dataEntry,
198                                           DatabaseEntry resultEntry)
199             throws DatabaseException {
200
201         return false;
202         }
203     }
204 }
205
Popular Tags