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 javax.resource.ResourceException;
021    import javax.resource.spi.ManagedConnection;
022    
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    
026    
027    /**
028     * MCFConnectionInterceptor.java
029     *
030     *
031     * @version $Rev: 723385 $ $Date: 2008-12-04 12:55:02 -0500 (Thu, 04 Dec 2008) $
032     */
033    public class MCFConnectionInterceptor implements ConnectionInterceptor {
034    
035        protected static final Logger log = LoggerFactory.getLogger(MCFConnectionInterceptor.class.getName());
036    
037        private ConnectionInterceptor stack;
038    
039        public MCFConnectionInterceptor() {
040        }
041    
042        public void getConnection(ConnectionInfo connectionInfo) throws ResourceException {
043            ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
044            if (mci.getManagedConnection() != null) {
045                return;
046            }
047    
048            try {
049                ManagedConnection mc = mci.getManagedConnectionFactory().createManagedConnection(
050                        mci.getSubject(),
051                        mci.getConnectionRequestInfo());
052                mci.setManagedConnection(mc);
053                GeronimoConnectionEventListener listener = new GeronimoConnectionEventListener(stack, mci);
054                mci.setConnectionEventListener(listener);
055                mc.addConnectionEventListener(listener);
056            } catch (ResourceException re) {
057                log.error("Error occurred creating ManagedConnection for " + connectionInfo, re);
058                throw re;
059            }
060        }
061    
062        public void returnConnection(
063                ConnectionInfo connectionInfo,
064                ConnectionReturnAction connectionReturnAction) {
065            ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
066            ManagedConnection mc = mci.getManagedConnection();
067            try {
068                mc.destroy();
069            } catch (ResourceException e) {
070                //log and forget
071            } catch (Error e) {
072                throw e;
073            } catch (Throwable t) {
074                //log and forget
075            }
076        }
077    
078        public void destroy() {
079            // MCF is the "tail" of the stack. So, we're all done...
080        }
081    
082        public void setStack(ConnectionInterceptor stack) {
083            this.stack = stack;
084        }
085    
086    }