KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > internal > admin > MuleManagerComponent


1 /*
2  * $Id: MuleManagerComponent.java 4259 2006-12-14 03:12:07Z aperepel $
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.impl.internal.admin;
12
13 import org.apache.commons.collections.MapUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.MuleException;
17 import org.mule.MuleManager;
18 import org.mule.config.MuleProperties;
19 import org.mule.config.i18n.Message;
20 import org.mule.config.i18n.Messages;
21 import org.mule.impl.MuleDescriptor;
22 import org.mule.impl.MuleEvent;
23 import org.mule.impl.MuleMessage;
24 import org.mule.impl.RequestContext;
25 import org.mule.impl.endpoint.MuleEndpoint;
26 import org.mule.impl.endpoint.MuleEndpointURI;
27 import org.mule.impl.internal.notifications.AdminNotification;
28 import org.mule.impl.message.ExceptionPayload;
29 import org.mule.providers.AbstractConnector;
30 import org.mule.providers.NullPayload;
31 import org.mule.transformers.wire.WireFormat;
32 import org.mule.umo.UMODescriptor;
33 import org.mule.umo.UMOEvent;
34 import org.mule.umo.UMOEventContext;
35 import org.mule.umo.UMOException;
36 import org.mule.umo.UMOMessage;
37 import org.mule.umo.UMOSession;
38 import org.mule.umo.endpoint.UMOEndpoint;
39 import org.mule.umo.endpoint.UMOEndpointURI;
40 import org.mule.umo.lifecycle.Callable;
41 import org.mule.umo.lifecycle.Initialisable;
42 import org.mule.umo.lifecycle.InitialisationException;
43 import org.mule.umo.provider.UMOConnector;
44 import org.mule.umo.provider.UMOMessageDispatcher;
45 import org.mule.umo.transformer.UMOTransformer;
46
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Map JavaDoc;
51
52 /**
53  * <code>MuleManagerComponent</code> is a MuleManager interal server component
54  * responsible for receiving remote requests and dispatching them locally. This
55  * allows developer to tunnel requests through http ssl to a Mule instance behind a
56  * firewall
57  */

