1 23 24 package com.sun.enterprise.connectors.util; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import com.sun.logging.LogDomains; 35 import java.util.logging.*; 36 37 43 public class ConnectionDefinitionUtils { 44 45 static Logger _logger= LogDomains.getLogger(LogDomains.RSR_LOGGER); 46 47 57 public static Set getConnectionDefinitionProperties(String connectionDefinitionClassName) { 58 HashSet propertySet= new HashSet (); 59 try { 60 Method [] methods= 61 Thread.currentThread().getContextClassLoader().loadClass(connectionDefinitionClassName).getMethods(); 62 for (int i= 0; i < methods.length; i++) { 63 if (isValidSetterMethod(methods[i])) { 67 String name= methods[i].getName(); 68 String propertyName= 69 name.substring( 70 (name.indexOf("set") + "set".length()), 71 name.length()); 72 propertySet.add(propertyName); 73 } 74 } 75 } catch (SecurityException e) { 76 handleException(e); 77 } catch (ClassNotFoundException e) { 78 handleException(e); 79 } 80 return propertySet; 81 } 82 83 private static boolean isValidSetterMethod(Method method) { 84 return ( 85 (method.getName().startsWith("set")) 86 && (method.getParameterTypes().length == 1) 87 && (isValidArgumentType(method))); 88 } 89 90 private static boolean isValidArgumentType(Method method) { 91 Class [] parameters= method.getParameterTypes(); 92 boolean isValid= true; 93 for (int i= 0; i < parameters.length; i++) { 94 Class param= parameters[i]; 95 if (!(param.isPrimitive() || param.equals(String .class))) 96 return false; 97 } 98 return isValid; 99 } 100 101 111 public static Map getConnectionDefinitionPropertiesAndDefaults(String connectionDefinitionClassName) { 112 Set s= getConnectionDefinitionProperties(connectionDefinitionClassName); 113 HashMap hm= new HashMap (); 114 Class connectionDefinitionClass; 115 try { 116 connectionDefinitionClass= 117 Thread.currentThread().getContextClassLoader().loadClass(connectionDefinitionClassName); 118 Object obj= connectionDefinitionClass.newInstance(); 119 for (Iterator iter= s.iterator(); iter.hasNext();) { 120 String property= (String ) iter.next(); 121 Object defaultVal= null; 122 try { 123 Method m= 124 connectionDefinitionClass.getMethod( 125 "get" + property, 126 new Class [] {}); 127 defaultVal= m.invoke(obj, new Object [] {}); 128 } catch (NoSuchMethodException e) { 132 } catch (IllegalArgumentException e) { 134 } catch (InvocationTargetException e) { 136 } 138 hm.put(property, defaultVal); 141 } 142 } catch (ClassNotFoundException e) { 143 handleException(e); 144 } catch (InstantiationException e) { 145 handleException(e); 146 } catch (IllegalAccessException e) { 147 handleException(e); 148 } catch (SecurityException e) { 149 handleException(e); 150 } 151 return hm; 152 } 153 154 private static void handleException(Exception ex) { 155 ex.printStackTrace(); 156 _logger.log( 157 Level.SEVERE, 158 "Exception while trying to find properties ", 159 ex.getMessage()); 160 } 161 162 public static void main(String [] args) { 164 165 Map m= 168 ConnectionDefinitionUtils 169 .getConnectionDefinitionPropertiesAndDefaults( 170 "sun.jdbc.odbc.ee.DataSource"); 171 172 for (Iterator iter= m.keySet().iterator(); iter.hasNext();) { 173 String element= (String ) iter.next(); 174 System.out.println(element + " : " + m.get(element)); 175 } 176 } 177 } 178 | Popular Tags |