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 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.ResourceEnvRef; 028 029 /** 030 * Object factory for Resources env. 031 * 032 * @author Remy Maucherat 033 * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:54 $ 034 */ 035 036 public class ResourceEnvFactory 037 implements ObjectFactory { 038 039 040 // ----------------------------------------------------------- Constructors 041 042 043 // -------------------------------------------------------------- Constants 044 045 046 // ----------------------------------------------------- Instance Variables 047 048 049 // --------------------------------------------------------- Public Methods 050 051 052 // -------------------------------------------------- ObjectFactory Methods 053 054 055 /** 056 * Crete a new Resource env instance. 057 * 058 * @param obj The reference object describing the DataSource 059 */ 060 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 061 Hashtable environment) 062 throws Exception { 063 064 if (obj instanceof ResourceEnvRef) { 065 Reference ref = (Reference) obj; 066 ObjectFactory factory = null; 067 RefAddr factoryRefAddr = ref.get(Constants.FACTORY); 068 if (factoryRefAddr != null) { 069 // Using the specified factory 070 String factoryClassName = 071 factoryRefAddr.getContent().toString(); 072 // Loading factory Rate073 ClassLoader tcl = 074 Thread.currentThread().getContextClassLoader(); 075 Class factoryClass = null; 076 if (tcl != null) { 077 try { 078 factoryClass = tcl.loadClass(factoryClassName); 079 } catch(ClassNotFoundException e) { 080 } 081 } else { 082 try { 083 factoryClass = Class.forName(factoryClassName); 084 } catch(ClassNotFoundException e) { 085 } 086 } 087 if (factoryClass != null) { 088 try { 089 factory = (ObjectFactory) factoryClass.newInstance(); 090 } catch(Throwable t) { 091 } 092 } 093 } 094 // Note: No defaults here 095 if (factory != null) { 096 return factory.getObjectInstance 097 (obj, name, nameCtx, environment); 098 } else { 099 throw new NamingException 100 ("Cannot create resource instance"); 101 } 102 } 103 104 return null; 105 106 } 107 108 109 }