1 /***
2 *
3 * Copyright 2004 Hiram Chirino
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 package org.codehaus.activemq.ra;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22
23 import javax.resource.ResourceException;
24 import javax.resource.spi.ConnectionEvent;
25 import javax.resource.spi.ConnectionEventListener;
26 import javax.resource.spi.ConnectionManager;
27 import javax.resource.spi.ConnectionRequestInfo;
28 import javax.resource.spi.ManagedConnection;
29 import javax.resource.spi.ManagedConnectionFactory;
30 import javax.security.auth.Subject;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36 /***
37 * A simple implementation of a ConnectionManager that can be extended so that it can
38 * see how the RA connections are interacting with it.
39 *
40 * @version $Revision: 1.1 $
41 */
42 public class ConnectionManagerAdapter implements ConnectionManager, ConnectionEventListener {
43
44 private static final Log log = LogFactory.getLog(ConnectionManagerAdapter.class);
45 ArrayList listners = new ArrayList();
46 ArrayList connections = new ArrayList();
47
48 /***
49 * Adds a listner to all connections created by this connection manager.
50 * This listner will be added to all previously created connections.
51 *
52 * @param l
53 */
54 public void addConnectionEventListener(ConnectionEventListener l ) {
55 for (Iterator iter = connections.iterator(); iter.hasNext();) {
56 ManagedConnection c = (ManagedConnection) iter.next();
57 c.addConnectionEventListener(l);
58 }
59 listners.add(l);
60 }
61
62 /***
63 * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory, javax.resource.spi.ConnectionRequestInfo)
64 */
65 public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info) throws ResourceException {
66 Subject subject = null;
67 ManagedConnection connection = connectionFactory.createManagedConnection(subject, info);
68 connection.addConnectionEventListener(this);
69 for (Iterator iter = listners.iterator(); iter.hasNext();) {
70 ConnectionEventListener l = (ConnectionEventListener) iter.next();
71 connection.addConnectionEventListener(l);
72 }
73 connections.add(connection);
74 return connection.getConnection(subject, info);
75 }
76
77 /***
78 * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent)
79 */
80 public void connectionClosed(ConnectionEvent event) {
81 connections.remove(event.getSource());
82 try {
83 ((ManagedConnection)event.getSource()).cleanup();
84 } catch (ResourceException e) {
85 log.warn("Error occured during the cleanup of a managed connection: ",e);
86 }
87 try {
88 ((ManagedConnection)event.getSource()).destroy();
89 } catch (ResourceException e) {
90 log.warn("Error occured during the destruction of a managed connection: ",e);
91 }
92 }
93
94 /***
95 * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent)
96 */
97 public void localTransactionStarted(ConnectionEvent event) {
98 }
99
100 /***
101 * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent)
102 */
103 public void localTransactionCommitted(ConnectionEvent event) {
104 }
105
106 /***
107 * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent)
108 */
109 public void localTransactionRolledback(ConnectionEvent event) {
110 }
111
112 /***
113 * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent)
114 */
115 public void connectionErrorOccurred(ConnectionEvent event) {
116 log.warn("Managed connection experiened an error: ",event.getException());
117 try {
118 ((ManagedConnection)event.getSource()).cleanup();
119 } catch (ResourceException e) {
120 log.warn("Error occured during the cleanup of a managed connection: ",e);
121 }
122 try {
123 ((ManagedConnection)event.getSource()).destroy();
124 } catch (ResourceException e) {
125 log.warn("Error occured during the destruction of a managed connection: ",e);
126 }
127 }
128
129 }