KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > connector > OTMJCAManagedConnection


1 package org.apache.ojb.otm.connector;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.PBKey;
19 import org.apache.ojb.otm.OTMConnection;
20 import org.apache.ojb.otm.core.Transaction;
21 import org.apache.ojb.otm.core.TransactionException;
22
23 import javax.resource.NotSupportedException JavaDoc;
24 import javax.resource.ResourceException JavaDoc;
25 import javax.resource.spi.ConnectionEvent JavaDoc;
26 import javax.resource.spi.ConnectionEventListener JavaDoc;
27 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
28 import javax.resource.spi.LocalTransaction JavaDoc;
29 import javax.resource.spi.ManagedConnection JavaDoc;
30 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
31 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
33 import javax.transaction.xa.XAResource JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Set JavaDoc;
40
41 /**
42  *
43  * @author <a HREF="mailto:mattbaird@yahoo.com">Matthew Baird<a>
44  */

45
46 public class OTMJCAManagedConnection implements ManagedConnection JavaDoc, LocalTransaction JavaDoc
47 {
48     private PBKey m_pbKey;
49     private OTMJCAManagedConnectionFactory m_managedConnectionFactory;
50     private PrintWriter JavaDoc m_logWriter;
51     private boolean m_destroyed;
52     private final Set JavaDoc m_handles = new HashSet JavaDoc();
53     private final Collection JavaDoc m_connectionEventListeners = new ArrayList JavaDoc();
54     private boolean m_managed = false;
55     /**
56      * the wrapped connection
57      */

58     private OTMConnection m_connection;
59     private Transaction m_tx;
60
61     OTMJCAManagedConnection(ManagedConnectionFactory JavaDoc mcf, OTMConnection conn, PBKey pbKey)
62     {
63         Util.log("In OTMJCAManagedConnection");
64         m_managedConnectionFactory = (OTMJCAManagedConnectionFactory) mcf;
65         m_pbKey = pbKey;
66         m_connection = conn;
67     }
68
69     /**
70      * get the underlying wrapped connection
71      * @return OTMConnection raw connection to the OTM.
72      */

73     OTMConnection getConnection()
74     {
75         if (m_connection == null)
76         {
77             OTMConnectionRuntimeException ex = new OTMConnectionRuntimeException("Connection is null.");
78             sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
79         }
80         return m_connection;
81     }
82
83     public Transaction getTransaction()
84     {
85         return this.m_tx;
86     }
87
88     public void setTransaction(Transaction tx)
89     {
90         if (this.m_tx != null) throw new IllegalStateException JavaDoc("Connection already has Transaction");
91         this.m_tx = tx;
92     }
93
94     public String JavaDoc getUserName()
95     {
96         return m_pbKey.getUser();
97     }
98
99     public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc connectionRequestInfo)
100             throws ResourceException JavaDoc
101     {
102         Util.log("In OTMJCAManagedConnection.getConnection");
103         OTMJCAConnection myCon = new OTMJCAConnection(this);
104         synchronized (m_handles)
105         {
106             m_handles.add(myCon);
107         }
108         return myCon;
109     }
110
111     public void destroy()
112     {
113         Util.log("In OTMJCAManagedConnection.destroy");
114         cleanup();
115         m_connection.close();
116         m_destroyed = true;
117     }
118
119     public void cleanup()
120     {
121         Util.log("In OTMJCAManagedConnection.cleanup");
122         synchronized (m_handles)
123         {
124             for (Iterator JavaDoc i = m_handles.iterator(); i.hasNext();)
125             {
126                 OTMJCAConnection lc = (OTMJCAConnection) i.next();
127                 lc.setManagedConnection(null);
128             }
129             m_handles.clear();
130         }
131     }
132
133     void closeHandle(OTMJCAConnection handle)
134     {
135         synchronized (m_handles)
136         {
137             m_handles.remove(handle);
138         }
139         sendEvents(ConnectionEvent.CONNECTION_CLOSED, null, handle);
140     }
141
142     public void associateConnection(Object JavaDoc connection)
143     {
144         Util.log("In OTMJCAManagedConnection.associateConnection");
145         if (connection == null)
146         {
147             throw new OTMConnectionRuntimeException("Cannot associate a null connection");
148         }
149         if (!(connection instanceof OTMJCAConnection))
150         {
151             throw new OTMConnectionRuntimeException("Cannot associate a connection of type: " + connection.getClass().getName() + " to a handle that manages: " + OTMJCAConnection.class.getName());
152         }
153         ((OTMJCAConnection) connection).setManagedConnection(this);
154         synchronized (m_handles)
155         {
156             m_handles.add(connection);
157         }
158     }
159
160     public void addConnectionEventListener(ConnectionEventListener JavaDoc cel)
161     {
162         synchronized (m_connectionEventListeners)
163         {
164             m_connectionEventListeners.add(cel);
165         }
166     }
167
168     public void removeConnectionEventListener(ConnectionEventListener JavaDoc cel)
169     {
170         synchronized (m_connectionEventListeners)
171         {
172             m_connectionEventListeners.remove(cel);
173         }
174     }
175
176     public XAResource JavaDoc getXAResource()
177             throws ResourceException JavaDoc
178     {
179         Util.log("In OTMJCAManagedConnection.getXAResource");
180         throw new NotSupportedException JavaDoc("public XAResource getXAResource() not supported in this release.");
181     }
182
183     /**
184      * the OTMConnection is the transaction
185      * @return
186      */

