1 23 24 package com.sun.gjc.util; 25 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.Vector ; 29 import javax.resource.ResourceException ; 30 31 import com.sun.logging.*; 32 import java.util.logging.Logger ; 33 import java.util.logging.Level ; 34 import com.sun.gjc.common.DataSourceObjectBuilder; 35 import com.sun.enterprise.util.i18n.StringManager; 36 37 43 public class MethodExecutor implements java.io.Serializable { 44 45 private static Logger _logger; 46 static { 47 _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER ); 48 } 49 private boolean debug = false; 50 51 private StringManager sm = StringManager.getManager( 52 DataSourceObjectBuilder.class ); 53 62 public void runJavaBeanMethod(String value, Method method, Object obj) throws ResourceException { 63 if (value==null || value.trim().equals("")) { 64 return; 65 } 66 try { 67 Class [] parameters = method.getParameterTypes(); 68 if ( parameters.length == 1) { 69 Object [] values = new Object [1]; 70 values[0] = convertType(parameters[0], value); 71 method.invoke(obj, values); 72 } 73 } catch (IllegalAccessException iae) { 74 _logger.log(Level.SEVERE, "jdbc.exc_jb_val", iae); 75 String msg = sm.getString("me.access_denied", method.getName()); 76 throw new ResourceException (msg); 77 } catch (IllegalArgumentException ie) { 78 _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ie); 79 String msg = sm.getString("me.illegal_args", method.getName()); 80 throw new ResourceException (msg); 81 } catch (InvocationTargetException ite) { 82 _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ite); 83 String msg = sm.getString("me.access_denied", method.getName()); 84 throw new ResourceException (msg); 85 } 86 } 87 88 97 public void runMethod(Method method, Object obj, Vector values) throws ResourceException { 98 try { 99 Class [] parameters = method.getParameterTypes(); 100 if (values.size() != parameters.length) { 101 return; 102 } 103 Object [] actualValues = new Object [parameters.length]; 104 for (int i =0; i<parameters.length ; i++) { 105 String val = (String ) values.get(i); 106 if (val.trim().equals("NULL")) { 107 actualValues[i] = null; 108 } else { 109 actualValues[i] = convertType(parameters[i], val); 110 } 111 } 112 method.invoke(obj, actualValues); 113 }catch (IllegalAccessException iae) { 114 _logger.log(Level.SEVERE, "jdbc.exc_jb_val", iae); 115 String msg = sm.getString("me.access_denied", method.getName()); 116 throw new ResourceException (msg); 117 } catch (IllegalArgumentException ie) { 118 _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ie); 119 String msg = sm.getString("me.illegal_args", method.getName()); 120 throw new ResourceException (msg); 121 } catch (InvocationTargetException ite) { 122 _logger.log(Level.SEVERE, "jdbc.exc_jb_val", ite); 123 String msg = sm.getString("me.access_denied", method.getName()); 124 throw new ResourceException (msg); 125 } 126 } 127 128 137 private Object convertType(Class type, String parameter) throws ResourceException { 138 try { 139 String typeName = type.getName(); 140 if ( typeName.equals("java.lang.String") || typeName.equals("java.lang.Object")) { 141 return parameter; 142 } 143 144 if (typeName.equals("int") || typeName.equals("java.lang.Integer")) { 145 return new Integer (parameter); 146 } 147 148 if (typeName.equals("short") || typeName.equals("java.lang.Short")) { 149 return new Short (parameter); 150 } 151 152 if (typeName.equals("byte") || typeName.equals("java.lang.Byte")) { 153 return new Byte (parameter); 154 } 155 156 if (typeName.equals("long") || typeName.equals("java.lang.Long")) { 157 return new Long (parameter); 158 } 159 160 if (typeName.equals("float") || typeName.equals("java.lang.Float")) { 161 return new Float (parameter); 162 } 163 164 if (typeName.equals("double") || typeName.equals("java.lang.Double")) { 165 return new Double (parameter); 166 } 167 168 if (typeName.equals("java.math.BigDecimal")) { 169 return new java.math.BigDecimal (parameter); 170 } 171 172 if (typeName.equals("java.math.BigInteger")) { 173 return new java.math.BigInteger (parameter); 174 } 175 176 if (typeName.equals("boolean") || typeName.equals("java.lang.Boolean")) { 177 return new Boolean (parameter); 178 } 179 180 return parameter; 181 } catch (NumberFormatException nfe) { 182 _logger.log(Level.SEVERE, "jdbc.exc_nfe", parameter); 183 String msg = sm.getString( "me.invalid_param", parameter ); 184 throw new ResourceException (msg); 185 } 186 } 187 188 } 189 190 | Popular Tags |