KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: XFireServiceComponent.java 4323 2006-12-19 15:55:15Z 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.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.lang.reflect.Constructor JavaDoc;
17 import java.util.Enumeration JavaDoc;
18
19 import javax.xml.stream.XMLStreamException;
20
21 import org.apache.commons.io.output.ByteArrayOutputStream;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.codehaus.xfire.XFire;
25 import org.codehaus.xfire.service.Service;
26 import org.codehaus.xfire.service.ServiceRegistry;
27 import org.codehaus.xfire.transport.Transport;
28 import org.codehaus.xfire.transport.TransportManager;
29 import org.codehaus.xfire.transport.http.HtmlServiceWriter;
30 import org.mule.MuleRuntimeException;
31 import org.mule.config.i18n.Message;
32 import org.mule.config.i18n.Messages;
33 import org.mule.impl.MuleDescriptor;
34 import org.mule.impl.MuleMessage;
35 import org.mule.impl.UMODescriptorAware;
36 import org.mule.providers.http.HttpConnector;
37 import org.mule.providers.http.HttpConstants;
38 import org.mule.providers.soap.SoapConstants;
39 import org.mule.providers.soap.xfire.transport.MuleLocalChannel;
40 import org.mule.providers.soap.xfire.transport.MuleLocalTransport;
41 import org.mule.providers.soap.xfire.transport.MuleUniversalTransport;
42 import org.mule.providers.streaming.OutStreamMessageAdapter;
43 import org.mule.providers.streaming.StreamMessageAdapter;
44 import org.mule.umo.UMODescriptor;
45 import org.mule.umo.UMOEventContext;
46 import org.mule.umo.UMOException;
47 import org.mule.umo.UMOMessage;
48 import org.mule.umo.lifecycle.Callable;
49 import org.mule.umo.lifecycle.Initialisable;
50 import org.mule.umo.lifecycle.InitialisationException;
51 import org.mule.umo.lifecycle.Lifecycle;
52 import org.mule.umo.manager.UMOWorkManager;
53 import org.mule.umo.provider.UMOStreamMessageAdapter;
54 import org.mule.util.StringUtils;
55
56 /**
57  * The Xfire service component receives requests for Xfire services it manages and
58  * marshalls requests and responses
59  *
60  */

