001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.hessian;
031:
032: import com.caucho.hessian.io.HessianInput;
033: import com.caucho.hessian.io.HessianProtocolException;
034: import com.caucho.hessian.io.HessianRemoteResolver;
035: import com.caucho.hessian.io.HessianSerializerOutput;
036: import com.caucho.transaction.TransactionImpl;
037: import com.caucho.transaction.TransactionManagerImpl;
038: import com.caucho.util.CharBuffer;
039: import com.caucho.vfs.ReadStream;
040:
041: import javax.ejb.EJBHome;
042: import javax.ejb.EJBObject;
043: import javax.ejb.Handle;
044: import javax.ejb.HomeHandle;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048:
049: public class HessianWriter extends HessianSerializerOutput {
050: private InputStream _is;
051: private HessianRemoteResolver _resolver;
052:
053: /**
054: * Creates a new Hessian output stream, initialized with an
055: * underlying output stream.
056: *
057: * @param os the underlying output stream.
058: */
059: public HessianWriter(InputStream is, OutputStream os) {
060: super (os);
061:
062: _is = is;
063: }
064:
065: /**
066: * Creates a new Hessian output stream, initialized with an
067: * underlying output stream.
068: *
069: * @param os the underlying output stream.
070: */
071: public HessianWriter(OutputStream os) {
072: super (os);
073: }
074:
075: /**
076: * Creates an uninitialized Hessian output stream.
077: */
078: public HessianWriter() {
079: }
080:
081: /**
082: * Initializes the output
083: */
084: public void init(OutputStream os) {
085: _serializerFactory = new QSerializerFactory();
086:
087: super .init(os);
088: }
089:
090: public void setRemoteResolver(HessianRemoteResolver resolver) {
091: _resolver = resolver;
092: }
093:
094: public HessianInput doCall() throws Throwable {
095: completeCall();
096:
097: if (!(_is instanceof ReadStream))
098: throw new IllegalStateException(
099: "Hessian call requires ReadStream");
100:
101: ReadStream is = (ReadStream) _is;
102:
103: String status = (String) is.getAttribute("status");
104:
105: if (!"200".equals(status)) {
106: CharBuffer cb = new CharBuffer();
107:
108: int ch;
109: while ((ch = is.readChar()) >= 0)
110: cb.append((char) ch);
111:
112: throw new HessianProtocolException("exception: " + cb);
113: }
114:
115: HessianInput in = new HessianReader();
116: in.setSerializerFactory(_serializerFactory);
117: in.setRemoteResolver(_resolver);
118: in.init(_is);
119:
120: in.startReply();
121:
122: String header;
123:
124: while ((header = in.readHeader()) != null) {
125: Object value = in.readObject();
126:
127: if (header.equals("xa-resource")) {
128: TransactionImpl xa = (TransactionImpl) TransactionManagerImpl
129: .getInstance().getTransaction();
130:
131: if (xa != null) {
132: HessianXAResource xaRes = new HessianXAResource(
133: (String) value);
134:
135: xa.enlistResource(xaRes);
136: }
137: }
138: }
139:
140: return in;
141: }
142:
143: public void close() {
144: try {
145: os.close();
146: _is.close();
147: } catch (Exception e) {
148: }
149: }
150:
151: /**
152: * Applications which override this can do custom serialization.
153: *
154: * @param object the object to write.
155: */
156: public void writeObjectImpl(Object obj) throws IOException {
157: if (obj instanceof EJBObject) {
158: EJBObject ejbObject = (EJBObject) obj;
159: EJBHome ejbHome = ejbObject.getEJBHome();
160:
161: Handle handle = ejbObject.getHandle();
162:
163: if (handle instanceof HessianHandle) {
164: HessianHandle hessianHandle = (HessianHandle) handle;
165:
166: Class api = ejbHome.getEJBMetaData()
167: .getRemoteInterfaceClass();
168:
169: writeRemote(api.getName(), hessianHandle.getURL());
170: return;
171: }
172: } else if (obj instanceof EJBHome) {
173: EJBHome ejbHome = (EJBHome) obj;
174:
175: HomeHandle handle = ejbHome.getHomeHandle();
176:
177: if (handle instanceof HessianHomeHandle) {
178: HessianHomeHandle hessianHandle = (HessianHomeHandle) handle;
179:
180: Class api = ejbHome.getEJBMetaData()
181: .getHomeInterfaceClass();
182:
183: writeRemote(api.getName(), hessianHandle
184: .getURL("hessian"));
185: return;
186: }
187: }
188:
189: super.writeObjectImpl(obj);
190: }
191: }
|