187     public LocalTransaction JavaDoc getLocalTransaction()
188     {
189         Util.log("In OTMJCAManagedConnection.getLocalTransaction");
190         return this;
191     }
192
193     public ManagedConnectionMetaData JavaDoc getMetaData()
194             throws ResourceException JavaDoc
195     {
196         Util.log("In OTMJCAManagedConnection.getMetaData");
197         return new OTMConnectionMetaData(this);
198     }
199
200     public void setLogWriter(PrintWriter JavaDoc out)
201             throws ResourceException JavaDoc
202     {
203         Util.log("In OTMJCAManagedConnection.setLogWriter");
204         m_logWriter = out;
205     }
206
207     public PrintWriter JavaDoc getLogWriter()
208             throws ResourceException JavaDoc
209     {
210         Util.log("In OTMJCAManagedConnection.getLogWriter");
211         return m_logWriter;
212     }
213
214     boolean isDestroyed()
215     {
216         Util.log("In OTMJCAManagedConnection.isDestroyed");
217         return m_destroyed;
218     }
219
220     ManagedConnectionFactory JavaDoc getManagedConnectionFactory()
221     {
222         Util.log("In OTMJCAManagedConnection.getManagedConnectionFactory");
223         return m_managedConnectionFactory;
224     }
225
226     public void begin() throws ResourceException JavaDoc
227     {
228         Util.log("In OTMJCAManagedConnection.begin");
229         if (!isManaged())
230         {
231             try
232             {
233                 m_tx = m_managedConnectionFactory.getKit().getTransaction(m_connection);
234                 m_tx.begin();
235                 setManaged(true);
236             }
237             catch (TransactionException e)
238             {
239                 ResourceException JavaDoc ex = new ResourceException JavaDoc(e.getMessage());
240                 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
241                 throw ex;
242             }
243         }
244         else
245         {
246             ResourceException JavaDoc ex = new ResourceException JavaDoc("You probably called begin again without calling Commit or Rollback, OTM does not support nested Local Transactions.");
247             sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
248             throw ex;
249         }
250     }
251
252     public void commit() throws ResourceException JavaDoc
253     {
254         Util.log("In OTMJCAManagedConnection.commit");
255         if (isManaged())
256         {
257             try
258             {
259                 setManaged(false);
260                 m_tx.commit();
261             }
262             catch (TransactionException e)
263             {
264                 m_tx.rollback();
265                 ResourceException JavaDoc ex = new ResourceException JavaDoc(e.getMessage());
266                 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
267                 throw ex;
268             }
269         }
270         else
271         {
272             ResourceException JavaDoc ex = new ResourceException JavaDoc("Cannot call commit when you are not in a Local Transaction.");
273             sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
274             throw ex;
275         }
276     }
277
278     public void rollback() throws ResourceException JavaDoc
279     {
280         Util.log("In OTMJCAManagedConnection.rollback");
281         if (isManaged())
282         {
283             try
284             {
285                 m_tx.rollback();
286                 setManaged(false);
287             }
288             catch (TransactionException e)
289             {
290                 ResourceException JavaDoc ex = new ResourceException JavaDoc(e.getMessage());
291                 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
292                 throw ex;
293             }
294         }
295         else
296         {
297             ResourceException JavaDoc ex = new ResourceException JavaDoc("Cannot call rollback when you are not in a Local Transaction.");
298             sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null);
299             throw ex;
300         }
301     }
302
303     private boolean isManaged()
304     {
305         return m_managed;
306     }
307
308     private void setManaged(boolean flag)
309     {
310         m_managed = flag;
311     }
312
313     /**
314      * Section 6.5.6 of the JCA 1.5 spec instructs ManagedConnection instances to notify connection listeners with
315      * close/error and local transaction-related events to its registered set of listeners.
316      *
317      * The events for begin/commit/rollback are only sent if the application server did NOT
318      * initiate the actions.
319      *
320      * This method dispatchs all events to the listeners based on the eventType
321      * @param eventType as enumerated in the ConnectionEvents interface
322      * @param ex an optional exception if we are sending an error message
323      * @param connectionHandle an optional connectionHandle if we have access to it.
324      */

