KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > plugin > ExternalClassLoader


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.plugin;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.net.URLClassLoader JavaDoc;
21 import java.net.URLStreamHandlerFactory JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 /**
25  * Classloader responsible for instanciating plugins which can also be outside.
26  * <p>
27  * Note, that this classloader tries to find the correct constructor based on
28  * the arguments.
29  * <p>
30  * Currently, this is not used.
31  *
32  * @author fdietz
33  */

34 public class ExternalClassLoader extends URLClassLoader JavaDoc {
35
36     private static final Logger JavaDoc LOG = Logger
37             .getLogger("org.columba.core.loader");
38
39     /**
40      * Constructor for ExternalClassLoader.
41      *
42      * @param urls
43      * @param parent
44      */

45     public ExternalClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
46         super(urls, parent);
47     }
48
49     /**
50      * Constructor for ExternalClassLoader.
51      *
52      * @param urls
53      */

54     public ExternalClassLoader(URL JavaDoc[] urls) {
55         super(urls);
56     }
57
58     /**
59      * Constructor for ExternalClassLoader.
60      *
61      * @param urls
62      * @param parent
63      * @param factory
64      */

65     public ExternalClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent,
66             URLStreamHandlerFactory JavaDoc factory) {
67         super(urls, parent, factory);
68     }
69
70     public void addURL(URL JavaDoc url) {
71         super.addURL(url);
72     }
73
74     public Class JavaDoc findClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
75         Class JavaDoc temp = super.findClass(className);
76
77         return temp;
78     }
79
80     public Object JavaDoc instanciate(String JavaDoc className) throws Exception JavaDoc {
81         Class JavaDoc actClass = findClass(className);
82
83         return actClass.newInstance();
84     }
85
86     public Object JavaDoc instanciate(String JavaDoc className, Object JavaDoc[] args) throws Exception JavaDoc {
87     
88         Class JavaDoc actClass = loadClass(className, false);
89
90         Constructor JavaDoc constructor = null;
91
92         // we can't just load the first constructor
93
// -> go find the correct constructor
94
// -> based on the arguments
95
if ((args == null) || (args.length == 0)) {
96
97             return actClass.newInstance();
98         }
99
100         constructor = ClassLoaderHelper.findConstructor(args, actClass);
101
102         // couldn't find correct constructor
103
if (constructor == null) {
104             LOG.severe("Couldn't find constructor for " + className
105                     + " with matching argument-list: ");
106             for (int i = 0; i < args.length; i++) {
107                 LOG.severe("argument[" + i + "]=" + args[i]);
108             }
109             return null;
110         }
111
112         return constructor.newInstance(args);
113     }
114
115     /* (non-Javadoc)
116      * @see java.net.URLClassLoader#findResource(java.lang.String)
117      */

118     public URL JavaDoc findResource(String JavaDoc name) {
119         
120         URL JavaDoc url = super.findResource(name);
121         return url;
122     }
123
124 }
Popular Tags