KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
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
33 /**
34  * A simple implementation of a ConnectionManager.
35  * An Application Server will have a better implementation with pooling and security etc.
36  *
37  * @version $Revision$
38  */

39 public class SimpleConnectionManager implements ConnectionManager JavaDoc, ConnectionEventListener JavaDoc {
40
41     private static final long serialVersionUID = -7662970495944876239L;
42     
43     private static final Log log = LogFactory.getLog(SimpleConnectionManager.class);
44
45     /**
46      * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory, javax.resource.spi.ConnectionRequestInfo)
47      */

48     public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc connectionFactory, ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
49         Subject JavaDoc subject = null;
50         ManagedConnection JavaDoc connection = connectionFactory.createManagedConnection(subject, info);
51         connection.addConnectionEventListener(this);
52         return connection.getConnection(subject, info);
53     }
54
55     /**
56      * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent)
57      */

58     public void connectionClosed(ConnectionEvent JavaDoc event) {
59         try {
60             ((ManagedConnection JavaDoc) event.getSource()).cleanup();
61         }
62         catch (ResourceException JavaDoc e) {
63             log.warn("Error occured during the cleanup of a managed connection: ", e);
64         }
65         try {
66             ((ManagedConnection JavaDoc) event.getSource()).destroy();
67         }
68         catch (ResourceException JavaDoc e) {
69             log.warn("Error occured during the destruction of a managed connection: ", e);
70         }
71     }
72
73     /**
74      * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent)
75      */

76     public void localTransactionStarted(ConnectionEvent JavaDoc event) {
77     }
78
79     /**
80      * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent)
81      */

82     public void localTransactionCommitted(ConnectionEvent JavaDoc event) {
83     }
84
85     /**
86      * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent)
87      */

88     public void localTransactionRolledback(ConnectionEvent JavaDoc event) {
89     }
90
91     /**
92      * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent)
93      */

94     public void connectionErrorOccurred(ConnectionEvent JavaDoc event) {
95         log.warn("Managed connection experiened an error: ", event.getException());
96         try {
97             ((ManagedConnection JavaDoc) event.getSource()).cleanup();
98         }
99         catch (ResourceException JavaDoc e) {
100             log.warn("Error occured during the cleanup of a managed connection: ", e);
101         }
102         try {
103             ((ManagedConnection JavaDoc) event.getSource()).destroy();
104         }
105         catch (ResourceException JavaDoc e) {
106             log.warn("Error occured during the destruction of a managed connection: ", e);
107         }
108     }
109
110 }
111
Popular Tags