1 21 22 package org.apache.derby.client; 23 24 import java.lang.reflect.Method ; 25 import java.util.Enumeration ; 26 27 import javax.naming.RefAddr ; 28 import javax.naming.Reference ; 29 import org.apache.derby.jdbc.ClientBaseDataSource; 30 31 import org.apache.derby.jdbc.ClientConnectionPoolDataSource; 32 import org.apache.derby.jdbc.ClientDataSource; 33 import org.apache.derby.jdbc.ClientXADataSource; 34 35 53 public class ClientDataSourceFactory implements javax.naming.spi.ObjectFactory { 54 55 public ClientDataSourceFactory() { 56 } 57 58 77 public Object getObjectInstance(Object refObj, 78 javax.naming.Name name, 79 javax.naming.Context nameContext, 80 java.util.Hashtable environment) throws java.lang.Exception { 81 javax.naming.Reference ref = (javax.naming.Reference ) refObj; 82 83 ClientBaseDataSource ds = null; 85 if (ref.getClassName().equals(ClientDataSource.className__)) { 86 ds = new ClientDataSource(); 87 } else if (ref.getClassName().equals(ClientXADataSource.className__)) { 88 ds = new ClientXADataSource(); 89 } else if (ref.getClassName().equals(ClientConnectionPoolDataSource.className__)) { 90 ds = new ClientConnectionPoolDataSource(); 91 } else { 92 return null; 93 } 94 95 ClientDataSourceFactory.setBeanProperties(ds, ref); 97 98 return ds; 99 } 100 101 102 private static final Class [] STRING_ARG = { "".getClass() }; 103 104 private static final Class [] INT_ARG = { Integer.TYPE }; 105 106 private static final Class [] BOOLEAN_ARG = { Boolean.TYPE }; 107 108 private static final Class [] SHORT_ARG = { Short.TYPE }; 109 110 118 private static void setBeanProperties(Object ds, Reference ref) 119 throws Exception { 120 121 for (Enumeration e = ref.getAll(); e.hasMoreElements();) { 122 123 RefAddr attribute = (RefAddr ) e.nextElement(); 124 125 String propertyName = attribute.getType(); 126 127 String value = (String ) attribute.getContent(); 128 129 String methodName = "set" 130 + propertyName.substring(0, 1).toUpperCase( 131 java.util.Locale.ENGLISH) 132 + propertyName.substring(1); 133 134 Method m; 135 136 Object argValue; 137 try { 138 m = ds.getClass().getMethod(methodName, STRING_ARG); 139 argValue = value; 140 } catch (NoSuchMethodException nsme) { 141 try { 142 m = ds.getClass().getMethod(methodName, INT_ARG); 143 argValue = Integer.valueOf(value); 144 } catch (NoSuchMethodException nsme2) { 145 try { 146 m = ds.getClass().getMethod(methodName, BOOLEAN_ARG); 147 argValue = Boolean.valueOf(value); 148 } catch (NoSuchMethodException nsme3) { 149 m = ds.getClass().getMethod(methodName, SHORT_ARG); 150 argValue = Short.valueOf(value); 151 } 152 } 153 } 154 m.invoke(ds, new Object [] { argValue }); 155 } 156 } 157 } 158 | Popular Tags |