KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > ThreadContext


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.geronimo.interop.SystemException;
23
24
25 public abstract class ThreadContext {
26     private static HashMap JavaDoc _primTypes;
27
28     private static ThreadLocal JavaDoc _defaultRmiHost = new ThreadLocal JavaDoc();
29
30     private static ThreadLocal JavaDoc _defaultRmiPort = new ThreadLocal JavaDoc();
31
32     static {
33         _primTypes = new HashMap JavaDoc();
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 JavaDoc getDefaultRmiHost() {
53         String JavaDoc host = (String JavaDoc) _defaultRmiHost.get();
54         if (host == null) {
55             host = "0";
56         }
57         return host;
58     }
59
60     public static int getDefaultRmiPort() {
61         Integer JavaDoc port = (Integer JavaDoc) _defaultRmiPort.get();
62         if (port == null) {
63             port = IntegerCache.get(0);
64         }
65         return port.intValue();
66     }
67
68     public static Class JavaDoc loadClass(String JavaDoc className) {
69         Class JavaDoc t = (Class JavaDoc) _primTypes.get(className);
70         if (t != null) {
71             return t;
72         }
73         try {
74             ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
75             if (loader == null) {
76                 return Class.forName(className);
77             } else {
78                 return loader.loadClass(className);
79             }
80         } catch (RuntimeException JavaDoc ex) {
81             throw (RuntimeException JavaDoc) ex;
82         } catch (Exception JavaDoc ex) {
83             throw new SystemException(ex);
84         }
85     }
86
87     public static Class JavaDoc loadClass(String JavaDoc className, Class JavaDoc parentClass) {
88         if (parentClass == null) {
89             return loadClass(className);
90         }
91         Class JavaDoc t = (Class JavaDoc) _primTypes.get(className);
92         if (t != null) {
93             return t;
94         }
95         try {
96             ClassLoader JavaDoc loader = parentClass.getClassLoader();
97             if (loader == null) {
98                 return loadClass(className);
99             } else {
100                 return loader.loadClass(className);
101             }
102         } catch (RuntimeException JavaDoc ex) {
103             throw (RuntimeException JavaDoc) ex;
104         } catch (Exception JavaDoc ex) {
105             throw new SystemException(ex);
106         }
107     }
108
109     public static Class JavaDoc loadClassOrReturnNullIfNotFound(String JavaDoc className) {
110         try {
111             return loadClass(className);
112         } catch (RuntimeException JavaDoc ex) {
113             return null;
114         }
115     }
116
117     public static Class JavaDoc loadClassOrReturnNullIfNotFound(String JavaDoc className, Class JavaDoc parentClass) {
118         if (parentClass == null) {
119             return loadClassOrReturnNullIfNotFound(className);
120         }
121         try {
122             return loadClass(className, parentClass);
123         } catch (RuntimeException JavaDoc ex) {
124             return null;
125         }
126     }
127
128     public static void setDefaultRmiHost(String JavaDoc 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