1 23 package com.sun.enterprise.util; 24 25 import java.net.*; 26 import java.io.*; 27 import java.util.Properties ; 28 import java.lang.reflect.*; 29 import javax.naming.*; 30 import javax.rmi.*; 31 import javax.rmi.CORBA.*; 32 import com.sun.enterprise.Switch; 33 import com.sun.enterprise.NamingManager; 34 35 import java.util.logging.*; 36 import com.sun.logging.*; 37 38 41 public final class Utility { 42 43 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 44 45 private static LocalStringManagerImpl localStrings = 46 new LocalStringManagerImpl(Utility.class); 47 48 public static void checkJVMVersion() 49 { 50 } 52 53 54 public static Properties getPropertiesFromFile(String file) 55 throws IOException 56 { 57 InputStream is = ClassLoader.getSystemResourceAsStream(file); 58 if (is != null) { 59 Properties config = new Properties (); 60 config.load(is); 61 return config; 62 } else { 63 String remoteclient = "/" + file; 64 InputStream is2 = Utility.class.getResourceAsStream(remoteclient); 65 Properties config = new Properties (); 66 config.load(is2); 67 return config; 68 } 69 } 70 71 72 75 public static String getLocalHost() { 76 String hostname = null; 77 try { 78 InetAddress ia = InetAddress.getLocalHost(); 79 hostname = ia.getHostName(); 80 } catch(UnknownHostException e) { 81 return "localhost"; 82 } 83 return hostname; 84 } 85 86 89 public static String getLocalAddress() { 90 String address = null; 91 try { 92 InetAddress ia = InetAddress.getLocalHost(); 93 address = ia.getHostAddress(); 94 } catch(UnknownHostException e) { 95 return "127.0.0.1"; 96 } 97 return address; 98 } 99 100 106 public static java.rmi.Remote lookupObject(String publishedName, 107 java.lang.Class anInterface) 108 throws javax.naming.NamingException { 109 110 Context ic = new InitialContext(); 111 java.lang.Object objRef = ic.lookup(publishedName); 112 return (java.rmi.Remote ) 113 PortableRemoteObject.narrow(objRef, anInterface); 114 } 115 116 117 124 public static int bytesToInt(byte[] array, int offset) 125 { 126 int b1, b2, b3, b4; 127 128 b1 = (array[offset++] << 24) & 0xFF000000; 129 b2 = (array[offset++] << 16) & 0x00FF0000; 130 b3 = (array[offset++] << 8) & 0x0000FF00; 131 b4 = (array[offset++] << 0) & 0x000000FF; 132 133 return (b1 | b2 | b3 | b4); 134 } 135 136 143 public static void intToBytes(int value, byte[] array, int offset) 144 { 145 array[offset++] = (byte)((value >>> 24) & 0xFF); 146 array[offset++] = (byte)((value >>> 16) & 0xFF); 147 array[offset++] = (byte)((value >>> 8) & 0xFF); 148 array[offset++] = (byte)((value >>> 0) & 0xFF); 149 } 150 151 158 public static long bytesToLong(byte[] array, int offset) 159 { 160 long l1, l2; 161 162 l1 = (long)bytesToInt(array, offset) << 32; 163 l2 = (long)bytesToInt(array, offset+4) & 0xFFFFFFFFL; 164 165 return (l1 | l2); 166 } 167 168 175 public static void longToBytes(long value, byte[] array, int offset) 176 { 177 array[offset++] = (byte)((value >>> 56) & 0xFF); 178 array[offset++] = (byte)((value >>> 48) & 0xFF); 179 array[offset++] = (byte)((value >>> 40) & 0xFF); 180 array[offset++] = (byte)((value >>> 32) & 0xFF); 181 array[offset++] = (byte)((value >>> 24) & 0xFF); 182 array[offset++] = (byte)((value >>> 16) & 0xFF); 183 array[offset++] = (byte)((value >>> 8) & 0xFF); 184 array[offset++] = (byte)((value >>> 0) & 0xFF); 185 } 186 187 190 public static void invokeApplicationMain(Class mainClass, String [] args) 191 throws InvocationTargetException, IllegalAccessException , 192 ClassNotFoundException 193 { 194 String err = localStrings.getLocalString ("utility.no.main", "", 195 new Object [] {mainClass}); 196 197 Method mainMethod = null; 201 try { 202 mainMethod = mainClass.getMethod("main", 203 new Class [] { String [].class } ); 204 } catch(NoSuchMethodException msme) { 205 _logger.log(Level.SEVERE,"enterprise_util.excep_in_utility",msme); 206 throw new ClassNotFoundException (err); 207 } 208 209 int modifiers = mainMethod.getModifiers (); 211 if (!Modifier.isPublic (modifiers) || 212 !Modifier.isStatic (modifiers)) { 213 err = localStrings.getLocalString( 214 "utility.main.notpublicorstatic", 215 "The main method is either not public or not static"); 216 _logger.log(Level.SEVERE,"enterprise_util.excep_in_utility_main.notpublicorstatic"); 217 throw new ClassNotFoundException (err); 218 } 219 220 if (!mainMethod.getReturnType().equals (Void.TYPE)) { 222 err = localStrings.getLocalString( 223 "utility.main.notvoid", 224 "The main method's return type is not void "); 225 _logger.log(Level.SEVERE,"enterprise_util.excep_in_utility_main.notvoid"); 226 throw new ClassNotFoundException (err); 227 } 228 229 Object params [] = new Object [1]; 231 params[0] = args; 232 mainMethod.invoke(null, params); 233 234 } 235 236 public static void invokeSetMethod(Object obj, String prop, String value) 237 throws NoSuchMethodException , InvocationTargetException, 238 IllegalAccessException 239 { 240 Class cl = obj.getClass(); 241 String setMeth = "set" + prop.substring(0,1).toUpperCase() + 243 prop.substring(1); 244 245 try { 247 Class [] cldef = {String .class}; 248 Method meth = cl.getMethod(setMeth, cldef); 249 Object [] params = {value}; 250 meth.invoke(obj, params); 251 return; 252 } catch (NoSuchMethodException ex) { 253 try { 254 Class [] cldef = {Integer.TYPE}; 256 Method meth = cl.getMethod(setMeth, cldef); 257 Object [] params = {Integer.valueOf(value)}; 258 meth.invoke(obj, params); 259 return; 260 } catch(NoSuchMethodException nsmex) { 261 Class [] cldef = {Boolean.TYPE}; 263 Method meth = cl.getMethod(setMeth, cldef); 264 Object [] params = {Boolean.valueOf(value)}; 265 meth.invoke(obj, params); 266 return; 267 } 268 } 269 } 270 271 272 public static void invokeSetMethodCaseInsensitive(Object obj, String prop, String value) 273 throws NoSuchMethodException , InvocationTargetException, 274 IllegalAccessException 275 { 276 String alternateMethodName = null; 277 Class cl = obj.getClass(); 278 279 String setMeth = "set" + prop; 280 281 282 Method[] methodsList = cl.getMethods(); 283 boolean methodFound = false; 284 int i=0; 285 for (i =0; i<methodsList.length; ++i) 286 { 287 if(methodsList[i].getName().equalsIgnoreCase(setMeth) == true) 288 { 289 Class [] parameterTypes = methodsList[i].getParameterTypes(); 290 if(parameterTypes.length == 1 ) 291 { 292 if(parameterTypes[0].getName().equals("java.lang.String")) 293 { 294 methodFound = true; 295 break; 296 } 297 else 298 alternateMethodName = methodsList[i].getName(); 299 } 300 301 } 302 } 303 if(methodFound == true) 304 { 305 Object [] params = {value}; 306 methodsList[i].invoke(obj, params); 307 return; 308 } 309 if(alternateMethodName != null) 310 { 311 try 312 { 313 Class [] cldef = {Integer.TYPE}; 315 Method meth = cl.getMethod(alternateMethodName, cldef); 316 Object [] params = {Integer.valueOf(value)}; 317 meth.invoke(obj, params); 318 return; 319 } 320 catch(NoSuchMethodException nsmex) 321 { 322 Class [] cldef = {Boolean.TYPE}; 324 Method meth = cl.getMethod(alternateMethodName, cldef); 325 Object [] params = {Boolean.valueOf(value)}; 326 meth.invoke(obj, params); 327 return; 328 } 329 330 } 331 else 332 throw new NoSuchMethodException (setMeth); 333 } 334 335 336 343 public static short intToShort(int value) 344 { 345 if (value > 32767) 346 return (short)(value - 65536) ; 347 return (short)value ; 348 } 349 350 public static int shortToInt(short value) 351 { 352 if (value < 0) 353 return value + 65536 ; 354 return value ; 355 } 356 357 363 public static ClassLoader getClassLoader() { 364 if (Thread.currentThread().getContextClassLoader() != null) { 365 return Thread.currentThread().getContextClassLoader(); 366 } else { 367 return ClassLoader.getSystemClassLoader(); 368 } 369 } 370 371 378 public static Class loadClass(String className) throws ClassNotFoundException { 379 return getClassLoader().loadClass(className); 380 } 381 382 386 public static ClassLoader setContextClassLoader(ClassLoader newClassLoader) 387 { 388 389 final ClassLoader classLoaderToSet = newClassLoader; 391 392 final Thread currentThread = Thread.currentThread(); 393 ClassLoader originalClassLoader = 394 currentThread.getContextClassLoader(); 395 396 java.security.AccessController.doPrivileged 397 (new java.security.PrivilegedAction () { 398 public java.lang.Object run() { 399 currentThread.setContextClassLoader(classLoaderToSet); 400 return null; 401 } 402 }); 403 return originalClassLoader; 404 } 405 406 public static void setDataDirectProperty() { 407 Environment e = Environment.obtain(); 408 if (e.getEnvironmentStatus() == Environment.CLOSED) { 409 return; 410 } else { 411 e.unlock(); 412 } 413 } 414 415 } 416 | Popular Tags |