Search in sources :

Example 81 with ConfigurationException

use of org.apache.commons.configuration.ConfigurationException in project sakuli by ConSol.

the class SakuliPropertyPlaceholderConfigurer method addPropertiesFromFile.

/**
 * Reads in the properties for a specific file
 *
 * @param props    Properties to update
 * @param filePath path to readable properties file
 * @param active   activate or deactivate the  function
 */
protected void addPropertiesFromFile(Properties props, String filePath, boolean active) {
    if (active) {
        logger.info("read in properties from '{}'", filePath);
        try {
            PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(filePath);
            Iterator<String> keyIt = propertiesConfiguration.getKeys();
            while (keyIt.hasNext()) {
                String key = keyIt.next();
                Object value = propertiesConfiguration.getProperty(key);
                props.put(key, value);
            }
        } catch (ConfigurationException | NullPointerException e) {
            throw new RuntimeException("Error by reading the property file '" + filePath + "'", e);
        }
    }
}
Also used : ConfigurationException(org.apache.commons.configuration.ConfigurationException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Example 82 with ConfigurationException

use of org.apache.commons.configuration.ConfigurationException in project titan by thinkaurelius.

the class GraphDatabaseConfiguration method getConfiguration.

/**
 * Load a properties file containing a Titan graph configuration or create a
 * stub configuration for a directory.
 * <p/>
 * If the argument is a file:
 * <p/>
 * <ol>
 * <li>Load its contents into a {@link PropertiesConfiguration}</li>
 * <li>For each key starting with {@link #STORAGE_NAMESPACE} and ending in
 * {@link #STORAGE_DIRECTORY_KEY} or {@link #STORAGE_CONF_FILE_KEY}, check
 * whether the associated value is a non-null, non-absolute path. If so,
 * then prepend the absolute path of the parent directory of
 * {@code dirorFile}. This has the effect of making non-absolute backend
 * paths relative to the config file's directory rather than the JVM's
 * working directory.
 * <li>Return the {@code PropertiesConfiguration}</li>
 * </ol>
 * <p/>
 * <p/>
 * Otherwise (if the argument is not a file):
 * <ol>
 * <li>Create a new {@link BaseConfiguration}</li>
 * <li>Set the key STORAGE_DIRECTORY_KEY in namespace STORAGE_NAMESPACE to
 * the absolute path of the argument</li>
 * <li>Return the {@code BaseConfiguration}</li>
 *
 * @param dirOrFile
 *            A properties file to load or directory in which to read and
 *            write data
 * @return A configuration derived from {@code dirOrFile}
 */
@SuppressWarnings("unchecked")
public static final Configuration getConfiguration(File dirOrFile) {
    Preconditions.checkNotNull(dirOrFile, "Need to specify a configuration file or storage directory");
    Configuration configuration;
    try {
        if (dirOrFile.isFile()) {
            configuration = new PropertiesConfiguration(dirOrFile);
            final File tmpParent = dirOrFile.getParentFile();
            final File configParent;
            if (null == tmpParent) {
                /*
                     * null usually means we were given a Titan config file path
                     * string like "foo.properties" that refers to the current
                     * working directory of the process.
                     */
                configParent = new File(System.getProperty("user.dir"));
            } else {
                configParent = tmpParent;
            }
            Preconditions.checkNotNull(configParent);
            Preconditions.checkArgument(configParent.isDirectory());
            final Pattern p = Pattern.compile(Pattern.quote(STORAGE_NAMESPACE) + "\\..*" + "(" + Pattern.quote(STORAGE_DIRECTORY_KEY) + "|" + Pattern.quote(STORAGE_CONF_FILE_KEY) + ")");
            final Iterator<String> keysToMangle = Iterators.filter(configuration.getKeys(), new Predicate<String>() {

                @Override
                public boolean apply(String key) {
                    if (null == key)
                        return false;
                    return p.matcher(key).matches();
                }
            });
            while (keysToMangle.hasNext()) {
                String k = keysToMangle.next();
                Preconditions.checkNotNull(k);
                String s = configuration.getString(k);
                if (null == s) {
                    log.warn("Configuration key {} has null value", k);
                    continue;
                }
                File storedir = new File(s);
                if (!storedir.isAbsolute()) {
                    configuration.setProperty(k, configParent.getAbsolutePath() + File.separator + s);
                    log.debug("Overwrote relative path for key {}: was {}, now {}", k, s, configuration.getProperty(k));
                } else {
                    log.debug("Loaded absolute path for key {}: {}", k, s);
                }
            }
        } else {
            configuration = new BaseConfiguration();
            configuration.setProperty(keyInNamespace(STORAGE_NAMESPACE, STORAGE_DIRECTORY_KEY), dirOrFile.getAbsolutePath());
        }
    } catch (ConfigurationException e) {
        throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e);
    }
    return configuration;
}
Also used : Pattern(java.util.regex.Pattern) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) Configuration(org.apache.commons.configuration.Configuration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) File(java.io.File)

Example 83 with ConfigurationException

use of org.apache.commons.configuration.ConfigurationException in project oxCore by GluuFederation.

the class FileConfiguration method loadProperties.

