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
}
}
}
}
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()));
}
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");
}
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");
}
}
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();
}
Aggregations