View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transaction.lookup;
8   
9   import org.mule.api.config.MuleConfiguration;
10  import org.mule.api.lifecycle.Initialisable;
11  import org.mule.api.lifecycle.InitialisationException;
12  import org.mule.api.transaction.TransactionManagerFactory;
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.util.JndiContextHelper;
15  import org.mule.util.StringUtils;
16  
17  import java.util.Map;
18  
19  import javax.naming.Context;
20  import javax.naming.NamingException;
21  import javax.transaction.TransactionManager;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * A factory performing a JNDI lookup for TransactionManager. <p/> NOTE: Java EE 1.4
28   * specification does not mandate application server vendors to expose a
29   * TransactionManager for direct use, nor does it name the standard way to locate it.
30   * For some servers the TransactionManager is not even available in the global JNDI
31   * namespace, so your only bet is to run Mule in the same JVM as the application
32   * server.
33   */
34  public class GenericTransactionManagerLookupFactory implements TransactionManagerFactory, Initialisable
35  {
36      protected final Log logger = LogFactory.getLog(getClass());
37  
38      protected Context context;
39  
40      private Map environment;
41  
42      private TransactionManager txManager;
43  
44      private String jndiName;
45  
46      public String getJndiName()
47      {
48          return jndiName;
49      }
50  
51      public void setJndiName(final String jndiName)
52      {
53          this.jndiName = jndiName;
54      }
55  
56      public TransactionManager getTxManager()
57      {
58          return txManager;
59      }
60  
61      public void setTxManager(final TransactionManager txManager)
62      {
63          this.txManager = txManager;
64      }
65  
66      public Map getEnvironment()
67      {
68          return environment;
69      }
70  
71      public void setEnvironment(final Map environment)
72      {
73          this.environment = environment;
74      }
75  
76      public Context getContext()
77      {
78          return context;
79      }
80  
81      public void setContext(final Context context)
82      {
83          this.context = context;
84      }
85  
86      /** @see org.mule.api.transaction.TransactionManagerFactory#create(MuleConfiguration)
87       * @param config */
88      public TransactionManager create(MuleConfiguration config) throws Exception
89      {
90          // implementing the Initilisable interface does not call it??
91          initialise();
92          if (txManager == null)
93          {
94              txManager = (TransactionManager) context.lookup(jndiName);
95          }
96  
97          return txManager;
98      }
99  
100     /**
101      * Method used to perform any initialisation work. If a fatal error occurs during
102      * initialisation an <code>InitialisationException</code> should be thrown,
103      * causing the Mule instance to shutdown. If the error is recoverable, say by
104      * retrying to connect, a <code>RecoverableException</code> should be thrown.
105      * There is no guarantee that by throwing a Recoverable exception that the Mule
106      * instance will not shut down.
107      *
108      * @throws org.mule.api.lifecycle.InitialisationException
109      *          if a fatal error occurs
110      *          causing the Mule instance to shutdown
111      */
112     public void initialise() throws InitialisationException
113     {
114         if (txManager == null && StringUtils.isEmpty(StringUtils.trim(jndiName)))
115         {
116             throw new InitialisationException(CoreMessages.propertiesNotSet("jndiName"), this);
117         }
118 
119         try
120         {
121             if (context == null)
122             {
123                 context = JndiContextHelper.initialise(getEnvironment());
124             }
125         }
126         catch (NamingException e)
127         {
128             throw new InitialisationException(CoreMessages.failedToCreate("Jndi context"),
129                     e, this);
130         }
131     }
132 }