1 29 30 package com.caucho.ejb.hessian; 31 32 import com.caucho.hessian.io.HessianRemoteObject; 33 import com.caucho.hessian.io.HessianRemoteResolver; 34 import com.caucho.transaction.TransactionImpl; 35 import com.caucho.transaction.TransactionManagerImpl; 36 import com.caucho.vfs.Path; 37 import com.caucho.vfs.ReadStream; 38 import com.caucho.vfs.ReadWritePair; 39 import com.caucho.vfs.Vfs; 40 import com.caucho.vfs.WriteStream; 41 42 import javax.ejb.EJBException ; 43 import javax.transaction.xa.Xid ; 44 import java.io.IOException ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 public abstract class HessianStub implements HessianRemoteObject { 52 private static final Logger log 53 = Logger.getLogger(HessianStub.class.getName()); 54 protected String _url; 55 56 protected transient Path _urlPath; 57 protected transient HessianClientContainer _client; 58 protected transient HessianRemoteResolver _resolver; 59 60 66 void _init(String url, HessianClientContainer client) 67 { 68 _url = url; 69 _urlPath = Vfs.lookup(url); 70 71 _hessian_setClientContainer(client); 72 } 73 74 public String getHessianURL() 75 { 76 return _url; 77 } 78 79 void _hessian_setURL(String url) 80 { 81 _url = url; 82 } 83 84 void _hessian_setURLPath(Path urlPath) 85 { 86 _urlPath = urlPath; 87 } 88 89 void _hessian_setClientContainer(HessianRemoteResolver resolver) 90 { 91 _resolver = resolver; 92 if (resolver instanceof HessianClientContainer) 93 _client = (HessianClientContainer) resolver; 94 } 95 96 protected HessianWriter _hessian_openWriter() 97 throws EJBException 98 { 99 try { 100 ReadWritePair pair = _urlPath.openReadWrite(); 101 ReadStream is = pair.getReadStream(); 102 WriteStream os = pair.getWriteStream(); 103 104 if (_client != null) { 105 String auth = _client.getBasicAuthentication(); 106 if (auth != null) 107 os.setAttribute("Authorization", auth); 108 } 109 110 HessianWriter out = new HessianWriter(is, os); 111 112 out.setRemoteResolver(_resolver); 113 114 return out; 115 } catch (IOException e) { 116 throw new EJBException (e); 117 } 118 } 119 120 protected void _hessian_writeHeaders(HessianWriter out) 121 throws IOException 122 { 123 try { 124 TransactionImpl xa = (TransactionImpl) TransactionManagerImpl.getInstance().getTransaction(); 125 126 if (xa != null) { 127 Xid xid = xa.getXid(); 128 129 String s = xidToString(xid.getGlobalTransactionId()); 130 131 out.writeHeader("xid"); 132 out.writeString(s); 133 } 134 } catch (Throwable e) { 135 log.log(Level.FINE, e.toString(), e); 136 } 137 } 138 139 protected void _hessian_freeWriter(HessianWriter out) 140 throws IOException 141 { 142 out.close(); 143 } 144 145 private static String xidToString(byte []id) 146 { 147 StringBuilder sb = new StringBuilder (); 148 149 for (int i = 0; i < id.length; i++) { 150 byte b = id[i]; 151 152 sb.append(toHex((b >> 4) & 0xf)); 153 sb.append(toHex(b & 0xf)); 154 } 155 156 return sb.toString(); 157 } 158 159 private static char toHex(int d) 160 { 161 if (d < 10) 162 return (char) ('0' + d); 163 else 164 return (char) ('a' + d - 10); 165 } 166 } 167 | Popular Tags |