001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.transaction;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.compass.core.config.CompassEnvironment;
022: import org.compass.core.config.CompassSettings;
023: import org.compass.core.transaction.manager.Glassfish;
024: import org.compass.core.transaction.manager.JBoss;
025: import org.compass.core.transaction.manager.JOTM;
026: import org.compass.core.transaction.manager.JOnAS;
027: import org.compass.core.transaction.manager.JRun4;
028: import org.compass.core.transaction.manager.OC4J;
029: import org.compass.core.transaction.manager.Orion;
030: import org.compass.core.transaction.manager.Resin;
031: import org.compass.core.transaction.manager.WebSphere;
032: import org.compass.core.transaction.manager.Weblogic;
033: import org.compass.core.util.ClassUtils;
034:
035: /**
036: * @author kimchy
037: */
038: public final class TransactionManagerLookupFactory {
039:
040: private static final Log log = LogFactory
041: .getLog(TransactionManagerLookupFactory.class);
042:
043: private static Class[] autoDetectOrder = { WebSphere.class,
044: Weblogic.class, JOnAS.class, JOTM.class, JBoss.class,
045: Glassfish.class, Orion.class, Resin.class, OC4J.class,
046: JRun4.class };
047:
048: private TransactionManagerLookupFactory() {
049: }
050:
051: public static TransactionManagerLookup getTransactionManagerLookup(
052: CompassSettings settings) throws TransactionException {
053: String tmLookupClass = settings
054: .getSetting(CompassEnvironment.Transaction.MANAGER_LOOKUP);
055: if (tmLookupClass == null) {
056: // try and auto detect the transaction manager
057: log
058: .info("JTA Transaction Manager Lookup setting not found, auto detecting....");
059: for (int i = 0; i < autoDetectOrder.length; i++) {
060: if (log.isDebugEnabled()) {
061: log.debug("Trying [" + autoDetectOrder[i].getName()
062: + "]");
063: }
064: TransactionManagerLookup tmLookup = detect(
065: autoDetectOrder[i], settings);
066: if (tmLookup != null) {
067: log.info("Detected JTA Transaction Manager ["
068: + autoDetectOrder[i].getName() + "]");
069: return tmLookup;
070: }
071: }
072: return null;
073: } else {
074: log.info("Instansiating TransactionManagerLookup ["
075: + tmLookupClass + "]");
076: try {
077: return (TransactionManagerLookup) ClassUtils.forName(
078: tmLookupClass, settings.getClassLoader())
079: .newInstance();
080: } catch (Exception e) {
081: throw new TransactionException(
082: "Could not instantiate TransactionManagerLookup",
083: e);
084: }
085: }
086: }
087:
088: private static TransactionManagerLookup detect(Class tmClass,
089: CompassSettings settings) {
090: try {
091: TransactionManagerLookup tmLookup = (TransactionManagerLookup) tmClass
092: .newInstance();
093: if (tmLookup.getTransactionManager(settings) != null) {
094: return tmLookup;
095: }
096: } catch (Exception e) {
097: // do nothing here
098: }
099: return null;
100: }
101: }
|