Search in sources :

Example 1 with Section

use of org.apache.hyracks.api.config.Section in project asterixdb by apache.

the class ConfigUsageTest method generateUsage.

public void generateUsage(String startDelim, String midDelim, String endDelim, EnumMap<Column, Boolean> align, PrintStream output) {
    ConfigManager configManager = getConfigManager();
    StringBuilder buf = new StringBuilder();
    final Column[] columns = Column.values();
    for (Section section : getSections(configManager)) {
        for (IOption option : getSectionOptions(configManager, section)) {
            for (Column column : columns) {
                if (align.computeIfAbsent(column, c -> false)) {
                    calculateMaxWidth(option, column);
                }
            }
        }
    }
    // output header
    for (Column column : columns) {
        buf.append(column.ordinal() == 0 ? startDelim : midDelim);
        pad(buf, StringUtils.capitalize(column.name().toLowerCase()), align.computeIfAbsent(column, c -> false) ? calculateMaxWidth(column, column.name()) : 0);
    }
    buf.append(endDelim).append('\n');
    StringBuilder sepLine = new StringBuilder();
    for (Column column : columns) {
        sepLine.append(column.ordinal() == 0 ? startDelim : midDelim);
        pad(sepLine, "", maxWidths.getOrDefault(column, 0), '-');
    }
    sepLine.append(endDelim).append('\n');
    buf.append(sepLine.toString().replace(' ', '-'));
    for (Section section : getSections(configManager)) {
        List<IOption> options = new ArrayList<>(getSectionOptions(configManager, section));
        options.sort(Comparator.comparing(IOption::ini));
        for (IOption option : options) {
            for (Column column : columns) {
                buf.append(column.ordinal() == 0 ? startDelim : midDelim);
                if (column == Column.SECTION) {
                    center(buf, extractValue(column, option), maxWidths.getOrDefault(column, 0));
                } else {
                    pad(buf, extractValue(column, option), maxWidths.getOrDefault(column, 0));
                }
            }
            buf.append(endDelim).append('\n');
        }
    }
    output.println(buf);
}
Also used : IOption(org.apache.hyracks.api.config.IOption) ArrayList(java.util.ArrayList) Section(org.apache.hyracks.api.config.Section) ConfigManager(org.apache.hyracks.control.common.config.ConfigManager)

Example 2 with Section

use of org.apache.hyracks.api.config.Section in project asterixdb by apache.

the class ConfigManager method parseIni.

private void parseIni() throws IOException {
    Ini ini = null;
    for (IOption option : iniPointerOptions) {
        Object pointer = get(option);
        if (pointer instanceof String) {
            ini = ConfigUtils.loadINIFile((String) pointer);
        } else if (pointer instanceof URL) {
            ini = ConfigUtils.loadINIFile((URL) pointer);
        } else if (pointer != null) {
            throw new IllegalArgumentException("config file pointer options must be of type String (for file) or " + "URL, instead of " + option.type().targetType());
        }
    }
    if (ini == null) {
        LOGGER.info("no INI file specified; skipping parsing");
        return;
    }
    LOGGER.info("parsing INI file: " + ini);
    for (Profile.Section section : ini.values()) {
        allSections.add(section.getName());
        final Section rootSection = Section.parseSectionName(section.getParent() == null ? section.getName() : section.getParent().getName());
        String node;
        if (rootSection == Section.EXTENSION) {
            parseExtensionIniSection(section);
            continue;
        } else if (rootSection == Section.NC) {
            node = section.getName().equals(section.getSimpleName()) ? null : section.getSimpleName();
        } else if (Section.parseSectionName(section.getName()) != null) {
            node = null;
        } else {
            throw new HyracksException("Unknown section in ini: " + section.getName());
        }
        Map<String, IOption> optionMap = getSectionOptionMap(rootSection);
        for (Map.Entry<String, String> iniOption : section.entrySet()) {
            String name = iniOption.getKey();
            final IOption option = optionMap == null ? null : optionMap.get(name);
            if (option == null) {
                handleUnknownOption(section, name);
                return;
            }
            final String value = iniOption.getValue();
            LOGGER.fine("setting " + option.toIniString() + " to " + value);
            final Object parsed = option.type().parse(value);
            invokeSetters(option, parsed, node);
        }
    }
}
Also used : Ini(org.ini4j.Ini) IOption(org.apache.hyracks.api.config.IOption) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) Section(org.apache.hyracks.api.config.Section) URL(java.net.URL) Profile(org.ini4j.Profile) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) CompositeMap(org.apache.commons.collections4.map.CompositeMap) SortedMap(java.util.SortedMap)

Example 3 with Section

use of org.apache.hyracks.api.config.Section in project asterixdb by apache.

