KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > streaming > StreamMessageAdapter


1 /*
2  * $Id: StreamMessageAdapter.java 3798 2006-11-04 04:07:14Z aperepel $
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.streaming;
12
13 import org.mule.providers.AbstractMessageAdapter;
14 import org.mule.providers.NullPayload;
15 import org.mule.umo.UMOEvent;
16 import org.mule.umo.provider.OutputHandler;
17 import org.mule.umo.provider.UMOStreamMessageAdapter;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 /**
24  * Provides a generic base class for stream based message flows in Mule. This adapter
25  * represents the 3 flows of data that Mule identifies, namely inbound, outbound and
26  * response flows. These are represented by three streams on the adapter.
27  *
28  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
29  * @version $Revision: 3798 $
30  */

31 public class StreamMessageAdapter extends AbstractMessageAdapter implements UMOStreamMessageAdapter
32 {
33     /**
34      * Serial version
35      */

36     private static final long serialVersionUID = 6794965828515586752L;
37
38     protected InputStream JavaDoc in;
39     protected InputStream JavaDoc response;
40     protected OutputStream JavaDoc out;
41     protected OutputHandler handler;
42     private NullPayload nullPayload = new NullPayload();
43
44     public StreamMessageAdapter(InputStream JavaDoc in)
45     {
46         this.in = in;
47     }
48
49     public StreamMessageAdapter(InputStream JavaDoc in, OutputStream JavaDoc out)
50     {
51         this.in = in;
52         this.out = out;
53     }
54
55     public StreamMessageAdapter(OutputHandler handler)
56     {
57         this.handler = handler;
58     }
59
60     public StreamMessageAdapter(OutputStream JavaDoc out, OutputHandler handler)
61     {
62         this.out = out;
63         this.handler = handler;
64     }
65
66     public StreamMessageAdapter(InputStream JavaDoc in, OutputStream JavaDoc out, OutputHandler handler)
67     {
68         this.in = in;
69         this.out = out;
70         this.handler = handler;
71     }
72
73     /**
74      * Converts the message implementation into a String representation
75      *
76      * @param encoding The encoding to use when transforming the message (if
77      * necessary). The parameter is used when converting from a byte array
78      * @return String representation of the message payload
79      * @throws Exception Implementation may throw an endpoint specific exception
80      */

81     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
82     {
83         throw new UnsupportedOperationException JavaDoc("getPayloadAsString");
84     }
85
86     /**
87      * Converts the message implementation into a String representation
88      *
89      * @return String representation of the message
90      * @throws Exception Implemetation may throw an endpoint specific exception
91      */

92     public byte[] getPayloadAsBytes() throws Exception JavaDoc
93     {
94         throw new UnsupportedOperationException JavaDoc("getPayloadAsBytes");
95     }
96
97     /**
98      * This is an InputStream if triggered from an inbound event or response. If the
99      * Message has a response stream it is assumed that the message the response
100      * stream should be used. If the Message has been triggered from an outbound
101      * request and NullPayload will be used
102      *
103      * @return the current message
104      */

105     public Object JavaDoc getPayload()
106     {
107         if (in != null)
108         {
109             return in;
110         }
111         return nullPayload;
112     }
113
114     public InputStream JavaDoc getInputStream()
115     {
116         return in;
117     }
118
119     public OutputStream JavaDoc getOutputStream()
120     {
121         return out;
122     }
123
124     public void write(UMOEvent event) throws IOException JavaDoc
125     {
126         handler.write(event, out);
127     }
128
129     public OutputHandler getOutputHandler()
130     {
131         return handler;
132     }
133
134     public void setOutputHandler(OutputHandler handler)
135     {
136         this.handler = handler;
137     }
138
139     /**
140      * The release method is called by Mule to notify this adapter that it is no
141      * longer needed. This method can be used to release any resources that a custom
142      * StreamAdapter may have associated with it.
143      */

144     public void release()
145     {
146         // nothing to do?
147
}
148
149 }
150
Popular Tags