Search in sources :

Example 1 with PolicyNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException 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 2 with PolicyNotFoundException

use of io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException in project apiman by apiman.

the class PolicyFactoryImpl method doLoadFromClasspath.

/**
 * Loads a policy from a class on the classpath.
 * @param policyImpl
 * @param handler
 */
protected void doLoadFromClasspath(String policyImpl, IAsyncResultHandler<IPolicy> handler) {
    IPolicy rval;
    String classname = policyImpl.substring(6);
    Class<?> c = null;
    // First try a simple Class.forName()
    try {
        c = Class.forName(classname);
    } catch (ClassNotFoundException e) {
    }
    // Didn't work?  Try using this class's classloader.
    if (c == null) {
        try {
            c = getClass().getClassLoader().loadClass(classname);
        } catch (ClassNotFoundException e) {
        }
    }
    // Still didn't work?  Try the thread's context classloader.
    if (c == null) {
        try {
            c = Thread.currentThread().getContextClassLoader().loadClass(classname);
        } catch (ClassNotFoundException e) {
        }
    }
    if (c == null) {
        handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(classname)));
        return;
    }
    try {
        rval = (IPolicy) c.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        handler.handle(AsyncResultImpl.<IPolicy>create(new PolicyNotFoundException(policyImpl, e)));
        return;
    }
    policyCache.put(policyImpl, rval);
    handler.handle(AsyncResultImpl.create(rval));
    return;
}
Also used : PolicyNotFoundException(io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException)

Aggregations

PolicyNotFoundException (io.apiman.gateway.engine.beans.exceptions.PolicyNotFoundException)2 Plugin (io.apiman.common.plugin.Plugin)1 PluginClassLoader (io.apiman.common.plugin.PluginClassLoader)1 PluginCoordinates (io.apiman.common.plugin.PluginCoordinates)1