1 package jca.simple; 2 3 import java.rmi.RemoteException ; 4 5 import javax.ejb.SessionBean ; 6 import javax.ejb.SessionContext ; 7 import javax.naming.Context ; 8 import javax.naming.InitialContext ; 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 { 25 26 30 private final String JE_ENV = "/tmp/je_store"; 31 private final boolean TRANSACTIONAL = true; 32 33 private SessionContext sessionCtx; 34 35 public void ejbCreate() { 36 } 37 38 public void ejbRemove() { 39 } 40 41 public void setSessionContext(SessionContext 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 key, String data) 56 throws RemoteException { 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 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 e) { 97 System.err.println("Failure in put" + e); 98 } 99 } 100 101 public void removeDatabase() 102 throws RemoteException { 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 117 dc.removeDatabase("db"); 118 } finally { 119 if (dc != null) { 120 dc.close(); 121 } 122 } 123 } catch (Exception e) { 124 System.err.println("Failure in remove " + e); 125 e.printStackTrace(); 126 } 127 } 128 129 public String get(String key) 130 throws RemoteException { 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 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 (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 e) { 167 System.err.println("Failure in get" + e); 168 e.printStackTrace(); 169 } 170 return null; 171 } 172 173 private JEConnection getConnection(String envDir) { 174 try { 175 EnvironmentConfig envConfig = new EnvironmentConfig(); 176 envConfig.setTransactional(true); 177 envConfig.setAllowCreate(true); 178 InitialContext iniCtx = new InitialContext (); 179 Context enc = (Context ) iniCtx.lookup("java:comp/env"); 180 Object ref = enc.lookup("ra/JEConnectionFactory"); 181 JEConnectionFactory dcf = (JEConnectionFactory) ref; 182 JEConnection dc = dcf.getConnection(envDir, envConfig); 183 return dc; 184 } catch(Exception 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 |