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