1 22 package org.jboss.ejb3.tx; 23 24 import javax.ejb.TransactionManagement ; 25 import javax.ejb.TransactionManagementType ; 26 import javax.ejb.ApplicationException ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import javax.transaction.TransactionManager ; 30 import org.jboss.aop.Advisor; 31 import org.jboss.aop.joinpoint.Invocation; 32 import org.jboss.aop.joinpoint.MethodInvocation; 33 34 import org.jboss.ejb3.InitialContextFactory; 35 import org.jboss.ejb3.EJBContainer; 36 import org.jboss.ejb3.metamodel.AssemblyDescriptor; 37 import org.jboss.ejb.ApplicationExceptionImpl; 38 39 import java.util.List ; 40 import java.util.Iterator ; 41 42 48 public class TxUtil 49 { 50 public static TransactionManager getTransactionManager() throws RuntimeException 51 { 52 try 53 { 54 InitialContext jndiContext = InitialContextFactory.getInitialContext(); 56 TransactionManager tm = (TransactionManager )jndiContext.lookup("java:TransactionManager"); 57 return tm; 58 } 59 catch (NamingException e) 60 { 61 throw new RuntimeException ("Unable to lookup TransactionManager", e); 62 } 63 } 64 65 public static TransactionManagementType getTransactionManagementType(Advisor c) 66 { 67 TransactionManagement transactionManagement = (TransactionManagement ) c.resolveAnnotation(TransactionManagement .class); 68 if (transactionManagement == null) return TransactionManagementType.CONTAINER; 69 return transactionManagement.value(); 70 } 71 72 public static ApplicationException getApplicationException(Class exceptionClass, Invocation invocation) 73 { 74 MethodInvocation ejb = (MethodInvocation) invocation; 75 EJBContainer container = (EJBContainer) ejb.getAdvisor(); 76 77 if (exceptionClass.isAnnotationPresent(ApplicationException .class)) 78 return (ApplicationException )exceptionClass.getAnnotation(ApplicationException .class); 79 80 AssemblyDescriptor assembly = container.getAssemblyDescriptor(); 81 82 if (assembly != null) 83 { 84 List exceptions = assembly.getApplicationExceptions(); 85 if (exceptions.size() > 0) 86 { 87 Iterator exceptionIterator = exceptions.iterator(); 88 while (exceptionIterator.hasNext()) 89 { 90 org.jboss.ejb3.metamodel.ApplicationException exception = (org.jboss.ejb3.metamodel.ApplicationException)exceptionIterator.next(); 91 if (exception.getExceptionClass().equals(exceptionClass.getName())) 92 return new ApplicationExceptionImpl(exception.getRollback()); 93 } 94 } 95 96 } 97 return null; 98 } 99 } 100 | Popular Tags |