Update of /cvsroot/nutch/playground/src/java/net/nutch/plugin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10313/src/java/net/nutch/plugin

Added Files:
        Extension.java PluginManifestParser.java PluginDescriptor.java 
        PluginRepository.java ExtensionPoint.java 
        PluginRuntimeException.java PluginClassLoader.java Plugin.java 
Log Message:
intial commit

--- NEW FILE: Extension.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

import java.net.URL;
import java.util.HashMap;

/**
 * A extension represent the implementation of a extension point.
 * @author joa23
 */
public class Extension {

    private PluginDescriptor fDescriptor;
    private String fId;
    private String fTargetPoint;
    private String fClazz;
    private HashMap fAttributes;

    /**
     * @param implementationId
     * @param implementationName
     * @param objectClass
     * @param implementationClazz
     */
    public Extension(
        PluginDescriptor pDescriptor,
        String pPoint,
        String pId,
        String pClazz) {
        fAttributes = new HashMap();
        setDiscriptor(pDescriptor);
        setTargetPoint(pPoint);
        setId(pId);
        setClazz(pClazz);
    }

    /**
     * @param point
     */
    private void setTargetPoint(String point) {
        fTargetPoint = point;

    }

    /**
     * return a attribute value, that can be setuped in the manifest file 
     * and is definied by the extension point xml schema.
     *  
     * @param pKey
     * @return String
     */
    public String getAttribute(String pKey) {
        return (String) fAttributes.get(pKey);
    }

    /**
     * Return the class url of the extension point implementation
     * @return String
     */
    public String getClazz() {
        return fClazz;
    }

    /**
     * return the id of a the extension.
     * @return String
     */
    public String getId() {
        return fId;
    }

    /**
     * add a attribute and  is only used until model creation at system start up.
     * @param pKey
     * @param pValue
     */
    public void addAttribute(String pKey, String pValue) {
        fAttributes.put(pKey, pValue);
    }

    /**
     * Sets the extensionClazz and is only used until model creation at system start 
up.
     * @param extensionClazz The extensionClazz to set
     */
    public void setClazz(String extensionClazz) {
        fClazz = extensionClazz;
    }

    /**
     * Sets the extensionID and is only used until model creation at system start up.
     * @param extensionID The extensionID to set
     */
    public void setId(String extensionID) {
        fId = extensionID;
    }

    /**
     * return the Id of the extension point, that is implemented by this extension.
     */
    public String getTargetPoint() {
        return fTargetPoint;

    }

    /**
     * return a instance of the extension. 
     * before we create a extension instance 
     * we startup the plugin if it is not already started.
     * The plugin instance and the extension instance use the same PluginClassloader.
     * Each Plugin has its own classloader and Plugin runtime libraries and 
     * the exported libraries of the depenedend plugins.
     * @return Object
     */
    public Object getExtensionInstance() throws PluginRuntimeException {
        try {
            PluginClassLoader loader = fDescriptor.getClassLoader();
            URL[] urls = loader.getURLs();
            for (int i = 0; i < urls.length; i++) {
                URL url = urls[i];
                System.err.println(url.getPath());
            }

            Class extensionClazz = loader.loadClass(getClazz());
            PluginRepository.getInstance().getPluginInstance(getDiscriptor());
            return extensionClazz.newInstance();

        } catch (ClassNotFoundException e) {
            throw new PluginRuntimeException(e);
        } catch (InstantiationException e) {
            throw new PluginRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new PluginRuntimeException(e);
        }
    }

    /**
     * return the plugin descriptor.
     * @return PluginDescriptor
     */
    public PluginDescriptor getDiscriptor() {
        return fDescriptor;
    }
    /**
     * set the plugin descriptor and is only used until model creation at system start 
up.
     * @return PluginDescriptor
     */
    public void setDiscriptor(PluginDescriptor pDescriptor) {
        fDescriptor = pDescriptor;
    }

}

--- NEW FILE: PluginManifestParser.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import net.nutch.util.LogFormatter;
import net.nutch.util.NutchConf;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * The plugin manifest parser just parse the manifest file in all plugin directories.
 * @author joa23
 */
