KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > tx > TxUtil


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.tx;
23
24 import javax.ejb.TransactionManagement JavaDoc;
25 import javax.ejb.TransactionManagementType JavaDoc;
26 import javax.ejb.ApplicationException JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.transaction.TransactionManager JavaDoc;
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 JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 /**
43  * Comment
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 58478 $
47  */

48 public class TxUtil
49 {
50    public static TransactionManager JavaDoc getTransactionManager() throws RuntimeException JavaDoc
51    {
52       try
53       {
54          //return TxManager.getInstance();
55
InitialContext JavaDoc jndiContext = InitialContextFactory.getInitialContext();
56          TransactionManager JavaDoc tm = (TransactionManager JavaDoc)jndiContext.lookup("java:TransactionManager");
57          return tm;
58       }
59       catch (NamingException JavaDoc e)
60       {
61          throw new RuntimeException JavaDoc("Unable to lookup TransactionManager", e);
62       }
63    }
64
65    public static TransactionManagementType JavaDoc getTransactionManagementType(Advisor c)
66    {
67       TransactionManagement JavaDoc transactionManagement = (TransactionManagement JavaDoc) c.resolveAnnotation(TransactionManagement JavaDoc.class);
68       if (transactionManagement == null) return TransactionManagementType.CONTAINER;
69       return transactionManagement.value();
70    }
71
72    public static ApplicationException JavaDoc getApplicationException(Class JavaDoc exceptionClass, Invocation invocation)
73    {
74       MethodInvocation ejb = (MethodInvocation) invocation;
75       EJBContainer container = (EJBContainer) ejb.getAdvisor();
76
77       if (exceptionClass.isAnnotationPresent(ApplicationException JavaDoc.class))
78          return (ApplicationException JavaDoc)exceptionClass.getAnnotation(ApplicationException JavaDoc.class);
79
80       AssemblyDescriptor assembly = container.getAssemblyDescriptor();
81
82       if (assembly != null)
83       {
84          List JavaDoc exceptions = assembly.getApplicationExceptions();
85          if (exceptions.size() > 0)
86          {
87             Iterator JavaDoc 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