Search in sources :

Example 6 with XMLConfiguration

use of org.apache.commons.configuration.XMLConfiguration in project zaproxy by zaproxy.

the class Constant method initializeFilesAndDirectories.

private void initializeFilesAndDirectories(ControlOverrides overrides) {
    FileCopier copier = new FileCopier();
    File f = null;
    // Set up the version from the manifest
    PROGRAM_VERSION = getVersionFromManifest();
    PROGRAM_TITLE = PROGRAM_NAME + " " + PROGRAM_VERSION;
    if (zapHome == null) {
        zapHome = getDefaultHomeDirectory(true);
    }
    zapHome = getAbsolutePath(zapHome);
    f = new File(zapHome);
    FILE_CONFIG = zapHome + FILE_CONFIG;
    FOLDER_SESSION = zapHome + FOLDER_SESSION;
    DBNAME_UNTITLED = zapHome + DBNAME_UNTITLED;
    DIRBUSTER_CUSTOM_DIR = zapHome + DIRBUSTER_DIR;
    FUZZER_DIR = zapHome + FUZZER_DIR;
    FOLDER_LOCAL_PLUGIN = zapHome + FOLDER_PLUGIN;
    try {
        System.setProperty(SYSTEM_PAROS_USER_LOG, zapHome);
        if (!f.isDirectory()) {
            if (f.exists()) {
                System.err.println("The home path is not a directory: " + zapHome);
                System.exit(1);
            }
            if (!f.mkdir()) {
                System.err.println("Unable to create home directory: " + zapHome);
                System.err.println("Is the path correct and there's write permission?");
                System.exit(1);
            }
        } else if (!f.canWrite()) {
            System.err.println("The home path is not writable: " + zapHome);
            System.exit(1);
        } else {
            Path installDir = Paths.get(getZapInstall()).toRealPath();
            if (installDir.equals(Paths.get(zapHome).toRealPath())) {
                System.err.println("The install dir should not be used as home dir: " + installDir);
                System.exit(1);
            }
        }
        setUpLogging();
        f = new File(FILE_CONFIG);
        if (!f.isFile()) {
            this.copyDefaultConfigs(f, false);
        }
        f = new File(FOLDER_SESSION);
        if (!f.isDirectory()) {
            LOG.info("Creating directory " + FOLDER_SESSION);
            if (!f.mkdir()) {
                // ZAP: report failure to create directory
                System.out.println("Failed to create directory " + f.getAbsolutePath());
            }
        }
        f = new File(DIRBUSTER_CUSTOM_DIR);
        if (!f.isDirectory()) {
            LOG.info("Creating directory " + DIRBUSTER_CUSTOM_DIR);
            if (!f.mkdir()) {
                // ZAP: report failure to create directory
                System.out.println("Failed to create directory " + f.getAbsolutePath());
            }
        }
        f = new File(FUZZER_DIR);
        if (!f.isDirectory()) {
            LOG.info("Creating directory " + FUZZER_DIR);
            if (!f.mkdir()) {
                // ZAP: report failure to create directory
                System.out.println("Failed to create directory " + f.getAbsolutePath());
            }
        }
        f = new File(FOLDER_LOCAL_PLUGIN);
        if (!f.isDirectory()) {
            LOG.info("Creating directory " + FOLDER_LOCAL_PLUGIN);
            if (!f.mkdir()) {
                // ZAP: report failure to create directory
                System.out.println("Failed to create directory " + f.getAbsolutePath());
            }
        }
    } catch (Exception e) {
        System.err.println("Unable to initialize home directory! " + e.getMessage());
        e.printStackTrace(System.err);
        System.exit(1);
    }
    // Upgrade actions
    try {
        try {
            // ZAP: Changed to use ZapXmlConfiguration, to enforce the same character encoding
            // when reading/writing configurations.
            XMLConfiguration config = new ZapXmlConfiguration(FILE_CONFIG);
            config.setAutoSave(false);
            long ver = config.getLong(VERSION_ELEMENT);
            if (ver == VERSION_TAG) {
            // Nothing to do
            } else if (isDevMode() || isDailyBuild()) {
            // Nothing to do
            } else {
                // Backup the old one
                LOG.info("Backing up config file to " + FILE_CONFIG + ".bak");
                f = new File(FILE_CONFIG);
                try {
                    copier.copy(f, new File(FILE_CONFIG + ".bak"));
                } catch (IOException e) {
                    String msg = "Failed to backup config file " + FILE_CONFIG + " to " + FILE_CONFIG + ".bak " + e.getMessage();
                    System.err.println(msg);
                    LOG.error(msg, e);
                }
                if (ver == V_PAROS_TAG) {
                    upgradeFrom1_1_0(config);
                    upgradeFrom1_2_0(config);
                }
                if (ver <= V_1_0_0_TAG) {
                // Nothing to do
                }
                if (ver <= V_1_1_0_TAG) {
                    upgradeFrom1_1_0(config);
                }
                if (ver <= V_1_2_0_TAG) {
                    upgradeFrom1_2_0(config);
                }
                if (ver <= V_1_2_1_TAG) {
                // Nothing to do
                }
                if (ver <= V_1_3_0_TAG) {
                // Nothing to do
                }
                if (ver <= V_1_3_1_TAG) {
                // Nothing to do
                }
                if (ver <= V_1_4_1_TAG) {
                    upgradeFrom1_4_1(config);
                }
                if (ver <= V_2_0_0_TAG) {
                    upgradeFrom2_0_0(config);
                }
                if (ver <= V_2_1_0_TAG) {
                // Nothing to do
                }
                if (ver <= V_2_2_0_TAG) {
                    upgradeFrom2_2_0(config);
                }
                if (ver <= V_2_2_2_TAG) {
                    upgradeFrom2_2_2(config);
                }
                if (ver <= V_2_3_1_TAG) {
                    upgradeFrom2_3_1(config);
                }
                if (ver <= V_2_4_3_TAG) {
                    upgradeFrom2_4_3(config);
                }
                if (ver <= V_2_5_0_TAG) {
                    upgradeFrom2_5_0(config);
                }
                if (ver <= V_2_7_0_TAG) {
                    upgradeFrom2_7_0(config);
                }
                if (ver <= V_2_8_0_TAG) {
                    upgradeFrom2_8_0(config);
                }
                if (ver <= V_2_9_0_TAG) {
                    upgradeFrom2_9_0(config);
                }
                if (ver <= V_2_10_0_TAG) {
                    upgradeFrom2_10_0(config);
                }
                if (ver <= V_2_11_1_TAG) {
                    upgradeFrom2_11_1(config);
                }
                // Execute always to pick installer choices.
                updateCfuFromDefaultConfig(config);
                LOG.info("Upgraded from " + ver);
                setLatestVersion(config);
            }
        } catch (ConfigurationException | ConversionException | NoSuchElementException e) {
            handleMalformedConfigFile(e);
        }
    } catch (Exception e) {
        System.err.println("Unable to upgrade config file " + FILE_CONFIG + " " + e.getMessage());
        e.printStackTrace(System.err);
        System.exit(1);
    }
    // ZAP: Init i18n
    Locale locale = loadLocale(overrides);
    Locale.setDefault(locale);
    messages = new I18N(locale);
}
Also used : Path(java.nio.file.Path) ConversionException(org.apache.commons.configuration.ConversionException) Locale(java.util.Locale) IOException(java.io.IOException) FileCopier(org.parosproxy.paros.model.FileCopier) URISyntaxException(java.net.URISyntaxException) ConversionException(org.apache.commons.configuration.ConversionException) InvalidParameterException(java.security.InvalidParameterException) ParseException(java.text.ParseException) NoSuchElementException(java.util.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ZapXmlConfiguration(org.zaproxy.zap.utils.ZapXmlConfiguration) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException) I18N(org.zaproxy.zap.utils.I18N)