public class PluginManifestParser {

    public static final Logger LOG =
        LogFormatter.getLogger("net.nutch.plugin.PluginManifestParser");
    /**
     * Return a list with plugin descriptors.  
     * @return ArrayList
     * @throws IOException
     * @throws MalformedURLException
     * @throws DocumentException
     */
    public static ArrayList parsePluginFolder() {
        ArrayList list = new ArrayList();
        String pluginFolder = NutchConf.get("plugin.folder");
        if (pluginFolder == null)
            throw new IllegalArgumentException("no plugin folder setuped...");
        File file = new File(pluginFolder);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File oneSubFolder = files[i];
          
            if ( oneSubFolder.isDirectory()) {
                String manifestPath =
                    oneSubFolder.getAbsolutePath()
                        + File.separator
                        + "plugin.xml";
                try {
                    list.add(parseManifestFile(manifestPath));
                } catch (MalformedURLException e) {
                    LOG.fine(e.toString());
                } catch (DocumentException e) {
                    LOG.fine(e.toString());
                }
            }
        }
        return list;

    }

    /**
     * @param manifestPath
     */
    private static PluginDescriptor parseManifestFile(String pManifestPath)
        throws MalformedURLException, DocumentException {
        Document document = parseXML(new File(pManifestPath).toURL());
        String pPath = new File(pManifestPath).getParent();
        return parsePlugin(document, pPath);

    }
    /**
      * 
      * @param url
      * @return Document
      * @throws DocumentException
      */
    private static Document parseXML(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }
    /**
     * @param document
     */
    private static PluginDescriptor parsePlugin(
        Document pDocument,
        String pPath)
        throws MalformedURLException {
        Element rootElement = pDocument.getRootElement();

        String id = rootElement.attributeValue("id");
        String name = rootElement.attributeValue("name");
        String version = rootElement.attributeValue("version");
        String providerName = rootElement.attributeValue("provider-name");
        String pluginClazz = rootElement.attributeValue("class");

        PluginDescriptor pluginDescriptor =
            new PluginDescriptor(
                id,
                version,
                name,
                providerName,
                pluginClazz,
                pPath);

        parseExtension(rootElement, pluginDescriptor);
        parseExtensionPoints(rootElement, pluginDescriptor);
        parseLibraries(rootElement, pluginDescriptor);
        return pluginDescriptor;
    }

    /**
     * @param rootElement
     * @param pluginDescriptor
     */
    private static void parseLibraries(
        Element pRootElement,
        PluginDescriptor pDescriptor)
        throws MalformedURLException {

        Element runtime = pRootElement.element("runtime");
        if (runtime == null)
            return;

        List libraries = runtime.elements("library");
        for (int i = 0; i < libraries.size(); i++) {
            Element library = (Element) libraries.get(i);
            String libName = library.attributeValue("name");
            Element exportElement = library.element("extport");
            if (exportElement != null)
                pDescriptor.addExportedLibRelative(libName);
            else
                pDescriptor.addNotExportedLibRelative(libName);
        }

    }

    /**
     * @param rootElement
     * @param pluginDescriptor
     */
    private static void parseExtensionPoints(
        Element pRootElement,
        PluginDescriptor pPluginDescriptor) {
        List list = pRootElement.elements("extension-point");
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                Element oneExtensionPoint = (Element) list.get(i);
                String id = oneExtensionPoint.attributeValue("id");
                String name = oneExtensionPoint.attributeValue("name");
                String schema = oneExtensionPoint.attributeValue("schema");

                ExtensionPoint extensionPoint =
                    new ExtensionPoint(id, name, schema);
                pPluginDescriptor.addExtensionPoint(extensionPoint);
            }
        }
    }

    /**
     * 
     * @param rootElement
     * @param pluginDescriptor
     */
    private static void parseExtension(
        Element pRootElement,
        PluginDescriptor pPluginDescriptor) {
        List extensions = pRootElement.elements("extension");
        if (extensions != null) {
            for (int i = 0; i < extensions.size(); i++) {
                Element oneExtension = (Element) extensions.get(i);
                String pointId = oneExtension.attributeValue("point");
                List extensionImplementations = oneExtension.elements();
                if (extensionImplementations != null) {
                    for (int j = 0; j < extensionImplementations.size(); j++) {
                        Element oneImplementation =
                            (Element) extensionImplementations.get(j);

                        String id = oneImplementation.attributeValue("id");
                        String clazz =
                            oneImplementation.attributeValue("class");
                        Extension extension =
                            new Extension(
                                pPluginDescriptor,
                                pointId,
                                id,
                                clazz);

                        List list = oneImplementation.attributes();
                        for (int k = 0; k < list.size(); k++) {
                            Attribute attribute = (Attribute) list.get(k);
                            String name = attribute.getName();
                            if (name.equals("id") && name.equals("class"))
                                continue;

                            String value = attribute.getValue();
                            extension.addAttribute(name, value);
                        }

                        pPluginDescriptor.addExtension(extension);
                    }

                }
            }
        }
    }

}

