KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > stream > StreamMessageDispatcher


1 /*
2  * $Id: StreamMessageDispatcher.java 3982 2006-11-22 14:28:01Z lajos $
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.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 JavaDoc;
24
25 /**
26  * <code>StreamMessageDispatcher</code> is a simple stream dispatcher that obtains
27  * a stream from the Stream Connector to write to. This is only really useful for
28  * testing purposes right now when writing to System.in and System.out. However, it
29  * is feasible to set any OutputStream on the Stream connector and have that written
30  * to.
31  */

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         // apply connector-specific properties
43
if (connector instanceof SystemStreamConnector)
44         {
45             SystemStreamConnector ssc = (SystemStreamConnector)connector;
46
47             String JavaDoc outputMessage = (String JavaDoc)endpoint.getProperties().get("outputMessage");
48             if (outputMessage != null)
49             {
50                 ssc.setOutputMessage(outputMessage);
51             }
52         }
53     }
54
55     /*
56      * (non-Javadoc)
57      *
58      * @see org.mule.umo.provider.UMOMessageDispatcher#getDelegateSession()
59      */

60     public Object JavaDoc getDelegateSession() throws UMOException
61     {
62         return null;
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.mule.umo.provider.UMOConnector#dispatch(org.mule.umo.UMOEvent)
69      */

70     protected synchronized void doDispatch(UMOEvent event) throws Exception JavaDoc
71     {
72         OutputStream JavaDoc out;
73         String JavaDoc 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 JavaDoc 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     /*
117      * (non-Javadoc)
118      *
119      * @see org.mule.umo.provider.UMOConnector#send(org.mule.umo.UMOEvent)
120      */

121     protected UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
122     {
123         doDispatch(event);
124         return event.getMessage();
125     }
126
127     /**
128      * Make a specific request to the underlying transport
129      *
130      * @param endpoint the endpoint to use when connecting to the resource
131      * @param timeout the maximum time the operation should block before returning.
132      * The call should return immediately if there is data available. If
133      * no data becomes available before the timeout elapses, null will be
134      * returned
135      * @return the result of the request wrapped in a UMOMessage object. Null will be
136      * returned if no data was avaialable
137      * @throws Exception if the call to the underlying protocal cuases an exception
138      */

139     protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
140     {
141         throw new UnsupportedOperationException JavaDoc("doReceive");
142     }
143
144     public UMOConnector getConnector()
145     {
146         return connector;
147     }
148
149     protected void doDispose()
150     {
151         // template method
152
}
153
154     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
155     {
156         // template method
157
}
158
159     protected void doDisconnect() throws Exception JavaDoc
160     {
161         // template method
162
}
163 }
164
Popular Tags