001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.naming.factory;
019:
020: import java.util.Hashtable;
021: import javax.naming.Name;
022: import javax.naming.Context;
023: import javax.naming.NamingException;
024: import javax.naming.Reference;
025: import javax.naming.RefAddr;
026: import javax.naming.spi.ObjectFactory;
027: import org.apache.naming.TransactionRef;
028:
029: /**
030: * Object factory for User trasactions.
031: *
032: * @author Remy Maucherat
033: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
034: */
035:
036: public class TransactionFactory implements ObjectFactory {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: // -------------------------------------------------------------- Constants
041:
042: // ----------------------------------------------------- Instance Variables
043:
044: // --------------------------------------------------------- Public Methods
045:
046: // -------------------------------------------------- ObjectFactory Methods
047:
048: /**
049: * Crete a new User transaction instance.
050: *
051: * @param obj The reference object describing the DataSource
052: */
053: public Object getObjectInstance(Object obj, Name name,
054: Context nameCtx, Hashtable environment) throws Exception {
055:
056: if (obj instanceof TransactionRef) {
057: Reference ref = (Reference) obj;
058: ObjectFactory factory = null;
059: RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
060: if (factoryRefAddr != null) {
061: // Using the specified factory
062: String factoryClassName = factoryRefAddr.getContent()
063: .toString();
064: // Loading factory
065: ClassLoader tcl = Thread.currentThread()
066: .getContextClassLoader();
067: Class factoryClass = null;
068: if (tcl != null) {
069: try {
070: factoryClass = tcl.loadClass(factoryClassName);
071: } catch (ClassNotFoundException e) {
072: NamingException ex = new NamingException(
073: "Could not load resource factory class");
074: ex.initCause(e);
075: throw ex;
076: }
077: } else {
078: try {
079: factoryClass = Class.forName(factoryClassName);
080: } catch (ClassNotFoundException e) {
081: NamingException ex = new NamingException(
082: "Could not load resource factory class");
083: ex.initCause(e);
084: throw ex;
085: }
086: }
087: if (factoryClass != null) {
088: try {
089: factory = (ObjectFactory) factoryClass
090: .newInstance();
091: } catch (Throwable t) {
092: if (t instanceof NamingException)
093: throw (NamingException) t;
094: NamingException ex = new NamingException(
095: "Could not create resource factory instance");
096: ex.initCause(t);
097: throw ex;
098: }
099: }
100: }
101: if (factory != null) {
102: return factory.getObjectInstance(obj, name, nameCtx,
103: environment);
104: } else {
105: throw new NamingException(
106: "Cannot create resource instance");
107: }
108:
109: }
110:
111: return null;
112:
113: }
114:
115: }
|