KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > burlap > BurlapWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.burlap;
30
31 import com.caucho.burlap.io.BurlapInput;
32 import com.caucho.burlap.io.BurlapOutput;
33 import com.caucho.hessian.io.HessianRemoteResolver;
34 import com.caucho.util.CharBuffer;
35 import com.caucho.vfs.ReadStream;
36
37 import java.io.OutputStream JavaDoc;
38
39 public class BurlapWriter extends BurlapOutput {
40   private ReadStream _is;
41   private HessianRemoteResolver _resolver;
42   
43   /**
44    * Creates a new Burlap output stream, initialized with an
45    * underlying output stream.
46    *
47    * @param os the underlying output stream.
48    */

49   public BurlapWriter(ReadStream is, OutputStream JavaDoc os)
50   {
51     super(os);
52
53     _is = is;
54   }
55   
56   /**
57    * Creates a new Burlap output stream, initialized with an
58    * underlying output stream.
59    *
60    * @param os the underlying output stream.
61    */

62   public BurlapWriter(OutputStream JavaDoc os)
63   {
64     super(os);
65   }
66
67   /**
68    * Creates an uninitialized Burlap output stream.
69    */

70   public BurlapWriter()
71   {
72   }
73   
74   /**
75    * Initializes the output
76    */

77   public void init(OutputStream JavaDoc os)
78   {
79     _serializerFactory = new BurlapSerializerFactory();
80
81     super.init(os);
82   }
83
84   public void setRemoteResolver(HessianRemoteResolver resolver)
85   {
86     _resolver = resolver;
87   }
88
89   public BurlapInput doCall()
90     throws Throwable JavaDoc
91   {
92     completeCall();
93
94     String JavaDoc status = (String JavaDoc) _is.getAttribute("status");
95
96     if (! "200".equals(status)) {
97       CharBuffer cb = new CharBuffer();
98
99       int ch;
100       while ((ch = _is.readChar()) >= 0)
101         cb.append((char) ch);
102
103       throw new BurlapProtocolException("exception: " + cb);
104     }
105
106     BurlapInput in = new BurlapReader();
107     in.setSerializerFactory(_serializerFactory);
108     in.setRemoteResolver(_resolver);
109     in.init(_is);
110
111     in.startReply();
112
113     return in;
114   }
115
116   public void close()
117   {
118     try {
119       os.close();
120       _is.close();
121     } catch (Exception JavaDoc e) {
122     }
123   }
124 }
125
Popular Tags