1 22 package org.jboss.test.recover.bean; 23 24 import java.io.Serializable ; 25 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.transaction.xa.XAException ; 29 import javax.transaction.xa.XAResource ; 30 import javax.transaction.xa.Xid ; 31 32 import org.jboss.naming.Util; 33 import org.jboss.system.ServiceMBeanSupport; 34 import org.jboss.tm.recovery.Recoverable; 35 import org.jboss.tm.recovery.RecoveryManagerServiceMBean; 36 import org.jboss.tm.recovery.RecoveryTestingException; 37 38 45 public class DummyRecoverableProxyService 46 extends ServiceMBeanSupport 47 implements DummyRecoverableProxyServiceMBean 48 { 49 private String jndiName; 50 private String remoteJndiName; 51 private RecoveryManagerServiceMBean manager; 52 private Recoverable proxy; 53 54 public String getJndiName() 55 { 56 return jndiName; 57 } 58 59 public void setJndiName(String jndiName) 60 { 61 this.jndiName = jndiName; 62 } 63 64 public String getRemoteJndiName() 65 { 66 return remoteJndiName; 67 } 68 69 public void setRemoteJndiName(String remoteJndiName) 70 { 71 this.remoteJndiName = remoteJndiName; 72 } 73 74 public void setManager(RecoveryManagerServiceMBean manager) 75 { 76 this.manager = manager; 77 } 78 79 public Recoverable getProxy() 80 { 81 return proxy; 82 } 83 84 public void startService() throws Exception 85 { 86 proxy = (Recoverable) Util.lookup(remoteJndiName, Recoverable.class); 87 log.debug("Got Recoverable proxy from remoteJndiName=" + remoteJndiName); 88 manager.registerRecoverable(proxy); 89 90 Context iniCtx = new InitialContext (); 91 Util.bind(iniCtx, jndiName, new RecoverableWrapper(proxy)); 92 log.debug("Bound wrapped proxy under jndiName=" + jndiName); 93 } 94 95 public void stopService() throws Exception 96 { 97 Context iniCtx = new InitialContext (); 98 Util.unbind(iniCtx, jndiName); 99 manager = null; 100 proxy = null; 101 } 102 103 public static class RecoverableWrapper 104 implements Recoverable, Serializable 105 { 106 private Recoverable inner; 107 private transient XAResource resourceWrapper = null; 108 109 RecoverableWrapper(Recoverable inner) 110 { 111 this.inner = inner; 112 } 113 114 public String getId() 115 { 116 return inner.getId(); 117 } 118 119 public XAResource getResource() 120 { 121 if (resourceWrapper == null) 122 resourceWrapper = new XAResourceWrapper(inner.getResource()); 123 124 return resourceWrapper; 125 } 126 127 public Xid [] scan() throws XAException 128 { 129 return inner.scan(); 130 } 131 132 public void cleanupResource() 133 { 134 inner.cleanupResource(); 135 } 136 137 public XAResource getUnwrappedResource() 138 { 139 return inner.getResource(); 140 } 141 142 } 143 144 private static class XAResourceWrapper 145 implements XAResource , Serializable 146 { 147 private XAResource inner; 148 149 XAResourceWrapper(XAResource xaRes) 150 { 151 inner = xaRes; 152 } 153 154 public void commit(Xid xid, boolean onePhase) throws XAException 155 { 156 try 157 { 158 inner.commit(xid, onePhase); 159 } 160 catch (RecoveryTestingException e) 161 { 162 crash(); 163 } 164 } 165 166 public void end(Xid xid, int flags) throws XAException 167 { 168 inner.end(xid, flags); 169 } 170 171 public void forget(Xid xid) throws XAException 172 { 173 inner.forget(xid); 174 } 175 176 public int getTransactionTimeout() throws XAException 177 { 178 return inner.getTransactionTimeout(); 179 } 180 181 public boolean isSameRM(XAResource xaRes) throws XAException 182 { 183 return inner.isSameRM(xaRes); 184 } 185 186 public int prepare(Xid xid) throws XAException 187 { 188 try 189 { 190 return inner.prepare(xid); 191 } 192 catch (RecoveryTestingException e) 193 { 194 crash(); 195 return 0; 196 } 197 } 198 199 public Xid [] recover(int flag) throws XAException 200 { 201 return inner.recover(flag); 202 } 203 204 public void rollback(Xid xid) throws XAException 205 { 206 try 207 { 208 inner.rollback(xid); 209 } 210 catch (RecoveryTestingException e) 211 { 212 crash(); 213 } 214 215 } 216 217 public boolean setTransactionTimeout(int seconds) throws XAException 218 { 219 return inner.setTransactionTimeout(seconds); 220 } 221 222 public void start(Xid xid, int flags) throws XAException 223 { 224 inner.start(xid, flags); 225 } 226 227 } 228 229 private static void crash() 230 { 231 Runtime.getRuntime().halt(1); 232 } 233 234 } 235 | Popular Tags |