KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ra > MuleResourceAdapter


1 /*
2  * $Id: MuleResourceAdapter.java 4310 2006-12-19 12:34:06Z lajos $
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 MuleSource MPL
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 java.io.IOException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import javax.resource.NotSupportedException JavaDoc;
20 import javax.resource.ResourceException JavaDoc;
21 import javax.resource.spi.ActivationSpec JavaDoc;
22 import javax.resource.spi.BootstrapContext JavaDoc;
23 import javax.resource.spi.ResourceAdapter JavaDoc;
24 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
25 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
26 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
27 import javax.transaction.xa.XAResource JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.mule.MuleManager;
32 import org.mule.config.ConfigurationBuilder;
33 import org.mule.config.ConfigurationException;
34 import org.mule.config.ThreadingProfile;
35 import org.mule.impl.MuleDescriptor;
36 import org.mule.impl.endpoint.MuleEndpointURI;
37 import org.mule.providers.AbstractConnector;
38 import org.mule.providers.service.ConnectorFactory;
39 import org.mule.umo.UMODescriptor;
40 import org.mule.umo.UMOException;
41 import org.mule.umo.endpoint.UMOEndpoint;
42 import org.mule.umo.endpoint.UMOEndpointURI;
43 import org.mule.umo.manager.UMOManager;
44 import org.mule.umo.manager.UMOWorkManager;
45 import org.mule.util.ClassUtils;
46
47 /**
48  * <code>MuleResourceAdapter</code> TODO
49  */

50 public class MuleResourceAdapter implements ResourceAdapter JavaDoc, Serializable JavaDoc
51 {
52     /**
53      * Serial version
54      */

55     private static final long serialVersionUID = 5727648958127416509L;
56
57     /**
58      * logger used by this class
59      */

60     protected transient Log logger = LogFactory.getLog(this.getClass());
61
62     private transient UMOManager manager;
63
64     private transient BootstrapContext JavaDoc bootstrapContext;
65     private MuleConnectionRequestInfo info = new MuleConnectionRequestInfo();
66     private final Map JavaDoc endpoints = new HashMap JavaDoc();
67
68     public MuleResourceAdapter()
69     {
70         MuleManager.getConfiguration().setModelType("jca");
71     }
72
73     private void readObject(ObjectInputStream JavaDoc ois) throws ClassNotFoundException JavaDoc, IOException JavaDoc
74     {
75         ois.defaultReadObject();
76         this.logger = LogFactory.getLog(this.getClass());
77         this.manager = MuleManager.getInstance();
78     }
79
80     /**
81      * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
82      */

83     public void start(BootstrapContext JavaDoc bootstrapContext) throws ResourceAdapterInternalException JavaDoc
84     {
85         this.bootstrapContext = bootstrapContext;
86         if (info.getConfigurations() != null)
87         {
88             if (MuleManager.isInstanciated())
89             {
90                 throw new ResourceAdapterInternalException JavaDoc(
91                     "A manager is already configured, cannot configure a new one using the configurations set on the Resource Adapter");
92             }
93             else
94             {
95                 ConfigurationBuilder builder = null;
96                 try
97                 {
98                     builder = (ConfigurationBuilder)ClassUtils.instanciateClass(
99                         info.getConfigurationBuilder(), ClassUtils.NO_ARGS);
100
101                 }
102                 catch (Exception JavaDoc e)
103                 {
104                     throw new ResourceAdapterInternalException JavaDoc(
105                         "Failed to instanciate configurationBuilder class: " + info.getConfigurationBuilder(),
106                         e);
107                 }
108
109                 try
110                 {
111
112                     manager = builder.configure(info.getConfigurations(), null);
113                 }
114                 catch (ConfigurationException e)
115                 {
116                     throw new ResourceAdapterInternalException JavaDoc("Failed to load configurations: "
117                                                                + info.getConfigurations(), e);
118                 }
119             }
120         }
121         manager = MuleManager.getInstance();
122     }
123
124     /**
125      * @see javax.resource.spi.ResourceAdapter#stop()
126      */

127     public void stop()
128     {
129         manager.dispose();
130         manager = null;
131         bootstrapContext = null;
132     }
133
134     /**
135      * @return the bootstrap context for this adapter
136      */

137     public BootstrapContext JavaDoc getBootstrapContext()
138     {
139         return bootstrapContext;
140     }
141
142     /**
143      * @see javax.resource.spi.ResourceAdapter#endpointActivation(javax.resource.spi.endpoint.MessageEndpointFactory,
144      * javax.resource.spi.ActivationSpec)
145      */

146     public void endpointActivation(MessageEndpointFactory JavaDoc endpointFactory, ActivationSpec JavaDoc activationSpec)
147         throws ResourceException JavaDoc
148     {
149         if (activationSpec.getResourceAdapter() != this)
150         {
151             throw new ResourceException JavaDoc("ActivationSpec not initialized with this ResourceAdapter instance");
152         }
153
154         if (activationSpec.getClass().equals(MuleActivationSpec.class))
155         {
156
157             try
158             {
159                 UMOEndpointURI uri = new MuleEndpointURI(((MuleActivationSpec)activationSpec).getEndpoint());
160                 UMOEndpoint endpoint = ConnectorFactory.createEndpoint(uri,
161                     UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
162
163                 ((AbstractConnector)endpoint.getConnector()).getReceiverThreadingProfile()
164                     .setWorkManagerFactory(new ThreadingProfile.WorkManagerFactory()
165                     {
166                         public UMOWorkManager createWorkManager(ThreadingProfile profile, String JavaDoc name)
167                         {
168                             return new DelegateWorkManager(bootstrapContext.getWorkManager());
169
170                         }
171                     });
172                 // TODO manage transactions
173
MessageEndpoint JavaDoc messageEndpoint = null;
174                 messageEndpoint = endpointFactory.createEndpoint(null);
175
176                 String JavaDoc name = "JcaComponent#" + messageEndpoint.hashCode();
177                 MuleDescriptor descriptor = new MuleDescriptor(name);
178                 descriptor.getInboundRouter().addEndpoint(endpoint);
179                 descriptor.setImplementationInstance(messageEndpoint);
180                 MuleManager.getInstance().getModel().registerComponent(descriptor);
181
182                 MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec)activationSpec);
183
184                 endpoints.put(key, descriptor);
185             }
186             catch (Exception JavaDoc e)
187             {
188                 logger.error(e.getMessage(), e);
189             }
190         }
191         else
192         {
193             throw new NotSupportedException JavaDoc("That type of ActicationSpec not supported: "
194                                             + activationSpec.getClass());
195         }
196
197     }
198
199     /**
200      * @see javax.resource.spi.ResourceAdapter#endpointDeactivation(javax.resource.spi.endpoint.MessageEndpointFactory,
201      * javax.resource.spi.ActivationSpec)
202      */

