View Javadoc

1   /*
2    * $Id: StreamMessageDispatcher.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.providers.stream;
12  
13  import org.mule.providers.AbstractMessageDispatcher;
14  import org.mule.providers.stream.i18n.StreamMessages;
15  import org.mule.umo.UMOEvent;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.endpoint.UMOImmutableEndpoint;
18  import org.mule.umo.provider.DispatchException;
19  import org.mule.util.StringUtils;
20  
21  import java.io.OutputStream;
22  
23  /**
24   * <code>StreamMessageDispatcher</code> is a simple stream dispatcher that obtains
25   * a stream from the Stream Connector to write to. This is only really useful for
26   * testing purposes right now when writing to System.in and System.out. However, it
27   * is feasible to set any OutputStream on the Stream connector and have that written
28   * to.
29   */
30  
31  public class StreamMessageDispatcher extends AbstractMessageDispatcher
32  {
33      private final StreamConnector connector;
34  
35      public StreamMessageDispatcher(UMOImmutableEndpoint endpoint)
36      {
37          super(endpoint);
38          this.connector = (StreamConnector)endpoint.getConnector();
39  
40          // apply connector-specific properties
41          if (connector instanceof SystemStreamConnector)
42          {
43              SystemStreamConnector ssc = (SystemStreamConnector)connector;
44  
45              String outputMessage = (String)endpoint.getProperties().get("outputMessage");
46              if (outputMessage != null)
47              {
48                  ssc.setOutputMessage(outputMessage);
49              }
50          }
51      }
52  
53      protected synchronized void doDispatch(UMOEvent event) throws Exception
54      {
55          OutputStream out = connector.getOutputStream();
56  
57          if (out == null)
58          {
59              throw new DispatchException(
60                  StreamMessages.couldNotFindStreamWithName(event.getEndpoint().getEndpointURI().getAddress()), 
61                  event.getMessage(), event.getEndpoint());
62          }
63  
64          if (connector instanceof SystemStreamConnector)
65          {
66              SystemStreamConnector ssc = (SystemStreamConnector)connector;
67              if (StringUtils.isNotBlank(ssc.getOutputMessage()))
68              {
69                  out.write(ssc.getOutputMessage().getBytes());
70              }
71          }
72  
73          Object data = event.getTransformedMessage();
74          if (data instanceof byte[])
75          {
76              out.write((byte[])data);
77          }
78          else
79          {
80              out.write(data.toString().getBytes());
81          }
82  
83          out.flush();
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see org.mule.umo.provider.UMOConnector#send(org.mule.umo.UMOEvent)
90       */
91      protected UMOMessage doSend(UMOEvent event) throws Exception
92      {
93          doDispatch(event);
94          return event.getMessage();
95      }
96  
97      /**
98       * Make a specific request to the underlying transport
99       * 
100      * @param timeout the maximum time the operation should block before returning.
101      *            The call should return immediately if there is data available. If
102      *            no data becomes available before the timeout elapses, null will be
103      *            returned
104      * @return the result of the request wrapped in a UMOMessage object. Null will be
105      *         returned if no data was avaialable
106      * @throws Exception if the call to the underlying protocal cuases an exception
107      */
108     protected UMOMessage doReceive(long timeout) throws Exception
109     {
110         throw new UnsupportedOperationException("doReceive");
111     }
112 
113     protected void doDispose()
114     {
115         // template method
116     }
117 
118     protected void doConnect() throws Exception
119     {
120         // template method
121     }
122 
123     protected void doDisconnect() throws Exception
124     {
125         // template method
126     }
127 
128 
129 
130 }