001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.activemq.ra;
018
019import javax.resource.ResourceException;
020import javax.resource.spi.ConnectionEvent;
021import javax.resource.spi.ConnectionEventListener;
022import javax.resource.spi.ConnectionManager;
023import javax.resource.spi.ConnectionRequestInfo;
024import javax.resource.spi.ManagedConnection;
025import javax.resource.spi.ManagedConnectionFactory;
026import javax.security.auth.Subject;
027
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * A simple implementation of a ConnectionManager. An Application Server will
033 * have a better implementation with pooling and security etc.
034 * 
035 * 
036 */
037public class SimpleConnectionManager implements ConnectionManager, ConnectionEventListener {
038
039    private static final long serialVersionUID = -7662970495944876239L;
040
041    private static final Logger LOG = LoggerFactory.getLogger(SimpleConnectionManager.class);
042
043    /**
044     * @see javax.resource.spi.ConnectionManager#allocateConnection(javax.resource.spi.ManagedConnectionFactory,
045     *      javax.resource.spi.ConnectionRequestInfo)
046     */
047    public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info) throws ResourceException {
048        Subject subject = null;
049        ManagedConnection connection = connectionFactory.createManagedConnection(subject, info);
050        connection.addConnectionEventListener(this);
051        return connection.getConnection(subject, info);
052    }
053
054    /**
055     * @see javax.resource.spi.ConnectionEventListener#connectionClosed(javax.resource.spi.ConnectionEvent)
056     */
057    public void connectionClosed(ConnectionEvent event) {
058        try {
059            ((ManagedConnection)event.getSource()).cleanup();
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        } catch (ResourceException e) {
066            LOG.warn("Error occured during the destruction of a managed connection: ", e);
067        }
068    }
069
070    /**
071     * @see javax.resource.spi.ConnectionEventListener#localTransactionStarted(javax.resource.spi.ConnectionEvent)
072     */
073    public void localTransactionStarted(ConnectionEvent event) {
074    }
075
076    /**
077     * @see javax.resource.spi.ConnectionEventListener#localTransactionCommitted(javax.resource.spi.ConnectionEvent)
078     */
079    public void localTransactionCommitted(ConnectionEvent event) {
080    }
081
082    /**
083     * @see javax.resource.spi.ConnectionEventListener#localTransactionRolledback(javax.resource.spi.ConnectionEvent)
084     */
085    public void localTransactionRolledback(ConnectionEvent event) {
086    }
087
088    /**
089     * @see javax.resource.spi.ConnectionEventListener#connectionErrorOccurred(javax.resource.spi.ConnectionEvent)
090     */
091    public void connectionErrorOccurred(ConnectionEvent event) {
092        LOG.warn("Managed connection experiened an error: ", event.getException());
093        try {
094            ((ManagedConnection)event.getSource()).cleanup();
095        } catch (ResourceException e) {
096            LOG.warn("Error occured during the cleanup of a managed connection: ", e);
097        }
098        try {
099            ((ManagedConnection)event.getSource()).destroy();
100        } catch (ResourceException e) {
101            LOG.warn("Error occured during the destruction of a managed connection: ", e);
102        }
103    }
104
105}