KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > dwrp > BaseScriptConduit


1 package org.directwebremoting.dwrp;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5
6 import javax.servlet.http.HttpServletResponse JavaDoc;
7
8 import org.directwebremoting.extend.ConverterManager;
9 import org.directwebremoting.extend.EnginePrivate;
10 import org.directwebremoting.extend.ScriptConduit;
11 import org.directwebremoting.util.DebuggingPrintWriter;
12 import org.directwebremoting.util.Logger;
13
14 /**
15  * A ScriptConduit that works with the parent Marshaller.
16  * In some ways this is nasty because it has access to essentially private parts
17  * of PollHandler, however there is nowhere sensible to store them
18  * within that class, so this is a hacky simplification.
19  * @author Joe Walker [joe at getahead dot ltd dot uk]
20  */

21 public abstract class BaseScriptConduit extends ScriptConduit
22 {
23     /**
24      * Simple ctor
25      * @param response Used to flush output
26      * @param batchId The id of the batch that we are responding to
27      * @param converterManager How we convert objects to script
28      * @throws IOException If stream ops fail
29      */

30     public BaseScriptConduit(HttpServletResponse JavaDoc response, String JavaDoc batchId, ConverterManager converterManager) throws IOException JavaDoc
31     {
32         super(RANK_FAST);
33
34         this.response = response;
35         this.batchId = batchId;
36         this.converterManager = converterManager;
37
38         response.setContentType(getOutboundMimeType());
39
40         if (log.isDebugEnabled())
41         {
42             // This might be considered evil - altering the program flow
43
// depending on the log status, however DebuggingPrintWriter is
44
// very thin and only about debugging
45
out = new DebuggingPrintWriter("", response.getWriter());
46         }
47         else
48         {
49             out = response.getWriter();
50         }
51
52         // Setup a debugging prefix
53
if (out instanceof DebuggingPrintWriter)
54         {
55             DebuggingPrintWriter dpw = (DebuggingPrintWriter) out;
56             dpw.setPrefix("out(" + hashCode() + "): ");
57         }
58
59         beginStream();
60     }
61
62     /**
63      * What mime type should we send to the browser for this data?
64      * @return A mime-type
65      */

66     protected abstract String JavaDoc getOutboundMimeType();
67
68     /**
69      * Called when we are initially setting up the stream. This does not send
70      * any data to the client, just sets it up for data.
71      * <p>This method is always called exactly once in the lifetime of a
72      * conduit, after {@link #preStreamSetup()} and before any scripts are sent.
73      */

74     protected abstract void beginStream();
75
76     /**
77      * Called when we are shutting the stream down.
78      * <p>This method is always called exactly once in the lifetime of a
79      * conduit, just before the stream is closed.
80      */

81     protected abstract void endStream();
82
83     /**
84      * A poll has finished, get the client to call us back
85      * @param timetoNextPoll How long before we tell the browser to come back?
86      * @throws IOException
87      */

88     public void close(int timetoNextPoll) throws IOException JavaDoc
89     {
90         try
91         {
92             EnginePrivate.remoteHandleCallback(this, batchId, "0", new Integer JavaDoc(timetoNextPoll));
93         }
94         catch (Exception JavaDoc ex)
95         {
96             EnginePrivate.remoteHandleException(this, batchId, "0", ex);
97             log.warn("--Erroring: batchId[" + batchId + "] message[" + ex.toString() + ']', ex);
98         }
99
100         endStream();
101     }
102
103     /**
104      * Ensure that output we have done is written to the client
105      * @return true/false depending on the write status
106      */

107     protected boolean flush()
108     {
109         out.flush();
110
111         // I'm not totally sure if this is the right thing to do.
112
// A PrintWriter that encounters an error never recovers so maybe
113
// we could be more robust by using a lower level object and
114
// working out what to do if something goes wrong. Annoyingly
115
// PrintWriter also throws the original exception away.
116
if (out.checkError())
117         {
118             log.debug("Error writing to stream");
119             // throw new IOException("Error writing to stream");
120
return false;
121         }
122
123         try
124         {
125             response.flushBuffer();
126             return true;
127         }
128         catch (IOException JavaDoc ex)
129         {
130             // This is likely to be because the user has gone away. Maybe
131
// we should do something clever like remove the script session?
132
log.debug("Error writing to HTTP response:" + ex);
133             return false;
134         }
135     }
136
137     /**
138      * How we convert parameters
139      */

140     protected ConverterManager converterManager = null;
141
142     /**
143      * Used to flush data to the output stream
144      */

145     protected final HttpServletResponse JavaDoc response;
146
147     /**
148      * The PrintWriter to send output to, and that we should synchronize against
149      */

150     protected final PrintWriter JavaDoc out;
151
152     /**
153      * What is the ID of the request that we are responding to?
154      */

155     protected final String JavaDoc batchId;
156
157     /**
158      * The log stream
159      */

160     private static final Logger log = Logger.getLogger(BaseScriptConduit.class);
161
162     /**
163      * The slab of data we send to IE to get it to stream
164      */

165     protected static final String JavaDoc fourKFlushData;
166     static
167     {
168         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(409600);
169         for (int i = 0; i < 4096; i++)
170         {
171             buffer.append(" ");
172         }
173         fourKFlushData = buffer.toString();
174     }
175 }
Popular Tags