Example usage for org.apache.commons.configuration2 Configuration subset

List of usage examples for org.apache.commons.configuration2 Configuration subset

Introduction

In this page you can find the example usage for org.apache.commons.configuration2 Configuration subset.

Prototype

Configuration subset(String prefix);

Source Link

Document

Return a decorator Configuration containing every key from the current Configuration that starts with the specified prefix.

Usage

From source file:io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder.java

public Swagger2MarkupConfigBuilder(Configuration configuration) {
    CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
    compositeConfiguration.addConfiguration(new SystemConfiguration());
    compositeConfiguration.addConfiguration(configuration);
    compositeConfiguration.addConfiguration(getDefaultConfiguration());

    Swagger2MarkupProperties swagger2MarkupProperties = new Swagger2MarkupProperties(compositeConfiguration);

    config.markupLanguage = swagger2MarkupProperties.getRequiredMarkupLanguage(MARKUP_LANGUAGE);
    config.swaggerMarkupLanguage = swagger2MarkupProperties.getRequiredMarkupLanguage(SWAGGER_MARKUP_LANGUAGE);
    config.generatedExamplesEnabled = swagger2MarkupProperties.getRequiredBoolean(GENERATED_EXAMPLES_ENABLED);
    config.basePathPrefixEnabled = swagger2MarkupProperties.getRequiredBoolean(BASE_PATH_PREFIX_ENABLED);
    config.separatedDefinitionsEnabled = swagger2MarkupProperties
            .getRequiredBoolean(SEPARATED_DEFINITIONS_ENABLED);
    config.separatedOperationsEnabled = swagger2MarkupProperties
            .getRequiredBoolean(SEPARATED_OPERATIONS_ENABLED);
    config.pathsGroupedBy = swagger2MarkupProperties.getGroupBy(PATHS_GROUPED_BY);
    config.outputLanguage = swagger2MarkupProperties.getLanguage(OUTPUT_LANGUAGE);
    config.inlineSchemaEnabled = swagger2MarkupProperties.getRequiredBoolean(INLINE_SCHEMA_ENABLED);
    config.interDocumentCrossReferencesEnabled = swagger2MarkupProperties
            .getRequiredBoolean(INTER_DOCUMENT_CROSS_REFERENCES_ENABLED);
    config.interDocumentCrossReferencesPrefix = swagger2MarkupProperties
            .getString(INTER_DOCUMENT_CROSS_REFERENCES_PREFIX, null);
    config.flatBodyEnabled = swagger2MarkupProperties.getRequiredBoolean(FLAT_BODY_ENABLED);
    config.pathSecuritySectionEnabled = swagger2MarkupProperties
            .getRequiredBoolean(PATH_SECURITY_SECTION_ENABLED);
    config.anchorPrefix = swagger2MarkupProperties.getString(ANCHOR_PREFIX, null);
    config.overviewDocument = swagger2MarkupProperties.getRequiredString(OVERVIEW_DOCUMENT);
    config.pathsDocument = swagger2MarkupProperties.getRequiredString(PATHS_DOCUMENT);
    config.definitionsDocument = swagger2MarkupProperties.getRequiredString(DEFINITIONS_DOCUMENT);
    config.securityDocument = swagger2MarkupProperties.getRequiredString(SECURITY_DOCUMENT);
    config.separatedOperationsFolder = swagger2MarkupProperties.getRequiredString(SEPARATED_OPERATIONS_FOLDER);
    config.separatedDefinitionsFolder = swagger2MarkupProperties
            .getRequiredString(SEPARATED_DEFINITIONS_FOLDER);
    config.tagOrderBy = swagger2MarkupProperties.getOrderBy(TAG_ORDER_BY);
    config.operationOrderBy = swagger2MarkupProperties.getOrderBy(OPERATION_ORDER_BY);
    config.definitionOrderBy = swagger2MarkupProperties.getOrderBy(DEFINITION_ORDER_BY);
    config.parameterOrderBy = swagger2MarkupProperties.getOrderBy(PARAMETER_ORDER_BY);
    config.propertyOrderBy = swagger2MarkupProperties.getOrderBy(PROPERTY_ORDER_BY);
    config.responseOrderBy = swagger2MarkupProperties.getOrderBy(RESPONSE_ORDER_BY);
    Optional<String> lineSeparator = swagger2MarkupProperties.getString(LINE_SEPARATOR);
    if (lineSeparator.isPresent() && StringUtils.isNoneBlank(lineSeparator.get())) {
        config.lineSeparator = LineSeparator.valueOf(lineSeparator.get());
    }//from   w ww. j a va  2  s. c  o  m

    config.pageBreakLocations = swagger2MarkupProperties.getPageBreakLocations(PAGE_BREAK_LOCATIONS);

    Optional<Pattern> headerPattern = swagger2MarkupProperties.getHeaderPattern(HEADER_REGEX);

    config.headerPattern = headerPattern.orElse(null);

    Configuration swagger2markupConfiguration = compositeConfiguration.subset(PROPERTIES_PREFIX);
    Configuration extensionsConfiguration = swagger2markupConfiguration.subset(EXTENSION_PREFIX);
    config.extensionsProperties = new Swagger2MarkupProperties(extensionsConfiguration);
}

