KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > TransactionManagerLocator


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.tm;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.transaction.TransactionManager JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.util.NestedRuntimeException;
30
31 /**
32  * Locates the transaction manager.
33  *
34  * @todo this really belongs in some integration layer with
35  * a more pluggable implementation
36  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
37  * @version $Revision: 37459 $
38  */

39 public class TransactionManagerLocator
40 {
41    /** Logger */
42    private static final Logger log = Logger.getLogger(TransactionManagerLocator.class);
43    
44    /** The instance */
45    private static TransactionManagerLocator instance = new TransactionManagerLocator();
46    
47    /** The transaction manager */
48    private TransactionManager JavaDoc tm;
49    
50    /**
51     * No external construction
52     */

53    private TransactionManagerLocator()
54    {
55    }
56    
57    /**
58     * Get the the locator
59     *
60     * @return the locator
61     */

62    public static TransactionManagerLocator getInstance()
63    {
64       return instance;
65    }
66    
67    /**
68     * Locate the transaction manager
69     *
70     * @return the transaction manager
71     */

72    public TransactionManager JavaDoc locate()
73    {
74       if (tm != null)
75          return tm;
76
77       TransactionManager JavaDoc result = tryJNDI();
78       if (result == null)
79          result = usePrivateAPI();
80       if (result == null)
81          throw new NestedRuntimeException("Unable to locate the transaction manager");
82       
83       return result;
84    }
85    
86    /**
87     * Locate the transaction manager in the well known jndi binding for JBoss
88     *
89     * @return the tm from jndi
90     */

91    protected TransactionManager JavaDoc tryJNDI()
92    {
93       try
94       {
95          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
96          tm = (TransactionManager JavaDoc) ctx.lookup(TransactionManagerService.JNDI_NAME);
97          if (log.isTraceEnabled())
98             log.trace("Got a transaction manager from jndi " + tm);
99       }
100       catch (NamingException JavaDoc e)
101       {
102          log.trace("Unable to lookup: " + TransactionManagerService.JNDI_NAME, e);
103       }
104       return tm;
105    }
106    
107    /**
108     * Use the private api<p>
109     *
110     * This is a fallback method for non JBossAS use.
111     *
112     * @return the tm from the private api
113     */

114    protected TransactionManager JavaDoc usePrivateAPI()
115    {
116       log.trace("Using the JBoss transaction manager");
117       return TxManager.getInstance();
118    }
119 }
120
Popular Tags