KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transaction > lookup > GenericTransactionManagerLookupFactory


1 /*
2  * $Id: GenericTransactionManagerLookupFactory.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.apache.commons.lang.StringUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.mule.config.i18n.Message;
17 import org.mule.config.i18n.Messages;
18 import org.mule.impl.container.JndiContextHelper;
19 import org.mule.umo.lifecycle.InitialisationException;
20 import org.mule.umo.manager.UMOTransactionManagerFactory;
21
22 import javax.naming.Context JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import javax.transaction.TransactionManager JavaDoc;
25
26 import java.util.Map JavaDoc;
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  * @author <a HREF="mailto:aperepel@gmail.com">Andrew Perepelytsya</a>
37  */

38 public class GenericTransactionManagerLookupFactory implements UMOTransactionManagerFactory
39 {
40     protected final Log logger = LogFactory.getLog(getClass());
41
42     protected Context JavaDoc context;
43
44     private Map JavaDoc environment;
45
46     private TransactionManager JavaDoc txManager;
47
48     private String JavaDoc jndiName;
49
50     public String JavaDoc getJndiName()
51     {
52         return jndiName;
53     }
54
55     public void setJndiName(final String JavaDoc jndiName)
56     {
57         this.jndiName = jndiName;
58     }
59
60     public TransactionManager JavaDoc getTxManager()
61     {
62         return txManager;
63     }
64
65     public void setTxManager(final TransactionManager JavaDoc txManager)
66     {
67         this.txManager = txManager;
68     }
69
70     public Map JavaDoc getEnvironment()
71     {
72         return environment;
73     }
74
75     public void setEnvironment(final Map JavaDoc environment)
76     {
77         this.environment = environment;
78     }
79
80     public Context JavaDoc getContext()
81     {
82         return context;
83     }
84
85     public void setContext(final Context JavaDoc context)
86     {
87         this.context = context;
88     }
89
90     /**
91      * @see org.mule.umo.manager.UMOTransactionManagerFactory#create()
92      */

93     public TransactionManager JavaDoc create() throws Exception JavaDoc
94     {
95         // implementing the Initilisable interface does not call it??
96
initialise();
97         if (txManager == null)
98         {
99             txManager = (TransactionManager JavaDoc)context.lookup(jndiName);
100         }
101
102         return txManager;
103     }
104
105     /**
106      * Method used to perform any initialisation work. If a fatal error occurs during
107      * initialisation an <code>InitialisationException</code> should be thrown,
108      * causing the Mule instance to shutdown. If the error is recoverable, say by
109      * retrying to connect, a <code>RecoverableException</code> should be thrown.
110      * There is no guarantee that by throwing a Recoverable exception that the Mule
111      * instance will not shut down.
112      *
113      * @throws org.mule.umo.lifecycle.InitialisationException if a fatal error occurs
114      * causing the Mule instance to shutdown
115      * @throws org.mule.umo.lifecycle.RecoverableException if an error occurs that
116      * can be recovered from
117      */

118     public void initialise() throws InitialisationException
119     {
120         if (txManager == null && StringUtils.isEmpty(StringUtils.trim(jndiName)))
121         {
122             throw new InitialisationException(new Message(Messages.PROPERTIES_X_NOT_SET, "jndiName"), this);
123         }
124
125         try
126         {
127             if (context == null)
128             {
129                 context = JndiContextHelper.initialise(getEnvironment());
130             }
131         }
132         catch (NamingException JavaDoc e)
133         {
134             throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X, "Jndi context"), e,
135                 this);
136         }
137     }
138 }
139
Popular Tags