001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
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.apache.naming.factory;
018:
019: import java.util.Hashtable;
020: import javax.naming.Name;
021: import javax.naming.Context;
022: import javax.naming.NamingException;
023: import javax.naming.Reference;
024: import javax.naming.RefAddr;
025: import javax.naming.spi.ObjectFactory;
026: import org.apache.naming.TransactionRef;
027:
028: /**
029: * Object factory for User trasactions.
030: *
031: * @author Remy Maucherat
032: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:54 $
033: */
034:
035: public class TransactionFactory implements ObjectFactory {
036:
037: // ----------------------------------------------------------- Constructors
038:
039: // -------------------------------------------------------------- Constants
040:
041: // ----------------------------------------------------- Instance Variables
042:
043: // --------------------------------------------------------- Public Methods
044:
045: // -------------------------------------------------- ObjectFactory Methods
046:
047: /**
048: * Crete a new User transaction instance.
049: *
050: * @param obj The reference object describing the DataSource
051: */
052: public Object getObjectInstance(Object obj, Name name,
053: Context nameCtx, Hashtable environment) throws Exception {
054:
055: if (obj instanceof TransactionRef) {
056: Reference ref = (Reference) obj;
057: ObjectFactory factory = null;
058: RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
059: if (factoryRefAddr != null) {
060: // Using the specified factory
061: String factoryClassName = factoryRefAddr.getContent()
062: .toString();
063: // Loading factory
064: ClassLoader tcl = Thread.currentThread()
065: .getContextClassLoader();
066: Class factoryClass = null;
067: if (tcl != null) {
068: try {
069: factoryClass = tcl.loadClass(factoryClassName);
070: } catch (ClassNotFoundException e) {
071: }
072: } else {
073: try {
074: factoryClass = Class.forName(factoryClassName);
075: } catch (ClassNotFoundException e) {
076: }
077: }
078: if (factoryClass != null) {
079: try {
080: factory = (ObjectFactory) factoryClass
081: .newInstance();
082: } catch (Throwable t) {
083: }
084: }
085: }
086: if (factory != null) {
087: return factory.getObjectInstance(obj, name, nameCtx,
088: environment);
089: } else {
090: throw new NamingException(
091: "Cannot create resource instance");
092: }
093:
094: }
095:
096: return null;
097:
098: }
099:
100: }
|