1 package org.hibernate.transaction; 3 4 import java.lang.reflect.InvocationHandler ; 5 import java.lang.reflect.Method ; 6 import java.lang.reflect.Proxy ; 7 import java.util.Properties ; 8 9 import javax.naming.NamingException ; 10 import javax.transaction.HeuristicMixedException ; 11 import javax.transaction.HeuristicRollbackException ; 12 import javax.transaction.InvalidTransactionException ; 13 import javax.transaction.NotSupportedException ; 14 import javax.transaction.RollbackException ; 15 import javax.transaction.Status ; 16 import javax.transaction.Synchronization ; 17 import javax.transaction.SystemException ; 18 import javax.transaction.Transaction ; 19 import javax.transaction.TransactionManager ; 20 import javax.transaction.xa.XAResource ; 21 22 import org.hibernate.HibernateException; 23 import org.hibernate.util.NamingHelper; 24 25 29 public class WebSphereExtendedJTATransactionLookup implements TransactionManagerLookup { 30 31 public TransactionManager getTransactionManager(Properties props) 32 throws HibernateException { 33 return new TransactionManagerAdapter(props); 34 } 35 36 public String getUserTransactionName() { 37 return "java:comp/UserTransaction"; 38 } 39 40 public static class TransactionManagerAdapter implements TransactionManager { 41 42 private final Properties properties; 43 private final Class synchronizationCallbackClass; 44 private final Method registerSynchronizationMethod; 45 private final Method getLocalIdMethod; 46 47 private TransactionManagerAdapter(Properties props) { 48 this.properties = props; 49 try { 50 synchronizationCallbackClass = Class.forName("com.ibm.websphere.jtaextensions.SynchronizationCallback"); 51 Class extendedJTATransactionClass = Class.forName("com.ibm.websphere.jtaextensions.ExtendedJTATransaction"); 52 registerSynchronizationMethod = extendedJTATransactionClass 53 .getMethod( "registerSynchronizationCallbackForCurrentTran", new Class [] { synchronizationCallbackClass } ); 54 getLocalIdMethod = extendedJTATransactionClass.getMethod( "getLocalId", null ); 55 56 } 57 catch (ClassNotFoundException cnfe) { 58 throw new HibernateException(cnfe); 59 } 60 catch (NoSuchMethodException nsme) { 61 throw new HibernateException(nsme); 62 } 63 } 64 65 public void begin() throws NotSupportedException , SystemException { 66 throw new UnsupportedOperationException (); 67 } 68 69 public void commit() throws RollbackException , HeuristicMixedException , 70 HeuristicRollbackException , SecurityException , 71 IllegalStateException , SystemException { 72 throw new UnsupportedOperationException (); 73 } 74 75 public int getStatus() throws SystemException { 76 throw new UnsupportedOperationException (); 77 } 78 79 public Transaction getTransaction() throws SystemException { 80 return new TransactionAdapter(properties); 81 } 82 83 public void resume(Transaction txn) throws InvalidTransactionException , 84 IllegalStateException , SystemException { 85 throw new UnsupportedOperationException (); 86 } 87 88 public void rollback() throws IllegalStateException , SecurityException , 89 SystemException { 90 throw new UnsupportedOperationException (); 91 } 92 93 public void setRollbackOnly() throws IllegalStateException , 94 SystemException { 95 throw new UnsupportedOperationException (); 96 } 97 98 public void setTransactionTimeout(int i) throws SystemException { 99 throw new UnsupportedOperationException (); 100 } 101 102 public Transaction suspend() throws SystemException { 103 throw new UnsupportedOperationException (); 104 } 105 106 public class TransactionAdapter implements Transaction { 107 108 private final Object extendedJTATransaction; 109 110 private TransactionAdapter(Properties props) { 111 try { 112 extendedJTATransaction = NamingHelper.getInitialContext(props) 113 .lookup("java:comp/websphere/ExtendedJTATransaction"); 114 } 115 catch (NamingException ne) { 116 throw new HibernateException(ne); 117 } 118 } 119 120 public void registerSynchronization(final Synchronization synchronization) 121 throws RollbackException , IllegalStateException , 122 SystemException { 123 124 final InvocationHandler ih = new InvocationHandler () { 125 126 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 127 if ( "afterCompletion".equals( method.getName() ) ) { 128 int status = args[2].equals(Boolean.TRUE) ? 129 Status.STATUS_COMMITTED : 130 Status.STATUS_UNKNOWN; 131 synchronization.afterCompletion(status); 132 } 133 else if ( "beforeCompletion".equals( method.getName() ) ) { 134 synchronization.beforeCompletion(); 135 } 136 else if ( "toString".equals( method.getName() ) ) { 137 return synchronization.toString(); 138 } 139 return null; 140 } 141 142 }; 143 144 final Object synchronizationCallback = Proxy.newProxyInstance( 145 getClass().getClassLoader(), 146 new Class [] { synchronizationCallbackClass }, 147 ih 148 ); 149 150 try { 151 registerSynchronizationMethod.invoke( 152 extendedJTATransaction, 153 new Object [] { synchronizationCallback } 154 ); 155 } 156 catch (Exception e) { 157 throw new HibernateException(e); 158 } 159 160 } 161 162 public int hashCode() { 163 return getLocalId().hashCode(); 164 } 165 166 public boolean equals(Object other) { 167 if ( !(other instanceof TransactionAdapter) ) return false; 168 TransactionAdapter that = (TransactionAdapter) other; 169 return getLocalId().equals( that.getLocalId() ); 170 } 171 172 private Object getLocalId() { 173 try { 174 return getLocalIdMethod.invoke(extendedJTATransaction, null); 175 } 176 catch (Exception e) { 177 throw new HibernateException(e); 178 } 179 } 180 181 public void commit() throws RollbackException , HeuristicMixedException , 182 HeuristicRollbackException , SecurityException , 183 IllegalStateException , SystemException { 184 throw new UnsupportedOperationException (); 185 } 186 187 public boolean delistResource(XAResource resource, int i) 188 throws IllegalStateException , SystemException { 189 throw new UnsupportedOperationException (); 190 } 191 192 public boolean enlistResource(XAResource resource) 193 throws RollbackException , IllegalStateException , 194 SystemException { 195 throw new UnsupportedOperationException (); 196 } 197 198 public int getStatus() throws SystemException { 199 return new Integer (0).equals( getLocalId() ) ? 200 Status.STATUS_NO_TRANSACTION : Status.STATUS_ACTIVE; 201 } 202 203 public void rollback() throws IllegalStateException , SystemException { 204 throw new UnsupportedOperationException (); 205 } 206 207 public void setRollbackOnly() throws IllegalStateException , 208 SystemException { 209 throw new UnsupportedOperationException (); 210 } 211 } 212 213 } 214 215 } 216
| Popular Tags
|