58
59 public class MuleManagerComponent implements Callable, Initialisable
60 {
61     /**
62      * logger used by this class
63      */

64     protected static final Log logger = LogFactory.getLog(MuleManagerComponent.class);
65
66     public static final String JavaDoc MANAGER_COMPONENT_NAME = "_muleManagerComponent";
67     public static final String JavaDoc MANAGER_ENDPOINT_NAME = "_muleManagerEndpoint";
68
69     /**
70      * Use Serialization by default
71      */

72     protected WireFormat wireFormat;
73
74     public void initialise() throws InitialisationException
75     {
76         if (wireFormat == null)
77         {
78             throw new InitialisationException(new Message(Messages.X_IS_NULL, "wireFormat"), this);
79         }
80     }
81
82     public Object JavaDoc onCall(UMOEventContext context) throws Exception JavaDoc
83     {
84         Object JavaDoc result;
85         logger.debug("Message received by MuleManagerComponent");
86         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(context.getTransformedMessageAsBytes());
87         AdminNotification action = (AdminNotification)wireFormat.read(in);
88         if (AdminNotification.ACTION_INVOKE == action.getAction())
89         {
90             result = invokeAction(action, context);
91         }
92         else if (AdminNotification.ACTION_SEND == action.getAction())
93         {
94             result = sendAction(action, context);
95         }
96         else if (AdminNotification.ACTION_DISPATCH == action.getAction())
97         {
98             result = sendAction(action, context);
99         }
100         else if (AdminNotification.ACTION_RECEIVE == action.getAction())
101         {
102             result = receiveAction(action, context);
103         }
104         else
105         {
106             result = handleException(null, new MuleException(new Message(
107                 Messages.EVENT_TYPE_X_NOT_RECOGNISED, "AdminNotification:" + action.getAction())));
108         }
109         return result;
110     }
111
112     protected Object JavaDoc invokeAction(AdminNotification action, UMOEventContext context) throws UMOException
113     {
114         String JavaDoc destComponent = null;
115         UMOMessage result = null;
116         String JavaDoc endpoint = action.getResourceIdentifier();
117         if (action.getResourceIdentifier().startsWith("mule:"))
118         {
119             destComponent = endpoint.substring(endpoint.lastIndexOf("/") + 1);
120         }
121         else
122         {
123             destComponent = endpoint;
124         }
125
126         if (destComponent != null)
127         {
128             UMOSession session = MuleManager.getInstance().getModel().getComponentSession(destComponent);
129             // Need to do this otherise when the event is invoked the
130
// transformer associated with the Mule Admin queue will be invoked, but
131
// the
132
// message will not be of expected type
133
UMOEndpoint ep = new MuleEndpoint(RequestContext.getEvent().getEndpoint());
134             ep.setTransformer(null);
135             UMOEvent event = new MuleEvent(action.getMessage(), ep, context.getSession(),
136                 context.isSynchronous());
137             RequestContext.setEvent(event);
138
139             if (context.isSynchronous())
140             {
141                 result = session.getComponent().sendEvent(event);
142                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
143                 wireFormat.write(out, result);
144                 return out.toByteArray();
145             }
146             else
147             {
148                 session.getComponent().dispatchEvent(event);
149                 return null;
150             }
151         }
152         else
153         {
154             return handleException(result, new MuleException(new Message(
155                 Messages.COULD_NOT_DETERMINE_DESTINATION_COMPONENT_FROM_ENDPOINT_X, endpoint)));
156         }
157     }
158
159     protected Object JavaDoc sendAction(AdminNotification action, UMOEventContext context) throws UMOException
160     {
161         UMOMessage result = null;
162         try
163         {
164             UMOEndpoint endpoint = new MuleEndpoint(action.getResourceIdentifier(), false);
165
166             if (AdminNotification.ACTION_DISPATCH == action.getAction())
167             {
168                 context.dispatchEvent(action.getMessage(), endpoint);
169                 return null;
170             }
171             else
172             {
173                 endpoint.setRemoteSync(true);
174                 result = context.sendEvent(action.getMessage(), endpoint);
175                 if (result == null)
176                 {
177                     return null;
178                 }
179                 else
180                 {
181                     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
182                     wireFormat.write(out, result);
183                     return out.toByteArray();
184                 }
185             }
186         }
187         catch (Exception JavaDoc e)
188         {
189             return handleException(result, e);
190         }
191     }
192
193     protected Object JavaDoc receiveAction(AdminNotification action, UMOEventContext context) throws UMOException
194     {
195         UMOMessage result = null;
196         try
197         {
198             UMOEndpointURI endpointUri = new MuleEndpointURI(action.getResourceIdentifier());
199             UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointUri,
200                 UMOEndpoint.ENDPOINT_TYPE_SENDER);
201
202             UMOMessageDispatcher dispatcher = endpoint.getConnector().getDispatcher(endpoint);
203             long timeout = MapUtils.getLongValue(action.getProperties(),
204                 MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, MuleManager.getConfiguration()
205                     .getSynchronousEventTimeout());
206
207             UMOEndpointURI ep = new MuleEndpointURI(action.getResourceIdentifier());
208             result = dispatcher.receive(ep, timeout);
209             if (result != null)
210             {
211                 // See if there is a default transformer on the connector
212
UMOTransformer trans = ((AbstractConnector)endpoint.getConnector()).getDefaultInboundTransformer();
213                 if (trans != null)
214                 {
215                     Object JavaDoc payload = trans.transform(result.getPayload());
216                     result = new MuleMessage(payload, result);
217                 }
218                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
219                 wireFormat.write(out, result);
220                 return out.toByteArray();
221             }
222             else
223             {
224                 return null;
225             }
226         }
227         catch (Exception JavaDoc e)
228         {
229             return handleException(result, e);
230         }
231
232     }
233
234     public static final UMODescriptor getDescriptor(UMOConnector connector,
235                                                     UMOEndpointURI endpointUri,
236                                                     WireFormat wireFormat) throws UMOException
237     {
238         UMOEndpoint endpoint = new MuleEndpoint();
239         endpoint.setConnector(connector);
240         endpoint.setEndpointURI(endpointUri);
241         endpoint.setName(MANAGER_ENDPOINT_NAME);
242         endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
243
244         MuleDescriptor descriptor = new MuleDescriptor();
245         descriptor.setName(MANAGER_COMPONENT_NAME);
246         descriptor.setInboundEndpoint(endpoint);
247         descriptor.setImplementation(MuleManagerComponent.class.getName());
248         descriptor.setContainerManaged(false);
249         Map JavaDoc props = new HashMap JavaDoc();
250         props.put("wireFormat", wireFormat);
251         descriptor.setProperties(props);
252         return descriptor;
253     }
254
255     /**
256      * Wraps an execption into a MuleMessage with an Exception payload and returns
257      * the Xml representation of it
258      *
259      * @param result the result of the invocation or null if the exception occurred
260      * before or during the invocation
261      * @param e the Exception thrown
262      * @return an Xml String message result
263      */

264     protected String JavaDoc handleException(UMOMessage result, Throwable JavaDoc e)
265     {
266         logger.error("Failed to process admin request: " + e.getMessage(), e);
267         if (result == null)
268         {
269             result = new MuleMessage(new NullPayload(), (Map JavaDoc)null);
270         }
271         result.setExceptionPayload(new ExceptionPayload(e));
272         try
273         {
274             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
275             wireFormat.write(out, result);
276             return out.toString(MuleManager.getConfiguration().getEncoding());
277         }
278         catch (Exception JavaDoc e1)
279         {
280             logger.error(e.toString(), e);
281             return e.getMessage();
282         }
283     }
284
285     public WireFormat getWireFormat()
286     {
287         return wireFormat;
288     }
289
290     public void setWireFormat(WireFormat wireFormat)
291     {
292         this.wireFormat = wireFormat;
293     }
294 }
295
Popular Tags