--- NEW FILE: PluginDescriptor.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import net.nutch.util.LogFormatter;

/**
 * The plugin descriptor provide access to all meta information of a plugin, 
 * as well to the internationalizable resources and.the plugin wide classloader.
 * The meta informations are implemented in a model. We have Plugin, ExtensionPoint 
and Extension.
 * To handle the plugin via a descriptor allow lazy loading mechanism. 
 *  
 * @author joa23
 */
public class PluginDescriptor {
    private String fPluginPath;
    private String fPluginClass;
    private String fPluginId;
    private String fVersion;
    private String fName;
    private String fProviderName;
    private HashMap fMessages = new HashMap();

    private ArrayList fExtensionPoints = new ArrayList();
    private ArrayList fDependencies = new ArrayList();
    private ArrayList fExportedLibs = new ArrayList();
    private ArrayList fNotExportedLibs = new ArrayList();
    private ArrayList fExtensions = new ArrayList();

    private PluginClassLoader fClassLoader;

    public static final Logger LOG =
        LogFormatter.getLogger("net.nutch.plugin.PluginDescriptor");

    /**
     * 
     * @param pId
     * @param pVersion
     * @param pName
     * @param pProviderName
     * @param pPluginclazz
     * @param pPath
     */
    public PluginDescriptor(
        String pId,
        String pVersion,
        String pName,
        String pProviderName,
        String pPluginclazz,
        String pPath) {

        setPath(pPath);
        setPluginId(pId);
        setVersion(pVersion);
        setName(pName);
        setProvidername(pProviderName);
        setPluginClass(pPluginclazz);
    }

    /**
     * @param pPath
     */
    private void setPath(String pPath) {
        fPluginPath = pPath;
    }
    /**
     * Returns the name of a plugin.
     * @return String
     */
    public String getName() {
        return fName;
    }
    /**
     * @param providerName
     */
    private void setProvidername(String providerName) {
        fProviderName = providerName;

    }

    /**
     * @param name
     */
    private void setName(String name) {
        fName = name;

    }

    /**
     * @param version
     */
    private void setVersion(String version) {
        fVersion = version;

    }

    /**
     * Returns the fully qualified name of the Java class which implements
     * the runtime support for this plug-in.
     *
     * @return the name of this plug-in's runtime class or <code>null</code>.
     */
    public String getPluginClass() {
        return fPluginClass;
    }

    /**
     *  Returns the unique identifier of the plug-in or <code>null</code>.
     * @return String
     */
    public String getPluginId() {
        return fPluginId;
    }
    /**
     * Returns a aaray of extensions.
     * @return Exception[]
     */
    public Extension[] getExtensions() {
        return (Extension[]) fExtensions.toArray(
            new Extension[fExtensions.size()]);
    }
    /**
     * Add a extension.
     * @param pException
     */
    public void addExtension(Extension pExtension) {
        fExtensions.add(pExtension);
    }
    /**
     * Sets the pluginClass.
     * @param pluginClass The pluginClass to set
     */
    private void setPluginClass(String pluginClass) {
        fPluginClass = pluginClass;
    }

