001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.plugin;
017:
018: import java.lang.reflect.Constructor;
019: import java.net.URL;
020: import java.net.URLClassLoader;
021: import java.net.URLStreamHandlerFactory;
022: import java.util.logging.Logger;
023:
024: /**
025: * Classloader responsible for instanciating plugins which can also be outside.
026: * <p>
027: * Note, that this classloader tries to find the correct constructor based on
028: * the arguments.
029: * <p>
030: * Currently, this is not used.
031: *
032: * @author fdietz
033: */
034: public class ExternalClassLoader extends URLClassLoader {
035:
036: private static final Logger LOG = Logger
037: .getLogger("org.columba.core.loader");
038:
039: /**
040: * Constructor for ExternalClassLoader.
041: *
042: * @param urls
043: * @param parent
044: */
045: public ExternalClassLoader(URL[] urls, ClassLoader parent) {
046: super (urls, parent);
047: }
048:
049: /**
050: * Constructor for ExternalClassLoader.
051: *
052: * @param urls
053: */
054: public ExternalClassLoader(URL[] urls) {
055: super (urls);
056: }
057:
058: /**
059: * Constructor for ExternalClassLoader.
060: *
061: * @param urls
062: * @param parent
063: * @param factory
064: */
065: public ExternalClassLoader(URL[] urls, ClassLoader parent,
066: URLStreamHandlerFactory factory) {
067: super (urls, parent, factory);
068: }
069:
070: public void addURL(URL url) {
071: super .addURL(url);
072: }
073:
074: public Class findClass(String className)
075: throws ClassNotFoundException {
076: Class temp = super .findClass(className);
077:
078: return temp;
079: }
080:
081: public Object instanciate(String className) throws Exception {
082: Class actClass = findClass(className);
083:
084: return actClass.newInstance();
085: }
086:
087: public Object instanciate(String className, Object[] args)
088: throws Exception {
089:
090: Class actClass = loadClass(className, false);
091:
092: Constructor constructor = null;
093:
094: // we can't just load the first constructor
095: // -> go find the correct constructor
096: // -> based on the arguments
097: if ((args == null) || (args.length == 0)) {
098:
099: return actClass.newInstance();
100: }
101:
102: constructor = ClassLoaderHelper.findConstructor(args, actClass);
103:
104: // couldn't find correct constructor
105: if (constructor == null) {
106: LOG.severe("Couldn't find constructor for " + className
107: + " with matching argument-list: ");
108: for (int i = 0; i < args.length; i++) {
109: LOG.severe("argument[" + i + "]=" + args[i]);
110: }
111: return null;
112: }
113:
114: return constructor.newInstance(args);
115: }
116:
117: /* (non-Javadoc)
118: * @see java.net.URLClassLoader#findResource(java.lang.String)
119: */
120: public URL findResource(String name) {
121:
122: URL url = super.findResource(name);
123: return url;
124: }
125:
126: }
|