View Javadoc

1   /*
2    * $Id: StreamMessageAdapter.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.streaming;
12  
13  import org.mule.impl.ThreadSafeAccess;
14  import org.mule.providers.AbstractMessageAdapter;
15  import org.mule.providers.NullPayload;
16  import org.mule.umo.UMOEvent;
17  import org.mule.umo.provider.OutputHandler;
18  import org.mule.umo.provider.UMOStreamMessageAdapter;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  /**
25   * Provides a generic base class for stream based message flows in Mule. This adapter
26   * represents the 3 flows of data that Mule identifies, namely inbound, outbound and
27   * response flows. These are represented by three streams on the adapter.
28   *
29   */
30  public class StreamMessageAdapter extends AbstractMessageAdapter implements UMOStreamMessageAdapter
31  {
32      /**
33       * Serial version
34       */
35      private static final long serialVersionUID = 6794965828515586752L;
36  
37      protected InputStream in;
38      protected OutputStream out;
39      protected OutputHandler handler;
40      private static NullPayload NULL_PAYLOAD = NullPayload.getInstance();
41  
42      public StreamMessageAdapter(InputStream in)
43      {
44          this.in = in;
45      }
46  
47      public StreamMessageAdapter(InputStream in, OutputStream out)
48      {
49          this.in = in;
50          this.out = out;
51      }
52  
53      public StreamMessageAdapter(OutputHandler handler)
54      {
55          this.handler = handler;
56      }
57  
58      public StreamMessageAdapter(OutputStream out, OutputHandler handler)
59      {
60          this.out = out;
61          this.handler = handler;
62      }
63  
64      public StreamMessageAdapter(InputStream in, OutputStream out, OutputHandler handler)
65      {
66          this.in = in;
67          this.out = out;
68          this.handler = handler;
69      }
70  
71      protected StreamMessageAdapter(StreamMessageAdapter template)
72      {
73          super(template);
74          in = template.in;
75          out = template.out;
76          handler = template.handler;
77      }
78  
79      /**
80       * Converts the message implementation into a String representation
81       * 
82       * @param encoding The encoding to use when transforming the message (if
83       *            necessary). The parameter is used when converting from a byte array
84       * @return String representation of the message payload
85       * @throws Exception Implementation may throw an endpoint specific exception
86       */
87      public String getPayloadAsString(String encoding) throws Exception
88      {
89          throw new UnsupportedOperationException("getPayloadAsString");
90      }
91  
92      /**
93       * Converts the message implementation into a String representation
94       * 
95       * @return String representation of the message
96       * @throws Exception Implemetation may throw an endpoint specific exception
97       */
98      public byte[] getPayloadAsBytes() throws Exception
99      {
100         throw new UnsupportedOperationException("getPayloadAsBytes");
101     }
102 
103     /**
104      * This is an InputStream if triggered from an inbound event or response. If the
105      * Message has a response stream it is assumed that the message the response
106      * stream should be used. If the Message has been triggered from an outbound
107      * request and NullPayload will be used
108      * 
109      * @return the current message
110      */
111     public Object getPayload()
112     {
113         if (in != null)
114         {
115             return in;
116         }
117         return NULL_PAYLOAD;
118     }
119 
120     public InputStream getInputStream()
121     {
122         return in;
123     }
124 
125     public OutputStream getOutputStream()
126     {
127         return out;
128     }
129 
130     public void write(UMOEvent event) throws IOException
131     {
132         handler.write(event, out);
133     }
134 
135     public OutputHandler getOutputHandler()
136     {
137         return handler;
138     }
139 
140     public void setOutputHandler(OutputHandler handler)
141     {
142         this.handler = handler;
143     }
144 
145     /**
146      * The release method is called by Mule to notify this adapter that it is no
147      * longer needed. This method can be used to release any resources that a custom
148      * StreamAdapter may have associated with it.
149      */
150     public void release()
151     {
152         // nothing to do?
153     }
154 
155     public ThreadSafeAccess newThreadCopy()
156     {
157         return new StreamMessageAdapter(this);
158     }
159 
160 }