Search in sources :

Example 1 with PluginSpec

use of io.apiman.common.plugin.PluginSpec in project apiman by apiman.

the class AbstractPluginRegistry method loadPlugin.

/**
 * @see io.apiman.manager.api.core.IPluginRegistry#loadPlugin(io.apiman.common.plugin.PluginCoordinates)
 */
@Override
public Plugin loadPlugin(PluginCoordinates coordinates) throws InvalidPluginException {
    boolean isSnapshot = PluginUtils.isSnapshot(coordinates);
    synchronized (mutex) {
        if (pluginCache.containsKey(coordinates)) {
            Plugin cachedPlugin = pluginCache.get(coordinates);
            if (isSnapshot) {
                pluginCache.remove(coordinates);
                try {
                    cachedPlugin.getLoader().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                return cachedPlugin;
            }
        }
        String pluginRelativePath = PluginUtils.getPluginRelativePath(coordinates);
        File pluginDir = new File(pluginsDir, pluginRelativePath);
        if (!pluginDir.exists()) {
            pluginDir.mkdirs();
        }
        // $NON-NLS-1$
        File pluginFile = new File(pluginDir, "plugin." + coordinates.getType());
        // Clean up stale files in the case of a snapshot.
        if (pluginFile.exists() && isSnapshot) {
            try {
                FileUtils.deleteDirectory(pluginFile.getParentFile());
                pluginFile.getParentFile().mkdirs();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Doesn't exist (or it's a snapshot)?  Better download it.
        if (!pluginFile.exists()) {
            downloadPlugin(pluginFile, coordinates);
        }
        // Still doesn't exist?  That's a failure.
        if (!pluginFile.exists()) {
            // $NON-NLS-1$
            throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.PluginNotFound"));
        }
        PluginClassLoader pluginClassLoader;
        try {
            pluginClassLoader = createPluginClassLoader(pluginFile);
        } catch (IOException e) {
            // $NON-NLS-1$
            throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e);
        }
        URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH);
        if (specFile == null) {
            // $NON-NLS-1$
            throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH));
        }
        try {
            PluginSpec spec = PluginUtils.readPluginSpecFile(specFile);
            Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader);
            pluginCache.put(coordinates, plugin);
            return plugin;
        } catch (Exception e) {
            // $NON-NLS-1$
            throw new InvalidPluginException(Messages.i18n.format("AbstractPluginRegistry.FailedToReadSpecFile", PluginUtils.PLUGIN_SPEC_PATH), e);
        }
    }
}
Also used : PluginSpec(io.apiman.common.plugin.PluginSpec) InvalidPluginException(io.apiman.manager.api.core.exceptions.InvalidPluginException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) InvalidPluginException(io.apiman.manager.api.core.exceptions.InvalidPluginException) Plugin(io.apiman.common.plugin.Plugin) PluginClassLoader(io.apiman.common.plugin.PluginClassLoader)

Example 2 with PluginSpec

use of io.apiman.common.plugin.PluginSpec in project apiman by apiman.

the class DefaultPluginRegistry method readPluginFile.

/**
 * Reads the plugin into an object.  This method will fail if the plugin is not valid.
 * This could happen if the file is not a java archive, or if the plugin spec file is
 * missing from the archive, etc.
 */
protected Plugin readPluginFile(PluginCoordinates coordinates, File pluginFile) throws Exception {
    try {
        PluginClassLoader pluginClassLoader = createPluginClassLoader(pluginFile);
        URL specFile = pluginClassLoader.getResource(PluginUtils.PLUGIN_SPEC_PATH);
        if (specFile == null) {
            // $NON-NLS-1$
            throw new Exception(Messages.i18n.format("DefaultPluginRegistry.MissingPluginSpecFile", PluginUtils.PLUGIN_SPEC_PATH));
        } else {
            PluginSpec spec = PluginUtils.readPluginSpecFile(specFile);
            Plugin plugin = new Plugin(spec, coordinates, pluginClassLoader);
            // $NON-NLS-1$
            LOGGER.info("Read apiman plugin: {0}", spec);
            return plugin;
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        throw new Exception(Messages.i18n.format("DefaultPluginRegistry.InvalidPlugin", pluginFile.getAbsolutePath()), e);
    }
}
Also used : PluginSpec(io.apiman.common.plugin.PluginSpec) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PluginClassLoader(io.apiman.common.plugin.PluginClassLoader) Plugin(io.apiman.common.plugin.Plugin)

Aggregations

Plugin (io.apiman.common.plugin.Plugin)2 PluginClassLoader (io.apiman.common.plugin.PluginClassLoader)2 PluginSpec (io.apiman.common.plugin.PluginSpec)2 IOException (java.io.IOException)2 URL (java.net.URL)2 InvalidPluginException (io.apiman.manager.api.core.exceptions.InvalidPluginException)1 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1