    /**
     * Sets the pluginId.
     * @param pluginId The pluginId to set
     */
    private void setPluginId(String pluginId) {
        fPluginId = pluginId;
    }

    /**
     * Add a extension point.
     * @param extensionPoint
     */
    public void addExtensionPoint(ExtensionPoint extensionPoint) {
        fExtensionPoints.add(extensionPoint);
    }
    /**
     * Returns a array with extension points.
     * @return ExtensionPoint[]
     */
    public ExtensionPoint[] getExtenstionPoints() {
        return (ExtensionPoint[]) fExtensionPoints.toArray(
            new ExtensionPoint[fExtensionPoints.size()]);
    }

    /**
     * Returns a array of plugin ids. 
     * @return String[]
     */
    public String[] getDependencies() {
        return (String[]) fDependencies.toArray(
            new String[fDependencies.size()]);
    }
    /**
     * Add a dependency 
     * @param pId
     */
    public void addDependency(String pId) {
        fDependencies.add(pId);
    }

    /**
     * Add a exported library with a plugin directory relative path. 
     * @param libName
     */
    public void addExportedLibRelative(String pLibPath)
        throws MalformedURLException {
        URL url = new File(getPluginPath() + File.separator + pLibPath).toURL();
        fExportedLibs.add(url);
    }

    /**
     * Returns the directory path of the plugin.
     * @return String
     */
    public String getPluginPath() {
        return fPluginPath;
    }
    /**
     * Returns a array exported librareis as URLs 
     * @return URL[]
     */
    public URL[] getExportedLibUrls() {
        return (URL[]) fExportedLibs.toArray(new URL[0]);
    }

    /**
     * Add a not exported library with a plugin directory relativ path.
     * @param libName
     */
    public void addNotExportedLibRelative(String pLibPath)
        throws MalformedURLException {
        URL url = new File(getPluginPath() + File.separator + pLibPath).toURL();
        fNotExportedLibs.add(url);
    }
    /**
     * Returns a array of libraries as URLs that are not exported by the plugin.
     * @return URL[]
     */
    public URL[] getNotExportedLibUrls() {
        return (URL[]) fNotExportedLibs.toArray(
            new URL[fNotExportedLibs.size()]);
    }

    /**
     * Returns a cached classloader for a plugin.
     * Until first time classloader creating all needed libraries are collected. 
     * A classloader use as first the plugins own libraries 
     * and add then all exported libraries of dependend plugins 
     * ass well 
     * 
     * @return PluginClassLoader
     */
    public PluginClassLoader getClassLoader() {
        if (fClassLoader != null)
            return fClassLoader;

        ArrayList arrayList = new ArrayList();
        arrayList.addAll(fExportedLibs);
        arrayList.addAll(fNotExportedLibs);
        arrayList.addAll(getDependencyLibs());
        File file = new File(getPluginPath());
        File[] files = file.listFiles();
        try {
            for (int i = 0; i < files.length; i++) {
                File file2 = files[i];
                String path = file2.getAbsolutePath();
                if (file2.getAbsolutePath().endsWith("properties"))
                    arrayList.add(file2.getParentFile().toURL());
            }
        } catch (MalformedURLException e) {
            LOG.fine(getPluginId() + " " + e.toString());
        }

        URL[] urls = (URL[]) arrayList.toArray(new URL[arrayList.size()]);
        fClassLoader =
            new PluginClassLoader(
                urls,
                PluginDescriptor.class.getClassLoader());
        return fClassLoader;
    }

    /**
     * @return Collection
     */
    private ArrayList getDependencyLibs() {
        ArrayList list = new ArrayList();
        collectLibs(list, this);
        return list;
    }