From source file:org.powertac.common.config.Configurator.java

/**
 * Creates and configures instances of the given class. In the configuration,
 * we expect to see a property of the form pkg.class.instances = a, b, c, ...
 * where a, b, c, etc. are the names of instances of the class to be created
 * and configured. These names are provided to the instances through their
 * constructors, and are expected to show up in the configuration as
 * instance names in clauses of the form
 * pkg.class.name.property = value.//w ww.j  ava 2 s  .  c om
 * Returns empty list in case instances cannot be created.
 */
public Collection<?> configureInstances(Class<?> type) {
    // If we don't have a configuration, we cannot do much.
    if (config == null) {
        log.error("Cannot configure - no Configuration set");
        return new ArrayList<Object>();
    }

    // compute the key for the instance list
    String classname = type.getName();
    log.debug("configuring instances for type " + classname);
    Configuration subset = extractSubsetForClassname(classname);
    // we should have a clause with the key "instances" giving the item
    // names, and a set of clauses for each item
    if (null == createdInstances.get(type)) {
        createdInstances.put(type, new HashSet<>());
    }
    Set<String> existingNames = createdInstances.get(type);
    List<Object> rawNames = subset.getList("instances");
    List<String> names = rawNames.stream().map(n -> n.toString()).filter(n -> !n.isEmpty())
            .collect(Collectors.toList());
    if (names.size() == 0) {
        log.warn("No instance names specified for class " + classname);
        return names;
    }
    dumpInstanceListMaybe(type, existingNames, names);

    // for each name, create an instance, add it to the result, and
    // configure it.
    LinkedHashMap<String, Object> itemMap = new LinkedHashMap<String, Object>();
    for (String name : names) {
        existingNames.add(name);
        try {
            Constructor<?> constructor = type.getConstructor(String.class);
            Object item = constructor.newInstance(name);
            itemMap.put(name, item);
        } catch (Exception e) {
            log.error("Unable to create instance " + name + " of class " + type + ": " + e.toString());
        }
    }
    // We now have a map of names and items to be configured.
    // We iterate through the map to avoid problems with items that did
    // not get created, although that seems far-fetched.
    for (String name : itemMap.keySet()) {
        configureInstance(itemMap.get(name), subset.subset(name), name);
    }
    return itemMap.values();
}

From source file:org.powertac.common.config.Configurator.java

/**
 * Configures a set of instances of some class. In the configuration,
 * we expect to see properties of the form
 * pkg.class.name.property = value/*from   w ww .  ja va 2s  .  c  om*/
 * where name is the name of an instance in the list to be
 * configured. These names must be accessible through a getName() method
 * on each instance.
 * It is an error if instances are not of the same class; in fact, the
 * class of the first instance in the list is used to form the pkg.class.
 * It also does not work for types that lack a getName() method returning
 * a String, or if the name does not match the name in the properties.
 * portion of the property names.
 */
public Collection<?> configureNamedInstances(List<?> instances) {
    // If we don't have a configuration, we cannot do much.
    if (config == null) {
        log.error("Cannot configure - no Configuration set");
        return null;
    }

    // We need a non-empty list of instances for this to work
    if (null == instances || 0 == instances.size()) {
        log.error("Cannot configure empty instance list");
        return null;
    }

    // compute the key for the instance list from the first element of the list
    String classname = instances.get(0).getClass().getName();
    log.debug("configuring instances for type " + classname);
    Configuration subset = extractSubsetForClassname(classname);

    // for each given instance, get it's name and configure it
    try {
        Method getNameMethod = instances.get(0).getClass().getMethod("getName");
        for (Object item : instances) {
            Object result = getNameMethod.invoke(item);
            String name = (String) result;
            configureInstance(item, subset.subset(name), name);
        }
    } catch (Exception e) {
        log.error("Could not get name of item");
        return null;
    }
    return instances;
}