Skip to content

Commit

Permalink
Fixed deprecated class usage (Apache StrSubstitutor) (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaki authored and jplock committed Aug 23, 2018
1 parent 5a78323 commit 5f707c9
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 50 deletions.
1 change: 1 addition & 0 deletions docs/source/about/release-notes.rst
Expand Up @@ -17,6 +17,7 @@ v2.0.0: Unreleased
* Fix ``UUIDParams`` accepting input of incorrect length (`#2382 <https://github.com/dropwizard/dropwizard/pull/2382>`_) * Fix ``UUIDParams`` accepting input of incorrect length (`#2382 <https://github.com/dropwizard/dropwizard/pull/2382>`_)
* Fix usage ``@SelfValidating`` with ``@BeanParam`` (`#2334 <https://github.com/dropwizard/dropwizard/pull/2334>`_) * Fix usage ``@SelfValidating`` with ``@BeanParam`` (`#2334 <https://github.com/dropwizard/dropwizard/pull/2334>`_)
* Fix resource endpoints injected via DI not being logged on startup (`#2389 <https://github.com/dropwizard/dropwizard/pull/2389>`_) * Fix resource endpoints injected via DI not being logged on startup (`#2389 <https://github.com/dropwizard/dropwizard/pull/2389>`_)
* Retired use of deprecated Apache ``StrSubstitutor`` and ``StrLookup`` classes and replaced them with Apache's ``StringSubstitutor`` and ``StringLookup`` (`#2462 <https://github.com/dropwizard/dropwizard/pull/2462>`_)


.. _rel-1.3.5: .. _rel-1.3.5:


Expand Down
@@ -1,11 +1,11 @@
package io.dropwizard.configuration; package io.dropwizard.configuration;


import org.apache.commons.text.StrLookup; import org.apache.commons.text.lookup.StringLookup;


/** /**
* A custom {@link org.apache.commons.text.StrLookup} implementation using environment variables as lookup source. * A custom {@link org.apache.commons.text.StrLookup} implementation using environment variables as lookup source.
*/ */
public class EnvironmentVariableLookup extends StrLookup<Object> { public class EnvironmentVariableLookup implements StringLookup {
private final boolean strict; private final boolean strict;


/** /**
Expand Down
@@ -1,11 +1,11 @@
package io.dropwizard.configuration; package io.dropwizard.configuration;


import org.apache.commons.text.StrSubstitutor; import org.apache.commons.text.StringSubstitutor;


/** /**
* A custom {@link StrSubstitutor} using environment variables as lookup source. * A custom {@link StrSubstitutor} using environment variables as lookup source.
*/ */
public class EnvironmentVariableSubstitutor extends StrSubstitutor { public class EnvironmentVariableSubstitutor extends StringSubstitutor {
public EnvironmentVariableSubstitutor() { public EnvironmentVariableSubstitutor() {
this(true, false); this(true, false);
} }
Expand Down
@@ -1,30 +1,32 @@
package io.dropwizard.configuration; package io.dropwizard.configuration;


import io.dropwizard.util.ByteStreams; import io.dropwizard.util.ByteStreams;
import org.apache.commons.text.StrSubstitutor;


import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;


import org.apache.commons.text.StringSubstitutor;

import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;


/** /**
* A delegating {@link ConfigurationSourceProvider} which replaces variables in the underlying configuration * A delegating {@link ConfigurationSourceProvider} which replaces variables in the underlying configuration
* source according to the rules of a custom {@link org.apache.commons.text.StrSubstitutor}. * source according to the rules of a custom {@link org.apache.commons.text.StringSubstitutor}.
*/ */
public class SubstitutingSourceProvider implements ConfigurationSourceProvider { public class SubstitutingSourceProvider implements ConfigurationSourceProvider {
private final ConfigurationSourceProvider delegate; private final ConfigurationSourceProvider delegate;
private final StrSubstitutor substitutor; private final StringSubstitutor substitutor;


/** /**
* Create a new instance. * Create a new instance.
* *
* @param delegate The underlying {@link io.dropwizard.configuration.ConfigurationSourceProvider}. * @param delegate The underlying {@link io.dropwizard.configuration.ConfigurationSourceProvider}.
* @param substitutor The custom {@link org.apache.commons.text.StrSubstitutor} implementation. * @param substitutor The custom {@link org.apache.commons.text.StringSubstitutor} implementation.
*/ */
public SubstitutingSourceProvider(ConfigurationSourceProvider delegate, StrSubstitutor substitutor) { public SubstitutingSourceProvider(ConfigurationSourceProvider delegate, StringSubstitutor substitutor) {
this.delegate = requireNonNull(delegate); this.delegate = requireNonNull(delegate);
this.substitutor = requireNonNull(substitutor); this.substitutor = requireNonNull(substitutor);
} }
Expand Down
@@ -1,12 +1,13 @@
package io.dropwizard.configuration; package io.dropwizard.configuration;


import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;


import org.junit.Test;

public class EnvironmentVariableSubstitutorTest { public class EnvironmentVariableSubstitutorTest {

@Test @Test
public void defaultConstructorDisablesSubstitutionInVariables() { public void defaultConstructorDisablesSubstitutionInVariables() {
EnvironmentVariableSubstitutor substitutor = new EnvironmentVariableSubstitutor(); EnvironmentVariableSubstitutor substitutor = new EnvironmentVariableSubstitutor();
Expand All @@ -30,7 +31,7 @@ public void constructorEnablesSubstitutionInVariables() {
@Test @Test
public void substitutorUsesEnvironmentVariableLookup() { public void substitutorUsesEnvironmentVariableLookup() {
EnvironmentVariableSubstitutor substitutor = new EnvironmentVariableSubstitutor(); EnvironmentVariableSubstitutor substitutor = new EnvironmentVariableSubstitutor();
assertThat(substitutor.getVariableResolver()).isInstanceOf(EnvironmentVariableLookup.class); assertThat(substitutor.getStringLookup()).isInstanceOf(EnvironmentVariableLookup.class);
} }


@Test @Test
Expand Down
@@ -1,30 +1,24 @@
package io.dropwizard.configuration; package io.dropwizard.configuration;


import org.apache.commons.text.StrLookup; import static org.assertj.core.api.Assertions.assertThat;
import org.apache.commons.text.StrSubstitutor; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.Test;


import javax.annotation.Nullable;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;


import static org.assertj.core.api.Assertions.assertThat; import org.apache.commons.text.StringSubstitutor;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.apache.commons.text.lookup.StringLookup;
import org.junit.Test;


public class SubstitutingSourceProviderTest { public class SubstitutingSourceProviderTest {
@Test @Test
public void shouldSubstituteCorrectly() throws IOException { public void shouldSubstituteCorrectly() throws IOException {
StrLookup<?> dummyLookup = new StrLookup<Object>() { StringLookup dummyLookup = (x) -> "baz";
@Override
public String lookup(String key) {
return "baz";
}
};
DummySourceProvider dummyProvider = new DummySourceProvider(); DummySourceProvider dummyProvider = new DummySourceProvider();
SubstitutingSourceProvider provider = new SubstitutingSourceProvider(dummyProvider, new StrSubstitutor(dummyLookup)); SubstitutingSourceProvider provider = new SubstitutingSourceProvider(dummyProvider, new StringSubstitutor(dummyLookup));


assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: baz".getBytes(StandardCharsets.UTF_8))); assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: baz".getBytes(StandardCharsets.UTF_8)));


Expand All @@ -36,28 +30,16 @@ public String lookup(String key) {


@Test @Test
public void shouldSubstituteOnlyExistingVariables() throws IOException { public void shouldSubstituteOnlyExistingVariables() throws IOException {
StrLookup<?> dummyLookup = new StrLookup<Object>() { StringLookup dummyLookup = (x) -> null;
@Override SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StringSubstitutor(dummyLookup));
@Nullable
public String lookup(String key) {
return null;
}
};
SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StrSubstitutor(dummyLookup));


assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: ${bar}".getBytes(StandardCharsets.UTF_8))); assertThat(provider.open("foo: ${bar}")).hasSameContentAs(new ByteArrayInputStream("foo: ${bar}".getBytes(StandardCharsets.UTF_8)));
} }


@Test @Test
public void shouldSubstituteWithDefaultValue() throws IOException { public void shouldSubstituteWithDefaultValue() throws IOException {
StrLookup<?> dummyLookup = new StrLookup<Object>() { StringLookup dummyLookup = (x) -> null;
@Override SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StringSubstitutor(dummyLookup));
@Nullable
public String lookup(String key) {
return null;
}
};
SubstitutingSourceProvider provider = new SubstitutingSourceProvider(new DummySourceProvider(), new StrSubstitutor(dummyLookup));


assertThat(provider.open("foo: ${bar:-default}")).hasSameContentAs(new ByteArrayInputStream("foo: default".getBytes(StandardCharsets.UTF_8))); assertThat(provider.open("foo: ${bar:-default}")).hasSameContentAs(new ByteArrayInputStream("foo: default".getBytes(StandardCharsets.UTF_8)));
} }
Expand Down
Expand Up @@ -19,7 +19,7 @@
import io.dropwizard.util.Resources; import io.dropwizard.util.Resources;
import io.dropwizard.validation.BaseValidator; import io.dropwizard.validation.BaseValidator;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StrSubstitutor; import org.apache.commons.text.StringSubstitutor;
import org.assertj.core.data.MapEntry; import org.assertj.core.data.MapEntry;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
Expand Down Expand Up @@ -96,7 +96,7 @@ public void testConfigure() throws Exception {
final File newAppLog = folder.newFile("example-new-app.log"); final File newAppLog = folder.newFile("example-new-app.log");
final File newAppNotAdditiveLog = folder.newFile("example-new-app-not-additive.log"); final File newAppNotAdditiveLog = folder.newFile("example-new-app-not-additive.log");
final File defaultLog = folder.newFile("example.log"); final File defaultLog = folder.newFile("example.log");
final StrSubstitutor substitutor = new StrSubstitutor(Maps.of( final StringSubstitutor substitutor = new StringSubstitutor(Maps.of(
"new_app", StringUtils.removeEnd(newAppLog.getAbsolutePath(), ".log"), "new_app", StringUtils.removeEnd(newAppLog.getAbsolutePath(), ".log"),
"new_app_not_additive", StringUtils.removeEnd(newAppNotAdditiveLog.getAbsolutePath(), ".log"), "new_app_not_additive", StringUtils.removeEnd(newAppNotAdditiveLog.getAbsolutePath(), ".log"),
"default", StringUtils.removeEnd(defaultLog.getAbsolutePath(), ".log") "default", StringUtils.removeEnd(defaultLog.getAbsolutePath(), ".log")
Expand Down
Expand Up @@ -11,7 +11,7 @@
import io.dropwizard.util.Resources; import io.dropwizard.util.Resources;
import io.dropwizard.util.Size; import io.dropwizard.util.Size;
import io.dropwizard.validation.BaseValidator; import io.dropwizard.validation.BaseValidator;
import org.apache.commons.text.StrSubstitutor; import org.apache.commons.text.StringSubstitutor;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void testParseConfig() throws Exception {
public void testTestTcpLogging() throws Exception { public void testTestTcpLogging() throws Exception {
DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider( DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider(
new ResourceConfigurationSourceProvider(), new ResourceConfigurationSourceProvider(),
new StrSubstitutor(Collections.singletonMap("tcp.server.port", tcpServer.getPort()))), new StringSubstitutor(Collections.singletonMap("tcp.server.port", tcpServer.getPort()))),
"yaml/logging-tcp.yml"); "yaml/logging-tcp.yml");
loggingFactory.configure(new MetricRegistry(), "tcp-test"); loggingFactory.configure(new MetricRegistry(), "tcp-test");


Expand All @@ -88,7 +88,7 @@ public void testTestTcpLogging() throws Exception {
public void testBufferingTcpLogging() throws Exception { public void testBufferingTcpLogging() throws Exception {
DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider( DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider(
new ResourceConfigurationSourceProvider(), new ResourceConfigurationSourceProvider(),
new StrSubstitutor(Collections.singletonMap("tcp.server.port", tcpServer.getPort()))), new StringSubstitutor(Collections.singletonMap("tcp.server.port", tcpServer.getPort()))),
"yaml/logging-tcp-buffered.yml"); "yaml/logging-tcp-buffered.yml");
loggingFactory.configure(new MetricRegistry(), "tcp-test"); loggingFactory.configure(new MetricRegistry(), "tcp-test");


Expand Down
Expand Up @@ -17,7 +17,7 @@


import javax.annotation.Nullable; import javax.annotation.Nullable;


import org.apache.commons.text.StrSubstitutor; import org.apache.commons.text.StringSubstitutor;
import org.assertj.core.api.Condition; import org.assertj.core.api.Condition;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
Expand Down Expand Up @@ -127,7 +127,7 @@ private void runLineTest(final Duration messageRate, final int lineCount, final


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final ConsoleAppenderFactory<ILoggingEvent> config = this.factory.build( final ConsoleAppenderFactory<ILoggingEvent> config = this.factory.build(
new SubstitutingSourceProvider(new FileConfigurationSourceProvider(), new StrSubstitutor(variables)), new SubstitutingSourceProvider(new FileConfigurationSourceProvider(), new StringSubstitutor(variables)),
this.findResource("/yaml/logging-message-rate.yml").getPath()); this.findResource("/yaml/logging-message-rate.yml").getPath());


final DefaultLoggingFactory defaultLoggingFactory = new DefaultLoggingFactory(); final DefaultLoggingFactory defaultLoggingFactory = new DefaultLoggingFactory();
Expand Down
Expand Up @@ -10,7 +10,7 @@
import io.dropwizard.util.Maps; import io.dropwizard.util.Maps;
import io.dropwizard.util.Resources; import io.dropwizard.util.Resources;
import io.dropwizard.validation.BaseValidator; import io.dropwizard.validation.BaseValidator;
import org.apache.commons.text.StrSubstitutor; import org.apache.commons.text.StringSubstitutor;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
Expand Down Expand Up @@ -62,7 +62,7 @@ private static File resourcePath(String path) throws URISyntaxException {
@Test @Test
public void testTlsLogging() throws Exception { public void testTlsLogging() throws Exception {
DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider( DefaultLoggingFactory loggingFactory = yamlConfigurationFactory.build(new SubstitutingSourceProvider(
new ResourceConfigurationSourceProvider(), new StrSubstitutor(Maps.of( new ResourceConfigurationSourceProvider(), new StringSubstitutor(Maps.of(
"tls.trust_store.path", resourcePath("stores/tls_client.jks").getAbsolutePath(), "tls.trust_store.path", resourcePath("stores/tls_client.jks").getAbsolutePath(),
"tls.trust_store.pass", "client_pass", "tls.trust_store.pass", "client_pass",
"tls.server_port", tcpServer.getPort() "tls.server_port", tcpServer.getPort()
Expand Down

0 comments on commit 5f707c9

Please sign in to comment.