Example 7 with XMLConfiguration

use of org.apache.commons.configuration.XMLConfiguration in project head by mifos.

the class LegacyAccountDao method dumpChartOfAccounts.

public String dumpChartOfAccounts() throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration();
    Query topLevelAccounts = getSession().getNamedQuery(NamedQueryConstants.GET_TOP_LEVEL_ACCOUNTS);
    List listAccounts = topLevelAccounts.list();
    Iterator it = listAccounts.iterator();
    LegacyAccountDao ap = new LegacyAccountDao();
    String assetsAccountGLCode = ap.getCategory(GLCategoryType.ASSET).getGlCode();
    String liabilitiesAccountGLCode = ap.getCategory(GLCategoryType.LIABILITY).getGlCode();
    String incomeAccountGLCode = ap.getCategory(GLCategoryType.INCOME).getGlCode();
    String expenditureAccountGLCode = ap.getCategory(GLCategoryType.EXPENDITURE).getGlCode();
    while (it.hasNext()) {
        COABO coa = (COABO) it.next();
        String name = coa.getAccountName();
        String glCode = coa.getAssociatedGlcode().getGlcode();
        String path = "ChartOfAccounts";
        if (glCode.equals(assetsAccountGLCode)) {
            path = path + ASSETS_GL_ACCOUNT_TAG;
        } else if (glCode.equals(liabilitiesAccountGLCode)) {
            path = path + LIABILITIES_GL_ACCOUNT_TAG;
        } else if (glCode.equals(incomeAccountGLCode)) {
            path = path + INCOME_GL_ACCOUNT_TAG;
        } else if (glCode.equals(expenditureAccountGLCode)) {
            path = path + EXPENDITURE_GL_ACCOUNT_TAG;
        } else {
            throw new RuntimeException("Unrecognized top level GLCode: " + glCode);
        }
        config.addProperty(path + "(-1)[@code]", glCode);
        config.addProperty(path + "[@name]", name);
        addAccountSubcategories(config, coa, path + GL_ACCOUNT_TAG);
    }
    StringWriter stringWriter = new StringWriter();
    config.save(stringWriter);
    String chart = stringWriter.toString();
    return chart;
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) MifosRuntimeException(org.mifos.core.MifosRuntimeException) Query(org.hibernate.Query) StringWriter(java.io.StringWriter) COABO(org.mifos.accounts.financial.business.COABO) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with XMLConfiguration

