KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ConnectionManagerAdapter


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

18 package org.apache.activemq.ra;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.resource.ResourceException JavaDoc;
24 import javax.resource.spi.ConnectionEvent JavaDoc;
25 import javax.resource.spi.ConnectionEventListener JavaDoc;
26 import javax.resource.spi.ConnectionManager JavaDoc;
27 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
28 import javax.resource.spi.ManagedConnection JavaDoc;
29 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
30 import javax.security.auth.Subject JavaDoc;
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$
41  */

42 public class ConnectionManagerAdapter implements ConnectionManager JavaDoc, ConnectionEventListener JavaDoc {
43     
44     private static final long serialVersionUID = 5205646563916645831L;
45     
46     private static final Log log = LogFactory.getLog(ConnectionManagerAdapter.class);
47     ArrayList JavaDoc listners = new ArrayList JavaDoc();
48     ArrayList JavaDoc connections = new ArrayList JavaDoc();
49     
50     /**
51      * Adds a listner to all connections created by this connection manager.
52      * This listner will be added to all previously created connections.
53      *
54      * @param l
55      */

56     public void addConnectionEventListener(ConnectionEventListener JavaDoc l ) {
57         for (Iterator JavaDoc iter = connections.iterator(); iter.hasNext();) {
58             ManagedConnection JavaDoc c = (ManagedConnection JavaDoc) iter.next();
59             c.addConnectionEventListener(l);
60         }
61         listners.add(l);
62     }
63     
64     /**
65      * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory, javax.resource.spi.ConnectionRequestInfo)
66      */

67     public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc connectionFactory, ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
68         Subject JavaDoc subject = null;
69         ManagedConnection JavaDoc connection = connectionFactory.createManagedConnection(subject, info);
70         connection.addConnectionEventListener(this);
71         for (Iterator JavaDoc iter = listners.iterator(); iter.hasNext();) {
72             ConnectionEventListener JavaDoc l = (ConnectionEventListener JavaDoc) iter.next();
73             connection.addConnectionEventListener(l);
74         }
75         connections.add(connection);
76         return connection.getConnection(subject, info);
77     }
78
79     /**
80      * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent)
81      */

82     public void connectionClosed(ConnectionEvent JavaDoc event) {
83         connections.remove(event.getSource());
84         try {
85             ((ManagedConnection JavaDoc)event.getSource()).cleanup();
86         } catch (ResourceException JavaDoc e) {
87             log.warn("Error occured during the cleanup of a managed connection: ",e);
88         }
89         try {
90             ((ManagedConnection JavaDoc)event.getSource()).destroy();
91         } catch (ResourceException JavaDoc e) {
92             log.warn("Error occured during the destruction of a managed connection: ",e);
93         }
94     }
95
96     /**
97      * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent)
98      */

99     public void localTransactionStarted(ConnectionEvent JavaDoc event) {
100     }
101
102     /**
103      * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent)
104      */

105     public void localTransactionCommitted(ConnectionEvent JavaDoc event) {
106     }
107
108     /**
109      * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent)
110      */

111     public void localTransactionRolledback(ConnectionEvent JavaDoc event) {
112     }
113
114     /**
115      * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent)
116      */

117     public void connectionErrorOccurred(ConnectionEvent JavaDoc event) {
118         log.warn("Managed connection experiened an error: ",event.getException());
119         try {
120             ((ManagedConnection JavaDoc)event.getSource()).cleanup();
121         } catch (ResourceException JavaDoc e) {
122             log.warn("Error occured during the cleanup of a managed connection: ",e);
123         }
124         try {
125             ((ManagedConnection JavaDoc)event.getSource()).destroy();
126         } catch (ResourceException JavaDoc e) {
127             log.warn("Error occured during the destruction of a managed connection: ",e);
128         }
129     }
130
131 }
132
Popular Tags