Java Code Examples for org.apache.nifi.nar.ExtensionManager#getClassLoader()

The following examples show how to use org.apache.nifi.nar.ExtensionManager#getClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardStateManagerProvider.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private static StateProvider instantiateStateProvider(final String type) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoaderForType = ExtensionManager.getClassLoader(type);
        final Class<?> rawClass;
        if (detectedClassLoaderForType == null) {
            // try to find from the current class loader
            rawClass = Class.forName(type);
        } else {
            // try to find from the registered classloader for that type
            rawClass = Class.forName(type, true, ExtensionManager.getClassLoader(type));
        }

        Thread.currentThread().setContextClassLoader(detectedClassLoaderForType);
        final Class<? extends StateProvider> mgrClass = rawClass.asSubclass(StateProvider.class);
        return mgrClass.newInstance();
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}
 
Example 2
Source File: AuthorizerFactoryBean.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private Authorizer createAuthorizer(final String identifier, final String authorizerClassName) throws Exception {
    // get the classloader for the specified authorizer
    final ClassLoader authorizerClassLoader = ExtensionManager.getClassLoader(authorizerClassName);
    if (authorizerClassLoader == null) {
        throw new Exception(String.format("The specified authorizer class '%s' is not known to this nifi.", authorizerClassName));
    }

    // get the current context classloader
    final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();

    final Authorizer instance;
    try {
        // set the appropriate class loader
        Thread.currentThread().setContextClassLoader(authorizerClassLoader);

        // attempt to load the class
        Class<?> rawAuthorizerClass = Class.forName(authorizerClassName, true, authorizerClassLoader);
        Class<? extends Authorizer> authorizerClass = rawAuthorizerClass.asSubclass(Authorizer.class);

        // otherwise create a new instance
        Constructor constructor = authorizerClass.getConstructor();
        instance = (Authorizer) constructor.newInstance();

        // method injection
        performMethodInjection(instance, authorizerClass);

        // field injection
        performFieldInjection(instance, authorizerClass);

        // call post construction lifecycle event
        instance.initialize(new StandardAuthorizerInitializationContext(identifier, this));
    } finally {
        if (currentClassLoader != null) {
            Thread.currentThread().setContextClassLoader(currentClassLoader);
        }
    }

    return withNarLoader(instance);
}
 
Example 3
Source File: FlowController.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private Processor instantiateProcessor(final String type, final String identifier) throws ProcessorInstantiationException {
    Processor processor;

    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoaderForType = ExtensionManager.getClassLoader(type, identifier);
        final Class<?> rawClass;
        if (detectedClassLoaderForType == null) {
            // try to find from the current class loader
            rawClass = Class.forName(type);
        } else {
            // try to find from the registered classloader for that type
            rawClass = Class.forName(type, true, ExtensionManager.getClassLoader(type, identifier));
        }

        Thread.currentThread().setContextClassLoader(detectedClassLoaderForType);
        final Class<? extends Processor> processorClass = rawClass.asSubclass(Processor.class);
        processor = processorClass.newInstance();
        final ComponentLog componentLogger = new SimpleProcessLogger(identifier, processor);
        final ProcessorInitializationContext ctx = new StandardProcessorInitializationContext(identifier, componentLogger, this, this, nifiProperties);
        processor.initialize(ctx);

        LogRepositoryFactory.getRepository(identifier).setLogger(componentLogger);
        return processor;
    } catch (final Throwable t) {
        throw new ProcessorInstantiationException(type, t);
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}
 
Example 4
Source File: FlowController.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public FlowFilePrioritizer createPrioritizer(final String type) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    FlowFilePrioritizer prioritizer;

    final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader detectedClassLoaderForType = ExtensionManager.getClassLoader(type);
        final Class<?> rawClass;
        if (detectedClassLoaderForType == null) {
            // try to find from the current class loader
            rawClass = Class.forName(type);
        } else {
            // try to find from the registered classloader for that type
            rawClass = Class.forName(type, true, ExtensionManager.getClassLoader(type));
        }

        Thread.currentThread().setContextClassLoader(detectedClassLoaderForType);
        final Class<? extends FlowFilePrioritizer> prioritizerClass = rawClass.asSubclass(FlowFilePrioritizer.class);
        final Object processorObj = prioritizerClass.newInstance();
        prioritizer = prioritizerClass.cast(processorObj);

        return prioritizer;
    } finally {
        if (ctxClassLoader != null) {
            Thread.currentThread().setContextClassLoader(ctxClassLoader);
        }
    }
}