1 23 package com.sun.enterprise.repository; 24 25 import java.io.*; 26 import java.util.*; 27 import java.lang.reflect.*; 28 import com.sun.enterprise.util.LocalStringManagerImpl; 29 import java.util.logging.*; 31 import com.sun.logging.*; 32 34 73 public class DataSourceParser { 74 75 private static Logger _logger=null; 77 static{ 78 _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER); 79 } 80 static private final String DS = "xadatasource"; 82 static private final String JNDINAME = "jndiname"; 83 static private final String CLASSNAME = "classname"; 84 static private final String PROP = "prop"; 85 static private final String DBUSER = "dbuser"; 86 static private final String DBPASSWORD = "dbpassword"; 87 88 static private LocalStringManagerImpl localStrings = 89 new LocalStringManagerImpl(DataSourceParser.class); 90 91 98 99 private DataSourceParser() { 100 } 101 102 112 public Vector parseProperties(Properties props) { 113 114 Vector list = new Vector(); 115 int count = 0; 116 while (true) { 117 String jndiName = 118 props.getProperty(DS + "." + count + "." + JNDINAME); 119 String className = 120 props.getProperty(DS + "." + count + "." + CLASSNAME); 121 String dbUser = 122 props.getProperty(DS + "." + count + "." + DBUSER); 123 String dbPassword = 124 props.getProperty(DS + "." + count + "." + DBPASSWORD); 125 if (jndiName == null || className == null) break; 126 try { 127 DataSourceInfo dsi = new DataSourceInfo(); 128 dsi.jndiName = jndiName; 129 dsi.dbUser = dbUser; 130 dsi.dbPassword = dbPassword; 131 Object obj = Class.forName(className).newInstance(); 132 dsi.dataSource = obj; 133 list.addElement(dsi); 134 } catch (Exception ex) { 135 Object [] args = {className}; 136 _logger.log(Level.SEVERE,"enterprise.repository_init_class_err", args); 141 } 143 count++; 144 } 145 146 Enumeration e = props.keys(); 148 while (e.hasMoreElements()) { 149 String key = (String ) e.nextElement(); 150 String val = props.getProperty(key); 151 if (key.startsWith(DS) && key.indexOf(PROP) != -1) { 152 try { 153 String s = key.substring(DS.length()+1, 154 key.indexOf(".", 155 DS.length()+1)); 156 int idx = Integer.parseInt(s); 157 String prop = key.substring(key.lastIndexOf(PROP + ".") + 158 PROP.length() + 1); 159 DataSourceInfo dsi = (DataSourceInfo) list.elementAt(idx); 160 Object obj = dsi.dataSource; 161 invokeSetMethod(obj, prop, val); 162 } catch (Exception ex) { 163 Object [] args = {key, val}; 164 _logger.log(Level.SEVERE,"enterprise.repository_prop_err", args); 169 } 171 } 172 } 173 return list; 174 } 175 176 private void invokeSetMethod(Object obj, String prop, String value) 177 throws NoSuchMethodException , InvocationTargetException, 178 IllegalAccessException 179 { 180 Class cl = obj.getClass(); 181 String setMeth = "set" + prop.substring(0,1).toUpperCase() + 183 prop.substring(1); 184 185 try { 187 Class [] cldef = {String .class}; 188 Method meth = cl.getMethod(setMeth, cldef); 189 Object [] params = {value}; 190 meth.invoke(obj, params); 191 return; 192 } catch (NoSuchMethodException ex) { 193 Class [] cldef = {Integer.TYPE}; 195 Method meth = cl.getMethod(setMeth, cldef); 196 Object [] params = {Integer.valueOf(value)}; 197 meth.invoke(obj, params); 198 return; 199 } 200 } 201 202 static public class DataSourceInfo { 203 public String jndiName; 204 public String dbUser; 205 public String dbPassword; 206 public Object dataSource; 207 208 public DataSourceInfo() {} 209 } 210 } 211 | Popular Tags |