001: /*
002: * $Id: GenericTransactionManagerLookupFactory.java 11244 2008-03-07 05:06:27Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transaction.lookup;
012:
013: import org.mule.api.lifecycle.Initialisable;
014: import org.mule.api.lifecycle.InitialisationException;
015: import org.mule.api.lifecycle.LifecycleTransitionResult;
016: import org.mule.api.transaction.TransactionManagerFactory;
017: import org.mule.config.i18n.CoreMessages;
018: import org.mule.util.JndiContextHelper;
019: import org.mule.util.StringUtils;
020:
021: import java.util.Map;
022:
023: import javax.naming.Context;
024: import javax.naming.NamingException;
025: import javax.transaction.TransactionManager;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * A factory performing a JNDI lookup for TransactionManager. <p/> NOTE: Java EE 1.4
032: * specification does not mandate application server vendors to expose a
033: * TransactionManager for direct use, nor does it name the standard way to locate it.
034: * For some servers the TransactionManager is not even available in the global JNDI
035: * namespace, so your only bet is to run Mule in the same JVM as the application
036: * server.
037: */
038: public class GenericTransactionManagerLookupFactory implements
039: TransactionManagerFactory, Initialisable {
040: protected final Log logger = LogFactory.getLog(getClass());
041:
042: protected Context context;
043:
044: private Map environment;
045:
046: private TransactionManager txManager;
047:
048: private String jndiName;
049:
050: public String getJndiName() {
051: return jndiName;
052: }
053:
054: public void setJndiName(final String jndiName) {
055: this .jndiName = jndiName;
056: }
057:
058: public TransactionManager getTxManager() {
059: return txManager;
060: }
061:
062: public void setTxManager(final TransactionManager txManager) {
063: this .txManager = txManager;
064: }
065:
066: public Map getEnvironment() {
067: return environment;
068: }
069:
070: public void setEnvironment(final Map environment) {
071: this .environment = environment;
072: }
073:
074: public Context getContext() {
075: return context;
076: }
077:
078: public void setContext(final Context context) {
079: this .context = context;
080: }
081:
082: /** @see org.mule.api.transaction.TransactionManagerFactory#create() */
083: public TransactionManager create() throws Exception {
084: // implementing the Initilisable interface does not call it??
085: initialise();
086: if (txManager == null) {
087: txManager = (TransactionManager) context.lookup(jndiName);
088: }
089:
090: return txManager;
091: }
092:
093: /**
094: * Method used to perform any initialisation work. If a fatal error occurs during
095: * initialisation an <code>InitialisationException</code> should be thrown,
096: * causing the Mule instance to shutdown. If the error is recoverable, say by
097: * retrying to connect, a <code>RecoverableException</code> should be thrown.
098: * There is no guarantee that by throwing a Recoverable exception that the Mule
099: * instance will not shut down.
100: *
101: * @throws org.mule.api.lifecycle.InitialisationException
102: * if a fatal error occurs
103: * causing the Mule instance to shutdown
104: */
105: public LifecycleTransitionResult initialise()
106: throws InitialisationException {
107: if (txManager == null
108: && StringUtils.isEmpty(StringUtils.trim(jndiName))) {
109: throw new InitialisationException(CoreMessages
110: .propertiesNotSet("jndiName"), this );
111: }
112:
113: try {
114: if (context == null) {
115: context = JndiContextHelper
116: .initialise(getEnvironment());
117: }
118: } catch (NamingException e) {
119: throw new InitialisationException(CoreMessages
120: .failedToCreate("Jndi context"), e, this);
121: }
122: return LifecycleTransitionResult.OK;
123: }
124: }
|