KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > io > AbstractHessianOutput


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.io;
50
51 import java.io.IOException JavaDoc;
52 import java.io.OutputStream JavaDoc;
53
54 /**
55  * Abstract output stream for Hessian requests.
56  *
57  * <pre>
58  * OutputStream os = ...; // from http connection
59  * AbstractOutput out = new HessianSerializerOutput(os);
60  * String value;
61  *
62  * out.startCall("hello"); // start hello call
63  * out.writeString("arg1"); // write a string argument
64  * out.completeCall(); // complete the call
65  * </pre>
66  */

67 abstract public class AbstractHessianOutput {
68   // serializer factory
69
protected SerializerFactory _serializerFactory;
70
71   /**
72    * Sets the serializer factory.
73    */

74   public void setSerializerFactory(SerializerFactory factory)
75   {
76     _serializerFactory = factory;
77   }
78
79   /**
80    * Gets the serializer factory.
81    */

82   public SerializerFactory getSerializerFactory()
83   {
84     return _serializerFactory;
85   }
86
87   /**
88    * Gets the serializer factory.
89    */

90   protected final SerializerFactory findSerializerFactory()
91   {
92     SerializerFactory factory = _serializerFactory;
93
94     if (factory == null)
95       _serializerFactory = factory = new SerializerFactory();
96
97     return factory;
98   }
99   
100   /**
101    * Initialize the output with a new underlying stream.
102    */

103   public void init(OutputStream JavaDoc os)
104   {
105   }
106
107   /**
108    * Writes a complete method call.
109    */

110   public void call(String JavaDoc method, Object JavaDoc []args)
111     throws IOException JavaDoc
112   {
113     startCall(method);
114     
115     if (args != null) {
116       for (int i = 0; i < args.length; i++)
117         writeObject(args[i]);
118     }
119     
120     completeCall();
121   }
122
123   /**
124    * Starts the method call:
125    *
126    * <code><pre>
127    * c major minor
128    * </pre></code>
129    *
130    * @param method the method name to call.
131    */

132   abstract public void startCall()
133     throws IOException JavaDoc;
134
135   /**
136    * Starts the method call:
137    *
138    * <code><pre>
139    * c major minor
140    * m b16 b8 method-namek
141    * </pre></code>
142    *
143    * @param method the method name to call.
144    */

145   abstract public void startCall(String JavaDoc method)
146     throws IOException JavaDoc;
147
148   /**
149    * Writes a header name. The header value must immediately follow.
150    *
151    * <code><pre>
152    * H b16 b8 foo <em>value</em>
153    * </pre></code>
154    */

155   abstract public void writeHeader(String JavaDoc name)
156     throws IOException JavaDoc;
157
158   /**
159    * Writes the method tag.
160    *
161    * <code><pre>
162    * m b16 b8 method-name
163    * </pre></code>
164    *
165    * @param method the method name to call.
166    */

167   abstract public void writeMethod(String JavaDoc method)
168     throws IOException JavaDoc;
169
170   /**
171    * Completes the method call:
172    *
173    * <code><pre>
174    * z
175    * </pre></code>
176    */

177   abstract public void completeCall()
178     throws IOException JavaDoc;
179
180   /**
181    * Writes a boolean value to the stream. The boolean will be written
182    * with the following syntax:
183    *
184    * <code><pre>
185    * T
186    * F
187    * </pre></code>
188    *
189    * @param value the boolean value to write.
190    */

191   abstract public void writeBoolean(boolean value)
192     throws IOException JavaDoc;
193
194   /**
195    * Writes an integer value to the stream. The integer will be written
196    * with the following syntax:
197    *
198    * <code><pre>
199    * I b32 b24 b16 b8
200    * </pre></code>
201    *
202    * @param value the integer value to write.
203    */

204   abstract public void writeInt(int value)
205     throws IOException JavaDoc;
206
207   /**
208    * Writes a long value to the stream. The long will be written
209    * with the following syntax:
210    *
211    * <code><pre>
212    * L b64 b56 b48 b40 b32 b24 b16 b8
213    * </pre></code>
214    *
215    * @param value the long value to write.
216    */

217   abstract public void writeLong(long value)
218     throws IOException JavaDoc;
219
220   /**
221    * Writes a double value to the stream. The double will be written
222    * with the following syntax:
223    *
224    * <code><pre>
225    * D b64 b56 b48 b40 b32 b24 b16 b8
226    * </pre></code>
227    *
228    * @param value the double value to write.
229    */

230   abstract public void writeDouble(double value)
231     throws IOException JavaDoc;
232
233   /**
234    * Writes a date to the stream.
235    *
236    * <code><pre>
237    * T b64 b56 b48 b40 b32 b24 b16 b8
238    * </pre></code>
239    *
240    * @param time the date in milliseconds from the epoch in UTC
241    */

242   abstract public void writeUTCDate(long time)
243     throws IOException JavaDoc;
244
245   /**
246    * Writes a null value to the stream.
247    * The null will be written with the following syntax
248    *
249    * <code><pre>
250    * N
251    * </pre></code>
252    *
253    * @param value the string value to write.
254    */

255   abstract public void writeNull()
256     throws IOException JavaDoc;
257
258   /**
259    * Writes a string value to the stream using UTF-8 encoding.
260    * The string will be written with the following syntax:
261    *
262    * <code><pre>
263    * S b16 b8 string-value
264    * </pre></code>
265    *
266    * If the value is null, it will be written as
267    *
268    * <code><pre>
269    * N
270    * </pre></code>
271    *
272    * @param value the string value to write.
273    */

274   abstract public void writeString(String JavaDoc value)
275     throws IOException JavaDoc;
276
277   /**
278    * Writes a string value to the stream using UTF-8 encoding.
279    * The string will be written with the following syntax:
280    *
281    * <code><pre>
282    * S b16 b8 string-value
283    * </pre></code>
284    *
285    * If the value is null, it will be written as
286    *
287    * <code><pre>
288    * N
289    * </pre></code>
290    *
291    * @param value the string value to write.
292    */

293   abstract public void writeString(char []buffer, int offset, int length)
294     throws IOException JavaDoc;
295
296   /**
297    * Writes a byte array to the stream.
298    * The array will be written with the following syntax:
299    *
300    * <code><pre>
301    * B b16 b18 bytes
302    * </pre></code>
303    *
304    * If the value is null, it will be written as
305    *
306    * <code><pre>
307    * N
308    * </pre></code>
309    *
310    * @param value the string value to write.
311    */

312   abstract public void writeBytes(byte []buffer)
313     throws IOException JavaDoc;
314   /**
315    * Writes a byte array to the stream.
316    * The array will be written with the following syntax:
317    *
318    * <code><pre>
319    * B b16 b18 bytes
320    * </pre></code>
321    *
322    * If the value is null, it will be written as
323    *
324    * <code><pre>
325    * N
326    * </pre></code>
327    *
328    * @param value the string value to write.
329    */

330   abstract public void writeBytes(byte []buffer, int offset, int length)
331     throws IOException JavaDoc;
332   
333   /**
334    * Writes a byte buffer to the stream.
335    */

336   abstract public void writeByteBufferStart()
337     throws IOException JavaDoc;
338   
339   /**
340    * Writes a byte buffer to the stream.
341    *
342    * <code><pre>
343    * b b16 b18 bytes
344    * </pre></code>
345    *
346    * @param value the string value to write.
347    */

348   abstract public void writeByteBufferPart(byte []buffer,
349                        int offset,
350                        int length)
351     throws IOException JavaDoc;
352   
353   /**
354    * Writes the last chunk of a byte buffer to the stream.
355    *
356    * <code><pre>
357    * b b16 b18 bytes
358    * </pre></code>
359    *
360    * @param value the string value to write.
361    */

362   abstract public void writeByteBufferEnd(byte []buffer,
363                       int offset,
364                       int length)
365     throws IOException JavaDoc;
366
367   /**
368    * Writes a reference.
369    *
370    * <code><pre>
371    * R b32 b24 b16 b8
372    * </pre></code>
373    *
374    * @param value the integer value to write.
375    */

376   abstract public void writeRef(int value)
377     throws IOException JavaDoc;
378
379   /**
380    * Removes a reference.
381    */

382   abstract public boolean removeRef(Object JavaDoc obj)
383     throws IOException JavaDoc;
384
385   /**
386    * Replaces a reference from one object to another.
387    */

388   abstract public boolean replaceRef(Object JavaDoc oldRef, Object JavaDoc newRef)
389     throws IOException JavaDoc;
390
391   /**
392    * Adds an object to the reference list. If the object already exists,
393    * writes the reference, otherwise, the caller is responsible for
394    * the serialization.
395    *
396    * <code><pre>
397    * R b32 b24 b16 b8
398    * </pre></code>
399    *
400    * @param object the object to add as a reference.
401    *
402    * @return true if the object has already been written.
403    */

404   abstract public boolean addRef(Object JavaDoc object)
405     throws IOException JavaDoc;
406
407   /**
408    * Writes a generic object to the output stream.
409    */

410   abstract public void writeObject(Object JavaDoc object)
411     throws IOException JavaDoc;
412
413   /**
414    * Writes the list header to the stream. List writers will call
415    * <code>writeListBegin</code> followed by the list contents and then
416    * call <code>writeListEnd</code>.
417    *
418    * <code><pre>
419    * &lt;list>
420    * &lt;type>java.util.ArrayList&lt;/type>
421    * &lt;length>3&lt;/length>
422    * &lt;int>1&lt;/int>
423    * &lt;int>2&lt;/int>
424    * &lt;int>3&lt;/int>
425    * &lt;/list>
426    * </pre></code>
427    */

428   abstract public boolean writeListBegin(int length, String JavaDoc type)
429     throws IOException JavaDoc;
430
431   /**
432    * Writes the tail of the list to the stream.
433    */

434   abstract public void writeListEnd()
435     throws IOException JavaDoc;
436
437   /**
438    * Writes the map header to the stream. Map writers will call
439    * <code>writeMapBegin</code> followed by the map contents and then
440    * call <code>writeMapEnd</code>.
441    *
442    * <code><pre>
443    * Mt b16 b8 type (<key> <value>)z
444    * </pre></code>
445    */

446   abstract public void writeMapBegin(String JavaDoc type)
447     throws IOException JavaDoc;
448
449   /**
450    * Writes the tail of the map to the stream.
451    */

452   abstract public void writeMapEnd()
453     throws IOException JavaDoc;
454
455   /**
456    * Writes the object header to the stream (for Hessian 2.0), or a
457    * Map for Hessian 1.0. Object writers will call
458    * <code>writeObjectBegin</code> followed by the map contents and then
459    * call <code>writeObjectEnd</code>.
460    *
461    * <code><pre>
462    * Ot b16 b8 type (<key> <value>)* z
463    * o b32 b24 b16 b8 <value>* z
464    * </pre></code>
465    *
466    * @return true if the object has already been defined.
467    */

468   public int writeObjectBegin(String JavaDoc type)
469     throws IOException JavaDoc
470   {
471     writeMapBegin(type);
472     
473     return -1;
474   }
475
476   /**
477    * Writes the end of the class.
478    */

479   public void writeClassFieldLength(int len)
480     throws IOException JavaDoc
481   {
482   }
483
484   /**
485    * Writes the tail of the object to the stream.
486    */

487   public void writeObjectEnd()
488     throws IOException JavaDoc
489   {
490   }
491
492   /**
493    * Writes a remote object reference to the stream. The type is the
494    * type of the remote interface.
495    *
496    * <code><pre>
497    * 'r' 't' b16 b8 type url
498    * </pre></code>
499    */

500   abstract public void writeRemote(String JavaDoc type, String JavaDoc url)
501     throws IOException JavaDoc;
502   
503   public void startReply()
504     throws IOException JavaDoc
505   {
506   }
507   
508   public void completeReply()
509     throws IOException JavaDoc
510   {
511   }
512   
513   public void writeFault(String JavaDoc code, String JavaDoc message, Object JavaDoc detail)
514     throws IOException JavaDoc
515   {
516   }
517
518   public void flush()
519     throws IOException JavaDoc
520   {
521   }
522
523   public void close()
524     throws IOException JavaDoc
525   {
526   }
527 }
528
Popular Tags