View Javadoc

1   /*
2    * $Id: DefaultConnectionManager.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.jca;
12  
13  import java.io.IOException;
14  import java.io.ObjectInputStream;
15  
16  import javax.resource.ResourceException;
17  import javax.resource.spi.ConnectionEvent;
18  import javax.resource.spi.ConnectionEventListener;
19  import javax.resource.spi.ConnectionManager;
20  import javax.resource.spi.ConnectionRequestInfo;
21  import javax.resource.spi.ManagedConnection;
22  import javax.resource.spi.ManagedConnectionFactory;
23  import javax.security.auth.Subject;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * <code>DefaultConnectionManager</code> TODO
30   */
31  public class DefaultConnectionManager implements ConnectionManager, ConnectionEventListener
32  {
33      /**
34       * Serial version
35       */
36      private static final long serialVersionUID = 1967602190602154760L;
37  
38      private transient Log logger = LogFactory.getLog(this.getClass());
39  
40      public DefaultConnectionManager()
41      {
42          super();
43      }
44  
45      private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException
46      {
47          ois.defaultReadObject();
48          this.logger = LogFactory.getLog(this.getClass());
49      }
50  
51      /**
52       * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory,
53       *      javax.resource.spi.ConnectionRequestInfo)
54       */
55      public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info)
56          throws ResourceException
57      {
58          Subject subject = null;
59          ManagedConnection connection = connectionFactory.createManagedConnection(subject, info);
60          connection.addConnectionEventListener(this);
61          return connection.getConnection(subject, info);
62      }
63  
64      /**
65       * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent)
66       */
67      public void connectionClosed(ConnectionEvent event)
68      {
69          try
70          {
71              ((ManagedConnection)event.getSource()).cleanup();
72          }
73          catch (ResourceException e)
74          {
75              logger.warn("Error occured during the cleanup of a managed connection: ", e);
76          }
77          try
78          {
79              ((ManagedConnection)event.getSource()).destroy();
80          }
81          catch (ResourceException e)
82          {
83              logger.warn("Error occured during the destruction of a managed connection: ", e);
84          }
85      }
86  
87      /**
88       * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent)
89       */
90      public void localTransactionStarted(ConnectionEvent event)
91      {
92          // TODO maybe later?
93      }
94  
95      /**
96       * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent)
97       */
98      public void localTransactionCommitted(ConnectionEvent event)
99      {
100         // TODO maybe later?
101     }
102 
103     /**
104      * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent)
105      */
106     public void localTransactionRolledback(ConnectionEvent event)
107     {
108         // TODO maybe later?
109     }
110 
111     /**
112      * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent)
113      */
114     public void connectionErrorOccurred(ConnectionEvent event)
115     {
116         logger.warn("Managed connection experiened an error: ", event.getException());
117         try
118         {
119             ((ManagedConnection)event.getSource()).cleanup();
120         }
121         catch (ResourceException e)
122         {
123             logger.warn("Error occured during the cleanup of a managed connection: ", e);
124         }
125         try
126         {
127             ((ManagedConnection)event.getSource()).destroy();
128         }
129         catch (ResourceException e)
130         {
131             logger.warn("Error occured during the destruction of a managed connection: ", e);
132         }
133     }
134 
135 }