325     void sendEvents(int eventType, Exception JavaDoc ex, Object JavaDoc connectionHandle)
326     {
327         ConnectionEvent JavaDoc ce = null;
328         if (ex == null)
329         {
330             ce = new ConnectionEvent JavaDoc(this, eventType);
331         }
332         else
333         {
334             ce = new ConnectionEvent JavaDoc(this, eventType, ex);
335         }
336         ce.setConnectionHandle(connectionHandle);
337         Collection JavaDoc copy = null;
338         synchronized (m_connectionEventListeners)
339         {
340             copy = new ArrayList JavaDoc(m_connectionEventListeners);
341         }
342         switch (ce.getId())
343         {
344             case ConnectionEvent.CONNECTION_CLOSED:
345                 for (Iterator JavaDoc i = copy.iterator(); i.hasNext();)
346                 {
347                     ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) i.next();
348                     cel.connectionClosed(ce);
349                 }
350                 break;
351             case ConnectionEvent.LOCAL_TRANSACTION_STARTED:
352                 for (Iterator JavaDoc i = copy.iterator(); i.hasNext();)
353                 {
354                     ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) i.next();
355                     cel.localTransactionStarted(ce);
356                 }
357                 break;
358             case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED:
359                 for (Iterator JavaDoc i = copy.iterator(); i.hasNext();)
360                 {
361                     ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) i.next();
362                     cel.localTransactionCommitted(ce);
363                 }
364                 break;
365             case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK:
366                 for (Iterator JavaDoc i = copy.iterator(); i.hasNext();)
367                 {
368                     ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) i.next();
369                     cel.localTransactionRolledback(ce);
370                 }
371                 break;
372             case ConnectionEvent.CONNECTION_ERROR_OCCURRED:
373                 for (Iterator JavaDoc i = copy.iterator(); i.hasNext();)
374                 {
375                     ConnectionEventListener JavaDoc cel = (ConnectionEventListener JavaDoc) i.next();
376                     cel.connectionErrorOccurred(ce);
377                 }
378                 break;
379             default:
380                 throw new IllegalArgumentException JavaDoc("Illegal eventType: " + ce.getId());
381         }
382     }
383 }
384
Popular Tags