KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > treecache > TomcatTransactionManagerLookup


1 /*
2  * Copyright 2002-2006 Jahia Ltd
3  *
4  * Licensed under the JAHIA SUSTAINABLE SOFTWARE LICENSE (JSSL),
5  * Version 1.0 (the "License"), or (at your option) any later version; you may
6  * not use this file except in compliance with the License. You should have
7  * received a copy of the License along with this program; if not, you may obtain
8  * a copy of the License at
9  *
10  * http://www.jahia.org/license/
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.jahia.services.cache.treecache;
19
20 import org.jboss.cache.TransactionManagerLookup;
21 import org.jboss.cache.transaction.DummyTransactionManager;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import javax.transaction.TransactionManager JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 /**
30  * User: Serge Huber
31  * Date: 31 oct. 2005
32  * Time: 16:46:52
33  * Copyright (C) Jahia Inc.
34  */

35 public class TomcatTransactionManagerLookup implements TransactionManagerLookup {
36
37     /**
38      * our logger
39      */

40     private static Log log= LogFactory.getLog(TomcatTransactionManagerLookup.class);
41
42     /**
43      * lookups performed?
44      */

45     private static boolean lookupDone=false;
46
47     /**
48      * no lookup available?
49      */

50     private static boolean lookupFailed=false;
51
52     /**
53      * the (final) used TransactionManager
54      */

55     private static TransactionManager tm=null;
56
57     /**
58      * JNDI locations for TransactionManagers we know of
59      */

60     private static String JavaDoc[][] knownJNDIManagers={
61        {"java:comp/env/UserTransaction", "Tomcat"},
62        {"java:/TransactionManager", "JBoss, JRun4"},
63        {"java:comp/UserTransaction", "Resin, Orion, JOnAS (JOTM)"},
64        {"javax.transaction.TransactionManager", "BEA WebLogic"}
65     };
66
67     /**
68      * Get the systemwide used TransactionManager
69      *
70      * @return TransactionManager
71      */

72     public TransactionManager getTransactionManager() {
73        if(!lookupDone)
74           doLookups();
75        if(tm != null)
76           return tm;
77        if(lookupFailed) {
78           //fall back to a dummy from JBossCache
79
tm= DummyTransactionManager.getInstance();
80           log.warn("Falling back to DummyTransactionManager from JBossCache");
81        }
82        return tm;
83     }
84
85     /**
86      * Try to figure out which TransactionManager to use
87      */

88     private static void doLookups() {
89        if(lookupFailed)
90           return;
91        InitialContext JavaDoc ctx;
92        try {
93           ctx=new InitialContext JavaDoc();
94        }
95        catch(NamingException JavaDoc e) {
96           log.error("Could not create an initial JNDI context!", e);
97           lookupFailed=true;
98           return;
99        }
100        //probe jndi lookups first
101
Object JavaDoc jndiObject=null;
102        for(int i=0; i < knownJNDIManagers.length; i++) {
103           try {
104              if(log.isDebugEnabled()) log.debug("Trying to lookup TransactionManager for " + knownJNDIManagers[i][1]);
105              jndiObject=ctx.lookup("java:comp/env/UserTransaction");
106           }
107           catch(NamingException JavaDoc e) {
108              log.info("Failed to perform a lookup for [" + knownJNDIManagers[i][0] + " (" + knownJNDIManagers[i][1] + ")]");
109           }
110           if(jndiObject instanceof TransactionManager) {
111              tm=(TransactionManager)jndiObject;
112              log.info("Found TransactionManager for " + knownJNDIManagers[i][1]);
113              return;
114           }
115        }
116     }
117
118 }
119
Popular Tags