use of org.apache.commons.configuration.XMLConfiguration in project engine by craftercms.

the class MultiConfigurationBuilder method getConfiguration.

@Override
public Configuration getConfiguration() throws ConfigurationException {
    List<Configuration> configs = new ArrayList<>();
    // Last configurations should be loaded and added first so that they have greater priority.
    logger.info("Loading XML configurations in the order in which the properties will be resolved");
    for (int i = configPaths.length - 1; i >= 0; i--) {
        try {
            Resource resource = resourceLoader.getResource(configPaths[i]);
            if (resource.exists()) {
                XMLConfiguration config = new XMLConfiguration();
                config.load(resource.getInputStream());
                logger.info("XML configuration loaded from " + resource);
                configs.add(config);
            }
        } catch (Exception e) {
            throw new ConfigurationException("Unable to load configuration at " + configPaths[i], e);
        }
    }
    if (configs.size() > 1) {
        CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
        for (Configuration config : configs) {
            combinedConfig.addConfiguration((AbstractConfiguration) config);
        }
        return combinedConfig;
    } else if (configs.size() == 1) {
        return configs.get(0);
    } else {
        return null;
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) Configuration(org.apache.commons.configuration.Configuration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) OverrideCombiner(org.apache.commons.configuration.tree.OverrideCombiner)

Example 9 with XMLConfiguration

use of org.apache.commons.configuration.XMLConfiguration in project incubator-rya by apache.

the class ToolConfigUtils method getUserArguments.

/**
 * Gets the set of user arguments from the user's config and/or their extra supplied
 * command line arguments.  This weeds out all the automatically generated parameters created
 * from initializing a {@link Configuration} object and should only give back a set of arguments
 * provided directly by the user.
 * @param conf the {@link Configuration} provided.
 * @param args the extra arguments from the command line.
 * @return a {@link Set} of argument strings.
 * @throws IOException
 */
public static Set<String> getUserArguments(final Configuration conf, final String[] args) throws IOException {
    String[] filteredArgs = new String[] {};
    if (Arrays.asList(args).contains("-conf")) {
        // parse args
        new GenericOptionsParser(conf, args);
        final List<String> commandLineArgs = new ArrayList<>();
        for (final String arg : args) {
            if (arg.startsWith("-D")) {
                commandLineArgs.add(arg);
            }
        }
        filteredArgs = commandLineArgs.toArray(new String[0]);
    } else {
        filteredArgs = args;
    }
    // Get the supplied config name from the resource string.
    // No real easy way of getting the name.
    // So, pulling it off the list of resource names in the Configuration's toString() method
    // where it should be the last one.
    final String confString = conf.toString();
    final String resourceString = StringUtils.removeStart(confString, "Configuration: ");
    final List<String> resourceNames = Arrays.asList(StringUtils.split(resourceString, ", "));
    final String configFilename = resourceNames.get(resourceNames.size() - 1);
    final Set<String> toolArgsSet = new HashSet<>();
    final File file = new File(configFilename);
    // should happen if no config is supplied.
    if (file.exists()) {
        XMLConfiguration configuration = null;
        try {
            configuration = new XMLConfiguration(configFilename);
            toolArgsSet.addAll(getConfigArguments(configuration));
        } catch (final ConfigurationException e) {
            log.error("Unable to load configuration file.", e);
        }
    }
    toolArgsSet.addAll(Arrays.asList(filteredArgs));
    return Collections.unmodifiableSet(toolArgsSet);
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ArrayList(java.util.ArrayList) File(java.io.File) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser) HashSet(java.util.HashSet)

Example 10 with XMLConfiguration

use of org.apache.commons.configuration.XMLConfiguration in project tmdm-common by Talend.

the class EncryptUtil method encyptXML.

public static void encyptXML(String location) {
    try {
        File file = new File(location);
        if (file.exists()) {
            XMLConfiguration config = new XMLConfiguration();
            config.setDelimiterParsingDisabled(true);
            config.load(file);
            // $NON-NLS-1$
            List<Object> dataSources = config.getList("datasource.[@name]");
            int index = -1;
            for (int i = 0; i < dataSources.size(); i++) {
                if (dataSources.get(i).equals(dataSourceName)) {
                    index = i;
                    break;
                }
            }
            updated = false;
            if (index >= 0) {
                // $NON-NLS-1$//$NON-NLS-2$
                HierarchicalConfiguration sub = config.configurationAt("datasource(" + index + ")");
                // $NON-NLS-1$
                encryptByXpath(sub, "master.rdbms-configuration.connection-password");
                // $NON-NLS-1$
                encryptByXpath(sub, "master.rdbms-configuration.init.connection-password");
                // $NON-NLS-1$
                encryptByXpath(sub, "staging.rdbms-configuration.connection-password");
                // $NON-NLS-1$
                encryptByXpath(sub, "staging.rdbms-configuration.init.connection-password");
                // $NON-NLS-1$
                encryptByXpath(sub, "system.rdbms-configuration.connection-password");
                // $NON-NLS-1$
                encryptByXpath(sub, "system.rdbms-configuration.init.connection-password");
            }
            if (updated) {
                config.save(file);
            }
        }
    } catch (Exception e) {
        // $NON-NLS-1$ //$NON-NLS-2$
        LOGGER.error("Encrypt password in '" + location + "' error.");
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) File(java.io.File)

Aggregations

XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)14 ConfigurationException (org.apache.commons.configuration.ConfigurationException)6 File (java.io.File)5 ZapXmlConfiguration (org.zaproxy.zap.utils.ZapXmlConfiguration)5 IOException (java.io.IOException)3 InvalidParameterException (java.security.InvalidParameterException)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 Locale (java.util.Locale)3 NoSuchElementException (java.util.NoSuchElementException)3 ConversionException (org.apache.commons.configuration.ConversionException)3 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Iterator (java.util.Iterator)2 ConfigurationNode (org.apache.commons.configuration.tree.ConfigurationNode)2 FileCopier (org.parosproxy.paros.model.FileCopier)2 I18N (org.zaproxy.zap.utils.I18N)2 ConfigTest (edu.uiuc.ncsa.security.core.configuration.ConfigTest)1 MyConfigurationException (edu.uiuc.ncsa.security.core.exceptions.MyConfigurationException)1