1 25 package org.objectweb.jonas.jdbc; 26 27 import java.lang.reflect.Method ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.Vector ; 32 33 import javax.resource.ResourceException ; 34 import javax.resource.spi.ConnectionRequestInfo ; 35 import javax.resource.spi.ManagedConnectionFactory ; 36 import javax.resource.spi.SecurityException ; 37 import javax.resource.spi.security.PasswordCredential ; 38 import javax.security.auth.Subject ; 39 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.util.monolog.api.Logger; 42 43 47 public class Utility { 48 49 static synchronized Object getDataSource(ManagedConnectionFactoryImpl mcf, PasswordCredential pc, Logger trace) 50 throws ResourceException { 51 52 MCFData prop = mcf.mcfData; 53 54 if (prop.getMCFData(MCFData.DSCLASS) == null) { 55 ResourceException re = new ResourceException ("A DataSource (dsClass) value must be specified"); 56 trace.log(BasicLevel.INFO, re.getMessage()); 57 } 58 String clsName = prop.getMCFData(MCFData.DSCLASS); 59 Class dsClass = null; 60 Object dsObj = null; 61 try { 62 dsClass = Class.forName(clsName, true, Thread.currentThread().getContextClassLoader()); 63 dsObj = dsClass.newInstance(); 64 if (trace.isLoggable(BasicLevel.DEBUG)) { 65 trace.log(BasicLevel.DEBUG, "dsClass(" + clsName + ") is " + dsObj); 66 } 67 } catch (ClassNotFoundException cnfe) { 68 throw new ResourceException ("Class Name not found:" + clsName); 69 } catch (Exception ex) { 70 throw new ResourceException ("Error in class: " + clsName + " " + ex.getMessage()); 71 } 72 73 Object [] param = new Object [1]; 74 String methodName = null; 75 String paramVal = null; 76 Method meth = null; 77 for (Enumeration e = prop.getProperties(); e.hasMoreElements();) { 78 int offset = Integer.parseInt((String ) e.nextElement()); 79 methodName = MCFData.dsMethodNames[offset]; 80 if (!(methodName.equals("setDSClass") || methodName.equals("setDbSpecificMethods") || offset > MCFData.JONASOFFSET)) { 81 try { 82 83 paramVal = prop.getProperty("" + offset); 84 if (trace.isLoggable(BasicLevel.DEBUG)) { 85 trace.log(BasicLevel.DEBUG, "calling method " + methodName + " with String " + paramVal); 86 } 87 meth = dsClass.getMethod(methodName, new Class [] {String .class}); 88 param[0] = paramVal; 89 meth.invoke(dsObj, param); 90 } catch (NoSuchMethodException ns) { 91 try { 92 93 if (trace.isLoggable(BasicLevel.DEBUG)) { 94 trace.log(BasicLevel.DEBUG, "calling method " + methodName + " with int " + paramVal); 95 } 96 meth = dsClass.getMethod(methodName, new Class [] {int.class}); 97 param[0] = new Integer (paramVal); 98 meth.invoke(dsObj, param); 99 } catch (NoSuchMethodException nsme) { 100 ; 103 } catch (NumberFormatException nfm) { 104 ; 106 } catch (Exception ex0) { 107 ex0.printStackTrace(); 108 throw new ResourceException ("Error on method: " + methodName + " " + ex0.getMessage()); 109 } 110 } catch (IllegalArgumentException iae) { 111 ; 112 } catch (Exception ex) { 113 ex.printStackTrace(); 114 throw new ResourceException ("Error on method: " + methodName + " " + ex.getMessage()); 115 } 116 } 117 if (methodName.equals("setDbSpecificMethods")) { 118 Vector meths = new Vector (); 119 Vector methParams = new Vector (); 120 Vector methTypes = new Vector (); 121 try { 122 Utility.parseValues(prop.getProperty("" + offset), meths, methParams, methTypes, trace); 123 } catch (Exception ex1) { 124 throw new ResourceException ("Error parsing dbSpecificMethods: " + ex1.getMessage()); 125 } 126 if (meths != null && meths.size() > 0) { 127 for (int i = 0; i < meths.size(); i++) { 128 try { 129 130 methodName = (String ) meths.elementAt(i); 131 Class toPass = null; 132 String curMethType = (String ) methTypes.elementAt(i); 133 if (curMethType.equalsIgnoreCase("String")) { 134 toPass = String .class; 135 param[0] = (String ) methParams.elementAt(i); 136 } else if (curMethType.equalsIgnoreCase("Integer")) { 137 toPass = Integer .class; 138 param[0] = Integer.valueOf((String ) methParams.elementAt(i)); 139 } else if (curMethType.equalsIgnoreCase("int")) { 140 toPass = int.class; 141 param[0] = Integer.valueOf((String ) methParams.elementAt(i)); 142 } else if (curMethType.equals("Float")) { 143 toPass = Float .class; 144 param[0] = Float.valueOf((String ) methParams.elementAt(i)); 145 } else if (curMethType.equals("float")) { 146 toPass = float.class; 147 param[0] = Float.valueOf((String ) methParams.elementAt(i)); 148 } else if (curMethType.equals("Boolean")) { 149 toPass = Boolean .class; 150 param[0] = Boolean.valueOf((String ) methParams.elementAt(i)); 151 } else if (curMethType.equals("boolean")) { 152 toPass = boolean.class; 153 param[0] = Boolean.valueOf((String ) methParams.elementAt(i)); 154 } else if (curMethType.equalsIgnoreCase("Character")) { 155 toPass = Character .class; 156 param[0] = new Character (((String ) methParams.elementAt(i)).charAt(0)); 157 } else if (curMethType.equalsIgnoreCase("char")) { 158 toPass = char.class; 159 param[0] = new Character (((String ) methParams.elementAt(i)).charAt(0)); 160 } else if (curMethType.equals("Double")) { 161 toPass = Double .class; 162 param[0] = Double.valueOf((String ) methParams.elementAt(i)); 163 } else if (curMethType.equals("double")) { 164 toPass = double.class; 165 param[0] = Double.valueOf((String ) methParams.elementAt(i)); 166 } else if (curMethType.equals("Byte")) { 167 toPass = Byte .class; 168 param[0] = Byte.valueOf((String ) methParams.elementAt(i)); 169 } else if (curMethType.equals("byte")) { 170 toPass = byte.class; 171 param[0] = Byte.valueOf((String ) methParams.elementAt(i)); 172 } else if (curMethType.equals("Short")) { 173 toPass = Short .class; 174 param[0] = Short.valueOf((String ) methParams.elementAt(i)); 175 } else if (curMethType.equals("short")) { 176 toPass = short.class; 177 param[0] = Short.valueOf((String ) methParams.elementAt(i)); 178 } else if (curMethType.equals("Long")) { 179 toPass = Long .class; 180 param[0] = Long.valueOf((String ) methParams.elementAt(i)); 181 } else if (curMethType.equals("long")) { 182 toPass = long.class; 183 param[0] = Long.valueOf((String ) methParams.elementAt(i)); 184 } 185 if (trace.isLoggable(BasicLevel.DEBUG)) { 186 trace.log(BasicLevel.DEBUG, "calling method " + methodName + " with " + param[0]); 187 } 188 meth = dsClass.getMethod(methodName, new Class [] {toPass}); 189 meth.invoke(dsObj, param); 190 } catch (NoSuchMethodException ns) { 191 throw new ResourceException ("No such method: " + methodName + " " + ns.getMessage()); 192 } catch (Exception ex) { 193 ex.printStackTrace(); 194 throw new ResourceException ("Error on method: " + methodName + " " + ex.getMessage()); 195 } 196 } 197 } 198 } 199 } 200 if (pc != null) { 201 try { 202 param[0] = pc.getUserName(); 203 meth = dsClass.getMethod("setUserName", new Class [] {Class.forName("String")}); 204 meth.invoke(dsObj, param); 205 206 param[0] = new String (pc.getPassword()); 207 meth = dsClass.getMethod("setPassword", new Class [] {Class.forName("String")}); 208 meth.invoke(dsObj, param); 209 } catch (Exception ex) { 210 throw new ResourceException ("Error on method: " + methodName + " " + ex.getMessage()); 211 } 212 } 213 214 if (trace.isLoggable(BasicLevel.DEBUG)) { 215 try { 216 meth = dsClass.getMethod("getURL", (Class []) null); 217 trace.log(BasicLevel.DEBUG, "URL is " + meth.invoke(dsObj, (Object []) null)); 218 } catch (Exception e) { 219 } 220 } 221 return dsObj; 222 223 } 224 225 private static void parseValues(String val, Vector vMeth, Vector vValues, Vector vTypes, Logger trace) 226 throws Exception { 227 if (trace.isLoggable(BasicLevel.DEBUG)) { 228 trace.log(BasicLevel.DEBUG, ""); 229 } 230 char delim = ':'; 231 boolean done = false; 232 String methName = null; 233 int offset = 0; 234 boolean parsed = false; 235 String parseVal = val.trim(); 236 String typeVal = ""; 237 String valVal = ""; 238 239 if (parseVal.length() == 0) { 240 return; 241 } 242 if (parseVal.startsWith(":")) { 243 delim = parseVal.charAt(1); 244 parseVal = parseVal.substring(2); 245 } 246 while (!parsed) { 247 offset = parseVal.indexOf('='); 248 if (offset < 0) { 249 throw new Exception ("Invalid value specified for dbSpecificMethods"); 250 } 251 methName = parseVal.substring(0, offset); 252 vMeth.add(methName); 253 parseVal = parseVal.substring(offset + 1); 254 if (parseVal.charAt(0) == delim) { 255 valVal = ""; 256 } else { 257 offset = parseVal.indexOf(delim); 258 if (offset < 0) { 259 valVal = parseVal; 260 offset = valVal.length() - 1; 261 } else { 262 valVal = parseVal.substring(0, offset); 263 } 264 } 265 vValues.add(valVal); 266 if (offset < 0) { 267 parsed = true; 268 } else { 269 parseVal = parseVal.substring(offset + 1); 270 if (parseVal.length() == 0) { 271 parsed = true; 272 } 273 } 274 if (parseVal.startsWith("" + delim)) { 275 parseVal = parseVal.substring(1); 276 offset = parseVal.indexOf(delim); 277 if (offset < 0) { 278 typeVal = parseVal; 279 } else { 280 typeVal = parseVal.substring(0, offset); 281 } 282 vTypes.add(typeVal); 283 if (offset < 0) { 284 parsed = true; 285 } else { 286 parseVal = parseVal.substring(offset + 1); 287 if (parseVal.length() == 0) { 288 parsed = true; 289 } 290 } 291 } else { 292 vTypes.add("String"); 293 } 294 if (trace.isLoggable(BasicLevel.DEBUG)) { 295 trace.log(BasicLevel.DEBUG, "Parsed: method(" + methName + ") value(" + valVal + ") type(" + typeVal + ")"); 296 } 297 } 298 } 299 300 307 static synchronized PasswordCredential getPasswordCredential(ManagedConnectionFactory mcf, Subject subject, 308 ConnectionRequestInfo info, java.io.PrintWriter out) throws ResourceException { 309 310 if (subject == null) { 311 if (info == null) { 312 return null; 313 } 314 ConnectionRequestInfoImpl crii = (ConnectionRequestInfoImpl) info; 315 PasswordCredential pc = new PasswordCredential (crii.user, crii.password.toCharArray()); 316 pc.setManagedConnectionFactory(mcf); 317 return pc; 318 } 319 Set cred = subject.getPrivateCredentials(PasswordCredential .class); 320 PasswordCredential pc = null; 321 for (Iterator iter = cred.iterator(); iter.hasNext();) { 322 PasswordCredential tmpPc = (PasswordCredential ) iter.next(); 323 if (tmpPc.getManagedConnectionFactory().equals(mcf)) { 324 pc = tmpPc; 325 break; 326 } 327 } 328 if (pc == null) { 329 SecurityException se = new SecurityException ("No PasswordCredential found"); 330 out.println("" + se); 331 throw se; 332 } 333 return pc; 334 } 335 336 } | Popular Tags |