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