1 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 ; 42 import javax.ejb.EJBObject ; 43 import javax.ejb.Handle ; 44 import javax.ejb.HomeHandle ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 49 public class HessianWriter extends HessianSerializerOutput { 50 private InputStream _is; 51 private HessianRemoteResolver _resolver; 52 53 59 public HessianWriter(InputStream is, OutputStream os) 60 { 61 super(os); 62 63 _is = is; 64 } 65 66 72 public HessianWriter(OutputStream os) 73 { 74 super(os); 75 } 76 77 80 public HessianWriter() 81 { 82 } 83 84 87 public void init(OutputStream 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 101 { 102 completeCall(); 103 104 if (! (_is instanceof ReadStream)) 105 throw new IllegalStateException ("Hessian call requires ReadStream"); 106 107 ReadStream is = (ReadStream) _is; 108 109 String status = (String ) 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 header; 129 130 while ((header = in.readHeader()) != null) { 131 Object 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 ) 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 e) { 153 } 154 } 155 156 161 public void writeObjectImpl(Object obj) 162 throws IOException 163 { 164 if (obj instanceof EJBObject ) { 165 EJBObject ejbObject = (EJBObject ) obj; 166 EJBHome ejbHome = ejbObject.getEJBHome(); 167 168 Handle handle = ejbObject.getHandle(); 169 170 if (handle instanceof HessianHandle) { 171 HessianHandle hessianHandle = (HessianHandle) handle; 172 173 Class api = ejbHome.getEJBMetaData().getRemoteInterfaceClass(); 174 175 writeRemote(api.getName(), hessianHandle.getURL()); 176 return; 177 } 178 } 179 else if (obj instanceof EJBHome ) { 180 EJBHome ejbHome = (EJBHome ) obj; 181 182 HomeHandle handle = ejbHome.getHomeHandle(); 183 184 if (handle instanceof HessianHomeHandle) { 185 HessianHomeHandle hessianHandle = (HessianHomeHandle) handle; 186 187 Class 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 |