KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > HessianWriter


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.hessian;
31
32 import com.caucho.hessian.io.HessianInput;
33 import com.caucho.hessian.io.HessianProtocolException;
34 import com.caucho.hessian.io.HessianRemoteResolver;
35 import com.caucho.hessian.io.HessianSerializerOutput;
36 import com.caucho.transaction.TransactionImpl;
37 import com.caucho.transaction.TransactionManagerImpl;
38 import com.caucho.util.CharBuffer;
39 import com.caucho.vfs.ReadStream;
40
41 import javax.ejb.EJBHome JavaDoc;
42 import javax.ejb.EJBObject JavaDoc;
43 import javax.ejb.Handle JavaDoc;
44 import javax.ejb.HomeHandle JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48
49 public class HessianWriter extends HessianSerializerOutput {
50   private InputStream JavaDoc _is;
51   private HessianRemoteResolver _resolver;
52   
53   /**
54    * Creates a new Hessian output stream, initialized with an
55    * underlying output stream.
56    *
57    * @param os the underlying output stream.
58    */

59   public HessianWriter(InputStream JavaDoc is, OutputStream JavaDoc os)
60   {
61     super(os);
62
63     _is = is;
64   }
65   
66   /**
67    * Creates a new Hessian output stream, initialized with an
68    * underlying output stream.
69    *
70    * @param os the underlying output stream.
71    */

72   public HessianWriter(OutputStream JavaDoc os)
73   {
74     super(os);
75   }
76
77   /**
78    * Creates an uninitialized Hessian output stream.
79    */

80   public HessianWriter()
81   {
82   }
83   
84   /**
85    * Initializes the output
86    */

87   public void init(OutputStream JavaDoc os)
88   {
89     _serializerFactory = new QSerializerFactory();
90
91     super.init(os);
92   }
93
94   public void setRemoteResolver(HessianRemoteResolver resolver)
95   {
96     _resolver = resolver;
97   }
98
99   public HessianInput doCall()
100     throws Throwable JavaDoc
101   {
102     completeCall();
103
104     if (! (_is instanceof ReadStream))
105       throw new IllegalStateException JavaDoc("Hessian call requires ReadStream");
106
107     ReadStream is = (ReadStream) _is;
108
109     String JavaDoc status = (String JavaDoc) is.getAttribute("status");
110
111     if (! "200".equals(status)) {
112       CharBuffer cb = new CharBuffer();
113
114       int ch;
115       while ((ch = is.readChar()) >= 0)
116         cb.append((char) ch);
117
118       throw new HessianProtocolException("exception: " + cb);
119     }
120
121     HessianInput in = new HessianReader();
122     in.setSerializerFactory(_serializerFactory);
123     in.setRemoteResolver(_resolver);
124     in.init(_is);
125
126     in.startReply();
127
128     String JavaDoc header;
129
130     while ((header = in.readHeader()) != null) {
131       Object JavaDoc value = in.readObject();
132
133       if (header.equals("xa-resource")) {
134     TransactionImpl xa = (TransactionImpl) TransactionManagerImpl.getInstance().getTransaction();
135
136     if (xa != null) {
137       HessianXAResource xaRes = new HessianXAResource((String JavaDoc) value);
138
139       xa.enlistResource(xaRes);
140     }
141       }
142     }
143
144     return in;
145   }
146
147   public void close()
148   {
149     try {
150       os.close();
151       _is.close();
152     } catch (Exception JavaDoc e) {
153     }
154   }
155   
156   /**
157    * Applications which override this can do custom serialization.
158    *
159    * @param object the object to write.
160    */

161   public void writeObjectImpl(Object JavaDoc obj)
162     throws IOException JavaDoc
163   {
164     if (obj instanceof EJBObject JavaDoc) {
165       EJBObject JavaDoc ejbObject = (EJBObject JavaDoc) obj;
166       EJBHome JavaDoc ejbHome = ejbObject.getEJBHome();
167       
168       Handle JavaDoc handle = ejbObject.getHandle();
169       
170       if (handle instanceof HessianHandle) {
171         HessianHandle hessianHandle = (HessianHandle) handle;
172
173         Class JavaDoc api = ejbHome.getEJBMetaData().getRemoteInterfaceClass();
174
175         writeRemote(api.getName(), hessianHandle.getURL());
176         return;
177       }
178     }
179     else if (obj instanceof EJBHome JavaDoc) {
180       EJBHome JavaDoc ejbHome = (EJBHome JavaDoc) obj;
181       
182       HomeHandle JavaDoc handle = ejbHome.getHomeHandle();
183       
184       if (handle instanceof HessianHomeHandle) {
185         HessianHomeHandle hessianHandle = (HessianHomeHandle) handle;
186
187         Class JavaDoc api = ejbHome.getEJBMetaData().getHomeInterfaceClass();
188
189         writeRemote(api.getName(), hessianHandle.getURL("hessian"));
190         return;
191       }
192     }
193     
194     super.writeObjectImpl(obj);
195   }
196 }
197
Popular Tags