1 17 18 19 package org.apache.naming.factory; 20 21 import java.util.Hashtable ; 22 import javax.naming.Name ; 23 import javax.naming.Context ; 24 import javax.naming.NamingException ; 25 import javax.naming.Reference ; 26 import javax.naming.RefAddr ; 27 import javax.naming.spi.ObjectFactory ; 28 import org.apache.naming.ResourceRef; 29 30 36 37 public class ResourceFactory 38 implements ObjectFactory { 39 40 41 43 44 46 47 49 50 52 53 55 56 61 public Object getObjectInstance(Object obj, Name name, Context nameCtx, 62 Hashtable environment) 63 throws Exception { 64 65 if (obj instanceof ResourceRef) { 66 Reference ref = (Reference ) obj; 67 ObjectFactory factory = null; 68 RefAddr factoryRefAddr = ref.get(Constants.FACTORY); 69 if (factoryRefAddr != null) { 70 String factoryClassName = 72 factoryRefAddr.getContent().toString(); 73 ClassLoader tcl = 75 Thread.currentThread().getContextClassLoader(); 76 Class factoryClass = null; 77 if (tcl != null) { 78 try { 79 factoryClass = tcl.loadClass(factoryClassName); 80 } catch(ClassNotFoundException e) { 81 NamingException ex = new NamingException  82 ("Could not load resource factory class"); 83 ex.initCause(e); 84 throw ex; 85 } 86 } else { 87 try { 88 factoryClass = Class.forName(factoryClassName); 89 } catch(ClassNotFoundException e) { 90 NamingException ex = new NamingException  91 ("Could not load resource factory class"); 92 ex.initCause(e); 93 throw ex; 94 } 95 } 96 if (factoryClass != null) { 97 try { 98 factory = (ObjectFactory ) factoryClass.newInstance(); 99 } catch (Throwable t) { 100 if (t instanceof NamingException ) 101 throw (NamingException ) t; 102 NamingException ex = new NamingException  103 ("Could not create resource factory instance"); 104 ex.initCause(t); 105 throw ex; 106 } 107 } 108 } else { 109 if (ref.getClassName().equals("javax.sql.DataSource")) { 110 String javaxSqlDataSourceFactoryClassName = 111 System.getProperty("javax.sql.DataSource.Factory", 112 Constants.DBCP_DATASOURCE_FACTORY); 113 try { 114 factory = (ObjectFactory ) 115 Class.forName(javaxSqlDataSourceFactoryClassName) 116 .newInstance(); 117 } catch (Throwable t) { 118 NamingException ex = new NamingException  119 ("Could not create resource factory instance"); 120 ex.initCause(t); 121 throw ex; 122 } 123 } else if (ref.getClassName().equals("javax.mail.Session")) { 124 String javaxMailSessionFactoryClassName = 125 System.getProperty("javax.mail.Session.Factory", 126 "org.apache.naming.factory.MailSessionFactory"); 127 try { 128 factory = (ObjectFactory ) 129 Class.forName(javaxMailSessionFactoryClassName) 130 .newInstance(); 131 } catch(Throwable t) { 132 NamingException ex = new NamingException  133 ("Could not create resource factory instance"); 134 ex.initCause(t); 135 throw ex; 136 } 137 } 138 } 139 if (factory != null) { 140 return factory.getObjectInstance 141 (obj, name, nameCtx, environment); 142 } else { 143 throw new NamingException  144 ("Cannot create resource instance"); 145 } 146 } 147 148 return null; 149 150 } 151 152 153 } 154 155
| Popular Tags
|