1 29 30 package com.caucho.ejb.hessian; 31 32 import com.caucho.ejb.AbstractServer; 33 import com.caucho.ejb.protocol.EjbProtocolManager; 34 import com.caucho.ejb.protocol.Skeleton; 35 import com.caucho.ejb.xa.TransactionContext; 36 import com.caucho.hessian.io.HessianDebugInputStream; 37 import com.caucho.hessian.io.HessianInput; 38 import com.caucho.hessian.io.HessianOutput; 39 import com.caucho.hessian.io.HessianProtocolException; 40 import com.caucho.hessian.io.HessianRemoteResolver; 41 import com.caucho.log.Log; 42 import com.caucho.util.CharBuffer; 43 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import java.io.PrintWriter ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 59 abstract public class HessianSkeleton extends Skeleton { 60 protected static final Logger log = Log.open(HessianSkeleton.class); 61 62 private AbstractServer _server; 63 private HessianRemoteResolver _resolver; 64 65 private boolean _isDebug; 66 67 void _setServer(AbstractServer server) 68 { 69 _server = server; 70 } 71 72 public void setDebug(boolean isDebug) 73 { 74 _isDebug = isDebug; 75 } 76 77 80 void _setResolver(HessianRemoteResolver resolver) 81 { 82 _resolver = resolver; 83 } 84 85 abstract protected void _setObject(Object object); 86 87 public void _service(InputStream is, OutputStream os) 88 throws Exception 89 { 90 java.io.StringWriter debugWriter = null; 91 92 if (_isDebug) { 93 debugWriter = new java.io.StringWriter (); 94 is = new HessianDebugInputStream(is, new PrintWriter (debugWriter)); 95 } 96 97 HessianInput in = new HessianReader(is); 98 HessianOutput out = new HessianWriter(os); 99 100 in.setRemoteResolver(_resolver); 101 in.readCall(); 102 103 String xid = null; 104 String header; 105 while ((header = in.readHeader()) != null) { 106 Object value = in.readObject(); 107 108 if ("xid".equals(header)) { 109 xid = (String ) value; 110 } 111 } 112 113 String method = in.readMethod(); 114 115 CharBuffer cb = new CharBuffer(); 116 cb.append(method); 117 118 String oldProtocol = EjbProtocolManager.setThreadProtocol("hessian"); 119 120 try { 121 TransactionContext xa = null; 122 123 if (xid != null) 124 xa = _server.getTransactionManager().startTransaction(xid); 125 126 _execute(cb, in, out, xa); 127 } catch (HessianProtocolException e) { 128 throw e; 129 } catch (Throwable e) { 130 log.log(Level.FINER, e.toString(), e); 131 132 out.startReply(); 133 out.writeFault("ServiceException", e.getMessage(), e); 134 out.completeReply(); 135 } finally { 136 EjbProtocolManager.setThreadProtocol(oldProtocol); 137 138 if (debugWriter != null) 139 log.fine(debugWriter.toString()); 140 141 if (xid != null) 142 _server.getTransactionManager().finishTransaction(xid); 143 } 144 } 145 146 protected void startReply(HessianOutput out, TransactionContext xa) 147 throws IOException 148 { 149 out.startReply(); 150 151 if (xa != null && ! xa.isEmpty()) { 152 EjbProtocolManager pm = _server.getServerManager().getProtocolManager(); 153 HessianProtocol hessian = (HessianProtocol) pm.getProtocol("hessian"); 154 155 if (hessian != null) { 156 out.writeHeader("xa-resource"); 157 out.writeString(hessian.calculateURL("/_ejb_xa_resource")); 158 } 159 } 160 } 161 162 abstract protected void _execute(CharBuffer method, 163 HessianInput in, 164 HessianOutput out, 165 TransactionContext xa) 166 throws Throwable ; 167 168 protected void _executeUnknown(CharBuffer method, 169 HessianInput in, 170 HessianOutput out) 171 throws Exception 172 { 173 if (method.matches("_hessian_getAttribute")) { 174 String key = in.readString(); 175 in.completeCall(); 176 177 out.startReply(); 178 179 if ("java.api.class".equals(key)) 180 out.writeString(_server.getRemoteHomeClass().getName()); 181 else if ("java.home.class".equals(key)) 182 out.writeString(_server.getRemoteHomeClass().getName()); 183 else if ("java.object.class".equals(key)) 184 out.writeString(_server.getRemoteObjectClass().getName()); 185 else if ("home-class".equals(key)) 186 out.writeString(_server.getRemoteHomeClass().getName()); 187 else if ("remote-class".equals(key)) 188 out.writeString(_server.getRemoteObjectClass().getName()); 189 else 190 out.writeNull(); 191 192 out.completeReply(); 193 } 194 else { 195 out.startReply(); 196 out.writeFault("NoMethod", "no such method: " + method, null); 197 out.completeReply(); 198 } 199 } 200 } 201 202 203 | Popular Tags |