KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > services > DefaultRuntimeClassLoader


1 /**
2  * Kilim
3  * Copyright (C) 2001 Kelua SA
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: kilim@objectweb.org
20  *
21  * Author: Bruno Dumant
22  *
23  * -----------------------------------------------------------------
24  * $Id: ClassPathFinderImpl.java,v 1.1.1.1 2002/08/06 13:13:49 bmi Exp $
25  * -----------------------------------------------------------------
26  */

27
28 package org.objectweb.kilim.model.services;
29
30 import java.lang.reflect.Array JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 import org.objectweb.kilim.KilimException;
34 import org.objectweb.kilim.description.Type;
35
36 /**
37  * @author horn
38  */

39 public class DefaultRuntimeClassLoader implements RuntimeClassLoader {
40    private HashMap JavaDoc caches;
41
42     /** instance is a singleton associated to a default runtime manager */
43    public static RuntimeClassLoader instance = new DefaultRuntimeClassLoader();
44      
45     /**
46      * @see java.lang.Object#Object()
47      */

48    public DefaultRuntimeClassLoader() {
49       caches = new HashMap JavaDoc();
50    }
51       
52     /**
53      * @see org.objectweb.kilim.model.RuntimeManager#getClass(Type)
54      */

55    public Class JavaDoc getClass(Type type) throws KilimException {
56       return getClass(type.getName());
57    }
58   
59     /**
60      * @see org.objectweb.kilim.model.RuntimeManager#getClass(String)
61      */

62    public Class JavaDoc getClass(String JavaDoc class_name) throws KilimException {
63       Class JavaDoc result = null;
64       ClassLoader JavaDoc class_loader = Thread.currentThread().getContextClassLoader();
65       Object JavaDoc cl_cache = caches.get(class_loader);
66       HashMap JavaDoc class_loader_cache = null;
67       if (cl_cache == null) {
68          class_loader_cache = new HashMap JavaDoc();
69          caches.put(class_loader, class_loader_cache);
70       } else {
71          class_loader_cache = (HashMap JavaDoc) cl_cache;
72          result = (Class JavaDoc) class_loader_cache.get(class_name);
73       }
74
75       if (result != null) {
76          return result;
77       }
78
79       int size = 0;
80       int index = 0;
81       while (true) {
82          index = class_name.indexOf('[', index);
83          if (index < 0) {
84             break;
85          } else {
86             size++;
87             //index = class_name.indexOf('[',index + 2);
88
index += 2;
89          }
90       }
91       if (size > 0) {
92          // class_name is an array type. Try to find the element type first.
93
String JavaDoc element_class_name = class_name.substring(0, class_name.indexOf('['));
94          Class JavaDoc element_class = getClass(element_class_name);
95          // create an array instance
96
int[] sizes = new int[size];
97          for (int i = 0; i < size; i++) {
98             sizes[i] = 0;
99          }
100          Object JavaDoc array = Array.newInstance(element_class, sizes);
101          // return its class
102
result = array.getClass();
103       } else {
104          result = primitiveClass(class_name);
105         
106          if (result == null) {
107             try {
108                result = class_loader.loadClass(class_name);
109             } catch (ClassNotFoundException JavaDoc ignored) { }
110             if (result == null) {
111                try {
112                   result = Class.forName(class_name);
113                } catch (ClassNotFoundException JavaDoc e) {
114                   throw new KilimException(e);
115                }
116             }
117          }
118       }
119       class_loader_cache.put(class_name, result);
120      
121       return result;
122    }
123
124    static Class JavaDoc primitiveClass(String JavaDoc name) {
125       switch (name.charAt(0)) {
126          case 'b':
127             switch (name.charAt(1)) {
128                case 'o':
129                   return "boolean".equals(name) ? boolean.class : null;
130                default:
131                   return "byte".equals(name) ? byte.class : null;
132             }
133          case 'c':
134             return "char".equals(name) ? char.class : null;
135          case 'd':
136             return "double".equals(name) ? double.class : null;
137          case 'f':
138             return "float".equals(name) ? float.class : null;
139          case 'i':
140             return "int".equals(name) ? int.class : null;
141          case 'l':
142             return "long".equals(name) ? long.class : null;
143          case 's':
144             return "short".equals(name) ? short.class : null;
145          default:
146             return "void".equals(name) ? void.class : null;
147       }
148    }
149 }
150
Popular Tags