1 21 package org.apache.derby.jdbc; 22 23 import java.sql.SQLException ; 24 import java.lang.reflect.*; 25 26 27 import java.io.Serializable ; 28 import java.io.PrintWriter ; 29 import java.util.Properties ; 30 31 32 import javax.naming.NamingException ; 33 import javax.naming.Referenceable ; 34 import javax.naming.Reference ; 35 import javax.naming.StringRefAddr ; 36 import javax.naming.spi.ObjectFactory ; 37 import javax.naming.Context ; 38 import javax.naming.Name ; 39 import javax.naming.Reference ; 40 import javax.naming.RefAddr ; 41 import java.util.Hashtable ; 42 import java.util.Enumeration ; 43 44 65 public class ReferenceableDataSource implements 66 javax.naming.Referenceable , 67 java.io.Serializable , 68 ObjectFactory 69 { 70 71 72 private static final long serialVersionUID = 1872877359127597176L; 73 74 75 private static final Class [] STRING_ARG = { "".getClass() }; 76 private static final Class [] INT_ARG = { Integer.TYPE }; 77 private static final Class [] BOOLEAN_ARG = { Boolean.TYPE }; 78 79 private String description; 80 private String dataSourceName; 81 private String databaseName; 82 private String password; 83 private String user; 84 private int loginTimeout; 85 86 87 88 transient private PrintWriter printer; 89 90 93 public ReferenceableDataSource() { 94 update(); 95 } 96 97 98 101 102 110 public final synchronized void setDatabaseName(String databaseName) { 111 this.databaseName = databaseName; 112 update(); 113 } 114 public String getDatabaseName() { 115 return databaseName; 116 } 117 118 124 public final void setDataSourceName(String dsn) { 125 dataSourceName = dsn; 126 } 127 128 public final String getDataSourceName() { 129 return dataSourceName; 130 } 131 132 138 public final void setDescription(String desc) { 139 description = desc; 140 } 141 142 public final String getDescription() { 143 return description; 144 } 145 146 151 public final void setUser(String user) { 152 this.user = user; 153 } 154 155 public final String getUser() { 156 return user; 157 } 158 159 164 public final void setPassword(String password) { 165 this.password = password; 166 } 167 168 public final String getPassword() { 169 return password; 170 } 171 172 175 176 177 188 public int getLoginTimeout() throws SQLException 189 { 190 return loginTimeout; 191 } 192 193 205 public void setLoginTimeout(int seconds) throws SQLException 206 { 207 loginTimeout = seconds; 208 } 209 210 211 227 public PrintWriter getLogWriter() throws SQLException 228 { 229 return printer; 230 } 231 232 248 public void setLogWriter(PrintWriter out) throws SQLException 249 { 250 printer = out; 251 } 252 253 256 257 260 261 280 public Object getObjectInstance(Object obj, 281 Name name, 282 Context nameCtx, 283 Hashtable environment) 284 throws Exception 285 { 286 Reference ref = (Reference )obj; 287 String classname = ref.getClassName(); 288 289 Object ds = Class.forName(classname).newInstance(); 290 291 for (Enumeration e = ref.getAll(); e.hasMoreElements(); ) { 292 293 RefAddr attribute = (RefAddr ) e.nextElement(); 294 295 String propertyName = attribute.getType(); 296 297 String value = (String ) attribute.getContent(); 298 299 String methodName = "set" + propertyName.substring(0,1).toUpperCase(java.util.Locale.ENGLISH) + propertyName.substring(1); 300 301 Method m; 302 303 Object argValue; 304 try { 305 m = ds.getClass().getMethod(methodName, STRING_ARG); 306 argValue = value; 307 } catch (NoSuchMethodException nsme) { 308 try { 309 m = ds.getClass().getMethod(methodName, INT_ARG); 310 argValue = Integer.valueOf(value); 311 } catch (NoSuchMethodException nsme2) { 312 m = ds.getClass().getMethod(methodName, BOOLEAN_ARG); 313 argValue = Boolean.valueOf(value); 314 } 315 } 316 m.invoke(ds, new Object [] { argValue }); 317 } 318 319 return ds; 320 } 321 322 327 public final Reference getReference() throws NamingException 328 { 329 Reference ref = new Reference (this.getClass().getName(), 332 "org.apache.derby.jdbc.ReferenceableDataSource", 333 null); 334 335 336 Method[] methods = this.getClass().getMethods(); 338 339 for (int i = 0; i < methods.length; i++) { 340 341 Method m = methods[i]; 342 343 if (m.getParameterTypes().length != 0) 345 continue; 346 347 if (Modifier.isStatic(m.getModifiers())) 349 continue; 350 351 String methodName = m.getName(); 353 if ((methodName.length() < 5) || !methodName.startsWith("get")) 354 continue; 355 356 357 358 Class returnType = m.getReturnType(); 359 360 if (Integer.TYPE.equals(returnType) || STRING_ARG[0].equals(returnType) || Boolean.TYPE.equals(returnType)) { 361 362 365 String propertyName = methodName.substring(3,4).toLowerCase(java.util.Locale.ENGLISH).concat(methodName.substring(4)); 366 367 try { 368 Object ov = m.invoke(this, null); 369 370 if (ov != null) { 373 ref.add(new StringRefAddr (propertyName, ov.toString())); 374 } 375 } catch (IllegalAccessException iae) { 376 } catch (InvocationTargetException ite) { 377 } 378 379 380 } 381 } 382 383 return ref; 384 } 385 386 387 void update() { 388 } 389 390 393 java.sql.Connection getConnection(String username, String password, boolean requestPassword) throws SQLException { 394 return null; 395 } 396 397 } 398 | Popular Tags |