View Javadoc

1   /*
2    * $Id: GenericTransactionManagerLookupFactory.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.config.i18n.CoreMessages;
14  import org.mule.impl.container.JndiContextHelper;
15  import org.mule.umo.lifecycle.InitialisationException;
16  import org.mule.umo.manager.UMOTransactionManagerFactory;
17  import org.mule.util.StringUtils;
18  
19  import java.util.Map;
20  
21  import javax.naming.Context;
22  import javax.naming.NamingException;
23  import javax.transaction.TransactionManager;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * A factory performing a JNDI lookup for TransactionManager. <p/> NOTE: Java EE 1.4
30   * specification does not mandate application server vendors to expose a
31   * TransactionManager for direct use, nor does it name the standard way to locate it.
32   * For some servers the TransactionManager is not even available in the global JNDI
33   * namespace, so your only bet is to run Mule in the same JVM as the application
34   * server.
35   * 
36   */
37  public class GenericTransactionManagerLookupFactory implements UMOTransactionManagerFactory
38  {
39      protected final Log logger = LogFactory.getLog(getClass());
40  
41      protected Context context;
42  
43      private Map environment;
44  
45      private TransactionManager txManager;
46  
47      private String jndiName;
48  
49      public String getJndiName()
50      {
51          return jndiName;
52      }
53  
54      public void setJndiName(final String jndiName)
55      {
56          this.jndiName = jndiName;
57      }
58  
59      public TransactionManager getTxManager()
60      {
61          return txManager;
62      }
63  
64      public void setTxManager(final TransactionManager txManager)
65      {
66          this.txManager = txManager;
67      }
68  
69      public Map getEnvironment()
70      {
71          return environment;
72      }
73  
74      public void setEnvironment(final Map environment)
75      {
76          this.environment = environment;
77      }
78  
79      public Context getContext()
80      {
81          return context;
82      }
83  
84      public void setContext(final Context context)
85      {
86          this.context = context;
87      }
88  
89      /**
90       * @see org.mule.umo.manager.UMOTransactionManagerFactory#create()
91       */
92      public TransactionManager create() 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.umo.lifecycle.InitialisationException if a fatal error occurs
113      *             causing the Mule instance to shutdown
114      * @throws org.mule.umo.lifecycle.RecoverableException if an error occurs that
115      *             can be recovered from
116      */
117     public void initialise() throws InitialisationException
118     {
119         if (txManager == null && StringUtils.isEmpty(StringUtils.trim(jndiName)))
120         {
121             throw new InitialisationException(CoreMessages.propertiesNotSet("jndiName"), this);
122         }
123 
124         try
125         {
126             if (context == null)
127             {
128                 context = JndiContextHelper.initialise(getEnvironment());
129             }
130         }
131         catch (NamingException e)
132         {
133             throw new InitialisationException(CoreMessages.failedToCreate("Jndi context"), 
134                 e, this);
135         }
136     }
137 }