1 29 30 package com.caucho.ejb.hessian; 31 32 import com.caucho.vfs.Path; 33 import com.caucho.vfs.Vfs; 34 35 import javax.transaction.xa.XAException ; 36 import javax.transaction.xa.XAResource ; 37 import javax.transaction.xa.Xid ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 44 public class HessianXAResource implements XAResource { 45 private static final Logger log 46 = Logger.getLogger(HessianXAResource.class.getName()); 47 48 private String _url; 49 private Path _path; 50 51 public HessianXAResource(String url) 52 { 53 _url = url; 54 55 _path = Vfs.lookup(url); 56 } 57 58 public boolean isSameRM(XAResource xares) 59 { 60 if (! (xares instanceof HessianXAResource)) 61 return false; 62 63 HessianXAResource rm = (HessianXAResource) xares; 64 65 return _url.equals(rm._url); 66 } 67 68 public void start(Xid xid, int flags) 69 throws XAException 70 { 71 } 72 73 public void end(Xid xid, int flags) 74 throws XAException 75 { 76 } 77 78 public boolean setTransactionTimeout(int seconds) 79 throws XAException 80 { 81 return true; 82 } 83 84 public int getTransactionTimeout() 85 throws XAException 86 { 87 return 0; 88 } 89 90 public int prepare(Xid xid) 91 throws XAException 92 { 93 try { 94 Object value = MetaStub.call(_path, "prepare", xidToString(xid)); 95 } catch (Throwable e) { 96 log.log(Level.WARNING, e.toString(), e); 97 98 throw new XAException (e.toString()); 99 } 100 101 103 return XA_OK; 104 } 105 106 public Xid []recover(int flag) 107 throws XAException 108 { 109 111 return null; 112 } 113 114 public void forget(Xid xid) 115 throws XAException 116 { 117 } 118 119 public void rollback(Xid xid) 120 throws XAException 121 { 122 try { 123 MetaStub.call(_path, "rollback", xidToString(xid)); 124 } catch (Throwable e) { 125 log.log(Level.WARNING, e.toString(), e); 126 127 throw new XAException (e.toString()); 128 } 129 } 130 131 public void commit(Xid xid, boolean onephase) 132 throws XAException 133 { 134 try { 135 if (onephase) 136 MetaStub.call(_path, "commitOnePhase", xidToString(xid)); 137 else 138 MetaStub.call(_path, "commit", xidToString(xid)); 139 } catch (Throwable e) { 140 log.log(Level.WARNING, e.toString(), e); 141 142 throw new XAException (e.toString()); 143 } 144 } 145 146 private static String xidToString(Xid xid) 147 { 148 byte []id = xid.getGlobalTransactionId(); 149 150 StringBuilder sb = new StringBuilder (); 151 152 for (int i = 0; i < id.length; i++) { 153 byte b = id[i]; 154 155 sb.append(toHex((b >> 4) & 0xf)); 156 sb.append(toHex(b & 0xf)); 157 } 158 159 return sb.toString(); 160 } 161 162 private static char toHex(int d) 163 { 164 if (d < 10) 165 return (char) ('0' + d); 166 else 167 return (char) ('a' + d - 10); 168 } 169 } 170 | Popular Tags |