1 10 11 package org.mule.providers.stream; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.providers.AbstractMessageDispatcher; 15 import org.mule.umo.UMOEvent; 16 import org.mule.umo.UMOException; 17 import org.mule.umo.UMOMessage; 18 import org.mule.umo.endpoint.UMOImmutableEndpoint; 19 import org.mule.umo.provider.DispatchException; 20 import org.mule.umo.provider.UMOConnector; 21 import org.mule.util.StringUtils; 22 23 import java.io.OutputStream ; 24 25 32 33 public class StreamMessageDispatcher extends AbstractMessageDispatcher 34 { 35 private final StreamConnector connector; 36 37 public StreamMessageDispatcher(UMOImmutableEndpoint endpoint) 38 { 39 super(endpoint); 40 this.connector = (StreamConnector)endpoint.getConnector(); 41 42 if (connector instanceof SystemStreamConnector) 44 { 45 SystemStreamConnector ssc = (SystemStreamConnector)connector; 46 47 String outputMessage = (String )endpoint.getProperties().get("outputMessage"); 48 if (outputMessage != null) 49 { 50 ssc.setOutputMessage(outputMessage); 51 } 52 } 53 } 54 55 60 public Object getDelegateSession() throws UMOException 61 { 62 return null; 63 } 64 65 70 protected synchronized void doDispatch(UMOEvent event) throws Exception 71 { 72 OutputStream out; 73 String streamName = event.getEndpoint().getEndpointURI().getAddress(); 74 75 if (StreamConnector.STREAM_SYSTEM_OUT.equalsIgnoreCase(streamName)) 76 { 77 out = System.out; 78 } 79 else if (StreamConnector.STREAM_SYSTEM_ERR.equalsIgnoreCase(streamName)) 80 { 81 out = System.err; 82 } 83 else 84 { 85 out = connector.getOutputStream(); 86 } 87 88 if (out == null) 89 { 90 throw new DispatchException(new Message("stream", 1, streamName), event.getMessage(), 91 event.getEndpoint()); 92 } 93 94 if (connector instanceof SystemStreamConnector) 95 { 96 SystemStreamConnector ssc = (SystemStreamConnector)connector; 97 if (StringUtils.isNotBlank(ssc.getOutputMessage())) 98 { 99 out.write(ssc.getOutputMessage().toString().getBytes()); 100 } 101 } 102 103 Object data = event.getTransformedMessage(); 104 if (data instanceof byte[]) 105 { 106 out.write((byte[])data); 107 } 108 else 109 { 110 out.write(data.toString().getBytes()); 111 } 112 113 out.flush(); 114 } 115 116 121 protected UMOMessage doSend(UMOEvent event) throws Exception 122 { 123 doDispatch(event); 124 return event.getMessage(); 125 } 126 127 139 protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 140 { 141 throw new UnsupportedOperationException ("doReceive"); 142 } 143 144 public UMOConnector getConnector() 145 { 146 return connector; 147 } 148 149 protected void doDispose() 150 { 151 } 153 154 protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception 155 { 156 } 158 159 protected void doDisconnect() throws Exception 160 { 161 } 163 } 164 | Popular Tags |