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;
019    
020    import java.util.Map;
021    
022    import javax.resource.ResourceException;
023    import javax.resource.spi.ActivationSpec;
024    import javax.resource.spi.BootstrapContext;
025    import javax.resource.spi.ResourceAdapter;
026    import javax.resource.spi.ResourceAdapterAssociation;
027    import javax.resource.spi.ResourceAdapterInternalException;
028    import javax.resource.spi.endpoint.MessageEndpointFactory;
029    import javax.transaction.SystemException;
030    import javax.transaction.xa.XAResource;
031    
032    import org.apache.geronimo.transaction.manager.NamedXAResource;
033    import org.apache.geronimo.transaction.manager.RecoverableTransactionManager;
034    import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;
035    
036    /**
037     * Dynamic GBean wrapper around a ResourceAdapter object, exposing the config-properties as
038     * GBean attributes.
039     *
040     * @version $Rev: 550546 $ $Date: 2007-06-25 12:52:11 -0400 (Mon, 25 Jun 2007) $
041     */
042    public class ResourceAdapterWrapper implements ResourceAdapter {
043    
044        private final String name;
045    
046        private final String resourceAdapterClass;
047    
048        private final BootstrapContext bootstrapContext;
049    
050        protected final ResourceAdapter resourceAdapter;
051    
052        private final Map<String,String> messageListenerToActivationSpecMap;
053    
054        private final RecoverableTransactionManager transactionManager;
055    
056    
057        /**
058         *  default constructor for enhancement proxy endpoint
059         */
060        public ResourceAdapterWrapper() {
061            this.name = null;
062            this.resourceAdapterClass = null;
063            this.bootstrapContext = null;
064            this.resourceAdapter = null;
065            this.messageListenerToActivationSpecMap = null;
066            this.transactionManager = null;
067        }
068    
069        public ResourceAdapterWrapper(String name,
070                String resourceAdapterClass,
071                Map<String, String> messageListenerToActivationSpecMap,
072                BootstrapContext bootstrapContext,
073                RecoverableTransactionManager transactionManager,
074                ClassLoader cl) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
075            this.name = name;
076            this.resourceAdapterClass = resourceAdapterClass;
077            this.bootstrapContext = bootstrapContext;
078            Class clazz = cl.loadClass(resourceAdapterClass);
079            resourceAdapter = (ResourceAdapter) clazz.newInstance();
080            this.messageListenerToActivationSpecMap = messageListenerToActivationSpecMap;
081            this.transactionManager = transactionManager;
082        }
083        
084        public ResourceAdapterWrapper(String name, ResourceAdapter resourceAdapter, Map<String, String> messageListenerToActivationSpecMap, BootstrapContext bootstrapContext, RecoverableTransactionManager transactionManager) {
085            this.name = name;
086            this.resourceAdapterClass = resourceAdapter.getClass().getName();
087            this.bootstrapContext = bootstrapContext;
088            this.resourceAdapter = resourceAdapter;
089            this.messageListenerToActivationSpecMap = messageListenerToActivationSpecMap;
090            this.transactionManager = transactionManager;
091        }
092    
093        public String getName() {
094            return name;
095        }
096    
097        public String getResourceAdapterClass() {
098            return resourceAdapterClass;
099        }
100    
101        public Map<String,String> getMessageListenerToActivationSpecMap() {
102            return messageListenerToActivationSpecMap;
103        }
104    
105        public ResourceAdapter getResourceAdapter() {
106            return resourceAdapter;
107        }
108    
109        public void registerResourceAdapterAssociation(final ResourceAdapterAssociation resourceAdapterAssociation) throws ResourceException {
110            resourceAdapterAssociation.setResourceAdapter(resourceAdapter);
111        }
112    
113        public void start(BootstrapContext ctx) throws ResourceAdapterInternalException {
114            throw new IllegalStateException("Don't call this");
115        }
116    
117        public void stop() {
118            throw new IllegalStateException("Don't call this");
119        }
120    
121        //endpoint handling
122        public void endpointActivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) throws ResourceException {
123            resourceAdapter.endpointActivation(messageEndpointFactory, activationSpec);
124        }
125    
126        public void doRecovery(ActivationSpec activationSpec, String containerId) {
127            try {
128                XAResource[] xaResources = getXAResources(new ActivationSpec[]{activationSpec});
129                if (xaResources == null || xaResources.length == 0) {
130                    return;
131                }
132                NamedXAResource xaResource = new WrapperNamedXAResource(xaResources[0], containerId);
133                transactionManager.recoverResourceManager(xaResource);
134            } catch (ResourceException e) {
135                transactionManager.recoveryError((SystemException) new SystemException("Could not get XAResource for recovery for mdb: " + containerId).initCause(e));
136            }
137    
138        }
139    
140        public void endpointDeactivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) {
141            resourceAdapter.endpointDeactivation(messageEndpointFactory, activationSpec);
142        }
143    
144        public XAResource[] getXAResources(ActivationSpec[] specs) throws ResourceException {
145            return resourceAdapter.getXAResources(specs);
146        }
147    
148        public void doStart() throws Exception {
149            resourceAdapter.start(bootstrapContext);
150        }
151    
152        public void doStop() {
153            resourceAdapter.stop();
154        }
155    
156        public void doFail() {
157            resourceAdapter.stop();
158        }
159    
160    }