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 org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 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 33 /*** 34 * A simple implementation of a ConnectionManager. 35 * An App Server will have a better implementation with pooling and security etc. 36 * 37 * @version $Revision: 1.4 $ 38 */ 39 public class SimpleConnectionManager implements ConnectionManager, ConnectionEventListener { 40 41 private static final Log log = LogFactory.getLog(SimpleConnectionManager.class); 42 43 /*** 44 * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory, javax.resource.spi.ConnectionRequestInfo) 45 */ 46 public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info) throws ResourceException { 47 Subject subject = null; 48 ManagedConnection connection = connectionFactory.createManagedConnection(subject, info); 49 connection.addConnectionEventListener(this); 50 return connection.getConnection(subject, info); 51 } 52 53 /*** 54 * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent) 55 */ 56 public void connectionClosed(ConnectionEvent event) { 57 try { 58 ((ManagedConnection) event.getSource()).cleanup(); 59 } 60 catch (ResourceException e) { 61 log.warn("Error occured during the cleanup of a managed connection: ", e); 62 } 63 try { 64 ((ManagedConnection) event.getSource()).destroy(); 65 } 66 catch (ResourceException e) { 67 log.warn("Error occured during the destruction of a managed connection: ", e); 68 } 69 } 70 71 /*** 72 * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent) 73 */ 74 public void localTransactionStarted(ConnectionEvent event) { 75 } 76 77 /*** 78 * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent) 79 */ 80 public void localTransactionCommitted(ConnectionEvent event) { 81 } 82 83 /*** 84 * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent) 85 */ 86 public void localTransactionRolledback(ConnectionEvent event) { 87 } 88 89 /*** 90 * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent) 91 */ 92 public void connectionErrorOccurred(ConnectionEvent event) { 93 log.warn("Managed connection experiened an error: ", event.getException()); 94 try { 95 ((ManagedConnection) event.getSource()).cleanup(); 96 } 97 catch (ResourceException e) { 98 log.warn("Error occured during the cleanup of a managed connection: ", e); 99 } 100 try { 101 ((ManagedConnection) event.getSource()).destroy(); 102 } 103 catch (ResourceException e) { 104 log.warn("Error occured during the destruction of a managed connection: ", e); 105 } 106 } 107 108 }