Search in sources :

Example 6 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project tomee by apache.

the class JarsTxtMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!outputFile.getParentFile().exists()) {
        FileUtils.mkdir(outputFile.getParentFile().getAbsolutePath());
    }
    FileWriter writer = null;
    try {
        writer = new FileWriter(outputFile);
        final TreeSet<String> set = new TreeSet<>();
        for (final Artifact a : (Set<Artifact>) project.getArtifacts()) {
            if (!acceptScope(a.getScope()) || !acceptType(a.getType())) {
                continue;
            }
            a.setScope(Artifact.SCOPE_PROVIDED);
            final StringBuilder line = new StringBuilder("mvn:").append(a.getGroupId()).append("/").append(a.getArtifactId()).append("/").append(version(a));
            final boolean isJar = JAR.equals(a.getType());
            if (!isJar) {
                line.append("/").append(a.getType());
            }
            if (a.getClassifier() != null) {
                if (isJar) {
                    line.append("/").append(JAR);
                }
                line.append("/").append(a.getClassifier());
            }
            if (hashAlgo != null) {
                final Artifact artifact = factory.createDependencyArtifact(a.getGroupId(), a.getArtifactId(), VersionRange.createFromVersion(a.getVersion()), a.getType(), a.getClassifier(), a.getScope());
                try {
                    resolver.resolve(artifact, remoteRepos, local);
                } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
                final File file = artifact.getFile();
                line.append("|").append(Files.hash((Set<URL>) Collections.singleton(file.toURI().toURL()), hashAlgo)).append("|").append(hashAlgo);
            }
            set.add(line.toString());
        }
        if (additionals != null) {
            if (placeHolders == null) {
                placeHolders = new HashMap<>();
            }
            final StrSubstitutor lookup = new StrSubstitutor(StrLookup.mapLookup(placeHolders));
            for (final String line : additionals) {
                final StringBuilder builder = new StringBuilder(line);
                if (hashAlgo != null) {
                    builder.append("|").append(Files.hash(urls(line, lookup), hashAlgo)).append("|").append(hashAlgo);
                }
                set.add(builder.toString());
            }
        }
        // written after to be sorted, more readable
        for (final String line : set) {
            writer.write(line);
            writer.write("\n");
        }
        writer.flush();
    } catch (final IOException e) {
        getLog().error(e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (final IOException e) {
            // no-op
            }
        }
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) TreeSet(java.util.TreeSet) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) File(java.io.File)

Example 7 with StrSubstitutor

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

the class DefaultDhisConfigurationProvider method substituteEnvironmentVariables.

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

Example 8 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project dropwizard by dropwizard.

the class DefaultLoggingFactoryTest method testConfigure.

@Test
public void testConfigure() throws Exception {
    final File newAppLog = folder.newFile("example-new-app.log");
    final File newAppNotAdditiveLog = folder.newFile("example-new-app-not-additive.log");
    final File defaultLog = folder.newFile("example.log");
    final StrSubstitutor substitutor = new StrSubstitutor(ImmutableMap.of("new_app", StringUtils.removeEnd(newAppLog.getAbsolutePath(), ".log"), "new_app_not_additive", StringUtils.removeEnd(newAppNotAdditiveLog.getAbsolutePath(), ".log"), "default", StringUtils.removeEnd(defaultLog.getAbsolutePath(), ".log")));
    final String configPath = Resources.getResource("yaml/logging_advanced.yml").getFile();
    final DefaultLoggingFactory config = factory.build(new SubstitutingSourceProvider(new FileConfigurationSourceProvider(), substitutor), configPath);
    config.configure(new MetricRegistry(), "test-logger");
    LoggerFactory.getLogger("com.example.app").debug("Application debug log");
    LoggerFactory.getLogger("com.example.app").info("Application log");
    LoggerFactory.getLogger("com.example.newApp").debug("New application debug log");
    LoggerFactory.getLogger("com.example.newApp").info("New application info log");
    LoggerFactory.getLogger("com.example.legacyApp").debug("Legacy application debug log");
    LoggerFactory.getLogger("com.example.legacyApp").info("Legacy application info log");
    LoggerFactory.getLogger("com.example.notAdditive").debug("Not additive application debug log");
    LoggerFactory.getLogger("com.example.notAdditive").info("Not additive application info log");
    config.stop();
    assertThat(Files.readLines(defaultLog, StandardCharsets.UTF_8)).containsOnly("INFO  com.example.app: Application log", "DEBUG com.example.newApp: New application debug log", "INFO  com.example.newApp: New application info log", "DEBUG com.example.legacyApp: Legacy application debug log", "INFO  com.example.legacyApp: Legacy application info log");
    assertThat(Files.readLines(newAppLog, StandardCharsets.UTF_8)).containsOnly("DEBUG com.example.newApp: New application debug log", "INFO  com.example.newApp: New application info log");
    assertThat(Files.readLines(newAppNotAdditiveLog, StandardCharsets.UTF_8)).containsOnly("DEBUG com.example.notAdditive: Not additive application debug log", "INFO  com.example.notAdditive: Not additive application info log");
}
Also used : SubstitutingSourceProvider(io.dropwizard.configuration.SubstitutingSourceProvider) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) MetricRegistry(com.codahale.metrics.MetricRegistry) File(java.io.File) FileConfigurationSourceProvider(io.dropwizard.configuration.FileConfigurationSourceProvider) Test(org.junit.Test)

