KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > runtime > TheClassLoader


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.corba.runtime;
31
32 /**
33  ** <p>The <tt>TheClassLoader</tt> class provides resources loading and class instantiation
34  ** features. It relies on the launcher's <tt>XBootClassLoader</tt> class.</p>
35  **/

36 public class TheClassLoader
37 {
38     //
39
static private String JavaDoc _class_name = "TheClassLoader";
40     static private org.objectweb.util.launcher.XBootClassLoader _xloader;
41
42     //
43
// internal operations
44
//
45

46     static private void
47     init()
48     {
49         _xloader = (org.objectweb.util.launcher.XBootClassLoader)Thread.currentThread().getContextClassLoader();
50     }
51
52     static private org.objectweb.util.launcher.XBootClassLoader
53     getXLoader()
54     {
55         if (_xloader==null) {
56             init();
57         }
58
59         return _xloader;
60     }
61
62     static private Object JavaDoc
63     callStaticClassMethod(String JavaDoc entrypt,
64                           Class JavaDoc[] argt,
65                           Object JavaDoc[] argv)
66     throws java.lang.ClassNotFoundException JavaDoc,
67            java.lang.NoSuchMethodException JavaDoc,
68            java.lang.reflect.InvocationTargetException JavaDoc,
69            java.lang.IllegalAccessException JavaDoc
70     {
71         // extract the class name and the method name.
72
int idx = entrypt.lastIndexOf('.');
73         java.lang.String JavaDoc class_name = entrypt.substring(0, idx);
74         java.lang.String JavaDoc method_name = entrypt.substring(idx+1);
75
76         //String opname = "callStaticClassMethod";
77
//String msg = "class name is: "+class_name+" ; method name is: "+method_name;
78
//TheLogger.debug(_class_name, opname, msg);
79

80         // Search the class using forName
81
Class JavaDoc the_class = Class.forName(class_name);
82
83         // Search the static method.
84
java.lang.reflect.Method JavaDoc the_method = the_class.getMethod(method_name, argt);
85
86         // Call the static method.
87
Object JavaDoc result = the_method.invoke(null, argv);
88
89         // Return the result.
90
return result;
91     }
92
93     //
94
// static operations
95
//
96

97     static public void
98     addResource(String JavaDoc path)
99     {
100         final String JavaDoc opname = "addResource";
101         // create URL
102
// NOTE: path is local (mandatory)
103
String JavaDoc url_str = path;
104         if (!url_str.startsWith("file:")) {
105             url_str = "file:"+url_str;
106         }
107
108         java.net.URL JavaDoc url = null;
109         try {
110             url = new java.net.URL JavaDoc(url_str);
111         } catch (java.net.MalformedURLException JavaDoc ex) {
112             // NOTE: TODO: should be reported as an exception as it will certainly lead
113
// to further errors
114
TheLogger.error(_class_name, opname, "FAILED (wrong URL format)", ex);
115         }
116
117         // add URL to the parent loader
118
final String JavaDoc msg = "Adding URL: "+url.toString();
119         TheLogger.debug(_class_name, opname, msg);
120         getXLoader().addURL(url);
121     }
122
123     static public Object JavaDoc
124     newInstance(String JavaDoc entrypt)
125     {
126         return newInstance(entrypt, new Class JavaDoc[0], new Object JavaDoc[0]);
127     }
128
129     static public Object JavaDoc
130     newInstance(String JavaDoc entrypt, Class JavaDoc[] argt, Object JavaDoc[] argv)
131     {
132         try {
133             return callStaticClassMethod(entrypt, argt, argv);
134         } catch (Exception JavaDoc ex) {
135             final String JavaDoc opname = "newInstance";
136             TheLogger.debug(_class_name, opname, "IGNORE", ex);
137             return null;
138         }
139     }
140 }
141
Popular Tags