KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public class DefaultPluginLoader {
34
35     private static final Logger JavaDoc LOG = Logger
36             .getLogger("org.columba.core.loader");
37
38     // we can't use SystemClassLoader here, because that doesn't work
39
// with java webstart
40
// -> instead we use this.getClass().getClassLoader()
41
// -> which seems to work perfectly
42

43     /*
44      * protected static ClassLoader loader = ClassLoader.getSystemClassLoader();
45      */

46     ClassLoader JavaDoc loader;
47
48     public DefaultPluginLoader() {
49         super();
50
51         loader = this.getClass().getClassLoader();
52
53     }
54
55     public IExtensionInterface loadPlugin(String JavaDoc id, String JavaDoc className,
56             Object JavaDoc[] arguments) throws Exception JavaDoc {
57
58         if (className == null)
59             throw new IllegalArgumentException JavaDoc("className == null");
60
61         IExtensionInterface plugin = null;
62
63         Class JavaDoc actClass;
64
65         actClass = loader.loadClass(className);
66
67         //
68
// we can't just load the first constructor
69
// -> go find the correct constructor based
70
// -> based on the arguments
71
//
72
if ((arguments == null) || (arguments.length == 0)) {
73
74             plugin = (IExtensionInterface) actClass.newInstance();
75
76         } else {
77             Constructor JavaDoc constructor;
78
79             constructor = ClassLoaderHelper
80                     .findConstructor(arguments, actClass);
81
82             // couldn't find correct constructor
83
if (constructor == null) {
84                 LOG.severe("Couldn't find constructor for " + className
85                         + " with matching argument-list: ");
86                 for (int i = 0; i < arguments.length; i++) {
87                     LOG.severe("argument[" + i + "]=" + arguments[i]);
88                 }
89
90                 return null;
91             } else {
92
93                 plugin = (IExtensionInterface) constructor
94                         .newInstance(arguments);
95
96             }
97
98         }
99
100         return plugin;
101     }
102
103 }
104
Popular Tags