View Javadoc

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