61 public class XFireServiceComponent implements Callable, Initialisable, Lifecycle, UMODescriptorAware
62 {
63     /**
64      * logger used by this class
65      */

66     protected transient Log logger = LogFactory.getLog(getClass());
67
68     protected XFire xfire;
69
70     // manager to the component
71
protected Transport transport;
72     protected Transport universalTransport;
73     protected String JavaDoc transportClass;
74
75     public void setDescriptor(UMODescriptor descriptor)
76     {
77         UMOWorkManager wm = ((MuleDescriptor)descriptor).getThreadingProfile().createWorkManager(
78             "xfire-local-transport");
79         try
80         {
81             wm.start();
82         }
83         catch (UMOException e)
84         {
85             throw new MuleRuntimeException(new Message(Messages.FAILED_TO_START_X,
86                 "local channel work manager", e));
87         }
88         if(transportClass == null)
89         {
90             transport = new MuleLocalTransport(wm);
91         }
92         else
93         {
94             try {
95                 Class JavaDoc transportClazz = Class.forName(transportClass);
96                 try{
97                     Constructor JavaDoc constructor = transportClazz.getConstructor(new Class JavaDoc[]{UMOWorkManager.class});
98                     transport = (Transport)constructor.newInstance(new Object JavaDoc[]{wm});
99                 }
100                 catch(NoSuchMethodException JavaDoc ne)
101                 {
102                     if (logger.isDebugEnabled())
103                     {
104                         logger.debug(ne.getCause());
105                     }
106                 }
107             if(transport == null)
108             {
109                 Constructor JavaDoc constructor = transportClazz.getConstructor(null);
110                 transport = (Transport)constructor.newInstance(null);
111             }
112         }
113         catch(Exception JavaDoc e)
114         {
115             throw new MuleRuntimeException(new Message(Messages.FAILED_LOAD_X, "xfire service transport", e));
116         }
117     }
118         
119         universalTransport = new MuleUniversalTransport();
120         
121         getTransportManager().register(transport);
122         getTransportManager().register(universalTransport);
123     }
124    
125     public Object JavaDoc onCall(UMOEventContext eventContext) throws Exception JavaDoc
126     {
127         if(logger.isDebugEnabled())
128         {
129             logger.debug(eventContext);
130         }
131
132         boolean wsdlRequested = false;
133         
134         //if http request
135
String JavaDoc request = eventContext.getMessage().getStringProperty(HttpConnector.HTTP_REQUEST_PROPERTY,
136             StringUtils.EMPTY);
137         if (request.toLowerCase().endsWith(org.mule.providers.soap.SoapConstants.WSDL_PROPERTY))
138         {
139             wsdlRequested = true;
140         }
141         else //if servlet request
142
{
143             Enumeration JavaDoc keys = eventContext.getEndpointURI().getParams().keys();
144             while(keys.hasMoreElements()){
145                 if ((keys.nextElement()).toString().equalsIgnoreCase(SoapConstants.WSDL_PROPERTY)) {
146                     wsdlRequested = true;
147                     break;
148                 }
149             }
150         }
151         
152         if (wsdlRequested)
153         {
154             ByteArrayOutputStream out = new ByteArrayOutputStream();
155             getXfire().generateWSDL(getServiceName(eventContext), out);
156             UMOMessage result = new MuleMessage(out.toString(eventContext.getEncoding()));
157             result.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/xml");
158             return result;
159         }
160         else
161         {
162             MuleLocalChannel channel = (MuleLocalChannel)transport.createChannel(eventContext.getEndpointURI()
163                 .getFullScheme());
164             return channel.onCall(eventContext);
165         }
166
167     }
168
169     public void start() throws UMOException
170     {
171         // template method
172
}
173
174     public void stop() throws UMOException
175     {
176         // template method
177
}
178
179     public void initialise() throws InitialisationException
180     {
181         if (xfire == null)
182         {
183             throw new InitialisationException(new Message(Messages.X_IS_NULL, "xfire"), this);
184         }
185     }
186
187     public void dispose()
188     {
189         // template method
190
}
191
192     protected TransportManager getTransportManager()
193     {
194         return getXfire().getTransportManager();
195     }
196
197     protected void generateService(OutStreamMessageAdapter response, String JavaDoc serviceName)
198         throws IOException JavaDoc, XMLStreamException
199     {
200         response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
201         Service endpoint = getServiceRegistry().getService(serviceName);
202         HtmlServiceWriter writer = new HtmlServiceWriter();
203         writer.write(response.getStream(), endpoint);
204     }
205
206     /**
207      * @param response
208      */

209     protected void generateServices(OutStreamMessageAdapter response) throws IOException JavaDoc, XMLStreamException
210     {
211         response.setProperty(HttpConstants.HEADER_CONTENT_TYPE, "text/html");
212
213         HtmlServiceWriter writer = new HtmlServiceWriter();
214         writer.write(response.getStream(), getServiceRegistry().getServices());
215     }
216
217     /**
218      * Gets the stream representation of the current message. If the message is set
219      * for streaming the input stream on the UMOStreamMEssageAdapter will be used,
220      * otherwise a byteArrayInputStream will be used to hold the byte[]
221      * representation of the current message.
222      *
223      * @param context the event context
224      * @return The inputstream for the current message
225      * @throws UMOException
226      */

227
228     protected InputStream JavaDoc getMessageStream(UMOEventContext context) throws UMOException
229     {
230         InputStream JavaDoc is;
231         UMOMessage eventMsg = context.getMessage();
232         Object JavaDoc eventMsgPayload = eventMsg.getPayload();
233
234         if (eventMsgPayload instanceof InputStream JavaDoc)
235         {
236             is = (InputStream JavaDoc)eventMsgPayload;
237         }
238         else if (eventMsg.getAdapter() instanceof UMOStreamMessageAdapter)
239         {
240             StreamMessageAdapter sma = (StreamMessageAdapter)eventMsg.getAdapter();
241             is = sma.getInputStream();
242
243         }
244         else
245         {
246             is = new ByteArrayInputStream JavaDoc(context.getTransformedMessageAsBytes());
247         }
248         return is;
249     }
250
251     /**
252      * Get the service that is mapped to the specified request.
253      *
254      * @param context the context from which to find the service name
255      * @return the service that is mapped to the specified request.
256      */

257     protected String JavaDoc getServiceName(UMOEventContext context)
258     {
259         String JavaDoc pathInfo = context.getEndpointURI().getPath();
260
261         if (StringUtils.isEmpty(pathInfo))
262         {
263             return context.getEndpointURI().getHost();
264         }
265
266         String JavaDoc serviceName;
267
268         int i = pathInfo.lastIndexOf("/");
269
270         if (i > -1)
271         {
272             serviceName = pathInfo.substring(i + 1);
273         }
274         else
275         {
276             serviceName = pathInfo;
277         }
278
279         return serviceName;
280     }
281
282     protected Service getService(String JavaDoc name)
283     {
284         return getXfire().getServiceRegistry().getService(name);
285     }
286
287     public XFire getXfire()
288     {
289         return xfire;
290     }
291
292     public void setXfire(XFire xfire)
293     {
294         this.xfire = xfire;
295     }
296
297     public void setTransport(Transport transport)
298     {
299         this.transport = transport;
300     }
301     
302     public void setTransportClass(String JavaDoc clazz)
303     {
304         transportClass = clazz;
305     }
306
307     public ServiceRegistry getServiceRegistry()
308     {
309         return xfire.getServiceRegistry();
310     }
311 }
312
Popular Tags