Search in sources :

Example 76 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class NarThreadContextClassLoader method createInstance.

/**
 * Constructs an instance of the given type using either default no args
 * constructor or a constructor which takes a NiFiProperties object
 * (preferred).
 *
 * @param <T> the type to create an instance for
 * @param implementationClassName the implementation class name
 * @param typeDefinition the type definition
 * @param nifiProperties the NiFiProperties instance
 * @return constructed instance
 * @throws InstantiationException if there is an error instantiating the class
 * @throws IllegalAccessException if there is an error accessing the type
 * @throws ClassNotFoundException if the class cannot be found
 */
public static <T> T createInstance(final String implementationClassName, final Class<T> typeDefinition, final NiFiProperties nifiProperties) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(NarThreadContextClassLoader.getInstance());
    try {
        final List<Bundle> bundles = ExtensionManager.getBundles(implementationClassName);
        if (bundles.size() == 0) {
            throw new IllegalStateException(String.format("The specified implementation class '%s' is not known to this nifi.", implementationClassName));
        }
        if (bundles.size() > 1) {
            throw new IllegalStateException(String.format("More than one bundle was found for the specified implementation class '%s', only one is allowed.", implementationClassName));
        }
        final Bundle bundle = bundles.get(0);
        final ClassLoader detectedClassLoaderForType = bundle.getClassLoader();
        final Class<?> rawClass = Class.forName(implementationClassName, true, detectedClassLoaderForType);
        Thread.currentThread().setContextClassLoader(detectedClassLoaderForType);
        final Class<?> desiredClass = rawClass.asSubclass(typeDefinition);
        if (nifiProperties == null) {
            return typeDefinition.cast(desiredClass.newInstance());
        }
        Constructor<?> constructor = null;
        try {
            constructor = desiredClass.getConstructor(NiFiProperties.class);
        } catch (NoSuchMethodException nsme) {
            try {
                constructor = desiredClass.getConstructor();
            } catch (NoSuchMethodException nsme2) {
                throw new IllegalStateException("Failed to find constructor which takes NiFiProperties as argument as well as the default constructor on " + desiredClass.getName(), nsme2);
            }
        }
        try {
            if (constructor.getParameterTypes().length == 0) {
                return typeDefinition.cast(constructor.newInstance());
            } else {
                return typeDefinition.cast(constructor.newInstance(nifiProperties));
            }
        } catch (InvocationTargetException ite) {
            throw new IllegalStateException("Failed to instantiate a component due to (see target exception)", ite);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) Bundle(org.apache.nifi.bundle.Bundle) URLClassLoader(java.net.URLClassLoader) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 77 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class NarUnpackerTest method testUnpackNarsFromEmptyDir.

@Test
public void testUnpackNarsFromEmptyDir() throws IOException {
    final File emptyDir = new File("./target/empty/dir");
    emptyDir.delete();
    emptyDir.deleteOnExit();
    assertTrue(emptyDir.mkdirs());
    final Map<String, String> others = new HashMap<>();
    others.put("nifi.nar.library.directory.alt", emptyDir.toString());
    NiFiProperties properties = loadSpecifiedProperties("/NarUnpacker/conf/nifi.properties", others);
    final ExtensionMapping extensionMapping = NarUnpacker.unpackNars(properties, SystemBundle.create(properties));
    assertEquals(1, extensionMapping.getAllExtensionNames().size());
    assertTrue(extensionMapping.getAllExtensionNames().keySet().contains("org.apache.nifi.processors.dummy.one"));
    final File extensionsWorkingDir = properties.getExtensionsWorkingDirectory();
    File[] extensionFiles = extensionsWorkingDir.listFiles();
    assertEquals(1, extensionFiles.length);
    assertEquals("dummy-one.nar-unpacked", extensionFiles[0].getName());
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.Test)

Example 78 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class FileUserGroupProviderTest method getNiFiProperties.

private NiFiProperties getNiFiProperties(final Properties properties) {
    final NiFiProperties nifiProperties = Mockito.mock(NiFiProperties.class);
    when(nifiProperties.getPropertyKeys()).thenReturn(properties.stringPropertyNames());
    when(nifiProperties.getProperty(anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocationOnMock) throws Throwable {
            return properties.getProperty((String) invocationOnMock.getArguments()[0]);
        }
    });
    return nifiProperties;
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString)

Example 79 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class TestPersistentProvenanceRepository method createTestableRepositoryConfiguration.

private RepositoryConfiguration createTestableRepositoryConfiguration(final NiFiProperties properties) {
    Class klass = PersistentProvenanceRepository.class;
    Method createRepositoryConfigurationMethod;
    RepositoryConfiguration configuration = null;
    try {
        createRepositoryConfigurationMethod = klass.getDeclaredMethod("createRepositoryConfiguration", NiFiProperties.class);
        createRepositoryConfigurationMethod.setAccessible(true);
        configuration = (RepositoryConfiguration) createRepositoryConfigurationMethod.invoke(null, properties);
    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        e.printStackTrace();
    }
    return configuration;
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) BeforeClass(org.junit.BeforeClass) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 80 with NiFiProperties

use of org.apache.nifi.util.NiFiProperties in project nifi by apache.

the class TestRangerNiFiAuthorizer method testKerberosEnabledWithoutPrincipal.

@Test
public void testKerberosEnabledWithoutPrincipal() {
    when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_KERBEROS_ENABLED_PROP))).thenReturn(new MockPropertyValue("true"));
    nifiProperties = Mockito.mock(NiFiProperties.class);
    when(nifiProperties.getKerberosServiceKeytabLocation()).thenReturn("");
    authorizer = new MockRangerNiFiAuthorizer(rangerBasePlugin);
    authorizer.setNiFiProperties(nifiProperties);
    try {
        authorizer.onConfigured(configurationContext);
        Assert.fail("Should have thrown exception");
    } catch (AuthorizerCreationException e) {
        // want to make sure this exception is from our authorizer code
        verifyOnlyAuthorizeCreationExceptions(e);
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) Test(org.junit.Test)

Aggregations

NiFiProperties (org.apache.nifi.util.NiFiProperties)98 Test (org.junit.Test)63 HashMap (java.util.HashMap)28 Properties (java.util.Properties)24 File (java.io.File)16 Bundle (org.apache.nifi.bundle.Bundle)13 Matchers.anyString (org.mockito.Matchers.anyString)13 IOException (java.io.IOException)10 HashSet (java.util.HashSet)10 Map (java.util.Map)8 X509Certificate (java.security.cert.X509Certificate)7 Mockito.anyString (org.mockito.Mockito.anyString)7 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 SystemBundle (org.apache.nifi.nar.SystemBundle)6 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IdentityMapping (org.apache.nifi.authorization.util.IdentityMapping)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileInputStream (java.io.FileInputStream)4