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.tm;
023:
024: import javax.naming.InitialContext;
025: import javax.naming.NamingException;
026: import javax.transaction.TransactionManager;
027:
028: import org.jboss.logging.Logger;
029: import org.jboss.util.NestedRuntimeException;
030:
031: /**
032: * Locates the transaction manager.
033: *
034: * @todo this really belongs in some integration layer with
035: * a more pluggable implementation
036: * @author <a href="adrian@jboss.com">Adrian Brock</a>
037: * @version $Revision: 57208 $
038: */
039: public class TransactionManagerLocator {
040: /** Logger */
041: private static final Logger log = Logger
042: .getLogger(TransactionManagerLocator.class);
043:
044: /** The instance */
045: private static TransactionManagerLocator instance = new TransactionManagerLocator();
046:
047: /** The transaction manager */
048: private TransactionManager tm;
049:
050: /**
051: * No external construction
052: */
053: private TransactionManagerLocator() {
054: }
055:
056: /**
057: * Get the the locator
058: *
059: * @return the locator
060: */
061: public static TransactionManagerLocator getInstance() {
062: return instance;
063: }
064:
065: /**
066: * Locate the transaction manager
067: *
068: * @return the transaction manager
069: */
070: public TransactionManager locate() {
071: if (tm != null)
072: return tm;
073:
074: TransactionManager result = tryJNDI();
075: if (result == null)
076: result = usePrivateAPI();
077: if (result == null)
078: throw new NestedRuntimeException(
079: "Unable to locate the transaction manager");
080:
081: return result;
082: }
083:
084: /**
085: * Locate the transaction manager in the well known jndi binding for JBoss
086: *
087: * @return the tm from jndi
088: */
089: protected TransactionManager tryJNDI() {
090: try {
091: InitialContext ctx = new InitialContext();
092: tm = (TransactionManager) ctx
093: .lookup(TransactionManagerService.JNDI_NAME);
094: if (log.isTraceEnabled())
095: log.trace("Got a transaction manager from jndi " + tm);
096: } catch (NamingException e) {
097: log.trace("Unable to lookup: "
098: + TransactionManagerService.JNDI_NAME, e);
099: }
100: return tm;
101: }
102:
103: /**
104: * Use the private api<p>
105: *
106: * This is a fallback method for non JBossAS use.
107: *
108: * @return the tm from the private api
109: */
110: protected TransactionManager usePrivateAPI() {
111: log.trace("Using the JBoss transaction manager");
112: return TxManager.getInstance();
113: }
114: }
|