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.ResourceRef;
028:
029: /**
030: * Object factory for Resources.
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 ResourceFactory 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 DataSource 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 ResourceRef) {
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: } else {
101: if (ref.getClassName().equals("javax.sql.DataSource")) {
102: String javaxSqlDataSourceFactoryClassName = System
103: .getProperty(
104: "javax.sql.DataSource.Factory",
105: Constants.DBCP_DATASOURCE_FACTORY);
106: try {
107: factory = (ObjectFactory) Class.forName(
108: javaxSqlDataSourceFactoryClassName)
109: .newInstance();
110: } catch (Throwable t) {
111: NamingException ex = new NamingException(
112: "Could not create resource factory instance");
113: ex.initCause(t);
114: throw ex;
115: }
116: } else if (ref.getClassName().equals(
117: "javax.mail.Session")) {
118: String javaxMailSessionFactoryClassName = System
119: .getProperty("javax.mail.Session.Factory",
120: "org.apache.naming.factory.MailSessionFactory");
121: try {
122: factory = (ObjectFactory) Class.forName(
123: javaxMailSessionFactoryClassName)
124: .newInstance();
125: } catch (Throwable t) {
126: NamingException ex = new NamingException(
127: "Could not create resource factory instance");
128: ex.initCause(t);
129: throw ex;
130: }
131: }
132: }
133: if (factory != null) {
134: return factory.getObjectInstance(obj, name, nameCtx,
135: environment);
136: } else {
137: throw new NamingException(
138: "Cannot create resource instance");
139: }
140: }
141:
142: return null;
143:
144: }
145:
146: }
|