KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > ThreadContext


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import gcc.*;
22 import java.util.*;
23
24 public abstract class ThreadContext
25 {
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     {
34         _primTypes = new HashMap();
35         _primTypes.put("boolean", boolean.class);
36         _primTypes.put("char", char.class);
37         _primTypes.put("byte", byte.class);
38         _primTypes.put("short", short.class);
39         _primTypes.put("int", int.class);
40         _primTypes.put("long", long.class);
41         _primTypes.put("float", float.class);
42         _primTypes.put("double", double.class);
43         _primTypes.put("boolean[]", boolean[].class);
44         _primTypes.put("char[]", char[].class);
45         _primTypes.put("byte[]", byte[].class);
46         _primTypes.put("short[]", short[].class);
47         _primTypes.put("int[]", int[].class);
48         _primTypes.put("long[]", long[].class);
49         _primTypes.put("float[]", float[].class);
50         _primTypes.put("double[]", double[].class);
51     }
52
53     public static String getDefaultRmiHost()
54     {
55         String host = (String)_defaultRmiHost.get();
56         if (host == null)
57         {
58             host = "0";
59         }
60         return host;
61     }
62
63     public static int getDefaultRmiPort()
64     {
65         Integer port = (Integer)_defaultRmiPort.get();
66         if (port == null)
67         {
68             port = IntegerCache.get(0);
69         }
70         return port.intValue();
71     }
72
73     public static Class loadClass(String className)
74     {
75         Class t = (Class)_primTypes.get(className);
76         if (t != null)
77         {
78             return t;
79         }
80         try
81         {
82             ClassLoader loader = Thread.currentThread().getContextClassLoader();
83             if (loader == null)
84             {
85                 return Class.forName(className);
86             }
87             else
88             {
89                 return loader.loadClass(className);
90             }
91         }
92         catch (RuntimeException ex)
93         {
94             throw (RuntimeException)ex;
95         }
96         catch (Exception ex)
97         {
98             throw new SystemException(ex);
99         }
100     }
101
102     public static Class loadClass(String className, Class parentClass)
103     {
104         if (parentClass == null)
105         {
106             return loadClass(className);
107         }
108         Class t = (Class)_primTypes.get(className);
109         if (t != null)
110         {
111             return t;
112         }
113         try
114         {
115             ClassLoader loader = parentClass.getClassLoader();
116             if (loader == null)
117             {
118                 return loadClass(className);
119             }
120             else
121             {
122                 return loader.loadClass(className);
123             }
124         }
125         catch (RuntimeException ex)
126         {
127             throw (RuntimeException)ex;
128         }
129         catch (Exception ex)
130         {
131             throw new SystemException(ex);
132         }
133     }
134
135     public static Class loadClassOrReturnNullIfNotFound(String className)
136     {
137         try
138         {
139             return loadClass(className);
140         }
141         catch (RuntimeException ex)
142         {
143             return null;
144         }
145     }
146
147     public static Class loadClassOrReturnNullIfNotFound(String className, Class parentClass)
148     {
149         if (parentClass == null)
150         {
151             return loadClassOrReturnNullIfNotFound(className);
152         }
153         try
154         {
155             return loadClass(className, parentClass);
156         }
157         catch (RuntimeException ex)
158         {
159             return null;
160         }
161     }
162
163     public static void setDefaultRmiHost(String host)
164     {
165         _defaultRmiHost.set(host);
166     }
167
168     public static void setDefaultRmiPort(int port)
169     {
170         _defaultRmiPort.set(IntegerCache.get(port));
171     }
172 }
173
Popular Tags