KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > jca > ra > JEManagedConnection


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: JEManagedConnection.java,v 1.13 2006/10/30 21:14:18 bostic Exp $
7  */

8
9 package com.sleepycat.je.jca.ra;
10
11 import java.io.PrintWriter JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.resource.ResourceException JavaDoc;
18 import javax.resource.spi.ConnectionEvent JavaDoc;
19 import javax.resource.spi.ConnectionEventListener JavaDoc;
20 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
21 import javax.resource.spi.LocalTransaction JavaDoc;
22 import javax.resource.spi.ManagedConnection JavaDoc;
23 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
24 import javax.security.auth.Subject JavaDoc;
25 import javax.transaction.xa.XAResource JavaDoc;
26
27 import com.sleepycat.je.Database;
28 import com.sleepycat.je.DatabaseConfig;
29 import com.sleepycat.je.DatabaseException;
30 import com.sleepycat.je.DbInternal;
31 import com.sleepycat.je.SecondaryConfig;
32 import com.sleepycat.je.SecondaryDatabase;
33 import com.sleepycat.je.TransactionConfig;
34 import com.sleepycat.je.XAEnvironment;
35
36 public class JEManagedConnection implements ManagedConnection JavaDoc {
37     private ArrayList JavaDoc listeners;
38     private JEConnection conn;
39     private XAEnvironment env;
40     private JELocalTransaction savedLT;
41     private TransactionConfig savedTransConfig;
42     private Map JavaDoc rwDatabaseHandleCache;
43     private Map JavaDoc roDatabaseHandleCache;
44     private Map JavaDoc rwSecondaryDatabaseHandleCache;
45     private Map JavaDoc roSecondaryDatabaseHandleCache;
46
47     JEManagedConnection(Subject JavaDoc subject, JERequestInfo jeInfo)
48     throws ResourceException JavaDoc {
49
50     try {
51         savedTransConfig = jeInfo.getTransactionConfig();
52         this.env = new XAEnvironment(jeInfo.getJERootDir(),
53                      jeInfo.getEnvConfig());
54     } catch (DatabaseException DE) {
55         throw new ResourceException JavaDoc(DE.toString());
56     }
57     listeners = new ArrayList JavaDoc();
58     savedLT = null;
59     rwDatabaseHandleCache = new HashMap JavaDoc();
60     roDatabaseHandleCache = new HashMap JavaDoc();
61     rwSecondaryDatabaseHandleCache = new HashMap JavaDoc();
62     roSecondaryDatabaseHandleCache = new HashMap JavaDoc();
63     }
64
65     public Object JavaDoc getConnection(Subject JavaDoc subject,
66                 ConnectionRequestInfo JavaDoc connectionRequestInfo)
67         throws ResourceException JavaDoc {
68
69     if (conn == null) {
70         conn = new JEConnection(this);
71     }
72     return conn;
73     }
74
75     protected XAEnvironment getEnvironment()
76     throws ResourceException JavaDoc {
77
78     return env;
79     }
80
81     public LocalTransaction JavaDoc getLocalTransaction()
82     throws ResourceException JavaDoc {
83
84     /*
85      * If there is no JEConnection associated with this ManagedConnection
86      * yet, then the ManagedConnection holds on to the JELocalTransaction.
87      * Once a JEConnection is associated (it may not ever happen), we hand
88      * off the JELocalTransaction to the JEConnection and forget about it
89      * in the ManagedConnection.
90      */

91     if (conn == null) {
92         savedLT = new JELocalTransaction(env, savedTransConfig, this);
93         return savedLT;
94     }
95
96     JELocalTransaction lt = conn.getLocalTransaction();
97     if (lt == null) {
98         if (savedLT == null) {
99         lt = new JELocalTransaction(env, savedTransConfig, this);
100         } else {
101         lt = savedLT;
102         }
103         conn.setLocalTransaction(lt);
104         savedLT = null;
105     }
106     return lt;
107     }
108    
109     public XAResource JavaDoc getXAResource()
110         throws ResourceException JavaDoc {
111
112     return (XAResource JavaDoc) env;
113     }
114
115     public void associateConnection(Object JavaDoc connection)
116     throws ResourceException JavaDoc {
117
118     conn = (JEConnection) connection;
119     conn.setManagedConnection(this, savedLT);
120     savedLT = null;
121     }
122
123     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener) {
124     listeners.add(listener);
125     }
126
127     public void
128     removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
129
130     listeners.remove(listener);
131     }
132
133     public ManagedConnectionMetaData JavaDoc getMetaData()
134         throws ResourceException JavaDoc {
135
136         return new JEConnectionMetaData();
137     }
138
139     public void setLogWriter(PrintWriter JavaDoc out)
140         throws ResourceException JavaDoc {
141
142     }
143
144     public PrintWriter JavaDoc getLogWriter()
145         throws ResourceException JavaDoc {
146
147     return null;
148     }
149
150     protected void close() {
151     ConnectionEvent JavaDoc connEvent =
152         new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
153     connEvent.setConnectionHandle(conn);
154     sendConnectionEvent(connEvent);
155     }
156
157     protected void sendConnectionEvent(ConnectionEvent JavaDoc connEvent) {
158     for (int i = listeners.size() - 1; i >= 0; i--) {
159         ConnectionEventListener JavaDoc listener =
160         (ConnectionEventListener JavaDoc) listeners.get(i);
161         if (connEvent.getId() == ConnectionEvent.CONNECTION_CLOSED) {
162         listener.connectionClosed(connEvent);
163         } else if (connEvent.getId() ==
164                ConnectionEvent.CONNECTION_ERROR_OCCURRED) {
165         listener.connectionErrorOccurred(connEvent);
166         } else if (connEvent.getId() ==
167                ConnectionEvent.LOCAL_TRANSACTION_STARTED) {
168         listener.localTransactionStarted(connEvent);
169         } else if (connEvent.getId() ==
170                ConnectionEvent.LOCAL_TRANSACTION_COMMITTED) {
171         listener.localTransactionCommitted(connEvent);
172         } else if (connEvent.getId() ==
173                ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK) {
174         listener.localTransactionRolledback(connEvent);
175         }
176     }
177     }
178
179     public void destroy()
180     throws ResourceException JavaDoc {
181
182     try {
183         cleanupDatabaseHandleCache(roDatabaseHandleCache);
184         cleanupDatabaseHandleCache(rwDatabaseHandleCache);
185         cleanupDatabaseHandleCache(roSecondaryDatabaseHandleCache);
186         cleanupDatabaseHandleCache(rwSecondaryDatabaseHandleCache);
187         env.close();
188     } catch (DatabaseException DE) {
189         throw new ResourceException JavaDoc(DE.toString());
190     }
191     }
192
193     public void cleanup() {
194     }
195
196     void removeDatabase(String JavaDoc dbName)
197     throws DatabaseException {
198
199     removeDatabaseFromCache(roDatabaseHandleCache, dbName);
200     removeDatabaseFromCache(rwDatabaseHandleCache, dbName);
201     removeDatabaseFromCache(roSecondaryDatabaseHandleCache, dbName);
202     removeDatabaseFromCache(rwSecondaryDatabaseHandleCache, dbName);
203     env.removeDatabase(null, dbName);
204     }
205
206     long truncateDatabase(String JavaDoc dbName, boolean returnCount)
207     throws DatabaseException {
208
209     removeDatabaseFromCache(roDatabaseHandleCache, dbName);
210     removeDatabaseFromCache(rwDatabaseHandleCache, dbName);
211     removeDatabaseFromCache(roSecondaryDatabaseHandleCache, dbName);
212     removeDatabaseFromCache(rwSecondaryDatabaseHandleCache, dbName);
213     return env.truncateDatabase(null, dbName, returnCount);
214     }
215
216     Database openDatabase(String JavaDoc dbName, DatabaseConfig config)
217     throws DatabaseException {
218
219     if (config.getReadOnly()) {
220         synchronized (roDatabaseHandleCache) {
221         return openDatabaseInternal
222             (roDatabaseHandleCache, dbName, config);
223         }
224     } else {
225         synchronized (rwDatabaseHandleCache) {
226         return openDatabaseInternal
227             (rwDatabaseHandleCache, dbName, config);
228         }
229     }
230     }
231
232     SecondaryDatabase openSecondaryDatabase(String JavaDoc dbName,
233                         Database primaryDatabase,
234                         SecondaryConfig config)
235     throws DatabaseException {
236
237     if (config.getReadOnly()) {
238         synchronized (roSecondaryDatabaseHandleCache) {
239         return openSecondaryDatabaseInternal
240             (roSecondaryDatabaseHandleCache, dbName,
241              primaryDatabase, config);
242         }
243     } else {
244         synchronized (rwSecondaryDatabaseHandleCache) {
245         return openSecondaryDatabaseInternal
246             (rwSecondaryDatabaseHandleCache, dbName,
247              primaryDatabase, config);
248         }
249     }
250     }
251
252     private Database openDatabaseInternal(Map JavaDoc databaseHandleCache,
253                       String JavaDoc dbName,
254                       DatabaseConfig config)
255     throws DatabaseException {
256
257     Database db;
258     if (config.getExclusiveCreate()) {
259         db = env.openDatabase(null, dbName, config);
260         databaseHandleCache.put(dbName, db);
261     } else {
262         db = (Database) databaseHandleCache.get(dbName);
263         if (db == null) {
264         db = env.openDatabase(null, dbName, config);
265         databaseHandleCache.put(dbName, db);
266         } else {
267         DbInternal.databaseConfigValidate(config, db.getConfig());
268         }
269     }
270     return db;
271     }
272
273     private SecondaryDatabase
274     openSecondaryDatabaseInternal(Map JavaDoc databaseHandleCache,
275                       String JavaDoc dbName,
276                       Database primaryDatabase,
277                       SecondaryConfig config)
278     throws DatabaseException {
279
280     SecondaryDatabase db;
281     if (config.getExclusiveCreate()) {
282         db = env.openSecondaryDatabase(null, dbName,
283                        primaryDatabase, config);
284         databaseHandleCache.put(dbName, db);
285     } else {
286         db = (SecondaryDatabase) databaseHandleCache.get(dbName);
287         if (db == null) {
288         db = env.openSecondaryDatabase(null, dbName,
289                            primaryDatabase, config);
290         databaseHandleCache.put(dbName, db);
291         } else {
292         DbInternal.databaseConfigValidate(config, db.getConfig());
293         }
294     }
295     return db;
296     }
297
298     private void removeDatabaseFromCache(Map JavaDoc cache, String JavaDoc dbName)
299     throws DatabaseException {
300
301     synchronized (cache) {
302         Database db = (Database) cache.get(dbName);
303         if (db == null) {
304         return;
305         }
306         db.close();
307         cache.remove(dbName);
308     }
309     }
310
311     private void cleanupDatabaseHandleCache(Map JavaDoc cache)
312     throws DatabaseException {
313
314     synchronized (cache) {
315         Iterator JavaDoc iter = cache.values().iterator();
316
317         while (iter.hasNext()) {
318         Database db = (Database) iter.next();
319         db.close();
320         }
321     }
322     }
323 }
324
Popular Tags