    /**
     * @param list
     */
    private void collectLibs(ArrayList pLibs, PluginDescriptor pDescriptor) {
        String[] pPluginIds = pDescriptor.getDependencies();
        for (int i = 0; i < pPluginIds.length; i++) {
            String id = pPluginIds[i];
            PluginDescriptor descriptor =
                PluginRepository.getInstance().getPluginDescriptor(id);
            URL[] libs = descriptor.getExportedLibUrls();
            for (int j = 0; j < libs.length; j++) {
                URL url = libs[j];
                pLibs.add(url);
            }
            collectLibs(pLibs, descriptor);
        }

    }

    /**
     * Returns a internationalizabel resource string. 
     * The resource bundles could be stored in root directory of a plugin in the well 
know i18n file name conventions. 
     * 
     * @param pKey
     * @param pLocale
     * @return String
     * @throws IOException
     */
    public String getResourceString(String pKey, Locale pLocale)
        throws IOException {
        if (fMessages.containsKey(pLocale.toString())) {
            ResourceBundle bundle =
                (ResourceBundle) fMessages.get(pLocale.toString());
            try {
                return bundle.getString(pKey);
            } catch (MissingResourceException e) {
                return '!' + pKey + '!';
            }
        }
        try {
            ResourceBundle res =
                ResourceBundle.getBundle("messages", pLocale, getClassLoader());
            return res.getString(pKey);
        } catch (MissingResourceException x) {
            return '!' + pKey + '!';
        }

    }

}

--- NEW FILE: PluginRepository.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Logger;

import net.nutch.util.LogFormatter;

import org.dom4j.DocumentException;

/**
 * The plugin repositority manage all plugins. 
 * 
 * At system boot up a repositority is builded by parsing the mainifest files of all 
plugins.
 * Plugins that require not existing other plugins are not registed. 
 * For each plugin a plugin descriptor instance will be created. The descriptor 
represent 
 * all meta information about a plugin.
 * So a plugin instance will be created later when it is required, this allow lazy 
plugin loading. 
 *   
 * @author joa23
 */
public class PluginRepository {

    private static PluginRepository fInstance;
    private ArrayList fRegisteredPlugins;

    private HashMap fExtensionPoints;
    private HashMap fActivatedPlugins;

    public static final Logger LOG =
        LogFormatter.getLogger("net.nutch.plugin.PluginRepository");
    /**
     * 
     * @see java.lang.Object#Object()
     */
    private PluginRepository()
        throws MalformedURLException, IOException, DocumentException {
        super();
        fActivatedPlugins = new HashMap();
        fExtensionPoints = new HashMap();
        fRegisteredPlugins =
            getDependencyCheckedPlugins(
                PluginManifestParser.parsePluginFolder());
        installExtensions(fRegisteredPlugins);

    }

    /**
     * @param fRegisteredPlugins
     */
    private void installExtensions(ArrayList pRegisteredPlugins) {
        for (int i = 0; i < pRegisteredPlugins.size(); i++) {
            PluginDescriptor descriptor =
                (PluginDescriptor) pRegisteredPlugins.get(i);
            Extension[] extensions = descriptor.getExtensions();
            for (int j = 0; j < extensions.length; j++) {
                Extension extension = extensions[j];
                String xpId = extension.getTargetPoint();
                ExtensionPoint point = getExtensionPoint(xpId);
                point.addExtension(extension);
            }
        }

    }

    /**
     * 
     * @param pLoadedPlugins
     * @return ArrayList
     */
    private ArrayList getDependencyCheckedPlugins(ArrayList pLoadedPlugins) {
        ArrayList availablePlugins = new ArrayList();
        for (int i = 0; i < pLoadedPlugins.size(); i++) {
            PluginDescriptor descriptor =
                (PluginDescriptor) pLoadedPlugins.get(i);
            String[] dependencyIDs = descriptor.getDependencies();
            boolean available = true;

            for (int j = 0; j < dependencyIDs.length; j++) {
                String id = dependencyIDs[j];
                if (!dependencyIsAvailabel(id, pLoadedPlugins)) {
                    available = false;
                    break;
                }
            }
            if (available) {
                availablePlugins.add(descriptor);
                ExtensionPoint[] points = descriptor.getExtenstionPoints();
                for (int j = 0; j < points.length; j++) {
                    ExtensionPoint point = points[j];
                    String xpId = point.getId();
                    fExtensionPoints.put(xpId, point);
                }
            }

        } // /pLoadedPlugins
        return availablePlugins;
    }