203     public void endpointDeactivation(MessageEndpointFactory JavaDoc endpointFactory, ActivationSpec JavaDoc activationSpec)
204     {
205
206         if (activationSpec.getClass().equals(MuleActivationSpec.class))
207         {
208             MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec)activationSpec);
209             UMODescriptor descriptor = (UMODescriptor)endpoints.get(key);
210             if (descriptor == null)
211             {
212                 logger.warn("No endpoint was registered with key: " + key);
213                 return;
214             }
215             try
216             {
217                 manager.getModel().unregisterComponent(descriptor);
218             }
219             catch (UMOException e)
220             {
221                 logger.error(e.getMessage(), e);
222             }
223
224         }
225
226     }
227
228     /**
229      * We only connect to one resource manager per ResourceAdapter instance, so any
230      * ActivationSpec will return the same XAResource.
231      *
232      * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[])
233      */

234     public XAResource JavaDoc[] getXAResources(ActivationSpec JavaDoc[] activationSpecs) throws ResourceException JavaDoc
235     {
236         return new XAResource JavaDoc[]{};
237     }
238
239     /**
240      * @return
241      */

242     public String JavaDoc getPassword()
243     {
244         return info.getPassword();
245     }
246
247     /**
248      * @return
249      */

250     public String JavaDoc getConfigurations()
251     {
252         return info.getConfigurations();
253     }
254
255     /**
256      * @return
257      */

258     public String JavaDoc getUserName()
259     {
260         return info.getUserName();
261     }
262
263     /**
264      * @param password
265      */

266     public void setPassword(String JavaDoc password)
267     {
268         info.setPassword(password);
269     }
270
271     /**
272      * @param configurations
273      */

274     public void setConfigurations(String JavaDoc configurations)
275     {
276         info.setConfigurations(configurations);
277     }
278
279     /**
280      * @param userid
281      */

282     public void setUserName(String JavaDoc userid)
283     {
284         info.setUserName(userid);
285     }
286
287     public String JavaDoc getConfigurationBuilder()
288     {
289         return info.getConfigurationBuilder();
290     }
291
292     public void setConfigurationBuilder(String JavaDoc configbuilder)
293     {
294         info.setConfigurationBuilder(configbuilder);
295     }
296
297     /**
298      * @return Returns the info.
299      */

300     public MuleConnectionRequestInfo getInfo()
301     {
302         return info;
303     }
304
305     public boolean equals(Object JavaDoc o)
306     {
307         if (this == o)
308         {
309             return true;
310         }
311         if (!(o instanceof MuleResourceAdapter))
312         {
313             return false;
314         }
315
316         final MuleResourceAdapter muleResourceAdapter = (MuleResourceAdapter)o;
317
318         if (info != null ? !info.equals(muleResourceAdapter.info) : muleResourceAdapter.info != null)
319         {
320             return false;
321         }
322
323         return true;
324     }
325
326     public int hashCode()
327     {
328         return (info != null ? info.hashCode() : 0);
329     }
330
331 }
332
Popular Tags