001: /**********************************************************************
002: Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.jta;
018:
019: import java.lang.reflect.Constructor;
020:
021: import javax.transaction.TransactionManager;
022:
023: import org.jpox.ClassLoaderResolver;
024: import org.jpox.OMFContext;
025:
026: /**
027: * Entry point for locating a JTA TransactionManager.
028: * @version $Revision: 1.8 $
029: */
030: public class TransactionManagerFinder {
031: /** The OMF Context. */
032: OMFContext omfContext;
033:
034: /** List of available locator class names. */
035: static String[] locators = null;
036:
037: /** Locator class to use (if any) */
038: String locator = null;
039:
040: /**
041: * Constructor.
042: * @param ctx Context for persistence
043: */
044: public TransactionManagerFinder(OMFContext ctx) {
045: if (locators == null) {
046: // Load up all available locators for future reference
047: locators = ctx.getPluginManager()
048: .getAttributeValuesForExtension(
049: "org.jpox.jta_locator", null, null,
050: "class-name");
051: }
052: if (ctx.getPersistenceConfiguration().getJtaLocator() != null) {
053: // User has selected a locator via persistence properties
054: locator = ctx.getPluginManager()
055: .getAttributeValueForExtension(
056: "org.jpox.jta_locator",
057: "name",
058: ctx.getPersistenceConfiguration()
059: .getJtaLocator(), "class-name");
060: }
061: omfContext = ctx;
062: }
063:
064: /**
065: * Accessor for the accessible JTA transaction manager.
066: * @param clr ClassLoader resolver
067: * @return The JTA manager found (if any)
068: */
069: public TransactionManager getTransactionManager(
070: ClassLoaderResolver clr) {
071: if (locator != null) {
072: // User has specified the locator to use
073: TransactionManager tm = getTransactionManagerForLocator(
074: clr, locator);
075: if (tm != null) {
076: return tm;
077: }
078: } else {
079: // User hasnt specified which locator so go through them all
080: for (int i = 0; i < locators.length; i++) {
081: TransactionManager tm = getTransactionManagerForLocator(
082: clr, locators[i]);
083: if (tm != null) {
084: return tm;
085: }
086: }
087: }
088: return null;
089: }
090:
091: /**
092: * Convenience method to get the TransactionManager for the specified locator class.
093: * @param clr ClassLoader resolver
094: * @param locatorClassName Class name for the locator
095: * @return The TransactionManager (if found)
096: */
097: protected TransactionManager getTransactionManagerForLocator(
098: ClassLoaderResolver clr, String locatorClassName) {
099: try {
100: Class cls = clr.classForName(locatorClassName);
101: if (cls != null) {
102: // Find the locator
103: TransactionManagerLocator loc = null;
104: try {
105: // Try with constructor taking OMFContext
106: Class[] params = new Class[] { OMFContext.class };
107: Constructor ctor = cls.getConstructor(params);
108: Object[] args = new Object[] { omfContext };
109: loc = (TransactionManagerLocator) ctor
110: .newInstance(args);
111: } catch (NoSuchMethodException nsme) {
112: // No constructor taking OMFContext so use default constructor
113: loc = (TransactionManagerLocator) cls.newInstance();
114: }
115:
116: if (loc != null) {
117: // Return the actual TransactionManager
118: TransactionManager tm = loc
119: .getTransactionManager(clr);
120: if (tm != null) {
121: return tm;
122: }
123: }
124: }
125: } catch (Exception e) {
126: // Do nothing
127: }
128: return null;
129: }
130: }
|