    /**
     * 
     * @param id
     * @param pLoadedPlugins
     * @return boolean
     */
    private boolean dependencyIsAvailabel(
        String id,
        ArrayList pLoadedPlugins) {
        if (pLoadedPlugins != null && id != null) {
            for (int i = 0; i < pLoadedPlugins.size(); i++) {
                PluginDescriptor descriptor =
                    (PluginDescriptor) pLoadedPlugins.get(i);
                if (descriptor.getPluginId().equals(id)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * The plugin repositority is a singelton. This method returns the instance.
     */
    public static PluginRepository getInstance() {
        if (fInstance != null)
            return fInstance;
        try {
            fInstance = new PluginRepository();
        } catch (MalformedURLException e) {
            LOG.fine(e.toString());
        } catch (IOException e) {
            LOG.fine(e.toString());
        } catch (DocumentException e) {
            LOG.fine(e.toString());
        }
        return fInstance;

    }

    /**
     * Returns all registed plugin descriptors.
     * @return PluginDescriptor[]
     */
    public PluginDescriptor[] getPluginDescriptors() {
        return (PluginDescriptor[]) fRegisteredPlugins.toArray(
            new PluginDescriptor[fRegisteredPlugins.size()]);
    }

    /**
     * return the descriptor of one plugin identified by a plugin id.
     * @param pPluginId
     * @return PluginDescriptor
     */
    public PluginDescriptor getPluginDescriptor(String pPluginId) {
        for (int i = 0; i < fRegisteredPlugins.size(); i++) {
            PluginDescriptor descriptor =
                (PluginDescriptor) fRegisteredPlugins.get(i);
            if (descriptor.getPluginId().equals(pPluginId))
                return descriptor;
        }
        return null;
    }

    /**
     * return a extension point indentified by a extension point id.
     * @param xpId
     */
    public ExtensionPoint getExtensionPoint(String pXpId) {
        return (ExtensionPoint) fExtensionPoints.get(pXpId);
    }

    /**
     * return a instance of a plugin. 
     * Plugin instances are cached. So a plugin exist only as one instance.
     * This allow a central management of plugin own resources. 
     * 
     * After creating the plugin instance the startUp() method is invoked. 
     * The plugin use a own classloader that is used as well by all instance of 
extensions of the same plugin.
     * This class loader use all exported libraries from the dependend plugins and all 
plugin libraries.  
     *  
     * 
     * @param pDescriptor
     * @return Plugin
     * @throws PluginRuntimeException
     */
    public Plugin getPluginInstance(PluginDescriptor pDescriptor)
        throws PluginRuntimeException {
        if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))
            return (Plugin) fActivatedPlugins.get(pDescriptor.getPluginId());
        try {

            PluginClassLoader loader = pDescriptor.getClassLoader();
            Class pluginClass = loader.loadClass(pDescriptor.getPluginClass());
            Constructor constructor =
                pluginClass.getConstructor(
                    new Class[] { PluginDescriptor.class });
            Plugin plugin =
                (Plugin) constructor.newInstance(new Object[] { pDescriptor });
            plugin.startUp();

            fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);
            return plugin;

        } catch (ClassNotFoundException e) {
            throw new PluginRuntimeException(e);
        } catch (InstantiationException e) {
            throw new PluginRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new PluginRuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new PluginRuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new PluginRuntimeException(e);
        }
    }

    /* (non-Javadoc)
     * @see java.lang.Object#finalize()
     */
    protected void finalize() throws Throwable {
        shotDownActivatedPlugins();
    }
    /**
     * 
     * @throws PluginRuntimeException
     */
    private void shotDownActivatedPlugins() throws PluginRuntimeException {
        Iterator iterator = fActivatedPlugins.keySet().iterator();
        while (iterator.hasNext()) {
            String pluginId = (String) iterator.next();
            Plugin object = (Plugin) fActivatedPlugins.get(pluginId);
            object.shutDown();
        }
    }

}

--- NEW FILE: ExtensionPoint.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

import java.util.ArrayList;

/**
 * The extension point provide meta information of a extension point.
 * @author joa23
 */
public class ExtensionPoint {
    private String ftId;
    private String fName;
    private String fSchema;
    private ArrayList fExtensions;
    /**
     * @param id
     * @param name
     * @param schema
     */
    public ExtensionPoint(String pId, String pName, String pSchema) {
        setId(pId);
        setName(pName);
        setSchema(pSchema);
        fExtensions = new ArrayList();
    }

