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