KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > xfire > XFireConnector


1 /*
2  * $Id: XFireConnector.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.providers.soap.xfire;
12
13 import java.util.List JavaDoc;
14
15 import org.codehaus.xfire.DefaultXFire;
16 import org.codehaus.xfire.XFire;
17 import org.codehaus.xfire.annotations.AnnotationServiceFactory;
18 import org.codehaus.xfire.annotations.WebAnnotations;
19 import org.codehaus.xfire.service.Service;
20 import org.codehaus.xfire.service.ServiceFactory;
21 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
22 import org.mule.MuleManager;
23 import org.mule.config.i18n.Message;
24 import org.mule.impl.MuleDescriptor;
25 import org.mule.impl.endpoint.MuleEndpoint;
26 import org.mule.impl.internal.notifications.ModelNotification;
27 import org.mule.impl.internal.notifications.ModelNotificationListener;
28 import org.mule.impl.internal.notifications.NotificationException;
29 import org.mule.providers.AbstractServiceEnabledConnector;
30 import org.mule.providers.soap.MethodFixInterceptor;
31 import org.mule.providers.http.HttpConnector;
32 import org.mule.providers.http.HttpConstants;
33 import org.mule.umo.UMOComponent;
34 import org.mule.umo.UMOException;
35 import org.mule.umo.endpoint.UMOEndpoint;
36 import org.mule.umo.endpoint.UMOEndpointURI;
37 import org.mule.umo.lifecycle.InitialisationException;
38 import org.mule.umo.manager.UMOServerNotification;
39 import org.mule.umo.provider.UMOMessageReceiver;
40 import org.mule.util.ClassUtils;
41 import org.mule.util.SystemUtils;
42
43 /**
44  * Configures Xfire to provide STaX-based Web Servies support to Mule.
45  */

