001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.tx;
023:
024: import javax.ejb.TransactionManagement;
025: import javax.ejb.TransactionManagementType;
026: import javax.ejb.ApplicationException;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.transaction.TransactionManager;
030: import org.jboss.aop.Advisor;
031: import org.jboss.aop.joinpoint.Invocation;
032: import org.jboss.aop.joinpoint.MethodInvocation;
033:
034: import org.jboss.ejb3.InitialContextFactory;
035: import org.jboss.ejb3.EJBContainer;
036: import org.jboss.ejb3.metamodel.AssemblyDescriptor;
037: import org.jboss.ejb.ApplicationExceptionImpl;
038:
039: import org.jboss.tm.TransactionManagerLocator;
040:
041: import java.util.List;
042: import java.util.Iterator;
043:
044: /**
045: * Comment
046: *
047: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
048: * @version $Revision: 60411 $
049: */
050: public class TxUtil {
051: public static TransactionManager getTransactionManager()
052: throws RuntimeException {
053: try {
054: //return TxManager.getInstance();
055: InitialContext jndiContext = InitialContextFactory
056: .getInitialContext();
057: TransactionManager tm = TransactionManagerLocator
058: .getInstance().locate();
059: return tm;
060: } catch (NamingException e) {
061: throw new RuntimeException(
062: "Unable to lookup TransactionManager", e);
063: }
064: }
065:
066: public static TransactionManagementType getTransactionManagementType(
067: Advisor c) {
068: TransactionManagement transactionManagement = (TransactionManagement) c
069: .resolveAnnotation(TransactionManagement.class);
070: if (transactionManagement == null)
071: return TransactionManagementType.CONTAINER;
072: return transactionManagement.value();
073: }
074:
075: public static ApplicationException getApplicationException(
076: Class exceptionClass, Invocation invocation) {
077: MethodInvocation ejb = (MethodInvocation) invocation;
078: EJBContainer container = (EJBContainer) ejb.getAdvisor();
079:
080: if (exceptionClass
081: .isAnnotationPresent(ApplicationException.class))
082: return (ApplicationException) exceptionClass
083: .getAnnotation(ApplicationException.class);
084:
085: AssemblyDescriptor assembly = container.getAssemblyDescriptor();
086:
087: if (assembly != null) {
088: List exceptions = assembly.getApplicationExceptions();
089: if (exceptions.size() > 0) {
090: Iterator exceptionIterator = exceptions.iterator();
091: while (exceptionIterator.hasNext()) {
092: org.jboss.ejb3.metamodel.ApplicationException exception = (org.jboss.ejb3.metamodel.ApplicationException) exceptionIterator
093: .next();
094: if (exception.getExceptionClass().equals(
095: exceptionClass.getName()))
096: return new ApplicationExceptionImpl(exception
097: .getRollback());
098: }
099: }
100:
101: }
102: return null;
103: }
104: }
|