1 23 24 package com.sun.gjc.common; 25 26 import java.lang.reflect.Method ; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 29 import java.util.StringTokenizer ; 30 import java.util.Enumeration ; 31 import com.sun.gjc.util.MethodExecutor; 32 import javax.resource.ResourceException ; 33 34 import com.sun.logging.*; 35 import java.util.logging.Logger ; 36 import java.util.logging.Level ; 37 import com.sun.enterprise.util.i18n.StringManager; 38 39 48 public class DataSourceObjectBuilder implements java.io.Serializable { 49 50 private DataSourceSpec spec; 51 52 private Hashtable driverProperties = null; 53 54 private MethodExecutor executor = null; 55 56 private static Logger _logger; 57 static { 58 _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER ); 59 } 60 private boolean debug = false; 61 62 private StringManager sm = StringManager.getManager( 63 DataSourceObjectBuilder.class ); 64 69 public DataSourceObjectBuilder(DataSourceSpec spec) { 70 this.spec = spec; 71 executor = new MethodExecutor(); 72 } 73 74 81 public Object constructDataSourceObject() throws ResourceException { 82 driverProperties = parseDriverProperties(spec); 83 Object dataSourceObject = getDataSourceObject(); 84 Method [] methods = dataSourceObject.getClass().getMethods(); 85 for (int i=0; i < methods.length; i++) { 86 String methodName = methods[i].getName(); 87 if (driverProperties.containsKey(methodName.toUpperCase())){ 90 Vector values = (Vector ) driverProperties.get(methodName.toUpperCase()); 91 executor.runMethod(methods[i],dataSourceObject, values); 92 } else if (methodName.equalsIgnoreCase("setUser")){ 93 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.USERNAME),methods[i],dataSourceObject); 94 95 } else if (methodName.equalsIgnoreCase("setPassword")){ 96 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.PASSWORD),methods[i],dataSourceObject); 97 98 } else if (methodName.equalsIgnoreCase("setLoginTimeOut")){ 99 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.LOGINTIMEOUT),methods[i],dataSourceObject); 100 101 } else if (methodName.equalsIgnoreCase("setLogWriter")){ 102 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.LOGWRITER),methods[i],dataSourceObject); 103 104 } else if (methodName.equalsIgnoreCase("setDatabaseName")){ 105 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.DATABASENAME),methods[i],dataSourceObject); 106 107 } else if (methodName.equalsIgnoreCase("setDataSourceName")){ 108 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.DATASOURCENAME),methods[i],dataSourceObject); 109 110 } else if (methodName.equalsIgnoreCase("setDescription")){ 111 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.DESCRIPTION),methods[i],dataSourceObject); 112 113 } else if (methodName.equalsIgnoreCase("setNetworkProtocol")){ 114 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.NETWORKPROTOCOL),methods[i],dataSourceObject); 115 116 } else if (methodName.equalsIgnoreCase("setPortNumber")){ 117 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.PORTNUMBER),methods[i],dataSourceObject); 118 119 } else if (methodName.equalsIgnoreCase("setRoleName")){ 120 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.ROLENAME),methods[i],dataSourceObject); 121 122 } else if (methodName.equalsIgnoreCase("setServerName")){ 123 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.SERVERNAME),methods[i],dataSourceObject); 124 125 } else if (methodName.equalsIgnoreCase("setMaxStatements")){ 126 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MAXSTATEMENTS),methods[i],dataSourceObject); 127 128 } else if (methodName.equalsIgnoreCase("setInitialPoolSize")){ 129 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.INITIALPOOLSIZE),methods[i],dataSourceObject); 130 131 } else if (methodName.equalsIgnoreCase("setMinPoolSize")){ 132 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MINPOOLSIZE),methods[i],dataSourceObject); 133 134 } else if (methodName.equalsIgnoreCase("setMaxPoolSize")){ 135 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MAXPOOLSIZE),methods[i],dataSourceObject); 136 137 } else if (methodName.equalsIgnoreCase("setMaxIdleTime")){ 138 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.MAXIDLETIME),methods[i],dataSourceObject); 139 140 } else if (methodName.equalsIgnoreCase("setPropertyCycle")){ 141 executor.runJavaBeanMethod(spec.getDetail(DataSourceSpec.PROPERTYCYCLE),methods[i],dataSourceObject); 142 143 } 144 } 145 return dataSourceObject; 146 } 147 148 158 private Hashtable parseDriverProperties(DataSourceSpec spec) throws ResourceException { 159 String delim = spec.getDetail(DataSourceSpec.DELIMITER); 160 161 String prop = spec.getDetail(DataSourceSpec.DRIVERPROPERTIES); 162 if ( prop == null || prop.trim().equals("")) { 163 return new Hashtable (); 164 } else if (delim == null || delim.equals("")) { 165 String msg = sm.getString("dsob.delim_not_specified"); 166 throw new ResourceException (msg); 167 } 168 169 Hashtable properties = new Hashtable (); 170 delim = delim.trim(); 171 String sep = delim+delim; 172 int sepLen = sep.length(); 173 String cache = prop; 174 Vector methods = new Vector (); 175 176 while (cache.indexOf(sep) != -1) { 177 int index = cache.indexOf(sep); 178 String name = cache.substring(0,index); 179 if (name.trim() != "") { 180 methods.add(name); 181 cache = cache.substring(index+sepLen); 182 } 183 } 184 185 Enumeration allMethods = methods.elements(); 186 while (allMethods.hasMoreElements()) { 187 String oneMethod = (String ) allMethods.nextElement(); 188 if (!oneMethod.trim().equals("")) { 189 String methodName = null; 190 Vector parms = new Vector (); 191 StringTokenizer methodDetails = new StringTokenizer (oneMethod,delim); 192 for (int i=0; methodDetails.hasMoreTokens();i++ ) { 193 String token = (String ) methodDetails.nextToken(); 194 if (i==0) { 195 methodName = token.toUpperCase(); 196 } else { 197 parms.add(token); 198 } 199 } 200 properties.put(methodName,parms); 201 } 202 } 203 return properties; 204 } 205 206 213 private Object getDataSourceObject() throws ResourceException { 214 String className = spec.getDetail(DataSourceSpec.CLASSNAME); 215 try { 216 Class dataSourceClass = Thread.currentThread().getContextClassLoader().loadClass(className); 217 Object dataSourceObject = dataSourceClass.newInstance(); 218 return dataSourceObject; 219 } catch(ClassNotFoundException cfne){ 220 _logger.log(Level.SEVERE, "jdbc.exc_cnfe_ds", cfne); 221 String msg = sm.getString( "dsob.class_not_found", className); 222 throw new ResourceException (msg); 223 } catch(InstantiationException ce) { 224 _logger.log(Level.SEVERE, "jdbc.exc_inst", className); 225 String msg = sm.getString( "dsob.error_instantiating", className ); 226 throw new ResourceException (msg); 227 } catch(IllegalAccessException ce) { 228 _logger.log(Level.SEVERE, "jdbc.exc_acc_inst", className); 229 String msg = sm.getString( "dsob.access_error", className ); 230 throw new ResourceException (msg); 231 } 232 } 233 234 } 235 | Popular Tags |