Search in sources :

Example 6 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dhis2-core by dhis2.

the class DefaultDhisConfigurationProvider method substituteEnvironmentVariables.

private void substituteEnvironmentVariables(Properties properties) {
    // Matches on ${...}
    final StringSubstitutor substitutor = new StringSubstitutor(System.getenv());
    properties.entrySet().forEach(entry -> entry.setValue(substitutor.replace(entry.getValue()).trim()));
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Example 7 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project midpoint by Evolveum.

the class ModelDiagController method getMemoryInformation.

@Override
public String getMemoryInformation(Task task, OperationResult parentResult) throws CommunicationException, ObjectNotFoundException, SchemaException, SecurityViolationException, ConfigurationException, ExpressionEvaluationException, IOException {
    OperationResult result = parentResult.createSubresult(GET_MEMORY_INFORMATION);
    try {
        securityEnforcer.authorize(AuthorizationConstants.AUTZ_ALL_URL, null, AuthorizationParameters.EMPTY, null, task, result);
        String pid = SystemUtil.getMyPid();
        if (pid == null) {
            result.recordPartialError("Cannot determine current process ID");
            return "Cannot determine current process ID";
        }
        StringSubstitutor substitutor = createCommandStringSubstitutor(pid);
        PrismObject<SystemConfigurationType> configuration = systemObjectCache.getSystemConfiguration(result);
        StringBuilder sb = new StringBuilder();
        String heapInfoCommand = getHeapInfoCommand(configuration, substitutor);
        sb.append("Getting heap information via: ").append(heapInfoCommand).append("\n\n");
        boolean finished1 = SystemUtil.executeCommand(heapInfoCommand, "", sb, HEAP_DIAG_TIMEOUT);
        if (!finished1) {
            sb.append("\nBeware: the command execution has not finished in given time limit.\n\n");
        } else {
            String histogramCommand = getHeapObjectsHistogramCommand(configuration, substitutor);
            sb.append("\n------------------------------------------------------------------------------------------------------------\n\n");
            sb.append("Getting heap object histogram via: ").append(histogramCommand).append("\n\n");
            boolean finished2 = SystemUtil.executeCommand(histogramCommand, "", sb, HEAP_DIAG_TIMEOUT);
            if (!finished2) {
                sb.append("\nBeware: the command execution has not finished in given time limit.\n\n");
            }
        }
        result.recordSuccess();
        return sb.toString();
    } catch (Throwable t) {
        result.recordFatalError(t.getMessage(), t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) RandomString(com.evolveum.midpoint.util.RandomString) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Example 8 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project Anserini by castorini.

the class GenerateRegressionDocsTest method main.

@Test
public void main() throws Exception {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    URL templatesRoot = GenerateRegressionDocsTest.class.getResource("/docgen/templates/");
    for (final File fileEntry : new File(templatesRoot.toURI()).listFiles()) {
        String fileName = fileEntry.getName();
        // This is the name of the test, which can be different from the name of the collection,
        // e.g., multiple topics run on the same collection.
        String testName = fileEntry.getName().replaceAll(".template", "");
        URL yaml = GenerateRegressionDocsTest.class.getResource(String.format("/regression/%s.yaml", testName));
        DataModel data = mapper.readValue(new File(yaml.toURI()), DataModel.class);
        String corpus = data.getCorpus();
        Map<String, String> valuesMap = new HashMap<>();
        valuesMap.put("yaml", String.format("../src/main/resources/regression/%s.yaml", testName));
        valuesMap.put("template", String.format("../src/main/resources/docgen/templates/%s.template", testName));
        valuesMap.put("test_name", testName);
        valuesMap.put("corpus", corpus);
        valuesMap.put("index_cmds", data.generateIndexingCommand(corpus));
        valuesMap.put("ranking_cmds", data.generateRankingCommand(corpus));
        valuesMap.put("eval_cmds", data.generateEvalCommand(corpus));
        valuesMap.put("effectiveness", data.generateEffectiveness(corpus));
        StringSubstitutor sub = new StringSubstitutor(valuesMap);
        URL template = GenerateRegressionDocsTest.class.getResource(String.format("/docgen/templates/%s.template", testName));
        Scanner scanner = new Scanner(new File(template.toURI()), "UTF-8");
        String text = scanner.useDelimiter("\\A").next();
        scanner.close();
        String resolvedString = sub.replace(text);
        FileUtils.writeStringToFile(new File(String.format("docs/regressions-%s.md", testName)), resolvedString, "UTF-8");
    }
}
Also used : Scanner(java.util.Scanner) HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL) Test(org.junit.Test)

Example 9 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project Anserini by castorini.

the class JDIQ2018EffectivenessDocsTest method main.

@Test
public void main() throws Exception {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    URL yaml = JDIQ2018EffectivenessDocsTest.class.getResource("/jdiq2018/models.yaml");
    Model data = mapper.readValue(new File(yaml.toURI()), Model.class);
    Map<String, String> valuesMap = new HashMap<>();
    valuesMap.put("results", data.generateEffectiveness());
    StringSubstitutor sub = new StringSubstitutor(valuesMap);
    URL template = GenerateRegressionDocsTest.class.getResource("/jdiq2018/doc.template");
    Scanner scanner = new Scanner(new File(template.toURI()), "UTF-8");
    String text = scanner.useDelimiter("\\A").next();
    scanner.close();
    String resolvedString = sub.replace(text);
    FileUtils.writeStringToFile(new File("docs/experiments-jdiq2018.md"), resolvedString, "UTF-8");
}
Also used : Scanner(java.util.Scanner) HashMap(java.util.HashMap) StringSubstitutor(org.apache.commons.text.StringSubstitutor) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL) Test(org.junit.Test)

Example 10 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project cuba by cuba-platform.

the class TestContainer method initAppProperties.

@SuppressWarnings("ResultOfMethodCallIgnored")
protected void initAppProperties() {
    Properties properties = new Properties();
    List<String> locations = getAppPropertiesFiles();
    DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
    for (String location : locations) {
        Resource resource = resourceLoader.getResource(location);
        if (resource.exists()) {
            try (InputStream stream = resource.getInputStream()) {
                BOMInputStream bomInputStream = new BOMInputStream(stream);
                properties.load(bomInputStream);
            } catch (IOException e) {
                throw new RuntimeException("Unable to read app properties " + location, e);
            }
        } else {
            log.warn("Resource {} not found, ignore it", location);
        }
    }
    StringSubstitutor substitutor = new StringSubstitutor(key -> {
        String subst = properties.getProperty(key);
        return subst != null ? subst : System.getProperty(key);
    });
    for (Object key : properties.keySet()) {
        String value = substitutor.replace(properties.getProperty((String) key));
        appProperties.put((String) key, value);
    }
    File dir;
    dir = new File(appProperties.get("cuba.confDir"));
    dir.mkdirs();
    dir = new File(appProperties.get("cuba.logDir"));
    dir.mkdirs();
    dir = new File(appProperties.get("cuba.tempDir"));
    dir.mkdirs();
    dir = new File(appProperties.get("cuba.dataDir"));
    dir.mkdirs();
}
Also used : BOMInputStream(org.apache.commons.io.input.BOMInputStream) StringSubstitutor(org.apache.commons.text.StringSubstitutor) BOMInputStream(org.apache.commons.io.input.BOMInputStream) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) ExternalResource(org.junit.rules.ExternalResource) IOException(java.io.IOException) File(java.io.File) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Aggregations

StringSubstitutor (org.apache.commons.text.StringSubstitutor)71 HashMap (java.util.HashMap)24 Test (org.junit.jupiter.api.Test)19 IOException (java.io.IOException)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 File (java.io.File)8 List (java.util.List)8 InputStream (java.io.InputStream)6 Collectors (java.util.stream.Collectors)6 StringLookup (org.apache.commons.text.lookup.StringLookup)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 JsonObject (com.google.gson.JsonObject)4 URL (java.net.URL)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Scanner (java.util.Scanner)4