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);
}
}
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());
}
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;
}
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;
}
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);
}
}
Aggregations