KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > caucho > hessian > HessianCauchoOutput


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.remote.caucho.hessian;
10
11 import java.io.IOException JavaDoc;
12 import java.io.OutputStream JavaDoc;
13
14 import com.caucho.hessian.io.HessianOutput;
15 import mx4j.tools.remote.caucho.CauchoOutput;
16 import mx4j.tools.remote.caucho.serialization.JMXSerializerFactory;
17
18 /**
19  * @version $Revision: 1.3 $
20  */

21 class HessianCauchoOutput implements CauchoOutput
22 {
23    private final OutputStream JavaDoc stream;
24    private final HessianOutput output;
25
26    HessianCauchoOutput(OutputStream JavaDoc stream)
27    {
28       this.stream = stream;
29       this.output = new HessianOutput();
30       output.setSerializerFactory(new JMXSerializerFactory());
31       output.init(stream);
32    }
33
34    public void startReply() throws IOException JavaDoc
35    {
36       output.startReply();
37    }
38
39    public void completeReply() throws IOException JavaDoc
40    {
41       output.completeReply();
42    }
43
44    /**
45     * A bug in the Hessian protocol requires this hack: headers cannot be written using
46     * HessianOutput, since the HessianOutput.startCall() already writes the method name,
47     * while HessianInput expects the headers before the method name
48     *
49     * @see #writeMethod
50     */

51    public void startCall() throws IOException JavaDoc
52    {
53       stream.write(99);
54       stream.write(0);
55       stream.write(1);
56    }
57
58    public void completeCall() throws IOException JavaDoc
59    {
60       output.completeCall();
61    }
62
63    public void writeHeader(String JavaDoc header) throws IOException JavaDoc
64    {
65       output.writeHeader(header);
66    }
67
68    /**
69     * Same hack as {@link #startCall}: this method is missing from HessianOutput
70     */

71    public void writeMethod(String JavaDoc methodName) throws IOException JavaDoc
72    {
73       stream.write(109);
74       int len = methodName.length();
75       stream.write(len >> 8);
76       stream.write(len);
77       output.printString(methodName, 0, len);
78    }
79
80    public void writeObject(Object JavaDoc object) throws IOException JavaDoc
81    {
82       output.writeObject(object);
83    }
84
85    public void writeFault(Throwable JavaDoc fault) throws IOException JavaDoc
86    {
87       output.writeFault(fault.getClass().getName(), fault.getMessage(), fault);
88    }
89 }
90
Popular Tags