001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.plugin;
019:
020: import java.lang.reflect.Constructor;
021: import java.util.logging.Logger;
022:
023: import org.columba.api.plugin.IExtensionInterface;
024:
025: /**
026: * Use default classloader to instanciate a class using a constructor
027: * with matching arguments.
028: * <p>
029: * Currently, this is not used.
030: *
031: * @author fdietz
032: */
033: public class DefaultPluginLoader {
034:
035: private static final Logger LOG = Logger
036: .getLogger("org.columba.core.loader");
037:
038: // we can't use SystemClassLoader here, because that doesn't work
039: // with java webstart
040: // -> instead we use this.getClass().getClassLoader()
041: // -> which seems to work perfectly
042:
043: /*
044: * protected static ClassLoader loader = ClassLoader.getSystemClassLoader();
045: */
046: ClassLoader loader;
047:
048: public DefaultPluginLoader() {
049: super ();
050:
051: loader = this .getClass().getClassLoader();
052:
053: }
054:
055: public IExtensionInterface loadPlugin(String id, String className,
056: Object[] arguments) throws Exception {
057:
058: if (className == null)
059: throw new IllegalArgumentException("className == null");
060:
061: IExtensionInterface plugin = null;
062:
063: Class actClass;
064:
065: actClass = loader.loadClass(className);
066:
067: //
068: // we can't just load the first constructor
069: // -> go find the correct constructor based
070: // -> based on the arguments
071: //
072: if ((arguments == null) || (arguments.length == 0)) {
073:
074: plugin = (IExtensionInterface) actClass.newInstance();
075:
076: } else {
077: Constructor constructor;
078:
079: constructor = ClassLoaderHelper.findConstructor(arguments,
080: actClass);
081:
082: // couldn't find correct constructor
083: if (constructor == null) {
084: LOG.severe("Couldn't find constructor for " + className
085: + " with matching argument-list: ");
086: for (int i = 0; i < arguments.length; i++) {
087: LOG.severe("argument[" + i + "]=" + arguments[i]);
088: }
089:
090: return null;
091: } else {
092:
093: plugin = (IExtensionInterface) constructor
094: .newInstance(arguments);
095:
096: }
097:
098: }
099:
100: return plugin;
101: }
102:
103: }
|