the class ConfigManager method getOptionNames.

public Set<String> getOptionNames(String sectionName) {
    Set<String> optionNames = new HashSet<>();
    Section section = Section.parseSectionName(sectionName);
    for (IOption option : getSectionOptionMap(section).values()) {
        optionNames.add(option.ini());
    }
    return optionNames;
}
Also used : IOption(org.apache.hyracks.api.config.IOption) Section(org.apache.hyracks.api.config.Section) HashSet(java.util.HashSet)

Example 4 with Section

use of org.apache.hyracks.api.config.Section in project asterixdb by apache.

the class ConfigManager method processCommandLine.

// use of System.err, System.exit()
@SuppressWarnings({ "squid:S106", "squid:S1147" })
private List<String> processCommandLine(Collection<Section> sections, OptionHandlerFilter usageFilter, BiConsumer<IOption, Object> setAction) throws CmdLineException {
    final Args4jBean bean = new Args4jBean();
    CmdLineParser cmdLineParser = new CmdLineParser(bean);
    final List<String> appArgs = new ArrayList<>();
    List<IOption> commandLineOptions = new ArrayList<>();
    for (Map.Entry<Section, Map<String, IOption>> sectionMapEntry : sectionMap.entrySet()) {
        if (!sections.contains(sectionMapEntry.getKey())) {
            continue;
        }
        for (IOption option : sectionMapEntry.getValue().values()) {
            if (option.section() != Section.VIRTUAL) {
                commandLineOptions.add(option);
            }
        }
    }
    commandLineOptions.sort(Comparator.comparing(IOption::cmdline));
    commandLineOptions.forEach(option -> cmdLineParser.addOption(new Args4jSetter(option, setAction, false), new Args4jOption(option, this, option.type().targetType())));
    if (!argListeners.isEmpty()) {
        cmdLineParser.addArgument(new Args4jSetter(o -> appArgs.add(String.valueOf(o)), true, String.class), new Args4jArgument());
    }
    LOGGER.fine("parsing cmdline: " + Arrays.toString(args));
    if (args == null || args.length == 0) {
        LOGGER.info("no command line args supplied");
        return appArgs;
    }
    try {
        cmdLineParser.parseArgument(args);
    } catch (CmdLineException e) {
        if (!bean.help) {
            ConfigUtils.printUsage(e, usageFilter, System.err);
            throw e;
        } else {
            LOGGER.log(Level.FINE, "Ignoring parse exception due to -help", e);
        }
    }
    if (bean.help) {
        ConfigUtils.printUsage(cmdLineParser, usageFilter, System.err);
        System.exit(0);
    } else if (bean.version) {
        System.err.println(versionString);
        System.exit(0);
    }
    return appArgs;
}
Also used : Arrays(java.util.Arrays) URL(java.net.URL) ConfigManagerApplicationConfig(org.apache.hyracks.control.common.application.ConfigManagerApplicationConfig) HashMap(java.util.HashMap) IConfigurator(org.apache.hyracks.api.config.IConfigurator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) IOption(org.apache.hyracks.api.config.IOption) HashSet(java.util.HashSet) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) CmdLineParser(org.kohsuke.args4j.CmdLineParser) OptionHandlerFilter(org.kohsuke.args4j.OptionHandlerFilter) Section(org.apache.hyracks.api.config.Section) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IApplicationConfig(org.apache.hyracks.api.config.IApplicationConfig) Set(java.util.Set) IOException(java.io.IOException) Option(org.kohsuke.args4j.Option) Logger(java.util.logging.Logger) Ini(org.ini4j.Ini) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) CmdLineException(org.kohsuke.args4j.CmdLineException) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) CompositeMap(org.apache.commons.collections4.map.CompositeMap) Profile(org.ini4j.Profile) IConfigManager(org.apache.hyracks.api.config.IConfigManager) Comparator(java.util.Comparator) Collections(java.util.Collections) SortedMap(java.util.SortedMap) CmdLineParser(org.kohsuke.args4j.CmdLineParser) ArrayList(java.util.ArrayList) IOption(org.apache.hyracks.api.config.IOption) Section(org.apache.hyracks.api.config.Section) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) CompositeMap(org.apache.commons.collections4.map.CompositeMap) SortedMap(java.util.SortedMap) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

IOption (org.apache.hyracks.api.config.IOption)4 Section (org.apache.hyracks.api.config.Section)4 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 EnumMap (java.util.EnumMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 CompositeMap (org.apache.commons.collections4.map.CompositeMap)2 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)2 HyracksException (org.apache.hyracks.api.exceptions.HyracksException)2 Ini (org.ini4j.Ini)2 Profile (org.ini4j.Profile)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1