Search in sources :

Example 1 with EndpointPluginContext

use of co.cask.cdap.api.plugin.EndpointPluginContext in project cdap by caskdata.

the class PluginService method getPluginEndpoint.

/**
   * load and instantiate the plugin and return {@link PluginEndpoint}
   * which can be used to invoke plugin method with request object.
   *
   * @param pluginInstantiator to instantiate plugin instances.
   * @param artifact artifact of the plugin
   * @param pluginClass class having the plugin method to invoke
   * @param endpointName name of the endpoint to invoke
   * @param endpointPluginContext endpoint plugin context that can optionally be passed to the method
   * @throws IOException if there was an exception getting the classloader
   * @throws ClassNotFoundException if plugin class cannot be loaded
   * @throws NotFoundException Not Found exception thrown if no matching plugin found.
   */
private PluginEndpoint getPluginEndpoint(final PluginInstantiator pluginInstantiator, Id.Artifact artifact, PluginClass pluginClass, String endpointName, final DefaultEndpointPluginContext endpointPluginContext) throws IOException, ClassNotFoundException, NotFoundException {
    ClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifact.toArtifactId());
    Class pluginClassLoaded = pluginClassLoader.loadClass(pluginClass.getClassName());
    final Object pluginInstance = pluginInstantiator.newInstanceWithoutConfig(artifact.toArtifactId(), pluginClass);
    Method[] methods = pluginClassLoaded.getMethods();
    for (final Method method : methods) {
        Path pathAnnotation = method.getAnnotation(Path.class);
        // method should have path annotation else continue
        if (pathAnnotation == null || !pathAnnotation.value().equals(endpointName)) {
            continue;
        }
        return new PluginEndpoint() {

            @Override
            public java.lang.reflect.Type getMethodParameterType() throws IllegalArgumentException {
                if (method.getParameterTypes().length == 0) {
                    // should not happen, checks should have happened during deploy artifact.
                    throw new IllegalArgumentException("No Method parameter type found");
                }
                return method.getGenericParameterTypes()[0];
            }

            @Override
            public java.lang.reflect.Type getResultType() {
                return method.getGenericReturnType();
            }

            @Override
            public Object invoke(Object request) throws IOException, ClassNotFoundException, InvocationTargetException, IllegalAccessException, IllegalArgumentException {
                if (method.getParameterTypes().length == 2 && EndpointPluginContext.class.isAssignableFrom(method.getParameterTypes()[1])) {
                    return method.invoke(pluginInstance, request, endpointPluginContext);
                } else if (method.getParameterTypes().length == 1) {
                    return method.invoke(pluginInstance, request);
                } else {
                    throw new IllegalArgumentException(String.format("Only method with 1 parameter and optional EndpointPluginContext as 2nd parameter is " + "allowed in Plugin endpoint method, Found %s parameters", method.getParameterTypes().length));
                }
            }
        };
    }
    ;
    // cannot find the endpoint in plugin method. should not happen as this is checked earlier.
    throw new NotFoundException("Could not find the plugin method with the requested method endpoint {}", endpointName);
}
Also used : Path(javax.ws.rs.Path) EndpointPluginContext(co.cask.cdap.api.plugin.EndpointPluginContext) DefaultEndpointPluginContext(co.cask.cdap.internal.app.runtime.DefaultEndpointPluginContext) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) PluginClass(co.cask.cdap.api.plugin.PluginClass) Method(java.lang.reflect.Method)

Aggregations

CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)1 EndpointPluginContext (co.cask.cdap.api.plugin.EndpointPluginContext)1 PluginClass (co.cask.cdap.api.plugin.PluginClass)1 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 DefaultEndpointPluginContext (co.cask.cdap.internal.app.runtime.DefaultEndpointPluginContext)1 Method (java.lang.reflect.Method)1 Path (javax.ws.rs.Path)1