protected void loadProperties() {
    try {
        this.propertiesConfiguration = new PropertiesConfiguration(this.fileName);
        this.loaded = true;
    } catch (ConfigurationException ex) {
        LOG.error(String.format("Failed to load '%s' configuration file from config folder", this.fileName));
    } catch (Exception e) {
        LOG.error(String.format("Failed to load '%s' configuration file from config folder", this.fileName));
        LOG.error(e.getMessage(), e);
    }
}
Also used : ConfigurationException(org.apache.commons.configuration.ConfigurationException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) NoSuchElementException(java.util.NoSuchElementException) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Example 84 with ConfigurationException

use of org.apache.commons.configuration.ConfigurationException in project incubator-gobblin by apache.

the class CliOptions method parseArgs.

/**
 * Parse command line arguments and return a {@link java.util.Properties} object for the Gobblin job found.
 * @param caller Class of the calling main method. Used for error logs.
 * @param args Command line arguments.
 * @param conf Hadoop configuration object
 * @return Instance of {@link Properties} for the Gobblin job to run.
 * @throws IOException
 */
public static Properties parseArgs(Class<?> caller, String[] args, Configuration conf) throws IOException {
    try {
        // Parse command-line options
        if (conf != null) {
            args = new GenericOptionsParser(conf, args).getCommandLine().getArgs();
        }
        CommandLine cmd = new DefaultParser().parse(options(), args);
        if (cmd.hasOption(HELP_OPTION.getOpt())) {
            printUsage(caller);
            System.exit(0);
        }
        String jobConfigLocation = JOB_CONFIG_OPTION.getLongOpt();
        if (!cmd.hasOption(jobConfigLocation)) {
            printUsage(caller);
            System.exit(1);
        }
        // Load job configuration properties
        Properties jobConfig;
        if (conf == null) {
            jobConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(jobConfigLocation));
        } else {
            jobConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(jobConfigLocation), conf);
            for (String configKey : jobConfig.stringPropertyNames()) {
                if (conf.get(configKey) != null) {
                    conf.unset(configKey);
                }
            }
            JobConfigurationUtils.putConfigurationIntoProperties(conf, jobConfig);
        }
        return jobConfig;
    } catch (ParseException | ConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) Properties(java.util.Properties) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 85 with ConfigurationException

use of org.apache.commons.configuration.ConfigurationException in project incubator-gobblin by apache.

the class PathAlterationListenerAdaptorForMonitor method loadNewJobConfigAndHandleNewJob.

public void loadNewJobConfigAndHandleNewJob(Path path, JobScheduler.Action action) {
    // Load the new job configuration and schedule the new job
    String customizedInfo = "";
    try {
        Properties jobProps = SchedulerUtils.loadGenericJobConfig(this.jobScheduler.properties, path, jobConfigFileDirPath, this.jobSpecResolver);
        LOG.debug("Loaded job properties: {}", jobProps);
        switch(action) {
            case SCHEDULE:
                boolean runOnce = Boolean.valueOf(jobProps.getProperty(ConfigurationKeys.JOB_RUN_ONCE_KEY, "false"));
                customizedInfo = "schedule";
                addToJobNameMap(jobProps);
                jobScheduler.scheduleJob(jobProps, runOnce ? new RunOnceJobListener() : new EmailNotificationJobListener());
                break;
            case RESCHEDULE:
                customizedInfo = "reschedule";
                rescheduleJob(jobProps);
                break;
            case UNSCHEDULE:
                throw new RuntimeException("Should not call loadNewJobConfigAndHandleNewJob for unscheduling jobs.");
            default:
                break;
        }
    } catch (ConfigurationException | IOException e) {
        LOG.error("Failed to load from job configuration file " + path.toString(), e);
    } catch (JobException je) {
        LOG.error("Failed to " + customizedInfo + " new job loaded from job configuration file " + path.toString(), je);
    }
}
Also used : JobException(org.apache.gobblin.runtime.JobException) EmailNotificationJobListener(org.apache.gobblin.runtime.listeners.EmailNotificationJobListener) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IOException(java.io.IOException) Properties(java.util.Properties) RunOnceJobListener(org.apache.gobblin.runtime.listeners.RunOnceJobListener)

Aggregations

ConfigurationException (org.apache.commons.configuration.ConfigurationException)168 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)64 IOException (java.io.IOException)53 File (java.io.File)40 URL (java.net.URL)17 Configuration (org.apache.commons.configuration.Configuration)14 MalformedURLException (java.net.MalformedURLException)13 ZapXmlConfiguration (org.zaproxy.zap.utils.ZapXmlConfiguration)13 BeforeClass (org.junit.BeforeClass)10 ArrayList (java.util.ArrayList)9 ActionEvent (java.awt.event.ActionEvent)8 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)8 ActionListener (java.awt.event.ActionListener)7 FileInputStream (java.io.FileInputStream)6 FileNotFoundException (java.io.FileNotFoundException)5 HashMap (java.util.HashMap)5 Properties (java.util.Properties)5 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)4 Iterator (java.util.Iterator)4 JCommander (com.beust.jcommander.JCommander)3