001    /**
002     *
003     * Copyright 2004-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.ResourceAdapter;
023    import javax.resource.spi.endpoint.MessageEndpointFactory;
024    import javax.transaction.SystemException;
025    import javax.transaction.xa.XAResource;
026    
027    import org.apache.geronimo.transaction.manager.NamedXAResource;
028    import org.apache.geronimo.transaction.manager.ResourceManager;
029    import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;
030    
031    /**
032     * Wrapper for ActivationSpec instances.
033     * The framework assumes all RequiredConfigProperties are of type String, although it
034     * is unclear if this is required by the spec.
035     *
036     * @version $Rev: 356022 $ $Date: 2005-12-11 12:58:34 -0800 (Sun, 11 Dec 2005) $
037     */
038    public class ActivationSpecWrapper implements ResourceManager {
039    
040        protected final ActivationSpec activationSpec;
041    
042        private final ResourceAdapterWrapper resourceAdapterWrapper;
043        private final String containerId;
044    
045        /**
046         * Default constructor required when a class is used as a GBean Endpoint.
047         */
048        public ActivationSpecWrapper() {
049            activationSpec = null;
050            containerId = null;
051            resourceAdapterWrapper = null;
052        }
053    
054        /**
055         * Normal managed constructor.
056         *
057         * @param activationSpecClass Class of admin object to be wrapped.
058         * @throws IllegalAccessException
059         * @throws InstantiationException
060         */
061        public ActivationSpecWrapper(final String activationSpecClass,
062                                     final String containerId,
063                                     final ResourceAdapterWrapper resourceAdapterWrapper,
064                                     final ClassLoader cl) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
065            Class clazz = cl.loadClass(activationSpecClass);
066            this.activationSpec = (ActivationSpec) clazz.newInstance();
067            this.containerId = containerId;
068            this.resourceAdapterWrapper = resourceAdapterWrapper;
069        }
070    
071        /**
072         */
073        public ActivationSpecWrapper(ActivationSpec activationSpec, ResourceAdapterWrapper resourceAdapterWrapper)  {
074            this.activationSpec = activationSpec;
075            this.resourceAdapterWrapper = resourceAdapterWrapper;
076            this.containerId = null;
077        }
078    
079        /**
080         * Returns class of wrapped ActivationSpec.
081         *
082         * @return class of wrapped ActivationSpec
083         */
084    //    public String getActivationSpecClass() {
085    //        return activationSpecClass;
086    //    }
087    
088        public String getContainerId() {
089            return containerId;
090        }
091    
092        public ResourceAdapterWrapper getResourceAdapterWrapper() {
093            return resourceAdapterWrapper;
094        }
095    
096    
097        //GBeanLifecycle implementation
098        public void activate(final MessageEndpointFactory messageEndpointFactory) throws ResourceException {
099            ResourceAdapter resourceAdapter = activationSpec.getResourceAdapter();
100            if (resourceAdapter == null) {
101                resourceAdapterWrapper.registerResourceAdapterAssociation(activationSpec);
102            }
103            resourceAdapterWrapper.endpointActivation(messageEndpointFactory, activationSpec);
104        }
105    
106        public void deactivate(final MessageEndpointFactory messageEndpointFactory) {
107            ResourceAdapter resourceAdapter = activationSpec.getResourceAdapter();
108            if (resourceAdapter != null) {
109                resourceAdapterWrapper.endpointDeactivation(messageEndpointFactory, activationSpec);
110            } else {
111                //this should never happen, activation spec should have been registered with r.a.
112                throw new IllegalStateException("ActivationSpec was never registered with ResourceAdapter");
113            }
114        }
115    
116        //Operations.
117        public NamedXAResource getRecoveryXAResources() throws SystemException {
118            if (resourceAdapterWrapper == null) {
119                throw new IllegalStateException("Attempting to use activation spec when it is not activated");
120            }
121            try {
122                XAResource[] xaResources = resourceAdapterWrapper.getXAResources(new ActivationSpec[]{activationSpec});
123                if (xaResources == null || xaResources.length == 0) {
124                    return null;
125                }
126                return new WrapperNamedXAResource(xaResources[0], containerId);
127            } catch (ResourceException e) {
128                throw (SystemException) new SystemException("Could not get XAResource for recovery for mdb: " + containerId).initCause(e);
129            }
130        }
131    
132        public void returnResource(NamedXAResource xaResource) {
133            //do nothing, no way to return anything.
134        }
135    
136    }