Example 9 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project dropwizard by dropwizard.

the class SubstitutingSourceProviderTest method shouldSubstituteCorrectly.

@Test
public void shouldSubstituteCorrectly() throws IOException {
    StrLookup<?> dummyLookup = new StrLookup<Object>() {

        @Override
        public String lookup(String key) {
            return "baz";
        }
    };
    DummySourceProvider dummyProvider = new DummySourceProvider();
    SubstitutingSourceProvider provider = new SubstitutingSourceProvider(dummyProvider, new StrSubstitutor(dummyLookup));
    String results = new String(ByteStreams.toByteArray(provider.open("foo: ${bar}")), StandardCharsets.UTF_8);
    assertThat(results).isEqualTo("foo: baz");
    // ensure that opened streams are closed
    try {
        dummyProvider.lastStream.read();
        failBecauseExceptionWasNotThrown(IOException.class);
    } catch (IOException e) {
        assertThat(e).hasMessage("Stream closed");
    }
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) StrLookup(org.apache.commons.lang3.text.StrLookup) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project pinot by linkedin.

the class AnomaliesResource method getExternalURL.

private String getExternalURL(MergedAnomalyResultDTO mergedAnomaly) {
    String metric = mergedAnomaly.getMetric();
    String dataset = mergedAnomaly.getCollection();
    Long startTime = mergedAnomaly.getStartTime();
    Long endTime = mergedAnomaly.getEndTime();
    MetricConfigDTO metricConfigDTO = metricConfigDAO.findByMetricAndDataset(metric, dataset);
    Map<String, String> context = new HashMap<>();
    context.put(MetricConfigBean.URL_TEMPLATE_START_TIME, String.valueOf(startTime));
    context.put(MetricConfigBean.URL_TEMPLATE_END_TIME, String.valueOf(endTime));
    StrSubstitutor strSubstitutor = new StrSubstitutor(context);
    Map<String, String> urlTemplates = metricConfigDTO.getExtSourceLinkInfo();
    if (urlTemplates == null) {
        return "";
    }
    for (Map.Entry<String, String> entry : urlTemplates.entrySet()) {
        String sourceName = entry.getKey();
        String urlTemplate = entry.getValue();
        String extSourceUrl = strSubstitutor.replace(urlTemplate);
        urlTemplates.put(sourceName, extSourceUrl);
    }
    return new JSONObject(urlTemplates).toString();
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) DimensionMap(com.linkedin.thirdeye.api.DimensionMap)

Aggregations

StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)17 StrLookup (org.apache.commons.lang3.text.StrLookup)5 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 MetricConfigDTO (com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)2 FileWriter (java.io.FileWriter)2 Map (java.util.Map)2 Properties (java.util.Properties)2 JSONObject (org.json.JSONObject)2 DomainVO (com.cloud.domain.DomainVO)1 AccountVO (com.cloud.user.AccountVO)1 UserVO (com.cloud.user.UserVO)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DimensionMap (com.linkedin.thirdeye.api.DimensionMap)1 MergedAnomalyResultDTO (com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO)1 FileConfigurationSourceProvider (io.dropwizard.configuration.FileConfigurationSourceProvider)1 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)1