Coverage Report - org.mule.impl.internal.admin.MuleManagerComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleManagerComponent
0%
0/95
0%
0/13
3.667
 
 1  
 /*
 2  
  * $Id: MuleManagerComponent.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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 CPAL v1.0
 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.mule.MuleException;
 14  
 import org.mule.MuleManager;
 15  
 import org.mule.config.MuleProperties;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.impl.MuleDescriptor;
 18  
 import org.mule.impl.MuleEvent;
 19  
 import org.mule.impl.MuleMessage;
 20  
 import org.mule.impl.MuleSession;
 21  
 import org.mule.impl.RequestContext;
 22  
 import org.mule.impl.endpoint.MuleEndpoint;
 23  
 import org.mule.impl.endpoint.MuleEndpointURI;
 24  
 import org.mule.impl.internal.notifications.AdminNotification;
 25  
 import org.mule.impl.message.ExceptionPayload;
 26  
 import org.mule.impl.model.ModelHelper;
 27  
 import org.mule.providers.AbstractConnector;
 28  
 import org.mule.providers.NullPayload;
 29  
 import org.mule.transformers.wire.WireFormat;
 30  
 import org.mule.umo.UMODescriptor;
 31  
 import org.mule.umo.UMOEvent;
 32  
 import org.mule.umo.UMOEventContext;
 33  
 import org.mule.umo.UMOException;
 34  
 import org.mule.umo.UMOMessage;
 35  
 import org.mule.umo.UMOSession;
 36  
 import org.mule.umo.endpoint.UMOEndpoint;
 37  
 import org.mule.umo.endpoint.UMOEndpointURI;
 38  
 import org.mule.umo.lifecycle.Callable;
 39  
 import org.mule.umo.lifecycle.Initialisable;
 40  
 import org.mule.umo.lifecycle.InitialisationException;
 41  
 import org.mule.umo.provider.UMOConnector;
 42  
 import org.mule.umo.transformer.UMOTransformer;
 43  
 
 44  
 import java.io.ByteArrayInputStream;
 45  
 import java.util.HashMap;
 46  
 import java.util.Map;
 47  
 
 48  
 import org.apache.commons.collections.MapUtils;
 49  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 50  
 import org.apache.commons.logging.Log;
 51  
 import org.apache.commons.logging.LogFactory;
 52  
 
 53  
 /**
 54  
  * <code>MuleManagerComponent</code> is a MuleManager interal server component
 55  
  * responsible for receiving remote requests and dispatching them locally. This
 56  
  * allows developer to tunnel requests through http ssl to a Mule instance behind a
 57  
  * firewall
 58  
  */
 59  
 
 60  0
 public class MuleManagerComponent implements Callable, Initialisable
 61  
 {
 62  
     /**
 63  
      * logger used by this class
 64  
      */
 65  0
     protected static final Log logger = LogFactory.getLog(MuleManagerComponent.class);
 66  
 
 67  
     public static final String MANAGER_COMPONENT_NAME = "_muleManagerComponent";
 68  
     public static final String MANAGER_ENDPOINT_NAME = "_muleManagerEndpoint";
 69  
 
 70  
     /**
 71  
      * Use Serialization by default
 72  
      */
 73  
     protected WireFormat wireFormat;
 74  
 
 75  
     public void initialise() throws InitialisationException
 76  
     {
 77  0
         if (wireFormat == null)
 78  
         {
 79  0
             throw new InitialisationException(CoreMessages.objectIsNull("wireFormat"), this);
 80  
         }
 81  0
     }
 82  
 
 83  
     public Object onCall(UMOEventContext context) throws Exception
 84  
     {
 85  
         Object result;
 86  0
         logger.debug("Message received by MuleManagerComponent");
 87  0
         ByteArrayInputStream in = new ByteArrayInputStream(context.getTransformedMessageAsBytes());
 88  0
         AdminNotification action = (AdminNotification) wireFormat.read(in);
 89  0
         if (AdminNotification.ACTION_INVOKE == action.getAction())
 90  
         {
 91  0
             result = invokeAction(action, context);
 92  
         }
 93  0
         else if (AdminNotification.ACTION_SEND == action.getAction() ||
 94  
                  AdminNotification.ACTION_DISPATCH == action.getAction())
 95  
         {
 96  0
             result = sendAction(action, context);
 97  
         }
 98  0
         else if (AdminNotification.ACTION_RECEIVE == action.getAction())
 99  
         {
 100  0
             result = receiveAction(action, context);
 101  
         }
 102  
         else
 103  
         {
 104  0
             result = handleException(null, new MuleException(
 105  
                 CoreMessages.eventTypeNotRecognised("AdminNotification:" + action.getAction())));
 106  
         }
 107  0
         return result;
 108  
     }
 109  
 
 110  
     protected Object invokeAction(AdminNotification action, UMOEventContext context) throws UMOException
 111  
     {
 112  0
         String destComponent = null;
 113  0
         UMOMessage result = null;
 114  0
         String endpoint = action.getResourceIdentifier();
 115  0
         if (action.getResourceIdentifier().startsWith("mule:"))
 116  
         {
 117  0
             destComponent = endpoint.substring(endpoint.lastIndexOf("/") + 1);
 118  
         }
 119  
         else
 120  
         {
 121  0
             destComponent = endpoint;
 122  
         }
 123  
 
 124  0
         if (destComponent != null)
 125  
         {
 126  0
             UMOSession session = new MuleSession(ModelHelper.getComponent(destComponent));
 127  
             // Need to do this otherise when the event is invoked the
 128  
             // transformer associated with the Mule Admin queue will be invoked, but
 129  
             // the
 130  
             // message will not be of expected type
 131  0
             UMOEndpoint ep = new MuleEndpoint(RequestContext.getEvent().getEndpoint());
 132  0
             ep.setTransformer(null);
 133  0
             UMOEvent event = new MuleEvent(action.getMessage(), ep, context.getSession(),
 134  
                 context.isSynchronous());
 135  0
             event = RequestContext.setEvent(event);
 136  
 
 137  0
             if (context.isSynchronous())
 138  
             {
 139  0
                 result = session.getComponent().sendEvent(event);
 140  0
                 ByteArrayOutputStream out = new ByteArrayOutputStream();
 141  0
                 wireFormat.write(out, result);
 142  0
                 return out.toByteArray();
 143  
             }
 144  
             else
 145  
             {
 146  0
                 session.getComponent().dispatchEvent(event);
 147  0
                 return null;
 148  
             }
 149  
         }
 150  
         else
 151  
         {
 152  0
             return handleException(result, new MuleException(
 153  
                 CoreMessages.couldNotDetermineDestinationComponentFromEndpoint(endpoint)));
 154  
         }
 155  
     }
 156  
 
 157  
     protected Object sendAction(AdminNotification action, UMOEventContext context) throws UMOException
 158  
     {
 159  0
         UMOMessage result = null;
 160  
         try
 161  
         {
 162  0
             UMOEndpoint endpoint = new MuleEndpoint(action.getResourceIdentifier(), false);
 163  
 
 164  0
             if (AdminNotification.ACTION_DISPATCH == action.getAction())
 165  
             {
 166  0
                 context.dispatchEvent(action.getMessage(), endpoint);
 167  0
                 return null;
 168  
             }
 169  
             else
 170  
             {
 171  0
                 endpoint.setRemoteSync(true);
 172  0
                 result = context.sendEvent(action.getMessage(), endpoint);
 173  0
                 if (result == null)
 174  
                 {
 175  0
                     return null;
 176  
                 }
 177  
                 else
 178  
                 {
 179  0
                     ByteArrayOutputStream out = new ByteArrayOutputStream();
 180  0
                     wireFormat.write(out, result);
 181  0
                     return out.toByteArray();
 182  
                 }
 183  
             }
 184  
         }
 185  0
         catch (Exception e)
 186  
         {
 187  0
             return handleException(result, e);
 188  
         }
 189  
     }
 190  
 
 191  
     protected Object receiveAction(AdminNotification action, UMOEventContext context) throws UMOException
 192  
     {
 193  0
         UMOMessage result = null;
 194  
         try
 195  
         {
 196  0
             UMOEndpointURI endpointUri = new MuleEndpointURI(action.getResourceIdentifier());
 197  0
             UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointUri,
 198  
                 UMOEndpoint.ENDPOINT_TYPE_SENDER);
 199  
 
 200  0
             long timeout = MapUtils.getLongValue(action.getProperties(),
 201  
                 MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, MuleManager.getConfiguration()
 202  
                     .getSynchronousEventTimeout());
 203  
 
 204  0
             UMOEndpointURI ep = new MuleEndpointURI(action.getResourceIdentifier());
 205  0
             result = endpoint.getConnector().receive(ep, timeout);
 206  0
             if (result != null)
 207  
             {
 208  
                 // See if there is a default transformer on the connector
 209  0
                 UMOTransformer trans = ((AbstractConnector) endpoint.getConnector()).getDefaultInboundTransformer();
 210  0
                 if (trans != null)
 211  
                 {
 212  0
                     Object payload = trans.transform(result.getPayload());
 213  0
                     result = new MuleMessage(payload, result);
 214  
                 }
 215  0
                 ByteArrayOutputStream out = new ByteArrayOutputStream();
 216  0
                 wireFormat.write(out, result);
 217  0
                 return out.toByteArray();
 218  
             }
 219  
             else
 220  
             {
 221  0
                 return null;
 222  
             }
 223  
         }
 224  0
         catch (Exception e)
 225  
         {
 226  0
             return handleException(result, e);
 227  
         }
 228  
 
 229  
     }
 230  
 
 231  
     public static final UMODescriptor getDescriptor(UMOConnector connector,
 232  
                                                     UMOEndpointURI endpointUri,
 233  
                                                     WireFormat wireFormat) throws UMOException
 234  
     {
 235  0
         UMOEndpoint endpoint = new MuleEndpoint();
 236  0
         endpoint.setConnector(connector);
 237  0
         endpoint.setEndpointURI(endpointUri);
 238  0
         endpoint.setName(MANAGER_ENDPOINT_NAME);
 239  0
         endpoint.setType(UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
 240  
 
 241  0
         MuleDescriptor descriptor = new MuleDescriptor();
 242  0
         descriptor.setName(MANAGER_COMPONENT_NAME);
 243  0
         descriptor.setInboundEndpoint(endpoint);
 244  0
         descriptor.setImplementation(MuleManagerComponent.class.getName());
 245  0
         descriptor.setContainerManaged(false);
 246  0
         Map props = new HashMap();
 247  0
         props.put("wireFormat", wireFormat);
 248  0
         descriptor.setProperties(props);
 249  0
         return descriptor;
 250  
     }
 251  
 
 252  
     /**
 253  
      * Wraps an exception into a MuleMessage with an Exception payload and returns
 254  
      * the Xml representation of it
 255  
      * 
 256  
      * @param result the result of the invocation or null if the exception occurred
 257  
      *            before or during the invocation
 258  
      * @param e the Exception thrown
 259  
      * @return an Xml String message result
 260  
      */
 261  
     protected String handleException(UMOMessage result, Throwable e)
 262  
     {
 263  0
         logger.error("Failed to process admin request: " + e.getMessage(), e);
 264  0
         if (result == null)
 265  
         {
 266  0
             result = new MuleMessage(NullPayload.getInstance(), (Map) null);
 267  
         }
 268  0
         result.setExceptionPayload(new ExceptionPayload(e));
 269  
         try
 270  
         {
 271  0
             ByteArrayOutputStream out = new ByteArrayOutputStream();
 272  0
             wireFormat.write(out, result);
 273  0
             return out.toString(MuleManager.getConfiguration().getEncoding());
 274  
         }
 275  0
         catch (Exception e1)
 276  
         {
 277  
             // TODO MULE-863: Is this sufficient?
 278  
             // log the inner exception here since the earlier exception was logged earlier
 279  0
             logger.error("Failed to format message, using direct string (details at debug level): " + e1.getMessage());
 280  0
             logger.debug(e1.toString(), e1);
 281  0
             return e.getMessage();
 282  
         }
 283  
     }
 284  
 
 285  
     public WireFormat getWireFormat()
 286  
     {
 287  0
         return wireFormat;
 288  
     }
 289  
 
 290  
     public void setWireFormat(WireFormat wireFormat)
 291  
     {
 292  0
         this.wireFormat = wireFormat;
 293  0
     }
 294  
 }