    /**
     * Returns the id of the  extension point.
     * @return String
     */
    public String getId() {
        return ftId;
    }

    /**
     * Returns the name of the extension point.
     * @return String
     */
    public String getName() {
        return fName;
    }

    /**
     * Returns a path to the xml schema of a extension point. 
     * @return String
     */
    public String getSchema() {
        return fSchema;
    }

    /**
     * Sets the extensionPointId.
     * @param extensionPointId The extensionPointId to set
     */
    private void setId(String pId) {
        ftId = pId;
    }

    /**
     * Sets the extensionPointName.
     * @param extensionPointName The extensionPointName to set
     */
    private void setName(String pName) {
        fName = pName;
    }

    /**
     * Sets the schema.
     * @param schema The schema to set
     */
    private void setSchema(String pSchema) {
        fSchema = pSchema;
    }

    /**
     * Add a extension that implement this extension point.
     * @param extension
     */
    public void addExtension(Extension extension) {
        fExtensions.add(extension);
    }
    /**
     * Returns a array of extensions that implement this 
     * @return Extension[]
     */
    public Extension[] getExtentens() {
        return (Extension[]) fExtensions.toArray(
            new Extension[fExtensions.size()]);
    }

}

--- NEW FILE: PluginRuntimeException.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

/**
 * Exception for plugin model.
 * @author joa23
 */
public class PluginRuntimeException extends Exception {

    /**
     * 
     * @see java.lang.Throwable#Throwable(Throwable)
     */
    public PluginRuntimeException(Throwable cause) {
        super(cause);
    }

}

--- NEW FILE: PluginClassLoader.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

import java.net.URL;
import java.net.URLClassLoader;

/**
 * A simple child of the normal URLCLassloader.
  * @author joa23
 */
public class PluginClassLoader extends URLClassLoader {

    /**
     * @param urls
     * @param parent
     */
    public PluginClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }

}

--- NEW FILE: Plugin.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   
 * Use subject to the conditions in http://www.nutch.org/LICENSE.txt. 
 */

package net.nutch.plugin;

/**
 * The abstract plugin clazz need to be extended by each plugin.
 * 
 * @author joa23
 */
public abstract class Plugin {

    private PluginDescriptor fDescriptor;

    /**
     * 
     */
    public Plugin(PluginDescriptor pDescriptor) {
        setDescriptor(pDescriptor);

    }

    /**
     * Until plugin start up some managebale resource could be started. e.g. a 
database connction.
     * @throws PluginRuntimeException
     */
    public abstract void startUp() throws PluginRuntimeException;

    /**
     * Until shutdown all resource could be released. 
     * @throws PluginRuntimeException
     */

    public abstract void shutDown() throws PluginRuntimeException;

    /**
     * Returns the descriptor the plugin.
     * @return PluginDescriptor
     */
    public PluginDescriptor getDescriptor() {
        return fDescriptor;
    }

    /**
     * 
     * @param descriptor The descriptor to set
     */
    private void setDescriptor(PluginDescriptor descriptor) {
        fDescriptor = descriptor;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#finalize()
     */
    protected void finalize() throws Throwable {
        super.finalize();
        shutDown();
    }

}



-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Nutch-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nutch-cvs

Reply via email to