46 public class XFireConnector extends AbstractServiceEnabledConnector
47     implements ModelNotificationListener
48 {
49     public static final String JavaDoc XFIRE_SERVICE_COMPONENT_NAME = "_xfireServiceComponent";
50     public static final String JavaDoc DEFAULT_MULE_NAMESPACE_URI = "http://www.muleumo.org";
51     public static final String JavaDoc XFIRE_PROPERTY = "xfire";
52     public static final String JavaDoc XFIRE_TRANSPORT = "transportClass";
53
54     private static final String JavaDoc CLASSNAME_ANNOTATIONS = "org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations";
55
56     protected MuleDescriptor xfireDescriptor;
57
58     private XFire xfire;
59
60     private ServiceFactory serviceFactory;
61
62     private boolean enableJSR181Annotations = false;
63
64     private List JavaDoc clientServices = null;
65     private List JavaDoc clientInHandlers = null;
66     private List JavaDoc clientOutHandlers = null;
67     private String JavaDoc clientTransport = null;
68
69     private String JavaDoc serviceTransport = null;
70     private List JavaDoc serverInHandlers = null;
71     private List JavaDoc serverOutHandlers = null;
72
73     public XFireConnector()
74     {
75         super();
76         registerProtocols();
77     }
78
79     protected void registerProtocols()
80     {
81         registerSupportedProtocol("http");
82         registerSupportedProtocol("https");
83         registerSupportedProtocol("jms");
84         registerSupportedProtocol("vm");
85         registerSupportedProtocol("servlet");
86     }
87
88     public String JavaDoc getProtocol()
89     {
90         return "xfire";
91     }
92
93     public void doInitialise() throws InitialisationException
94     {
95         super.doInitialise();
96         try
97         {
98             MuleManager.getInstance().registerListener(this);
99         }
100         catch (NotificationException e)
101         {
102             throw new InitialisationException(e, this);
103         }
104
105         if (xfire == null)
106         {
107             xfire = new DefaultXFire();
108         }
109
110         if (clientServices != null)
111         {
112             ObjectServiceFactory factory = new ObjectServiceFactory();
113             for (int i = 0; i < clientServices.size(); i++)
114             {
115                 try
116                 {
117                     Class JavaDoc clazz = Class.forName(clientServices.get(i).toString());
118                     Service service = factory.create(clazz);
119                     xfire.getServiceRegistry().register(service);
120                 }
121                 catch (ClassNotFoundException JavaDoc e)
122                 {
123                     throw new InitialisationException(new Message("xfire", 10, clientServices
124                         .get(i)), e, this);
125                 }
126             }
127         }
128
129         if (serviceFactory == null)
130         {
131             if (enableJSR181Annotations)
132             {
133                 // are we running under Java 5 (at least)?
134
if (!SystemUtils.isJavaVersionAtLeast(150))
135                 {
136                     throw new InitialisationException(new Message("xfire", 9), this);
137                 }
138                 try
139                 {
140                     WebAnnotations wa = (WebAnnotations)ClassUtils.instanciateClass(
141                         CLASSNAME_ANNOTATIONS, null, this.getClass());
142                     serviceFactory = new AnnotationServiceFactory(wa, xfire.getTransportManager());
143                 }
144                 catch (Exception JavaDoc ex)
145                 {
146                     throw new InitialisationException(new Message("xfire", 10,
147                         CLASSNAME_ANNOTATIONS), ex, this);
148                 }
149             }
150             else
151             {
152                 serviceFactory = new MuleObjectServiceFactory(xfire.getTransportManager());
153             }
154         }
155         if (serviceFactory instanceof ObjectServiceFactory)
156         {
157             ObjectServiceFactory osf = (ObjectServiceFactory)serviceFactory;
158             if (osf.getTransportManager() == null)
159             {
160                 osf.setTransportManager(xfire.getTransportManager());
161             }
162         }
163     }
164
165     public XFire getXfire()
166     {
167         return xfire;
168     }
169
170     public void setXfire(XFire xfire)
171     {
172         this.xfire = xfire;
173     }
174
175     protected void registerReceiverWithMuleService(UMOMessageReceiver receiver, UMOEndpointURI ep)
176         throws UMOException
177     {
178         // If this is the first receiver we need to create the Axis service
179
// component
180
// this will be registered with Mule when the Connector starts
181
if (xfireDescriptor == null)
182         {
183             // See if the axis descriptor has already been added. This allows
184
// developers to override the default configuration, say to increase
185
// the threadpool
186
xfireDescriptor = (MuleDescriptor)MuleManager.getInstance().getModel().getDescriptor(
187                 XFIRE_SERVICE_COMPONENT_NAME + getName());
188             if (xfireDescriptor == null)
189             {
190                 xfireDescriptor = createxfireDescriptor();
191             }
192             else
193             {
194                 // Lets unregister the 'template' instance, configure it and
195
// then register
196
// again later
197
MuleManager.getInstance().getModel().unregisterComponent(xfireDescriptor);
198             }
199             // if the axis server hasn't been set, set it now. The Axis server
200
// may be set externally
201
if (xfireDescriptor.getProperties().get(XFIRE_PROPERTY) == null)
202             {
203                 xfireDescriptor.getProperties().put(XFIRE_PROPERTY, xfire);
204             }
205             if (serviceTransport != null
206                 && xfireDescriptor.getProperties().get(XFIRE_TRANSPORT) == null)
207             {
208                 xfireDescriptor.getProperties().put(XFIRE_TRANSPORT, serviceTransport);
209             }
210             xfireDescriptor.setContainerManaged(false);
211         }
212         String JavaDoc serviceName = receiver.getComponent().getDescriptor().getName();
213
214         // No determine if the endpointUri requires a new connector to be
215
// registed in the case of http we only need to register the new
216
// endpointUri if the port is different
217
String JavaDoc endpoint = receiver.getEndpointURI().getAddress();
218         String JavaDoc scheme = ep.getScheme().toLowerCase();
219
220         // Default to using synchronous for socket based protocols unless the
221
// synchronous property has been set explicitly
222
boolean sync = false;
223         if (!receiver.getEndpoint().isSynchronousSet())
224         {
225             if (scheme.equals("http") || scheme.equals("https") || scheme.equals("ssl")
226                 || scheme.equals("tcp"))
227             {
228                 sync = true;
229             }
230         }
231         else
232         {
233             sync = receiver.getEndpoint().isSynchronous();
234         }
235
236         // If we are using sockets then we need to set the endpoint name appropiately
237
// and if using http/https
238
// we need to default to POST and set the Content-Type
239
if (scheme.equals("http") || scheme.equals("https") || scheme.equals("ssl")
240             || scheme.equals("tcp") || scheme.equals("servlet"))
241         {
242             endpoint += "/" + serviceName;
243             receiver.getEndpoint().getProperties().put(HttpConnector.HTTP_METHOD_PROPERTY, "POST");
244             receiver.getEndpoint().getProperties().put(HttpConstants.HEADER_CONTENT_TYPE,
245                 "text/xml");
246         }
247
248         UMOEndpoint serviceEndpoint = new MuleEndpoint(endpoint, true);
249         serviceEndpoint.setSynchronous(sync);
250         serviceEndpoint.setName(ep.getScheme() + ":" + serviceName);
251
252         // Set the transformers on the endpoint too
253
serviceEndpoint.setTransformer(receiver.getEndpoint().getTransformer());
254         receiver.getEndpoint().setTransformer(null);
255
256         serviceEndpoint.setResponseTransformer(receiver.getEndpoint().getResponseTransformer());
257         receiver.getEndpoint().setResponseTransformer(null);
258
259         // set the filter on the axis endpoint on the real receiver endpoint
260
serviceEndpoint.setFilter(receiver.getEndpoint().getFilter());
261         // Remove the Axis filter now
262
receiver.getEndpoint().setFilter(null);
263
264         // set the Security filter on the axis endpoint on the real receiver
265
// endpoint
266
serviceEndpoint.setSecurityFilter(receiver.getEndpoint().getSecurityFilter());
267         // Remove the Axis Receiver Security filter now
268
receiver.getEndpoint().setSecurityFilter(null);
269         xfireDescriptor.getInboundRouter().addEndpoint(serviceEndpoint);
270     }
271
272     protected MuleDescriptor createxfireDescriptor()
273     {
274         MuleDescriptor xfireDescriptor = (MuleDescriptor)MuleManager.getInstance().getModel()
275             .getDescriptor(XFIRE_SERVICE_COMPONENT_NAME + getName());
276         if (xfireDescriptor == null)
277         {
278             xfireDescriptor = new MuleDescriptor(XFIRE_SERVICE_COMPONENT_NAME + getName());
279             xfireDescriptor.setImplementation(XFireServiceComponent.class.getName());
280         }
281         return xfireDescriptor;
282     }
283
284     public ServiceFactory getServiceFactory()
285     {
286         return serviceFactory;
287     }
288
289     public void setServiceFactory(ObjectServiceFactory serviceFactory)
290     {
291         this.serviceFactory = serviceFactory;
292     }
293
294     /**
295      * The method determines the key used to store the receiver against.
296      *
297      * @param component the component for which the endpoint is being registered
298      * @param endpoint the endpoint being registered for the component
299      * @return the key to store the newly created receiver against. In this case it
300      * is the component name, which is equivilent to the Axis service name.
301      */

302     protected Object JavaDoc getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
303     {
304         if (endpoint.getEndpointURI().getPort() == -1)
305         {
306             return component.getDescriptor().getName();
307         }
308         else
309         {
310             return endpoint.getEndpointURI().getAddress() + "/"
311                    + component.getDescriptor().getName();
312         }
313     }
314
315     public boolean isEnableJSR181Annotations()
316     {
317         return enableJSR181Annotations;
318     }
319
320     public void setEnableJSR181Annotations(boolean enableJSR181Annotations)
321     {
322         this.enableJSR181Annotations = enableJSR181Annotations;
323     }
324
325     public List JavaDoc getClientServices()
326     {
327         return clientServices;
328     }
329
330     public void setClientServices(List JavaDoc clientServices)
331     {
332         this.clientServices = clientServices;
333     }
334
335     public List JavaDoc getClientInHandlers()
336     {
337         return clientInHandlers;
338     }
339
340     public void setClientInHandlers(List JavaDoc handlers)
341     {
342         clientInHandlers = handlers;
343     }
344
345     public List JavaDoc getClientOutHandlers()
346     {
347         return clientOutHandlers;
348     }
349
350     public void setClientOutHandlers(List JavaDoc handlers)
351     {
352         clientOutHandlers = handlers;
353     }
354
355     public String JavaDoc getClientTransport()
356     {
357         return clientTransport;
358     }
359
360     public void setClientTransport(String JavaDoc transportClass)
361     {
362         clientTransport = transportClass;
363     }
364
365     public String JavaDoc getServiceTransport()
366     {
367         return serviceTransport;
368     }
369
370     public void setServiceTransport(String JavaDoc transportClass)
371     {
372         serviceTransport = transportClass;
373     }
374
375     public void onNotification(UMOServerNotification event)
376     {
377         if (event.getAction() == ModelNotification.MODEL_STARTED)
378         {
379             // We need to register the xfire service component once the model
380
// starts because
381
// when the model starts listeners on components are started, thus
382
// all listener
383
// need to be registered for this connector before the xfire service
384
// component is registered. The implication of this is that to add a
385
// new service and a
386
// different http port the model needs to be restarted before the
387
// listener is available
388
if (!MuleManager.getInstance().getModel().isComponentRegistered(
389                 XFIRE_SERVICE_COMPONENT_NAME + getName()))
390             {
391                 try
392                 {
393                     // Descriptor might be null if no inbound endpoints have
394
// been register for the xfire connector
395
if (xfireDescriptor == null)
396                     {
397                         xfireDescriptor = createxfireDescriptor();
398                     }
399                     xfireDescriptor.addInterceptor(new MethodFixInterceptor());
400
401                     if (xfireDescriptor.getProperties().get("xfire") == null)
402                     {
403                         xfireDescriptor.getProperties().put("xfire", xfire);
404                     }
405                     MuleManager.getInstance().getModel().registerComponent(xfireDescriptor);
406                 }
407                 catch (UMOException e)
408                 {
409                     handleException(e);
410                 }
411             }
412         }
413     }
414
415     public List JavaDoc getServerInHandlers()
416     {
417         return serverInHandlers;
418     }
419
420     public void setServerInHandlers(List JavaDoc serverInHandlers)
421     {
422         this.serverInHandlers = serverInHandlers;
423     }
424
425     public List JavaDoc getServerOutHandlers()
426     {
427         return serverOutHandlers;
428     }
429
430     public void setServerOutHandlers(List JavaDoc serverOutHandlers)
431     {
432         this.serverOutHandlers = serverOutHandlers;
433     }
434 }
435
Popular Tags