View Javadoc

1   /*
2    * $Id: MuleResourceAdapter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.ra;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.ConfigurationBuilder;
15  import org.mule.config.ConfigurationException;
16  import org.mule.config.ThreadingProfile;
17  import org.mule.impl.MuleDescriptor;
18  import org.mule.impl.endpoint.MuleEndpointURI;
19  import org.mule.providers.AbstractConnector;
20  import org.mule.providers.service.TransportFactory;
21  import org.mule.umo.UMODescriptor;
22  import org.mule.umo.UMOException;
23  import org.mule.umo.endpoint.UMOEndpoint;
24  import org.mule.umo.endpoint.UMOEndpointURI;
25  import org.mule.umo.manager.UMOManager;
26  import org.mule.umo.manager.UMOWorkManager;
27  import org.mule.util.ClassUtils;
28  
29  import java.io.IOException;
30  import java.io.ObjectInputStream;
31  import java.io.Serializable;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import javax.resource.NotSupportedException;
36  import javax.resource.ResourceException;
37  import javax.resource.spi.ActivationSpec;
38  import javax.resource.spi.BootstrapContext;
39  import javax.resource.spi.ResourceAdapter;
40  import javax.resource.spi.ResourceAdapterInternalException;
41  import javax.resource.spi.endpoint.MessageEndpoint;
42  import javax.resource.spi.endpoint.MessageEndpointFactory;
43  import javax.transaction.xa.XAResource;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <code>MuleResourceAdapter</code> TODO
50   */
51  public class MuleResourceAdapter implements ResourceAdapter, Serializable
52  {
53      /**
54       * Serial version
55       */
56      private static final long serialVersionUID = 5727648958127416509L;
57  
58      /**
59       * logger used by this class
60       */
61      protected transient Log logger = LogFactory.getLog(this.getClass());
62  
63      private transient UMOManager manager;
64  
65      private transient BootstrapContext bootstrapContext;
66      private MuleConnectionRequestInfo info = new MuleConnectionRequestInfo();
67      private final Map endpoints = new HashMap();
68  
69      public MuleResourceAdapter()
70      {
71          MuleManager.getConfiguration().setModelType(JcaModel.JCA_MODEL_TYPE);
72      }
73  
74      private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException
75      {
76          ois.defaultReadObject();
77          this.logger = LogFactory.getLog(this.getClass());
78          this.manager = MuleManager.getInstance();
79      }
80  
81      /**
82       * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
83       */
84      public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException
85      {
86          this.bootstrapContext = bootstrapContext;
87          if (info.getConfigurations() != null)
88          {
89              if (MuleManager.isInstanciated())
90              {
91                  throw new ResourceAdapterInternalException(
92                      "A manager is already configured, cannot configure a new one using the configurations set on the Resource Adapter");
93              }
94              else
95              {
96                  ConfigurationBuilder builder = null;
97                  try
98                  {
99                      builder = (ConfigurationBuilder)ClassUtils.instanciateClass(
100                         info.getConfigurationBuilder(), ClassUtils.NO_ARGS);
101 
102                 }
103                 catch (Exception e)
104                 {
105                     throw new ResourceAdapterInternalException(
106                         "Failed to instanciate configurationBuilder class: " + info.getConfigurationBuilder(),
107                         e);
108                 }
109 
110                 try
111                 {
112 
113                     manager = builder.configure(info.getConfigurations(), null);
114                 }
115                 catch (ConfigurationException e)
116                 {
117                     throw new ResourceAdapterInternalException("Failed to load configurations: "
118                                                                + info.getConfigurations(), e);
119                 }
120             }
121         }
122         manager = MuleManager.getInstance();
123     }
124 
125     /**
126      * @see javax.resource.spi.ResourceAdapter#stop()
127      */
128     public void stop()
129     {
130         manager.dispose();
131         manager = null;
132         bootstrapContext = null;
133     }
134 
135     /**
136      * @return the bootstrap context for this adapter
137      */
138     public BootstrapContext getBootstrapContext()
139     {
140         return bootstrapContext;
141     }
142 
143     /**
144      * @see javax.resource.spi.ResourceAdapter#endpointActivation(javax.resource.spi.endpoint.MessageEndpointFactory,
145      *      javax.resource.spi.ActivationSpec)
146      */
147     public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec)
148         throws ResourceException
149     {
150         if (activationSpec.getResourceAdapter() != this)
151         {
152             throw new ResourceException("ActivationSpec not initialized with this ResourceAdapter instance");
153         }
154 
155         if (activationSpec.getClass().equals(MuleActivationSpec.class))
156         {
157 
158             try
159             {
160                 UMOEndpointURI uri = new MuleEndpointURI(((MuleActivationSpec)activationSpec).getEndpoint());
161                 UMOEndpoint endpoint = TransportFactory.createEndpoint(uri,
162                     UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
163 
164                 ((AbstractConnector)endpoint.getConnector()).getReceiverThreadingProfile()
165                     .setWorkManagerFactory(new ThreadingProfile.WorkManagerFactory()
166                     {
167                         public UMOWorkManager createWorkManager(ThreadingProfile profile, String name)
168                         {
169                             return new DelegateWorkManager(bootstrapContext.getWorkManager());
170 
171                         }
172                     });
173                 // TODO manage transactions
174                 MessageEndpoint messageEndpoint = null;
175                 messageEndpoint = endpointFactory.createEndpoint(null);
176 
177                 String name = "JcaComponent#" + messageEndpoint.hashCode();
178                 MuleDescriptor descriptor = new MuleDescriptor(name);
179                 descriptor.getInboundRouter().addEndpoint(endpoint);
180                 descriptor.setImplementationInstance(messageEndpoint);
181                 MuleManager.getInstance().lookupModel(JcaModel.JCA_MODEL_TYPE).registerComponent(descriptor);
182 
183                 MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec)activationSpec);
184 
185                 endpoints.put(key, descriptor);
186             }
187             catch (Exception e)
188             {
189                 logger.error(e.getMessage(), e);
190             }
191         }
192         else
193         {
194             throw new NotSupportedException("That type of ActicationSpec not supported: "
195                                             + activationSpec.getClass());
196         }
197 
198     }
199 
200     /**
201      * @see javax.resource.spi.ResourceAdapter#endpointDeactivation(javax.resource.spi.endpoint.MessageEndpointFactory,
202      *      javax.resource.spi.ActivationSpec)
203      */
204     public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec)
205     {
206 
207         if (activationSpec.getClass().equals(MuleActivationSpec.class))
208         {
209             MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec)activationSpec);
210             UMODescriptor descriptor = (UMODescriptor)endpoints.get(key);
211             if (descriptor == null)
212             {
213                 logger.warn("No endpoint was registered with key: " + key);
214                 return;
215             }
216             try
217             {
218                 manager.lookupModel(JcaModel.JCA_MODEL_TYPE).unregisterComponent(descriptor);
219             }
220             catch (UMOException e)
221             {
222                 logger.error(e.getMessage(), e);
223             }
224 
225         }
226 
227     }
228 
229     /**
230      * We only connect to one resource manager per ResourceAdapter instance, so any
231      * ActivationSpec will return the same XAResource.
232      * 
233      * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[])
234      */
235     public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException
236     {
237         return new XAResource[]{};
238     }
239 
240     /**
241      * @return
242      */
243     public String getPassword()
244     {
245         return info.getPassword();
246     }
247 
248     /**
249      * @return
250      */
251     public String getConfigurations()
252     {
253         return info.getConfigurations();
254     }
255 
256     /**
257      * @return
258      */
259     public String getUserName()
260     {
261         return info.getUserName();
262     }
263 
264     /**
265      * @param password
266      */
267     public void setPassword(String password)
268     {
269         info.setPassword(password);
270     }
271 
272     /**
273      * @param configurations
274      */
275     public void setConfigurations(String configurations)
276     {
277         info.setConfigurations(configurations);
278     }
279 
280     /**
281      * @param userid
282      */
283     public void setUserName(String userid)
284     {
285         info.setUserName(userid);
286     }
287 
288     public String getConfigurationBuilder()
289     {
290         return info.getConfigurationBuilder();
291     }
292 
293     public void setConfigurationBuilder(String configbuilder)
294     {
295         info.setConfigurationBuilder(configbuilder);
296     }
297 
298     /**
299      * @return Returns the info.
300      */
301     public MuleConnectionRequestInfo getInfo()
302     {
303         return info;
304     }
305 
306     public boolean equals(Object o)
307     {
308         if (this == o)
309         {
310             return true;
311         }
312         if (!(o instanceof MuleResourceAdapter))
313         {
314             return false;
315         }
316 
317         final MuleResourceAdapter muleResourceAdapter = (MuleResourceAdapter)o;
318 
319         if (info != null ? !info.equals(muleResourceAdapter.info) : muleResourceAdapter.info != null)
320         {
321             return false;
322         }
323 
324         return true;
325     }
326 
327     public int hashCode()
328     {
329         return (info != null ? info.hashCode() : 0);
330     }
331 
332 }