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     */
017    
018    package org.apache.geronimo.connector.outbound;
019    
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.Collections;
023    import java.util.List;
024    
025    import javax.resource.spi.ConnectionEvent;
026    import javax.resource.spi.ConnectionEventListener;
027    
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * ConnectionEventListener.java
033     *
034     *
035     * Created: Thu Oct  2 14:57:43 2003
036     *
037     * @version 1.0
038     */
039    public class GeronimoConnectionEventListener implements ConnectionEventListener {
040    
041        private static Log log = LogFactory.getLog(GeronimoConnectionEventListener.class.getName());
042    
043        private final ManagedConnectionInfo managedConnectionInfo;
044        private final ConnectionInterceptor stack;
045        private final List<ConnectionInfo> connectionInfos = new ArrayList<ConnectionInfo>();
046        private boolean errorOccurred = false;
047    
048        public GeronimoConnectionEventListener(
049                final ConnectionInterceptor stack,
050                final ManagedConnectionInfo managedConnectionInfo) {
051            this.stack = stack;
052            this.managedConnectionInfo = managedConnectionInfo;
053        }
054    
055        public void connectionClosed(ConnectionEvent connectionEvent) {
056            if (connectionEvent.getSource() != managedConnectionInfo.getManagedConnection()) {
057                throw new IllegalArgumentException(
058                        "ConnectionClosed event received from wrong ManagedConnection. Expected "
059                        + managedConnectionInfo.getManagedConnection()
060                        + ", actual "
061                        + connectionEvent.getSource());
062            }
063            if (log.isTraceEnabled()) {
064                log.trace("connectionClosed called with " + connectionEvent.getConnectionHandle() + " for MCI: " + managedConnectionInfo + " and MC: " + managedConnectionInfo.getManagedConnection());
065            }
066            ConnectionInfo ci = new ConnectionInfo(managedConnectionInfo);
067            ci.setConnectionHandle(connectionEvent.getConnectionHandle());
068            try {
069                stack.returnConnection(ci, ConnectionReturnAction.RETURN_HANDLE);
070            } catch (Throwable e) {
071                if (log.isTraceEnabled()) {
072                    log.trace("connectionClosed failed with " + connectionEvent.getConnectionHandle() + " for MCI: " + managedConnectionInfo + " and MC: " + managedConnectionInfo.getManagedConnection(), e);
073                }
074                if (e instanceof Error) {
075                    throw (Error)e;
076                }
077            }
078        }
079    
080        public void connectionErrorOccurred(ConnectionEvent connectionEvent) {
081            if (connectionEvent.getSource() != managedConnectionInfo.getManagedConnection()) {
082                throw new IllegalArgumentException(
083                        "ConnectionError event received from wrong ManagedConnection. Expected "
084                        + managedConnectionInfo.getManagedConnection()
085                        + ", actual "
086                        + connectionEvent.getSource());
087            }
088            log.warn("connectionErrorOccurred called with " + connectionEvent.getConnectionHandle(), connectionEvent.getException());
089            boolean errorOccurred = this.errorOccurred;
090            this.errorOccurred = true;
091            if (!errorOccurred) {
092                ConnectionInfo ci = new ConnectionInfo(managedConnectionInfo);
093                ci.setConnectionHandle(connectionEvent.getConnectionHandle());
094                stack.returnConnection(ci, ConnectionReturnAction.DESTROY);
095            }
096        }
097    
098        public void localTransactionStarted(ConnectionEvent event) {
099            //TODO implement this method
100        }
101    
102        /**
103         * The <code>localTransactionCommitted</code> method
104         *
105         * @param event a <code>ConnectionEvent</code> value
106         * todo implement this method
107         */
108        public void localTransactionCommitted(ConnectionEvent event) {
109        }
110    
111        /**
112         * The <code>localTransactionRolledback</code> method
113         *
114         * @param event a <code>ConnectionEvent</code> value
115         * todo implement this method
116         */
117        public void localTransactionRolledback(ConnectionEvent event) {
118        }
119    
120        public void addConnectionInfo(ConnectionInfo connectionInfo) {
121            assert connectionInfo.getConnectionHandle() != null;
122            connectionInfos.add(connectionInfo);
123        }
124    
125        public void removeConnectionInfo(ConnectionInfo connectionInfo) {
126            assert connectionInfo.getConnectionHandle() != null;
127            connectionInfos.remove(connectionInfo);
128        }
129    
130        public boolean hasConnectionInfos() {
131            return !connectionInfos.isEmpty();
132        }
133    
134        public void clearConnectionInfos() {
135            connectionInfos.clear();
136        }
137    
138        public boolean hasConnectionInfo(ConnectionInfo connectionInfo) {
139            return connectionInfos.contains(connectionInfo);
140        }
141    
142        public boolean isFirstConnectionInfo(ConnectionInfo connectionInfo) {
143            return !connectionInfos.isEmpty() && connectionInfos.get(0) == connectionInfo;
144        }
145    
146        public Collection<ConnectionInfo> getConnectionInfos() {
147            return Collections.unmodifiableCollection(connectionInfos);
148        }
149    
150    }