01: /**********************************************************************
02: Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15: Contributors:
16: ...
17: **********************************************************************/package org.jpox.jta;
18:
19: import javax.naming.InitialContext;
20: import javax.naming.NamingException;
21: import javax.transaction.TransactionManager;
22:
23: import org.jpox.ClassLoaderResolver;
24: import org.jpox.exceptions.JPOXException;
25: import org.jpox.util.Localiser;
26:
27: /**
28: * Locator for a JTA TransactionManager using JNDI context namings.
29: * All extending classes must provide the method <i>getJNDIName()</i> returning a name that is then looked up
30: * via JNDI to return the manager object.
31: *
32: * @version $Revision: 1.1 $
33: */
34: public abstract class JNDIBasedTransactionManagerLocator implements
35: TransactionManagerLocator {
36: /** Localisation utility for output messages */
37: protected static final Localiser LOCALISER = Localiser
38: .getInstance("org.jpox.Localisation");
39:
40: /**
41: * Accessor for the JNDI name to use.
42: * @return The JNDI name where the txn manager is stored.
43: */
44: public abstract String getJNDIName();
45:
46: /**
47: * Method to return the TransactionManager looking it up using JNDI.
48: * @param clr ClassLoader Resolver
49: * @return The TransactionManager
50: */
51: public TransactionManager getTransactionManager(
52: ClassLoaderResolver clr) {
53: try {
54: InitialContext ctx = new InitialContext();
55: try {
56: return (TransactionManager) ctx.lookup(getJNDIName());
57: } catch (Exception e) {
58: return null;
59: }
60: } catch (NamingException ne) {
61: // probably NoInitialContextException, other NamingExceptions due to bad names are silently caught above
62: throw new JPOXException(LOCALISER.msg("015029"), ne);
63: }
64: }
65: }
|