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.ResourceRef;
027:
028: /**
029: * Object factory for Resources.
030: *
031: * @author Remy Maucherat
032: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:54 $
033: */
034:
035: public class ResourceFactory 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 DataSource 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 ResourceRef) {
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: throw new NamingException(
072: "Could not create resource factory, ClassNotFoundException:"
073: + e.getMessage());
074: }
075: } else {
076: try {
077: factoryClass = Class.forName(factoryClassName);
078: } catch (ClassNotFoundException e) {
079: throw new NamingException(
080: "Could not create resource factory, ClassNotFoundException:"
081: + e.getMessage());
082: }
083: }
084: if (factoryClass != null) {
085: try {
086: factory = (ObjectFactory) factoryClass
087: .newInstance();
088: } catch (Throwable t) {
089: if (t instanceof NamingException)
090: throw (NamingException) t;
091: throw new NamingException(
092: "Could not create resource factory instance, "
093: + t.getMessage());
094: }
095: }
096: } else {
097: if (ref.getClassName().equals("javax.sql.DataSource")) {
098: String javaxSqlDataSourceFactoryClassName = System
099: .getProperty(
100: "javax.sql.DataSource.Factory",
101: Constants.DBCP_DATASOURCE_FACTORY);
102: try {
103: factory = (ObjectFactory) Class.forName(
104: javaxSqlDataSourceFactoryClassName)
105: .newInstance();
106: } catch (Throwable t) {
107:
108: }
109: } else if (ref.getClassName().equals(
110: "javax.mail.Session")) {
111: String javaxMailSessionFactoryClassName = System
112: .getProperty("javax.mail.Session.Factory",
113: "org.apache.naming.factory.MailSessionFactory");
114: try {
115: factory = (ObjectFactory) Class.forName(
116: javaxMailSessionFactoryClassName)
117: .newInstance();
118: } catch (Throwable t) {
119: }
120: }
121: }
122: if (factory != null) {
123: return factory.getObjectInstance(obj, name, nameCtx,
124: environment);
125: } else {
126: throw new NamingException(
127: "Cannot create resource instance");
128: }
129: }
130:
131: return null;
132:
133: }
134:
135: }
|