KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > extend > EnginePrivate


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.extend;
17
18 import java.io.IOException JavaDoc;
19
20 import org.directwebremoting.ScriptBuffer;
21 import org.directwebremoting.dwrp.ProtocolConstants;
22 import org.directwebremoting.impl.DefaultRemoter;
23 import org.directwebremoting.proxy.ScriptProxy;
24 import org.directwebremoting.util.JavascriptUtil;
25 import org.directwebremoting.util.Logger;
26
27 /**
28  * An abstraction of the DWREngine Javascript class for use by
29  * {@link org.directwebremoting.dwrp.BaseCallMarshaller},
30  * {@link org.directwebremoting.dwrp.PollHandler} and a few others that need
31  * to call internal functions in engine.js
32  * @author Joe Walker [joe at getahead dot ltd dot uk]
33  */

34 public class EnginePrivate extends ScriptProxy
35 {
36     /**
37      * Call the dwr.engine._remoteHandleResponse() in the browser
38      * @param conduit The browser pipe to write to
39      * @param batchId The identifier of the batch that we are handling a response for
40      * @param callId The identifier of the call that we are handling a response for
41      * @param data The data to pass to the callback function
42      * @throws IOException If writing fails.
43      * @throws MarshallException If objects in the script can not be marshalled
44      */

45     public static void remoteHandleCallback(ScriptConduit conduit, String JavaDoc batchId, String JavaDoc callId, Object JavaDoc data) throws IOException JavaDoc, MarshallException
46     {
47         ScriptBuffer script = new ScriptBuffer();
48         script.appendScript("dwr.engine._remoteHandleCallback(\'")
49               .appendScript(batchId)
50               .appendScript("\',\'")
51               .appendScript(callId)
52               .appendScript("\',")
53               .appendData(data)
54               .appendScript(");");
55
56         conduit.addScript(script);
57     }
58
59     /**
60      * Call the dwr.engine._remoteHandleException() in the browser
61      * @param conduit The browser pipe to write to
62      * @param batchId The identifier of the batch that we are handling a response for
63      * @param callId The id of the call we are replying to
64      * @param ex The exception to throw on the remote end
65      * @throws IOException If writing fails.
66      */

67     public static void remoteHandleMarshallException(ScriptConduit conduit, String JavaDoc batchId, String JavaDoc callId, MarshallException ex) throws IOException JavaDoc
68     {
69         try
70         {
71             ScriptBuffer script = new ScriptBuffer();
72             script.appendScript("dwr.engine._remoteHandleException(\'")
73                   .appendScript(batchId)
74                   .appendScript("\',\'")
75                   .appendScript(callId)
76                   .appendScript("\',")
77                   .appendData(ex)
78                   .appendScript(");");
79
80             conduit.addScript(script);
81         }
82         catch (MarshallException mex)
83         {
84             ScriptBuffer script = new ScriptBuffer();
85             script.appendScript("dwr.engine._remoteHandleException(\'")
86                   .appendScript(batchId)
87                   .appendScript("\',\'")
88                   .appendScript(callId)
89                   .appendScript("\',")
90                   .appendData(ex)
91                   .appendScript(");");
92
93             addScriptWithoutException(conduit, script);
94         }
95     }
96
97     /**
98      * Call the dwr.engine._remoteHandleException() in the browser
99      * @param conduit The browser pipe to write to
100      * @param batchId The identifier of the batch that we are handling a response for
101      * @param callId The id of the call we are replying to
102      * @param ex The exception to throw on the remote end
103      * @throws IOException If writing fails.
104      */

105     public static void remoteHandleException(ScriptConduit conduit, String JavaDoc batchId, String JavaDoc callId, Throwable JavaDoc ex) throws IOException JavaDoc
106     {
107         try
108         {
109             ScriptBuffer script = new ScriptBuffer();
110             script.appendScript("dwr.engine._remoteHandleException(\'")
111                   .appendScript(batchId)
112                   .appendScript("\',\'")
113                   .appendScript(callId)
114                   .appendScript("\',")
115                   .appendData(ex)
116                   .appendScript(");");
117
118             conduit.addScript(script);
119         }
120         catch (MarshallException mex)
121         {
122             ScriptBuffer script = new ScriptBuffer();
123             script.appendScript("dwr.engine._remoteHandleException(\'")
124                   .appendScript(batchId)
125                   .appendScript("\',\'")
126                   .appendScript(callId)
127                   .appendScript("\',")
128                   .appendData(ex)
129                   .appendScript(");");
130
131             addScriptWithoutException(conduit, script);
132         }
133     }
134
135     /**
136      * Call the dwr.engine._remoteHandleServerException() in the browser
137      * @param batchId The identifier of the batch that we are handling a response for
138      * @param ex The execption from which we make a reply
139      * @return The string that calls dwr.engine._remoteHandleServerException()
140      */

141     public static String JavaDoc getRemoteHandleBatchExceptionScript(String JavaDoc batchId, Exception JavaDoc ex)
142     {
143         StringBuffer JavaDoc reply = new StringBuffer JavaDoc();
144
145         String JavaDoc output = JavascriptUtil.escapeJavaScript(ex.getMessage());
146         String JavaDoc params = "{ name:'" + ex.getClass().getName() + "', message:'" + output + "' }";
147         if (batchId != null)
148         {
149             params += ", '" + batchId + "'";
150         }
151
152         reply.append(ProtocolConstants.SCRIPT_CALL_REPLY).append("\r\n");
153         reply.append("if (window.dwr) dwr.engine._remoteHandleBatchException(").append(params).append(");\r\n");
154         reply.append("else if (window.parent.dwr) window.parent.dwr.engine._remoteHandleBatchException(").append(params).append(");\r\n");
155
156         return reply.toString();
157     }
158
159     /**
160      * Call the dwr.engine._remotePollCometDisabled() in the browser
161      * @param batchId The identifier of the batch that we are handling a response for
162      * @return The string that calls dwr.engine._remoteHandleServerException()
163      */

164     public static String JavaDoc getRemotePollCometDisabledScript(String JavaDoc batchId)
165     {
166         StringBuffer JavaDoc reply = new StringBuffer JavaDoc();
167
168         String JavaDoc params = "{ name:'dwr.engine.pollAndCometDisabled', message:'Polling and Comet are disabled. See the server logs.' }";
169         if (batchId != null)
170         {
171             params += ", '" + batchId + "'";
172         }
173
174         reply.append(ProtocolConstants.SCRIPT_CALL_REPLY).append("\r\n");
175         reply.append("if (window.dwr) dwr.engine._remotePollCometDisabled(").append(params).append(");\r\n");
176         reply.append("else if (window.parent.dwr) window.parent.dwr.engine._remotePollCometDisabled(").append(params).append(");\r\n");
177
178         return reply.toString();
179     }
180
181     /**
182      * Take an XML string, and convert it into some Javascript that when
183      * executed will return a DOM object that represents the same XML object
184      * @param xml The XML string to convert
185      * @return The Javascript
186      */

187     public static String JavaDoc xmlStringToJavascriptDom(String JavaDoc xml)
188     {
189         String JavaDoc xmlout = JavascriptUtil.escapeJavaScript(xml);
190         return "dwr.engine._unserializeDocument(\"" + xmlout + "\")";
191     }
192
193     /**
194      * Get a string which will initialize a dwr.engine object
195      * @return A dwr.engine init script
196      */

197     public static String JavaDoc getEngineInitScript()
198     {
199         return "// Provide a default path to dwr.engine\n" +
200                "if (dwr == null) var dwr = {};\n" +
201                "if (dwr.engine == null) dwr.engine = {};\n" +
202                "if (DWREngine == null) var DWREngine = dwr.engine;\n" +
203                '\n';
204     }
205
206     /**
207      * {@link DefaultRemoter} needs to know the name of the execute function
208      * @return The execute function name
209      */

210     public static String JavaDoc getExecuteFunctionName()
211     {
212         return "dwr.engine._execute";
213     }
214
215     /**
216      * A script to send at the beginning of an iframe response
217      * @param batchId The id of the current batch
218      * @param useWindowParent Will the exec happen from a child iframe which is
219      * the case for normal iframe based calls, or from the main window, which is
220      * the case for iframe streamed polling.
221      * @return A script to init the environment
222      */

223     public static String JavaDoc remoteBeginIFrameResponse(String JavaDoc batchId, boolean useWindowParent)
224     {
225         String JavaDoc prefix = "";
226         if (useWindowParent)
227         {
228             prefix = "window.parent.";
229         }
230
231         return prefix + "dwr.engine._remoteBeginIFrameResponse(this.frameElement"+(batchId == null?"":", '" + batchId+"'") + ");";
232     }
233
234     /**
235      * A script to send at the end of an iframe response
236      * @param batchId The id of the current batch
237      * @param useWindowParent Will the exec happen from a child iframe which is
238      * the case for normal iframe based calls, or from the main window, which is
239      * the case for iframe streamed polling.
240      * @return A script to tidy up the environment
241      */

242     public static String JavaDoc remoteEndIFrameResponse(String JavaDoc batchId, boolean useWindowParent)
243     {
244         String JavaDoc prefix = "";
245         if (useWindowParent)
246         {
247             prefix = "window.parent.";
248         }
249
250         return prefix + "dwr.engine._remoteEndIFrameResponse("+(batchId == null?"":"'" + batchId+"'")+");";
251     }
252
253     /**
254      * Prepare a script for execution in an iframe environment
255      * @param script The script to modify
256      * @return The modified script
257      */

258     public static String JavaDoc remoteEval(String JavaDoc script)
259     {
260         return "window.parent.dwr.engine._eval(\"" + JavascriptUtil.escapeJavaScript(script) + "\");";
261     }
262
263     /**
264      * Calling {@link ScriptConduit#addScript(ScriptBuffer)} throws a
265      * {@link MarshallException} which we want to ignore since it almost
266      * certainly can't happen for real.
267      * @param conduit The browser pipe to write to
268      * @param script The buffer to add to the current {@link ScriptConduit}
269      * @throws IOException If the write process fails
270      */

271     private static void addScriptWithoutException(ScriptConduit conduit, ScriptBuffer script) throws IOException JavaDoc
272     {
273         try
274         {
275             conduit.addScript(script);
276         }
277         catch (MarshallException ex)
278         {
279             log.warn("This exception can't happen", ex);
280         }
281     }
282
283     /**
284      * The log stream
285      */

286     private static final Logger log = Logger.getLogger(EnginePrivate.class);
287 }
288
Popular Tags