1 18 package org.apache.geronimo.interop.util; 19 20 import java.util.HashMap ; 21 22 import org.apache.geronimo.interop.SystemException; 23 24 25 public abstract class ThreadContext { 26 private static HashMap _primTypes; 27 28 private static ThreadLocal _defaultRmiHost = new ThreadLocal (); 29 30 private static ThreadLocal _defaultRmiPort = new ThreadLocal (); 31 32 static { 33 _primTypes = new HashMap (); 34 _primTypes.put("boolean", boolean.class); 35 _primTypes.put("char", char.class); 36 _primTypes.put("byte", byte.class); 37 _primTypes.put("short", short.class); 38 _primTypes.put("int", int.class); 39 _primTypes.put("long", long.class); 40 _primTypes.put("float", float.class); 41 _primTypes.put("double", double.class); 42 _primTypes.put("boolean[]", boolean[].class); 43 _primTypes.put("char[]", char[].class); 44 _primTypes.put("byte[]", byte[].class); 45 _primTypes.put("short[]", short[].class); 46 _primTypes.put("int[]", int[].class); 47 _primTypes.put("long[]", long[].class); 48 _primTypes.put("float[]", float[].class); 49 _primTypes.put("double[]", double[].class); 50 } 51 52 public static String getDefaultRmiHost() { 53 String host = (String ) _defaultRmiHost.get(); 54 if (host == null) { 55 host = "0"; 56 } 57 return host; 58 } 59 60 public static int getDefaultRmiPort() { 61 Integer port = (Integer ) _defaultRmiPort.get(); 62 if (port == null) { 63 port = IntegerCache.get(0); 64 } 65 return port.intValue(); 66 } 67 68 public static Class loadClass(String className) { 69 Class t = (Class ) _primTypes.get(className); 70 if (t != null) { 71 return t; 72 } 73 try { 74 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 75 if (loader == null) { 76 return Class.forName(className); 77 } else { 78 return loader.loadClass(className); 79 } 80 } catch (RuntimeException ex) { 81 throw (RuntimeException ) ex; 82 } catch (Exception ex) { 83 throw new SystemException(ex); 84 } 85 } 86 87 public static Class loadClass(String className, Class parentClass) { 88 if (parentClass == null) { 89 return loadClass(className); 90 } 91 Class t = (Class ) _primTypes.get(className); 92 if (t != null) { 93 return t; 94 } 95 try { 96 ClassLoader loader = parentClass.getClassLoader(); 97 if (loader == null) { 98 return loadClass(className); 99 } else { 100 return loader.loadClass(className); 101 } 102 } catch (RuntimeException ex) { 103 throw (RuntimeException ) ex; 104 } catch (Exception ex) { 105 throw new SystemException(ex); 106 } 107 } 108 109 public static Class loadClassOrReturnNullIfNotFound(String className) { 110 try { 111 return loadClass(className); 112 } catch (RuntimeException ex) { 113 return null; 114 } 115 } 116 117 public static Class loadClassOrReturnNullIfNotFound(String className, Class parentClass) { 118 if (parentClass == null) { 119 return loadClassOrReturnNullIfNotFound(className); 120 } 121 try { 122 return loadClass(className, parentClass); 123 } catch (RuntimeException ex) { 124 return null; 125 } 126 } 127 128 public static void setDefaultRmiHost(String host) { 129 _defaultRmiHost.set(host); 130 } 131 132 public static void setDefaultRmiPort(int port) { 133 _defaultRmiPort.set(IntegerCache.get(port)); 134 } 135 } 136 | Popular Tags |