1 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 ; 48 import java.io.ByteArrayOutputStream ; 49 import java.util.HashMap ; 50 import java.util.Map ; 51 52 58 59 public class MuleManagerComponent implements Callable, Initialisable 60 { 61 64 protected static final Log logger = LogFactory.getLog(MuleManagerComponent.class); 65 66 public static final String MANAGER_COMPONENT_NAME = "_muleManagerComponent"; 67 public static final String MANAGER_ENDPOINT_NAME = "_muleManagerEndpoint"; 68 69 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 onCall(UMOEventContext context) throws Exception 83 { 84 Object result; 85 logger.debug("Message received by MuleManagerComponent"); 86 ByteArrayInputStream in = new ByteArrayInputStream (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 invokeAction(AdminNotification action, UMOEventContext context) throws UMOException 113 { 114 String destComponent = null; 115 UMOMessage result = null; 116 String 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 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 out = new ByteArrayOutputStream (); 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 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 out = new ByteArrayOutputStream (); 182 wireFormat.write(out, result); 183 return out.toByteArray(); 184 } 185 } 186 } 187 catch (Exception e) 188 { 189 return handleException(result, e); 190 } 191 } 192 193 protected Object 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 UMOTransformer trans = ((AbstractConnector)endpoint.getConnector()).getDefaultInboundTransformer(); 213 if (trans != null) 214 { 215 Object payload = trans.transform(result.getPayload()); 216 result = new MuleMessage(payload, result); 217 } 218 ByteArrayOutputStream out = new ByteArrayOutputStream (); 219 wireFormat.write(out, result); 220 return out.toByteArray(); 221 } 222 else 223 { 224 return null; 225 } 226 } 227 catch (Exception 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 props = new HashMap (); 250 props.put("wireFormat", wireFormat); 251 descriptor.setProperties(props); 252 return descriptor; 253 } 254 255 264 protected String handleException(UMOMessage result, Throwable 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 )null); 270 } 271 result.setExceptionPayload(new ExceptionPayload(e)); 272 try 273 { 274 ByteArrayOutputStream out = new ByteArrayOutputStream (); 275 wireFormat.write(out, result); 276 return out.toString(MuleManager.getConfiguration().getEncoding()); 277 } 278 catch (Exception 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 |