Search in sources :

Example 11 with InitializationException

use of org.apache.nifi.reporting.InitializationException in project nifi by apache.

the class DBCPConnectionPool method getDriverClassLoader.

/**
 * using Thread.currentThread().getContextClassLoader(); will ensure that you are using the ClassLoader for you NAR.
 *
 * @throws InitializationException
 *             if there is a problem obtaining the ClassLoader
 */
protected ClassLoader getDriverClassLoader(String locationString, String drvName) throws InitializationException {
    if (locationString != null && locationString.length() > 0) {
        try {
            // Split and trim the entries
            final ClassLoader classLoader = ClassLoaderUtils.getCustomClassLoader(locationString, this.getClass().getClassLoader(), (dir, name) -> name != null && name.endsWith(".jar"));
            // Workaround which allows to use URLClassLoader for JDBC driver loading.
            // (Because the DriverManager will refuse to use a driver not loaded by the system ClassLoader.)
            final Class<?> clazz = Class.forName(drvName, true, classLoader);
            if (clazz == null) {
                throw new InitializationException("Can't load Database Driver " + drvName);
            }
            final Driver driver = (Driver) clazz.newInstance();
            DriverManager.registerDriver(new DriverShim(driver));
            return classLoader;
        } catch (final MalformedURLException e) {
            throw new InitializationException("Invalid Database Driver Jar Url", e);
        } catch (final Exception e) {
            throw new InitializationException("Can't load Database Driver", e);
        }
    } else {
        // That will ensure that you are using the ClassLoader for you NAR.
        return Thread.currentThread().getContextClassLoader();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Driver(java.sql.Driver) InitializationException(org.apache.nifi.reporting.InitializationException) MalformedURLException(java.net.MalformedURLException) InitializationException(org.apache.nifi.reporting.InitializationException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLException(java.sql.SQLException)

Example 12 with InitializationException

use of org.apache.nifi.reporting.InitializationException in project nifi by apache.

the class CSVRecordLookupService method onEnabled.

@OnEnabled
public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException {
    this.csvFile = context.getProperty(CSV_FILE).getValue();
    this.csvFormat = CSVFormat.Predefined.valueOf(context.getProperty(CSV_FORMAT).getValue()).getFormat();
    this.lookupKeyColumn = context.getProperty(LOOKUP_KEY_COLUMN).getValue();
    this.ignoreDuplicates = context.getProperty(IGNORE_DUPLICATES).asBoolean();
    this.watcher = new SynchronousFileWatcher(Paths.get(csvFile), new LastModifiedMonitor(), 30000L);
    try {
        loadCache();
    } catch (final IllegalStateException e) {
        throw new InitializationException(e.getMessage(), e);
    }
}
Also used : SynchronousFileWatcher(org.apache.nifi.util.file.monitor.SynchronousFileWatcher) InitializationException(org.apache.nifi.reporting.InitializationException) LastModifiedMonitor(org.apache.nifi.util.file.monitor.LastModifiedMonitor) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 13 with InitializationException

use of org.apache.nifi.reporting.InitializationException in project nifi by apache.

the class SimpleCsvFileLookupService method onEnabled.

@OnEnabled
public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, FileNotFoundException {
    this.csvFile = context.getProperty(CSV_FILE).getValue();
    this.csvFormat = CSVFormat.Predefined.valueOf(context.getProperty(CSV_FORMAT).getValue()).getFormat();
    this.lookupKeyColumn = context.getProperty(LOOKUP_KEY_COLUMN).getValue();
    this.lookupValueColumn = context.getProperty(LOOKUP_VALUE_COLUMN).getValue();
    this.ignoreDuplicates = context.getProperty(IGNORE_DUPLICATES).asBoolean();
    this.watcher = new SynchronousFileWatcher(Paths.get(csvFile), new LastModifiedMonitor(), 30000L);
    try {
        loadCache();
    } catch (final IllegalStateException e) {
        throw new InitializationException(e.getMessage(), e);
    }
}
Also used : SynchronousFileWatcher(org.apache.nifi.util.file.monitor.SynchronousFileWatcher) InitializationException(org.apache.nifi.reporting.InitializationException) LastModifiedMonitor(org.apache.nifi.util.file.monitor.LastModifiedMonitor) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 14 with InitializationException

use of org.apache.nifi.reporting.InitializationException in project nifi by apache.

the class CommonsConfigurationLookupService method onEnabled.

@OnEnabled
public void onEnabled(final ConfigurationContext context) throws InitializationException {
    final String config = context.getProperty(CONFIGURATION_FILE).getValue();
    final FileBasedBuilderParameters params = new Parameters().fileBased().setFile(new File(config));
    this.builder = new ReloadingFileBasedConfigurationBuilder<>(resultClass).configure(params);
    builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, new EventListener<ConfigurationBuilderEvent>() {

        @Override
        public void onEvent(ConfigurationBuilderEvent event) {
            if (builder.getReloadingController().checkForReloading(null)) {
                getLogger().debug("Reloading " + config);
            }
        }
    });
    try {
        // Try getting configuration to see if there is any issue, for example wrong file format.
        // Then throw InitializationException to keep this service in 'Enabling' state.
        builder.getConfiguration();
    } catch (ConfigurationException e) {
        throw new InitializationException(e);
    }
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) FileBasedBuilderParameters(org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) FileBasedBuilderParameters(org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters) ReloadingFileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder) InitializationException(org.apache.nifi.reporting.InitializationException) File(java.io.File) ConfigurationBuilderEvent(org.apache.commons.configuration2.builder.ConfigurationBuilderEvent) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 15 with InitializationException

use of org.apache.nifi.reporting.InitializationException in project nifi by apache.

the class StandardSSLContextService method onConfigured.

@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {
    configContext = context;
    final Collection<ValidationResult> results = new ArrayList<>();
    results.addAll(validateStore(context.getProperties(), KeystoreValidationGroup.KEYSTORE));
    results.addAll(validateStore(context.getProperties(), KeystoreValidationGroup.TRUSTSTORE));
    if (!results.isEmpty()) {
        final StringBuilder sb = new StringBuilder(this + " is not valid due to:");
        for (final ValidationResult result : results) {
            sb.append("\n").append(result.toString());
        }
        throw new InitializationException(sb.toString());
    }
    if (countNulls(context.getProperty(KEYSTORE).getValue(), context.getProperty(KEYSTORE_PASSWORD).getValue(), context.getProperty(KEYSTORE_TYPE).getValue(), context.getProperty(TRUSTSTORE).getValue(), context.getProperty(TRUSTSTORE_PASSWORD).getValue(), context.getProperty(TRUSTSTORE_TYPE).getValue()) >= 4) {
        throw new InitializationException(this + " does not have the KeyStore or the TrustStore populated");
    }
    // verify that the filename, password, and type match
    createSSLContext(ClientAuth.REQUIRED);
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) InitializationException(org.apache.nifi.reporting.InitializationException) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Aggregations

InitializationException (org.apache.nifi.reporting.InitializationException)30 IOException (java.io.IOException)10 OnEnabled (org.apache.nifi.annotation.lifecycle.OnEnabled)7 MalformedURLException (java.net.MalformedURLException)6 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)5 SSLContextService (org.apache.nifi.ssl.SSLContextService)5 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 ProcessException (org.apache.nifi.processor.exception.ProcessException)4 StandardSSLContextService (org.apache.nifi.ssl.StandardSSLContextService)4 Test (org.junit.Test)4 ConnectException (java.net.ConnectException)3 KeyManagementException (java.security.KeyManagementException)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UnrecoverableKeyException (java.security.UnrecoverableKeyException)3 CertificateException (java.security.cert.CertificateException)3 Driver (java.sql.Driver)3 ArrayList (java.util.ArrayList)3 MockRecordParser (org.apache.nifi.serialization.record.MockRecordParser)3