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    
022    /**
023     * ConnectionHandleInterceptor.java
024     *
025     *
026     * @version $Rev: 476049 $ $Date: 2006-11-17 05:35:17 +0100 (Fri, 17 Nov 2006) $
027     */
028    public class ConnectionHandleInterceptor implements ConnectionInterceptor {
029    
030        private final ConnectionInterceptor next;
031    
032        public ConnectionHandleInterceptor(ConnectionInterceptor next) {
033            this.next = next;
034        }
035    
036        /**
037         * in: connectionInfo not null, managedConnectionInfo not null. ManagedConnection may or may not be null.  ConnectionHandle may or may not be null
038         * out: managedConnection not null. connection handle not null. managedConnectionInfo has connection handle registered.  Connection handle is associated with ManagedConnection.
039         * @param connectionInfo
040         * @throws ResourceException
041         */
042        public void getConnection(ConnectionInfo connectionInfo) throws ResourceException {
043            next.getConnection(connectionInfo);
044            ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
045            if (connectionInfo.getConnectionHandle() == null) {
046                connectionInfo.setConnectionHandle(
047                        mci.getManagedConnection().getConnection(
048                                mci.getSubject(),
049                                mci.getConnectionRequestInfo()));
050                mci.addConnectionHandle(connectionInfo);
051    
052            } else if (!mci.hasConnectionInfo(connectionInfo)) {
053                mci.getManagedConnection().associateConnection(
054                        connectionInfo.getConnectionHandle());
055                mci.addConnectionHandle(connectionInfo);
056            }
057            connectionInfo.setTrace();
058        }
059    
060        /**
061         *  in: connectionInfo not null, managedConnectionInfo not null, managedConnection not null.  Handle can be null if mc is being destroyed from pool.
062         * out: managedCOnnectionInfo null, handle not in mci.handles.
063         * @param connectionInfo
064         * @param connectionReturnAction
065         */
066        public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) {
067            if (connectionInfo.getConnectionHandle() != null) {
068                connectionInfo.getManagedConnectionInfo().removeConnectionHandle(
069                        connectionInfo);
070            }
071            next.returnConnection(connectionInfo, connectionReturnAction);
072        }
073    
074        public void destroy() {
075            next.destroy();
076        }
077    }