Example usage for java.net URLClassLoader URLClassLoader

List of usage examples for java.net URLClassLoader URLClassLoader

Introduction

In this page you can find the example usage for java.net URLClassLoader URLClassLoader.

Prototype

public URLClassLoader(URL[] urls) 

Source Link

Document

Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader .

Usage

From source file:org.lockss.plugin.PluginManager.java

protected void processOneRegistryJar(CachedUrl cu, String url, ArchivalUnit au, Map tmpMap) {
    Integer curVersion = Integer.valueOf(cu.getVersion());

    if (cuNodeVersionMap.get(url) == null) {
        cuNodeVersionMap.put(url, Integer.valueOf(-1));
    }//  ww w.j  a  v  a  2  s  . c o  m

    // If we've already visited this CU, skip it unless the current
    // repository node is a different version (older OR newer)
    Integer oldVersion = (Integer) cuNodeVersionMap.get(url);

    if (oldVersion.equals(curVersion)) {
        log.debug2(url + ": JAR repository and map versions are identical.  Skipping...");
        return;
    }

    File blessedJar = null;
    if (cu.getContentSize() == 0) {
        log.debug("Empty plugin jar: " + cu);
        return;
    } else {
        try {
            // Validate and bless the JAR file from the CU.
            blessedJar = jarValidator.getBlessedJar(cu);
            log.debug2("Plugin jar: " + cu.getUrl() + " -> " + blessedJar);
        } catch (IOException ex) {
            log.error("Error processing jar file: " + url, ex);
            return;
        } catch (JarValidator.JarValidationException ex) {
            log.error("CachedUrl did not validate: " + cu, ex);
            return;
        }
    }

    // Update the cuNodeVersion map now that we have the blessed Jar.
    cuNodeVersionMap.put(url, curVersion);

    if (blessedJar != null) {
        // Get the list of plugins to load from this jar.
        List loadPlugins = null;
        try {
            loadPlugins = getJarPluginClasses(blessedJar);
        } catch (IOException ex) {
            log.error("Error while getting list of plugins for " + blessedJar);
            return; // skip this CU.

        }
        log.debug2("Blessed jar: " + blessedJar + ", plugins: " + loadPlugins);

        // Although this -should- never happen, it's possible.
        if (loadPlugins.size() == 0) {
            log.warning("Jar " + blessedJar + " does not contain any plugins.  Skipping...");
            return; // skip this CU.
        }

        // Load the plugin classes
        ClassLoader pluginLoader = null;
        URL blessedUrl;
        try {
            blessedUrl = blessedJar.toURL();
            URL[] urls = new URL[] { blessedUrl };
            pluginLoader = preferLoadablePlugin ? new LoadablePluginClassLoader(urls)
                    : new URLClassLoader(urls);
        } catch (MalformedURLException ex) {
            log.error("Malformed URL exception attempting to create " + "classloader for plugin JAR "
                    + blessedJar);
            return; // skip this CU.
        }

        String pluginName = null;

        for (Iterator pluginIter = loadPlugins.iterator(); pluginIter.hasNext();) {
            pluginName = (String) pluginIter.next();
            String key = pluginKeyFromName(pluginName);

            Plugin plugin;
            PluginInfo info;
            try {
                info = retrievePlugin(pluginName, pluginLoader);
                if (info == null) {
                    log.warning("Probable plugin packaging error: plugin " + pluginName + " not found in "
                            + cu.getUrl());
                    continue;
                } else {
                    info.setCuUrl(url);
                    info.setRegistryAu(au);
                    List urls = info.getResourceUrls();
                    if (urls != null && !urls.isEmpty()) {
                        String jar = urls.get(0).toString();
                        if (jar != null) {
                            // If the blessed jar path is a substring of the jar:
                            // url from which the actual plugin resource or class
                            // was loaded, then it is a loadable plugin.
                            boolean isLoadable = jar.indexOf(blessedUrl.getFile()) > 0;
                            info.setIsOnLoadablePath(isLoadable);
                        }
                    }
                    plugin = info.getPlugin();
                }
            } catch (Exception ex) {
                log.error(String.format("Unable to load plugin %s", pluginName), ex);
                continue;
            }

            PluginVersion version = null;

            try {
                version = new PluginVersion(plugin.getVersion());
                info.setVersion(version);
            } catch (IllegalArgumentException ex) {
                // Don't let this runtime exception stop the daemon.  Skip the plugin.
                log.error(String.format("Skipping plugin %s: %s", pluginName, ex.getMessage()));
                // must stop plugin to enable it to be collected
                plugin.stopPlugin();
                return;
            }

            if (pluginMap.containsKey(key)) {
                // Plugin already exists in the global plugin map.
                // Replace it with a new version if one is available.
                log.debug2("Plugin " + key + " is already in global pluginMap.");
                Plugin otherPlugin = getPlugin(key);
                PluginVersion otherVer = new PluginVersion(otherPlugin.getVersion());
                if (version.toLong() > otherVer.toLong()) {
                    if (log.isDebug2()) {
                        log.debug2("Existing plugin " + plugin.getPluginId() + ": Newer version " + version
                                + " found.");
                    }
                    tmpMap.put(key, info);
                } else {
                    if (log.isDebug2()) {
                        log.debug2("Existing plugin " + plugin.getPluginId() + ": No newer version found.");
                    }
                    // must stop plugin to enable it to be collected
                    plugin.stopPlugin();
                }
            } else if (!tmpMap.containsKey(key)) {
                // Plugin doesn't yet exist in the temporary map, add it.
                tmpMap.put(key, info);

                if (log.isDebug2()) {
                    log.debug2("Plugin " + plugin.getPluginId() + ": No previous version in temp map.");
                }
            } else {
                // Plugin already exists in the temporary map, use whichever
                // version is higher.
                PluginVersion otherVer = ((PluginInfo) tmpMap.get(key)).getVersion();

                if (version.toLong() > otherVer.toLong()) {
                    if (log.isDebug2()) {
                        log.debug2("Plugin " + plugin.getPluginId() + ": version " + version
                                + " is newer than version " + otherVer + " already in temp map, overwriting.");
                    }
                    // Overwrite old key in temp map
                    tmpMap.put(key, info);
                } else {
                    // must stop plugin to enable it to be collected
                    plugin.stopPlugin();
                }
            }
        }
    }
}