KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > glue > GlueMessageDispatcher


1 /*
2  * $Id: GlueMessageDispatcher.java 4219 2006-12-09 10:15:14Z 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.glue;
12
13 import electric.glue.context.ProxyContext;
14 import electric.glue.context.ThreadContext;
15 import electric.proxy.IProxy;
16 import electric.registry.Registry;
17 import org.mule.config.MuleProperties;
18 import org.mule.impl.MuleMessage;
19 import org.mule.providers.AbstractMessageDispatcher;
20 import org.mule.umo.UMOEvent;
21 import org.mule.umo.UMOException;
22 import org.mule.umo.UMOMessage;
23 import org.mule.umo.endpoint.MalformedEndpointException;
24 import org.mule.umo.endpoint.UMOImmutableEndpoint;
25 import org.mule.umo.provider.DispatchException;
26 import org.mule.umo.provider.ReceiveException;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * <code>GlueMessageDispatcher</code> will make web services calls using the Glue
33  * invoking mechanism.
34  */

35
36 public class GlueMessageDispatcher extends AbstractMessageDispatcher
37 {
38     protected IProxy proxy = null;
39
40     public GlueMessageDispatcher(UMOImmutableEndpoint endpoint)
41     {
42         super(endpoint);
43     }
44
45     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
46     {
47         if (proxy == null)
48         {
49             String JavaDoc bindAddress = endpoint.getEndpointURI().getAddress();
50             String JavaDoc method = (String JavaDoc)endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
51             if (bindAddress.indexOf(".wsdl") == -1 && method != null)
52             {
53                 bindAddress = bindAddress.replaceAll("/" + method, ".wsdl/" + method);
54             }
55             int i = bindAddress.indexOf("?");
56             if (i > -1)
57             {
58                 bindAddress = bindAddress.substring(0, i);
59             }
60
61             // add credentials to the request
62
if (endpoint.getEndpointURI().getUsername() != null)
63             {
64                 ProxyContext context = new ProxyContext();
65                 context.setAuthUser(endpoint.getEndpointURI().getUsername());
66                 context.setAuthPassword(new String JavaDoc(endpoint.getEndpointURI().getPassword()));
67                 proxy = Registry.bind(bindAddress, context);
68             }
69             else
70             {
71                 proxy = Registry.bind(bindAddress);
72             }
73         }
74     }
75
76     protected void doDisconnect() throws Exception JavaDoc
77     {
78         proxy = null;
79     }
80
81     protected void doDispatch(UMOEvent event) throws Exception JavaDoc
82     {
83         doSend(event);
84     }
85
86     protected UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
87     {
88
89         String JavaDoc method = event.getMessage().getStringProperty(MuleProperties.MULE_METHOD_PROPERTY, null);
90         if (method == null)
91         {
92             method = (String JavaDoc)event.getEndpoint().getProperty(MuleProperties.MULE_METHOD_PROPERTY);
93         }
94         setContext(event);
95
96         Object JavaDoc payload = event.getTransformedMessage();
97         Object JavaDoc[] args;
98         if (payload instanceof Object JavaDoc[])
99         {
100             args = (Object JavaDoc[])payload;
101         }
102         else
103         {
104             args = new Object JavaDoc[]{payload};
105         }
106         if (event.getMessage().getReplyTo() != null)
107         {
108             ThreadContext.setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, event.getMessage().getReplyTo());
109         }
110         if (event.getMessage().getCorrelationId() != null)
111         {
112             ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, event.getMessage()
113                 .getCorrelationId());
114         }
115         try
116         {
117             Object JavaDoc result = proxy.invoke(method, args);
118             if (result == null)
119             {
120                 return null;
121             }
122             else
123             {
124                 return new MuleMessage(result);
125             }
126         }
127         catch (Throwable JavaDoc t)
128         {
129             throw new DispatchException(event.getMessage(), event.getEndpoint(), t);
130         }
131     }
132
133     /**
134      * Make a specific request to the underlying transport
135      *
136      * @param endpoint the endpoint to use when connecting to the resource
137      * @param timeout the maximum time the operation should block before returning.
138      * The call should return immediately if there is data available. If
139      * no data becomes available before the timeout elapses, null will be
140      * returned
141      * @return the result of the request wrapped in a UMOMessage object. Null will be
142      * returned if no data was avaialable
143      * @throws Exception if the call to the underlying protocal cuases an exception
144      */

145     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
146     {
147         Map JavaDoc props = new HashMap JavaDoc();
148         props.putAll(endpoint.getProperties());
149         String JavaDoc method = (String JavaDoc)props.remove(MuleProperties.MULE_METHOD_PROPERTY);
150         try
151         {
152             Object JavaDoc result = proxy.invoke(method, props.values().toArray());
153             return new MuleMessage(result);
154         }
155         catch (Throwable JavaDoc t)
156         {
157             throw new ReceiveException(endpoint, timeout, t);
158         }
159     }
160
161     public Object JavaDoc getDelegateSession() throws UMOException
162     {
163         return null;
164     }
165
166     protected void doDispose()
167     {
168         // template method
169
}
170
171     protected String JavaDoc getMethod(String JavaDoc endpoint) throws MalformedEndpointException
172     {
173         int i = endpoint.lastIndexOf("/");
174         String JavaDoc method = endpoint.substring(i + 1);
175         if (method.indexOf(".wsdl") != -1)
176         {
177             throw new MalformedEndpointException(
178                 "Soap url must contain method to invoke as a param [method=X] or as the last path element");
179         }
180         else
181         {
182             return method;
183             // endpointUri = endpointUri.substring(0, endpointUri.length() -
184
// (method.length() + 1));
185
}
186     }
187
188     protected void setContext(UMOEvent event)
189     {
190         Object JavaDoc replyTo = event.getMessage().getReplyTo();
191         if (replyTo != null)
192         {
193             ThreadContext.setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo);
194         }
195
196         String JavaDoc correlationId = event.getMessage().getCorrelationId();
197         if (replyTo != null)
198         {
199             ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, correlationId);
200         }
201
202         int value = event.getMessage().getCorrelationSequence();
203         if (value > 0)
204         {
205             ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY,
206                 String.valueOf(value));
207         }
208
209         value = event.getMessage().getCorrelationGroupSize();
210         if (value > 0)
211         {
212             ThreadContext.setProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY,
213                 String.valueOf(value));
214         }
215     }
216 }
217
Popular Tags