Search in sources :

Example 1 with Plugin

use of io.crate.Plugin in project crate by crate.

the class PluginLoader method findImplementations.

private Collection<Class<? extends Plugin>> findImplementations() {
    if (!isAccessibleDirectory(pluginsPath, logger)) {
        return Collections.emptyList();
    }
    File[] plugins = pluginsPath.toFile().listFiles();
    if (plugins == null) {
        return Collections.emptyList();
    }
    Collection<Class<? extends Plugin>> allImplementations = new ArrayList<>();
    for (File plugin : plugins) {
        if (!plugin.canRead()) {
            logger.debug("[{}] is not readable.", plugin.getAbsolutePath());
            continue;
        }
        // check if its an elasticsearch plugin
        Path esDescriptorFile = plugin.toPath().resolve(PluginInfo.ES_PLUGIN_PROPERTIES);
        try {
            if (esDescriptorFile.toFile().exists()) {
                continue;
            }
        } catch (Exception e) {
        // ignore
        }
        List<URL> pluginUrls = new ArrayList<>();
        logger.trace("--- adding plugin [{}]", plugin.getAbsolutePath());
        try {
            URL pluginURL = plugin.toURI().toURL();
            // jar-hell check the plugin against the parent classloader
            try {
                checkJarHell(pluginURL);
            } catch (Exception e) {
                String msg = String.format(Locale.ENGLISH, "failed to load plugin %s due to jar hell", pluginURL);
                logger.error(msg, e);
                throw new RuntimeException(msg, e);
            }
            pluginUrls.add(pluginURL);
            if (!plugin.isFile()) {
                // gather files to add
                List<File> libFiles = Lists.newArrayList();
                File[] pluginFiles = plugin.listFiles();
                if (pluginFiles != null) {
                    libFiles.addAll(Arrays.asList(pluginFiles));
                }
                File libLocation = new File(plugin, "lib");
                if (libLocation.exists() && libLocation.isDirectory()) {
                    File[] pluginLibFiles = libLocation.listFiles();
                    if (pluginLibFiles != null) {
                        libFiles.addAll(Arrays.asList(pluginLibFiles));
                    }
                }
                // if there are jars in it, add it as well
                for (File libFile : libFiles) {
                    if (!(libFile.getName().endsWith(".jar") || libFile.getName().endsWith(".zip"))) {
                        continue;
                    }
                    URL libURL = libFile.toURI().toURL();
                    // jar-hell check the plugin lib against the parent classloader
                    try {
                        checkJarHell(libURL);
                        pluginUrls.add(libURL);
                    } catch (Exception e) {
                        String msg = String.format(Locale.ENGLISH, "Library %s of plugin %s already loaded", libURL, pluginURL);
                        logger.error(msg, e);
                        throw new RuntimeException(msg, e);
                    }
                }
            }
        } catch (MalformedURLException e) {
            String msg = String.format(Locale.ENGLISH, "failed to add plugin [%s]", plugin);
            logger.error(msg, e);
            throw new RuntimeException(msg, e);
        }
        Collection<Class<? extends Plugin>> implementations = findImplementations(pluginUrls);
        if (implementations == null || implementations.isEmpty()) {
            String msg = String.format(Locale.ENGLISH, "Path [%s] does not contain a valid Crate or Elasticsearch plugin", plugin.getAbsolutePath());
            RuntimeException e = new RuntimeException(msg);
            logger.error(msg, e);
            throw e;
        }
        jarsToLoad.addAll(pluginUrls);
        allImplementations.addAll(implementations);
    }
    return allImplementations;
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) File(java.io.File) Plugin(io.crate.Plugin)

Aggregations

Plugin (io.crate.Plugin)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1