001 /**
002 *
003 * Copyright 2004 Hiram Chirino
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.ra;
019
020 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022
023 import javax.resource.ResourceException;
024 import javax.resource.spi.ConnectionEvent;
025 import javax.resource.spi.ConnectionEventListener;
026 import javax.resource.spi.ConnectionManager;
027 import javax.resource.spi.ConnectionRequestInfo;
028 import javax.resource.spi.ManagedConnection;
029 import javax.resource.spi.ManagedConnectionFactory;
030 import javax.security.auth.Subject;
031
032
033 /**
034 * A simple implementation of a ConnectionManager.
035 * An App Server will have a better implementation with pooling and security etc.
036 *
037 * @version $Revision: 1.1.1.1 $
038 */
039 public class DefaultConnectionManager implements ConnectionManager, ConnectionEventListener {
040
041 private static final Log log = LogFactory.getLog(DefaultConnectionManager.class);
042
043 /**
044 * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory, javax.resource.spi.ConnectionRequestInfo)
045 */
046 public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info) throws ResourceException {
047 Subject subject = null;
048 ManagedConnection connection = connectionFactory.createManagedConnection(subject, info);
049 connection.addConnectionEventListener(this);
050 return connection.getConnection(subject, info);
051 }
052
053 /**
054 * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent)
055 */
056 public void connectionClosed(ConnectionEvent event) {
057 try {
058 ((ManagedConnection) event.getSource()).cleanup();
059 }
060 catch (ResourceException e) {
061 log.warn("Error occured during the cleanup of a managed connection: ", e);
062 }
063 try {
064 ((ManagedConnection) event.getSource()).destroy();
065 }
066 catch (ResourceException e) {
067 log.warn("Error occured during the destruction of a managed connection: ", e);
068 }
069 }
070
071 /**
072 * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent)
073 */
074 public void localTransactionStarted(ConnectionEvent event) {
075 }
076
077 /**
078 * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent)
079 */
080 public void localTransactionCommitted(ConnectionEvent event) {
081 }
082
083 /**
084 * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent)
085 */
086 public void localTransactionRolledback(ConnectionEvent event) {
087 }
088
089 /**
090 * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent)
091 */
092 public void connectionErrorOccurred(ConnectionEvent event) {
093 log.warn("Managed connection experiened an error: ", event.getException());
094 try {
095 ((ManagedConnection) event.getSource()).cleanup();
096 }
097 catch (ResourceException e) {
098 log.warn("Error occured during the cleanup of a managed connection: ", e);
099 }
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 }