001    /**
002     *
003     * Copyright 2003-2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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 javax.resource.ResourceException;
021    import javax.resource.spi.ActivationSpec;
022    import javax.resource.spi.BootstrapContext;
023    import javax.resource.spi.ResourceAdapter;
024    import javax.resource.spi.ResourceAdapterAssociation;
025    import javax.resource.spi.ResourceAdapterInternalException;
026    import javax.resource.spi.endpoint.MessageEndpointFactory;
027    import javax.transaction.xa.XAResource;
028    
029    /**
030     * Dynamic GBean wrapper around a ResourceAdapter object, exposing the config-properties as
031     * GBean attributes.
032     *
033     * @version $Rev: 430508 $ $Date: 2006-08-10 12:56:47 -0700 (Thu, 10 Aug 2006) $
034     */
035    public class ResourceAdapterWrapper implements ResourceAdapter {
036    
037        private final String resourceAdapterClass;
038    
039        private final BootstrapContext bootstrapContext;
040    
041        protected final ResourceAdapter resourceAdapter;
042    
043    
044        /**
045         *  default constructor for enhancement proxy endpoint
046         */
047        public ResourceAdapterWrapper() {
048            this.resourceAdapterClass = null;
049            this.bootstrapContext = null;
050            this.resourceAdapter = null;
051        }
052    
053        public ResourceAdapterWrapper(String resourceAdapterClass,
054                BootstrapContext bootstrapContext,
055                ClassLoader cl) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
056            this.resourceAdapterClass = resourceAdapterClass;
057            this.bootstrapContext = bootstrapContext;
058            Class clazz = cl.loadClass(resourceAdapterClass);
059            resourceAdapter = (ResourceAdapter) clazz.newInstance();
060        }
061        
062        public ResourceAdapterWrapper(ResourceAdapter resourceAdapter, BootstrapContext bootstrapContext) {
063            this.resourceAdapterClass = resourceAdapter.getClass().getName();
064            this.bootstrapContext = bootstrapContext;
065            this.resourceAdapter = resourceAdapter;
066        }
067    
068        public String getResourceAdapterClass() {
069            return resourceAdapterClass;
070        }
071    
072        public void registerResourceAdapterAssociation(final ResourceAdapterAssociation resourceAdapterAssociation) throws ResourceException {
073            resourceAdapterAssociation.setResourceAdapter(resourceAdapter);
074        }
075    
076        public void start(BootstrapContext ctx) throws ResourceAdapterInternalException {
077            throw new IllegalStateException("Don't call this");
078        }
079    
080        public void stop() {
081            throw new IllegalStateException("Don't call this");
082        }
083    
084        //endpoint handling
085        public void endpointActivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) throws ResourceException {
086            resourceAdapter.endpointActivation(messageEndpointFactory, activationSpec);
087        }
088    
089        public void endpointDeactivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) {
090            resourceAdapter.endpointDeactivation(messageEndpointFactory, activationSpec);
091        }
092    
093        public XAResource[] getXAResources(ActivationSpec[] specs) throws ResourceException {
094            return resourceAdapter.getXAResources(specs);
095        }
096    
097        public void doStart() throws Exception {
098            resourceAdapter.start(bootstrapContext);
099        }
100    
101        public void doStop() {
102            resourceAdapter.stop();
103        }
104    
105        public void doFail() {
106            resourceAdapter.stop();
107        }
108    
109    }