Search in sources :

Example 1 with PluginClassLoader

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

the class DefaultPluginRegistry method createPluginClassLoader.

/**
 * Creates a plugin classloader for the given plugin file.
 */
protected PluginClassLoader createPluginClassLoader(final File pluginFile) throws IOException {
    return new PluginClassLoader(pluginFile, Thread.currentThread().getContextClassLoader()) {

        @Override
        protected File createWorkDir(File pluginArtifactFile) throws IOException {
            // $NON-NLS-1$
            File workDir = new File(pluginFile.getParentFile(), ".work");
            workDir.mkdirs();
            return workDir;
        }
    };
}
Also used : File(java.io.File) PluginClassLoader(io.apiman.common.plugin.PluginClassLoader)

Example 2 with PluginClassLoader

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

the class PluginResourceImpl method getPolicyForm.

/**
 * @see IPluginResource#getPolicyForm(java.lang.Long, java.lang.String)
 */
@Override
public String getPolicyForm(Long pluginId, String policyDefId) throws PluginNotFoundException, PluginResourceNotFoundException, PolicyDefinitionNotFoundException {
    // No permission check is needed
    PluginBean pbean;
    PolicyDefinitionBean pdBean;
    try {
        pbean = storage.getPlugin(pluginId);
        if (pbean == null) {
            throw ExceptionFactory.pluginNotFoundException(pluginId);
        }
        pdBean = storage.getPolicyDefinition(policyDefId);
    } catch (AbstractRestException e) {
        throw e;
    } catch (Exception e) {
        throw new SystemErrorException(e);
    }
    PluginCoordinates coordinates = new PluginCoordinates(pbean.getGroupId(), pbean.getArtifactId(), pbean.getVersion(), pbean.getClassifier(), pbean.getType());
    try {
        if (pdBean == null) {
            throw ExceptionFactory.policyDefNotFoundException(policyDefId);
        }
        if (pdBean.getPluginId() == null || !pdBean.getPluginId().equals(pbean.getId())) {
            throw ExceptionFactory.pluginNotFoundException(pluginId);
        }
        if (pdBean.getFormType() == PolicyFormType.JsonSchema && pdBean.getForm() != null) {
            String formPath = pdBean.getForm();
            if (!formPath.startsWith("/")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                formPath = "META-INF/apiman/policyDefs/" + formPath;
            } else {
                formPath = formPath.substring(1);
            }
            Plugin plugin = pluginRegistry.loadPlugin(coordinates);
            PluginClassLoader loader = plugin.getLoader();
            InputStream resource = null;
            try {
                resource = loader.getResourceAsStream(formPath);
                if (resource == null) {
                    throw ExceptionFactory.pluginResourceNotFoundException(formPath, coordinates);
                }
                StringWriter writer = new StringWriter();
                IOUtils.copy(resource, writer);
                return writer.toString();
            } finally {
                IOUtils.closeQuietly(resource);
            }
        } else {
            throw ExceptionFactory.pluginResourceNotFoundException(null, coordinates);
        }
    } catch (AbstractRestException e) {
        throw e;
    } catch (Throwable t) {
        throw new SystemErrorException(t);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PolicyDefinitionBean(io.apiman.manager.api.beans.policies.PolicyDefinitionBean) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) PluginBean(io.apiman.manager.api.beans.plugins.PluginBean) NewPluginBean(io.apiman.manager.api.beans.plugins.NewPluginBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) PluginNotFoundException(io.apiman.manager.api.rest.exceptions.PluginNotFoundException) PluginResourceNotFoundException(io.apiman.manager.api.rest.exceptions.PluginResourceNotFoundException) InvalidPluginException(io.apiman.manager.api.core.exceptions.InvalidPluginException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) IOException(java.io.IOException) PluginAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PluginAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) Plugin(io.apiman.common.plugin.Plugin) PluginClassLoader(io.apiman.common.plugin.PluginClassLoader)

Example 3 with PluginClassLoader

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

the class PolicyFactoryImpl method doLoadFromPlugin.

/**
 * Loads a policy from a plugin.
 * @param policyImpl
 * @param handler
 */
private void doLoadFromPlugin(final String policyImpl, final IAsyncResultHandler<IPolicy> handler) {
    PluginCoordinates coordinates = PluginCoordinates.fromPolicySpec(policyImpl);
    if (coordinates == null) {
        handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl)));
        return;
    }
    int ssidx = policyImpl.indexOf('/');
    if (ssidx == -1) {
        handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl)));
        return;
    }
    final String classname = policyImpl.substring(ssidx + 1);
    this.pluginRegistry.loadPlugin(coordinates, new IAsyncResultHandler<Plugin>() {

        @Override
        public void handle(IAsyncResult<Plugin> result) {
            if (result.isSuccess()) {
                IPolicy rval;
                Plugin plugin = result.getResult();
                PluginClassLoader pluginClassLoader = plugin.getLoader();
                ClassLoader oldCtxLoader = Thread.currentThread().getContextClassLoader();
                try {
                    Thread.currentThread().setContextClassLoader(pluginClassLoader);
                    Class<?> c = pluginClassLoader.loadClass(classname);
                    rval = (IPolicy) c.newInstance();
                } catch (Exception e) {
                    handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, e)));
                    return;
                } finally {
                    Thread.currentThread().setContextClassLoader(oldCtxLoader);
                }
                policyCache.put(policyImpl, rval);
                handler.handle(AsyncResultImpl.create(rval));
            } else {
                handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, result.getError())));
            }
        }
    });
}
Also used : PolicyNotFoundException(io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException) PolicyNotFoundException(io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException) PluginClassLoader(io.apiman.common.plugin.PluginClassLoader) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) Plugin(io.apiman.common.plugin.Plugin) PluginClassLoader(io.apiman.common.plugin.PluginClassLoader)

Example 4 with PluginClassLoader

use of io.apiman.common.plugin.PluginClassLoader 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 5 with PluginClassLoader

use of io.apiman.common.plugin.PluginClassLoader 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

PluginClassLoader (io.apiman.common.plugin.PluginClassLoader)6 Plugin (io.apiman.common.plugin.Plugin)4 File (java.io.File)3 IOException (java.io.IOException)3 PluginCoordinates (io.apiman.common.plugin.PluginCoordinates)2 PluginSpec (io.apiman.common.plugin.PluginSpec)2 InvalidPluginException (io.apiman.manager.api.core.exceptions.InvalidPluginException)2 URL (java.net.URL)2 PolicyNotFoundException (io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException)1 NewPluginBean (io.apiman.manager.api.beans.plugins.NewPluginBean)1 PluginBean (io.apiman.manager.api.beans.plugins.PluginBean)1 PolicyDefinitionBean (io.apiman.manager.api.beans.policies.PolicyDefinitionBean)1 StorageException (io.apiman.manager.api.core.exceptions.StorageException)1 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)1 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)1 PluginAlreadyExistsException (io.apiman.manager.api.rest.exceptions.PluginAlreadyExistsException)1 PluginNotFoundException (io.apiman.manager.api.rest.exceptions.PluginNotFoundException)1 PluginResourceNotFoundException (io.apiman.manager.api.rest.exceptions.PluginResourceNotFoundException)1 PolicyDefinitionNotFoundException (io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException)1 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)1