1 24 package org.objectweb.jalisto.se.jca; 25 26 import org.objectweb.jalisto.se.impl.trace.Trace; 27 28 import javax.transaction.xa.XAException ; 29 import javax.transaction.xa.XAResource ; 30 import javax.transaction.xa.Xid ; 31 import java.io.PrintStream ; 32 import java.io.PrintWriter ; 33 34 public class JalistoXAResourceWrapper implements XAResource { 35 36 public JalistoXAResourceWrapper(JalistoManagedConnection mc, Trace trace) { 37 this.trace = trace; 38 this.mc = mc; 39 } 40 41 public boolean isAvailable() { 42 return (currentJdoxar == null); 43 } 44 45 public boolean isSameRM(XAResource resource) throws XAException { 46 try { 47 JalistoXAResourceWrapper candidate = (JalistoXAResourceWrapper) resource; 48 if (currentJdoxar == null) { 49 return (candidate.currentJdoxar == null); 50 } 51 return currentJdoxar.isSameJDOXAR(candidate.currentJdoxar); 52 } catch (ClassCastException cce) { 53 return false; 54 } 55 } 56 57 public boolean setTransactionTimeout(int i) throws XAException { 58 this.timeout = i; 59 return true; 60 } 61 62 public int getTransactionTimeout() throws XAException { 63 return timeout; 64 } 65 66 public void cleanUp() { 67 out("cleanUpJalisto()"); 68 currentJdoxar = null; 69 } 70 71 public void commit(Xid xid, boolean b) throws XAException { 72 JalistoXAResource res = JalistoXARPool.getBoundXAResource(xid, mc); 73 if (res != null) { 74 res.commit(xid, b); 75 } else { 76 } 78 } 79 80 public void end(Xid xid, int i) throws XAException { 81 out("end"); 82 } 83 84 public void forget(Xid xid) throws XAException { 85 out("forget"); 86 } 87 88 public int prepare(Xid xid) throws XAException { 89 out("prepare"); 90 return XA_OK; 92 } 93 94 public Xid [] recover(int i) throws XAException { 95 out("recover"); 96 return new Xid [0]; 97 } 98 99 public void rollback(Xid xid) throws XAException { 100 JalistoXAResource res = JalistoXARPool.getBoundXAResource(xid, mc); 101 if (res != null) { 102 res.rollback(xid); 103 } else { 104 } 106 } 107 108 public void start(Xid xid, int i) throws XAException { 109 try { 110 currentJdoxar = JalistoXARPool.getOrBindJalistoXAResource(xid, mc); 111 currentJdoxar.start(xid, mc); 112 } catch (Throwable t) { 113 t.printStackTrace(); 114 throw new JalistoXAException("ERROR during IES association : ", t); 115 } 116 out("start"); 117 } 118 119 public void boundUnderlyingXaResource(Xid xid) throws XAException { 120 try { 121 currentJdoxar = JalistoXARPool.getOrBindJalistoXAResource(xid, mc); 122 mc.setManagedJalistoSession(currentJdoxar.getManagedJalistoSession()); 123 } catch (Throwable t) { 124 t.printStackTrace(); 125 throw new JalistoXAException("ERROR during IES association : ", t); 126 } 127 out("boundUnderlyingXaResource"); 128 } 129 130 private void out(Object o) { 131 if (trace != null) { 132 trace.println(Trace.JCA, TRACE_ID + " {0}", o); 133 } 134 } 135 136 public class JalistoXAException extends XAException { 137 private Throwable cause; 138 139 public JalistoXAException(String message, Throwable throwable) { 140 super(message); 141 this.cause = throwable; 142 } 143 144 public JalistoXAException(Throwable throwable) { 145 super(throwable.getClass().getName() + ": " + throwable.getMessage()); 146 this.cause = throwable; 147 } 148 149 public void printStackTrace() { 150 this.printStackTrace(System.err); 151 } 152 153 public void printStackTrace(PrintStream s) { 154 super.printStackTrace(s); 155 if (cause != null) { 156 s.println("CAUSED BY:"); 157 cause.printStackTrace(s); 158 } 159 } 160 161 public void printStackTrace(PrintWriter s) { 162 super.printStackTrace(s); 163 if (cause != null) { 164 s.println("CAUSED BY:"); 165 cause.printStackTrace(s); 166 } 167 } 168 } 169 170 171 private JalistoManagedConnection mc; 172 private JalistoXAResource currentJdoxar = null; 173 private int timeout; 174 175 private Trace trace; 176 177 178 private static final String TRACE_ID = "[JalistoXAResourceWrapper]"; 179 180 } 181 | Popular Tags |