1 8 9 package mx4j.tools.adaptor.http; 10 11 import java.lang.reflect.Constructor ; 12 import java.text.DateFormat ; 13 import java.text.ParseException ; 14 import java.util.Comparator ; 15 import java.util.Date ; 16 import javax.management.ObjectInstance ; 17 import javax.management.openmbean.OpenType ; 18 19 24 public class CommandProcessorUtil 25 { 26 private static final DateFormat [] allFormats = new DateFormat []{ 29 DateFormat.getDateInstance(), 30 DateFormat.getTimeInstance(), 31 DateFormat.getDateTimeInstance(), 32 DateFormat.getDateInstance(DateFormat.SHORT), 34 DateFormat.getDateInstance(DateFormat.MEDIUM), 35 DateFormat.getDateInstance(DateFormat.LONG), 36 DateFormat.getDateInstance(DateFormat.FULL), 37 DateFormat.getTimeInstance(DateFormat.SHORT), 39 DateFormat.getTimeInstance(DateFormat.MEDIUM), 40 DateFormat.getTimeInstance(DateFormat.LONG), 41 DateFormat.getTimeInstance(DateFormat.FULL), 42 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT), 44 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM), 45 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG), 46 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL), 47 48 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT), 49 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM), 50 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG), 51 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL), 52 53 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT), 54 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM), 55 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG), 56 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL), 57 58 DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT), 59 DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM), 60 DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG), 61 DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL) 62 }; 63 64 private static final String [] BASIC_TYPES = new String []{ 65 "int", "long", "short", "byte", "float", "double", "boolean" 66 }; 67 68 77 protected static Object createParameterValue(String parameterType, String parameterValue) 78 throws Exception 79 { 80 if (parameterType.equals("java.lang.String")) 81 { 82 return parameterValue; 83 } 84 else if (parameterType.equals("java.lang.Integer") || parameterType.equals("int")) 85 { 86 return Integer.valueOf(parameterValue); 87 } 88 else if (parameterType.equals("java.lang.Long") || parameterType.equals("long")) 89 { 90 return Long.valueOf(parameterValue); 91 } 92 else if (parameterType.equals("java.lang.Short") || parameterType.equals("short")) 93 { 94 return Short.valueOf(parameterValue); 95 } 96 else if (parameterType.equals("java.lang.Byte") || parameterType.equals("byte")) 97 { 98 return Byte.valueOf(parameterValue); 99 } 100 else if (parameterType.equals("java.lang.Float") || parameterType.equals("float")) 101 { 102 return Float.valueOf(parameterValue); 103 } 104 else if (parameterType.equals("java.lang.Double") || parameterType.equals("double")) 106 { 107 return Double.valueOf(parameterValue); 108 } 109 else if (parameterType.equals("java.lang.Boolean") || parameterType.equals("boolean")) 110 { 111 return Boolean.valueOf(parameterValue); 112 } 113 else if (parameterType.equals("java.lang.Void")) 114 { 115 return Void.TYPE; 116 } 117 else if (parameterType.equals("java.util.Date")) 118 { 119 Date value = null; 123 for (int i = 0; i < allFormats.length; i++) 124 { 125 synchronized (allFormats[i]) 126 { 127 try 128 { 129 System.out.println(parameterValue + " " + allFormats[i]); 130 value = allFormats[i].parse(parameterValue); 131 break; 133 } 134 catch (ParseException e) 135 { 136 } 138 } 139 } 140 if (value == null) 141 { 142 throw new ParseException ("Not possible to parse", 0); 143 } 144 return value; 145 } 146 else if (parameterType.equals("java.lang.Number")) 147 { 148 Number value = null; 149 try 151 { 152 value = Long.valueOf(parameterValue); 153 } 154 catch (NumberFormatException e) 155 { 156 } 157 if (value == null) 159 { 160 try 161 { 162 value = Double.valueOf(parameterValue); 163 } 164 catch (NumberFormatException e) 165 { 166 } 167 } 168 if (value == null) 169 { 170 throw new NumberFormatException ("Not possible to parse"); 171 } 172 return value; 173 } 174 if (parameterType.equals("java.lang.Character") || parameterType.equals("char")) 175 { 176 if (parameterValue.length() > 0) 177 { 178 return new Character (parameterValue.charAt(0)); 179 } 180 else 181 { 182 throw new NumberFormatException ("Can not initialize Character from empty String"); 183 } 184 } 185 191 Class cls = null; 192 java.lang.reflect.Constructor ctor = null; 193 try 194 { 195 cls = Class.forName(parameterType); 196 ctor = cls.getConstructor(new Class []{String .class}); 197 return ctor.newInstance(new Object []{parameterValue}); 198 } 199 catch (ClassNotFoundException cnfe) 200 { 201 203 throw new IllegalArgumentException ("Invalid parameter type: " + parameterType); 204 } 205 catch (NoSuchMethodException nsme) 206 { 207 throw new IllegalArgumentException ("Invalid parameter type: " + parameterType); 209 } 210 catch (Exception ex) 211 { 212 217 throw ex; 218 } 219 } 220 221 230 protected static boolean canCreateParameterValue(String parameterType) 231 { 232 int count = OpenType.ALLOWED_CLASSNAMES.length; 233 for (int i = 0; i < count; i++) 234 { 235 if (OpenType.ALLOWED_CLASSNAMES[i].equals(parameterType)) 236 { 237 return true; 238 } 239 } 240 count = BASIC_TYPES.length; 241 for (int i = 0; i < count; i++) 242 { 243 if (BASIC_TYPES[i].equals(parameterType)) 244 { 245 return true; 246 } 247 } 248 249 Class cls = null; 250 try 251 { 252 cls = Class.forName(parameterType); 253 cls.getConstructor(new Class []{String .class}); 254 return true; 256 } 257 catch (ClassNotFoundException cnfe) 258 { 259 261 return false; 262 } 263 catch (NoSuchMethodException nsme) 264 { 265 return false; 267 } 268 catch (Exception ex) 269 { 270 275 return false; 276 } 277 } 278 279 public static Comparator createObjectNameComparator() 280 { 281 return new ToStringComparator(); 282 } 283 284 public static Comparator createObjectInstanceComparator() 285 { 286 return new ObjectInstanceComparator(); 287 } 288 289 public static Comparator createConstructorComparator() 290 { 291 return new ConstructorComparator(); 292 } 293 294 public static Comparator createClassComparator() 295 { 296 return new ToStringComparator(); 297 } 298 299 private static class ToStringComparator implements Comparator 300 { 301 public int compare(Object o1, Object o2) 302 { 303 return o1.toString().compareTo(o2.toString()); 304 } 305 } 306 307 private static class ObjectInstanceComparator implements Comparator 308 { 309 private ToStringComparator comp = new ToStringComparator(); 310 311 public int compare(Object o1, Object o2) 312 { 313 ObjectInstance oi1 = (ObjectInstance )o1; 314 ObjectInstance oi2 = (ObjectInstance )o2; 315 return comp.compare(oi1.getObjectName(), oi2.getObjectName()); 316 } 317 } 318 319 private static class ConstructorComparator implements Comparator 320 { 321 public int compare(Object o1, Object o2) 322 { 323 Constructor c1 = (Constructor )o1; 324 Constructor c2 = (Constructor )o2; 325 Class [] params1 = c1.getParameterTypes(); 327 Class [] params2 = c2.getParameterTypes(); 328 if (params1.length == params2.length) 329 { 330 for (int i = 0; i < params1.length; i++) 331 { 332 if (!params1[i].equals(params2[i])) 333 { 334 return params2[i].toString().compareTo(params1[i].toString()); 335 } 336 } 337 return 0; 338 } 339 else 340 { 341 return params1.length - params2.length; 342 } 343 } 344 } 345 346 } 347 | Popular Tags |