View Javadoc

1   /**
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.connector;
19  
20  import java.util.Map;
21  
22  import javax.resource.ResourceException;
23  import javax.resource.spi.ActivationSpec;
24  import javax.resource.spi.BootstrapContext;
25  import javax.resource.spi.ResourceAdapter;
26  import javax.resource.spi.ResourceAdapterAssociation;
27  import javax.resource.spi.ResourceAdapterInternalException;
28  import javax.resource.spi.endpoint.MessageEndpointFactory;
29  import javax.transaction.SystemException;
30  import javax.transaction.xa.XAResource;
31  
32  import org.apache.geronimo.transaction.manager.NamedXAResource;
33  import org.apache.geronimo.transaction.manager.RecoverableTransactionManager;
34  import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;
35  
36  /**
37   * Dynamic GBean wrapper around a ResourceAdapter object, exposing the config-properties as
38   * GBean attributes.
39   *
40   * @version $Rev: 550546 $ $Date: 2007-06-25 12:52:11 -0400 (Mon, 25 Jun 2007) $
41   */
42  public class ResourceAdapterWrapper implements ResourceAdapter {
43  
44      private final String name;
45  
46      private final String resourceAdapterClass;
47  
48      private final BootstrapContext bootstrapContext;
49  
50      protected final ResourceAdapter resourceAdapter;
51  
52      private final Map<String,String> messageListenerToActivationSpecMap;
53  
54      private final RecoverableTransactionManager transactionManager;
55  
56  
57      /**
58       *  default constructor for enhancement proxy endpoint
59       */
60      public ResourceAdapterWrapper() {
61          this.name = null;
62          this.resourceAdapterClass = null;
63          this.bootstrapContext = null;
64          this.resourceAdapter = null;
65          this.messageListenerToActivationSpecMap = null;
66          this.transactionManager = null;
67      }
68  
69      public ResourceAdapterWrapper(String name,
70              String resourceAdapterClass,
71              Map<String, String> messageListenerToActivationSpecMap,
72              BootstrapContext bootstrapContext,
73              RecoverableTransactionManager transactionManager,
74              ClassLoader cl) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
75          this.name = name;
76          this.resourceAdapterClass = resourceAdapterClass;
77          this.bootstrapContext = bootstrapContext;
78          Class clazz = cl.loadClass(resourceAdapterClass);
79          resourceAdapter = (ResourceAdapter) clazz.newInstance();
80          this.messageListenerToActivationSpecMap = messageListenerToActivationSpecMap;
81          this.transactionManager = transactionManager;
82      }
83      
84      public ResourceAdapterWrapper(String name, ResourceAdapter resourceAdapter, Map<String, String> messageListenerToActivationSpecMap, BootstrapContext bootstrapContext, RecoverableTransactionManager transactionManager) {
85          this.name = name;
86          this.resourceAdapterClass = resourceAdapter.getClass().getName();
87          this.bootstrapContext = bootstrapContext;
88          this.resourceAdapter = resourceAdapter;
89          this.messageListenerToActivationSpecMap = messageListenerToActivationSpecMap;
90          this.transactionManager = transactionManager;
91      }
92  
93      public String getName() {
94          return name;
95      }
96  
97      public String getResourceAdapterClass() {
98          return resourceAdapterClass;
99      }
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 }