diff --git a/api/pom.xml b/api/pom.xml index bf1589ebd2..0f5b2bdffd 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -31,6 +31,11 @@ + + com.typesafe + config + 1.2.1 + org.slf4j slf4j-api diff --git a/api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigDefaults.java b/api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigDefaults.java index 698ee7f0c8..17b58c6c71 100644 --- a/api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigDefaults.java +++ b/api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigDefaults.java @@ -12,7 +12,10 @@ */ package org.asynchttpclient; -import static org.asynchttpclient.util.MiscUtils.getBoolean; +import org.asynchttpclient.util.AsyncPropertiesHelper; +import org.asynchttpclient.util.DefaultHostnameVerifier; + +import javax.net.ssl.HostnameVerifier; public final class AsyncHttpClientConfigDefaults { @@ -22,106 +25,106 @@ private AsyncHttpClientConfigDefaults() { public static final String ASYNC_CLIENT = AsyncHttpClientConfig.class.getName() + "."; public static int defaultMaxConnections() { - return Integer.getInteger(ASYNC_CLIENT + "maxConnections", -1); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "maxConnections"); } public static int defaultMaxConnectionsPerHost() { - return Integer.getInteger(ASYNC_CLIENT + "maxConnectionsPerHost", -1); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "maxConnectionsPerHost"); } public static int defaultConnectionTimeout() { - return Integer.getInteger(ASYNC_CLIENT + "connectionTimeout", 60 * 1000); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "connectionTimeout"); } public static int defaultPooledConnectionIdleTimeout() { - return Integer.getInteger(ASYNC_CLIENT + "pooledConnectionIdleTimeout", 60 * 1000); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "pooledConnectionIdleTimeout"); } public static int defaultReadTimeout() { - return Integer.getInteger(ASYNC_CLIENT + "readTimeout", 60 * 1000); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "readTimeout"); } public static int defaultRequestTimeout() { - return Integer.getInteger(ASYNC_CLIENT + "requestTimeout", 60 * 1000); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "requestTimeout"); } public static int defaultWebSocketTimeout() { - return Integer.getInteger(ASYNC_CLIENT + "webSocketTimeout", 15 * 60 * 1000); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "webSocketTimeout"); } public static int defaultConnectionTTL() { - return Integer.getInteger(ASYNC_CLIENT + "connectionTTL", -1); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "connectionTTL"); } public static boolean defaultFollowRedirect() { - return Boolean.getBoolean(ASYNC_CLIENT + "followRedirect"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "followRedirect"); } public static int defaultMaxRedirects() { - return Integer.getInteger(ASYNC_CLIENT + "maxRedirects", 5); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "maxRedirects"); } public static boolean defaultCompressionEnforced() { - return Boolean.getBoolean(ASYNC_CLIENT + "compressionEnforced"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "compressionEnforced"); } public static String defaultUserAgent() { - return System.getProperty(ASYNC_CLIENT + "userAgent", "AHC/2.0"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(ASYNC_CLIENT + "userAgent"); } public static int defaultIoThreadMultiplier() { - return Integer.getInteger(ASYNC_CLIENT + "ioThreadMultiplier", 2); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "ioThreadMultiplier"); } public static boolean defaultUseProxySelector() { - return Boolean.getBoolean(ASYNC_CLIENT + "useProxySelector"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "useProxySelector"); } public static boolean defaultUseProxyProperties() { - return Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "useProxyProperties"); } public static boolean defaultStrict302Handling() { - return Boolean.getBoolean(ASYNC_CLIENT + "strict302Handling"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "strict302Handling"); } public static boolean defaultAllowPoolingConnections() { - return getBoolean(ASYNC_CLIENT + "allowPoolingConnections", true); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "allowPoolingConnections"); } public static boolean defaultUseRelativeURIsWithConnectProxies() { - return getBoolean(ASYNC_CLIENT + "useRelativeURIsWithConnectProxies", true); - } + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "useRelativeURIsWithConnectProxies"); + } public static int defaultMaxRequestRetry() { - return Integer.getInteger(ASYNC_CLIENT + "maxRequestRetry", 5); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "maxRequestRetry"); } public static boolean defaultAllowPoolingSslConnections() { - return getBoolean(ASYNC_CLIENT + "allowPoolingSslConnections", true); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "allowPoolingSslConnections"); } public static boolean defaultDisableUrlEncodingForBoundRequests() { - return Boolean.getBoolean(ASYNC_CLIENT + "disableUrlEncodingForBoundRequests"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "disableUrlEncodingForBoundRequests"); } public static boolean defaultRemoveQueryParamOnRedirect() { - return getBoolean(ASYNC_CLIENT + "removeQueryParamOnRedirect", true); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "removeQueryParamOnRedirect"); } public static boolean defaultSpdyEnabled() { - return Boolean.getBoolean(ASYNC_CLIENT + "spdyEnabled"); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "spdyEnabled"); } public static int defaultSpdyInitialWindowSize() { - return Integer.getInteger(ASYNC_CLIENT + "spdyInitialWindowSize", 10 * 1024 * 1024); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "spdyInitialWindowSize"); } public static int defaultSpdyMaxConcurrentStreams() { - return Integer.getInteger(ASYNC_CLIENT + "spdyMaxConcurrentStreams", 100); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT + "spdyMaxConcurrentStreams"); } public static boolean defaultAcceptAnyCertificate() { - return getBoolean(ASYNC_CLIENT + "acceptAnyCertificate", false); + return AsyncPropertiesHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT + "acceptAnyCertificate"); } } diff --git a/api/src/main/java/org/asynchttpclient/PerHostConnectionPoolPartioning.java b/api/src/main/java/org/asynchttpclient/PerHostConnectionPoolPartitioning.java similarity index 92% rename from api/src/main/java/org/asynchttpclient/PerHostConnectionPoolPartioning.java rename to api/src/main/java/org/asynchttpclient/PerHostConnectionPoolPartitioning.java index 51a8ddcc51..0139a99fe3 100644 --- a/api/src/main/java/org/asynchttpclient/PerHostConnectionPoolPartioning.java +++ b/api/src/main/java/org/asynchttpclient/PerHostConnectionPoolPartitioning.java @@ -18,7 +18,7 @@ import org.asynchttpclient.uri.Uri; import org.asynchttpclient.util.AsyncHttpProviderUtils; -public enum PerHostConnectionPoolPartioning implements ConnectionPoolPartitioning { +public enum PerHostConnectionPoolPartitioning implements ConnectionPoolPartitioning { INSTANCE; diff --git a/api/src/main/java/org/asynchttpclient/RequestBuilderBase.java b/api/src/main/java/org/asynchttpclient/RequestBuilderBase.java index d2b704171e..6aaa41c265 100644 --- a/api/src/main/java/org/asynchttpclient/RequestBuilderBase.java +++ b/api/src/main/java/org/asynchttpclient/RequestBuilderBase.java @@ -66,7 +66,7 @@ private static final class RequestImpl implements Request { private int requestTimeoutInMs; private long rangeOffset; public String charset; - private ConnectionPoolPartitioning connectionPoolPartitioning = PerHostConnectionPoolPartioning.INSTANCE; + private ConnectionPoolPartitioning connectionPoolPartitioning = PerHostConnectionPoolPartitioning.INSTANCE; private List queryParams; public RequestImpl() { diff --git a/api/src/main/java/org/asynchttpclient/util/AsyncPropertiesHelper.java b/api/src/main/java/org/asynchttpclient/util/AsyncPropertiesHelper.java new file mode 100644 index 0000000000..e46c2d1132 --- /dev/null +++ b/api/src/main/java/org/asynchttpclient/util/AsyncPropertiesHelper.java @@ -0,0 +1,25 @@ +package org.asynchttpclient.util; + +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; + +public class AsyncPropertiesHelper { + + public static final String ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE = "ahc.properties"; + public static final String DEFAULTAHC_PROPERTIES = "ahc-default.properties"; + + public static Config getAsyncHttpClientConfig(){ + return ConfigFactory.load(ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE) + .withFallback(ConfigFactory.load(DEFAULTAHC_PROPERTIES)); + } + + /** + * This method invalidates the property caches. So if a system property has been changed and the + * effect of this change is to be seen then call reloadProperties() and then getAsyncHttpClientConfig() + * to get the new property values. + */ + public static void reloadProperties(){ + ConfigFactory.invalidateCaches(); + } + +} diff --git a/api/src/main/resources/ahc-default.properties b/api/src/main/resources/ahc-default.properties new file mode 100644 index 0000000000..9ea9756cf3 --- /dev/null +++ b/api/src/main/resources/ahc-default.properties @@ -0,0 +1,27 @@ +org.asynchttpclient.AsyncHttpClientConfig.maxConnections=-1 +org.asynchttpclient.AsyncHttpClientConfig.maxConnectionsPerHost=-1 +org.asynchttpclient.AsyncHttpClientConfig.connectionTimeout=60000 +org.asynchttpclient.AsyncHttpClientConfig.pooledConnectionIdleTimeout=60000 +org.asynchttpclient.AsyncHttpClientConfig.readTimeout=60000 +org.asynchttpclient.AsyncHttpClientConfig.requestTimeout=60000 +org.asynchttpclient.AsyncHttpClientConfig.webSocketTimeout=900000 +org.asynchttpclient.AsyncHttpClientConfig.connectionTTL=-1 +org.asynchttpclient.AsyncHttpClientConfig.followRedirect=false +org.asynchttpclient.AsyncHttpClientConfig.maxRedirects=5 +org.asynchttpclient.AsyncHttpClientConfig.compressionEnforced=false +org.asynchttpclient.AsyncHttpClientConfig.userAgent=NING/1.0 +org.asynchttpclient.AsyncHttpClientConfig.ioThreadMultiplier=2 +org.asynchttpclient.AsyncHttpClientConfig.useProxySelector=false +org.asynchttpclient.AsyncHttpClientConfig.useProxyProperties=false +org.asynchttpclient.AsyncHttpClientConfig.strict302Handling=false +org.asynchttpclient.AsyncHttpClientConfig.allowPoolingConnections=true +org.asynchttpclient.AsyncHttpClientConfig.useRelativeURIsWithConnectProxies=true +org.asynchttpclient.AsyncHttpClientConfig.requestCompressionLevel=-1 +org.asynchttpclient.AsyncHttpClientConfig.maxRequestRetry=5 +org.asynchttpclient.AsyncHttpClientConfig.allowPoolingSslConnections=true +org.asynchttpclient.AsyncHttpClientConfig.disableUrlEncodingForBoundRequests=false +org.asynchttpclient.AsyncHttpClientConfig.removeQueryParamOnRedirect=true +org.asynchttpclient.AsyncHttpClientConfig.spdyEnabled=false +org.asynchttpclient.AsyncHttpClientConfig.spdyInitialWindowSize=10485760 +org.asynchttpclient.AsyncHttpClientConfig.spdyMaxConcurrentStreams=100 +org.asynchttpclient.AsyncHttpClientConfig.acceptAnyCertificate=false \ No newline at end of file diff --git a/api/src/test/java/org/asynchttpclient/AsyncHttpClientDefaultsTest.java b/api/src/test/java/org/asynchttpclient/AsyncHttpClientDefaultsTest.java new file mode 100644 index 0000000000..67034b7cb5 --- /dev/null +++ b/api/src/test/java/org/asynchttpclient/AsyncHttpClientDefaultsTest.java @@ -0,0 +1,190 @@ +package org.asynchttpclient; + +import org.asynchttpclient.util.AsyncPropertiesHelper; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.lang.reflect.Method; + +@Test +public class AsyncHttpClientDefaultsTest { + public static final String ASYNC_CLIENT = AsyncHttpClientConfig.class.getName() + "."; + + public void testDefaultMaxTotalConnections() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultMaxConnections(),-1); + testIntegerSystemProperty("maxConnections", "defaultMaxConnections", "100"); + } + + public void testDefaultMaxConnectionPerHost() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultMaxConnectionsPerHost(), -1); + testIntegerSystemProperty("maxConnectionsPerHost", "defaultMaxConnectionsPerHost", "100"); + } + + public void testDefaultConnectionTimeOutInMs() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultConnectionTimeout(), 60 * 1000); + testIntegerSystemProperty("connectionTimeout", "defaultConnectionTimeout", "100"); + } + + public void testDefaultIdleConnectionInPoolTimeoutInMs() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultPooledConnectionIdleTimeout(), 60 * 1000); + testIntegerSystemProperty("pooledConnectionIdleTimeout", "defaultPooledConnectionIdleTimeout", "100"); + } + + public void testDefaultIdleConnectionTimeoutInMs() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultReadTimeout(), 60 * 1000); + testIntegerSystemProperty("readTimeout", "defaultReadTimeout", "100"); + } + + public void testDefaultRequestTimeoutInMs() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultRequestTimeout(), 60 * 1000); + testIntegerSystemProperty("requestTimeout", "defaultRequestTimeout", "100"); + } + + public void testDefaultWebSocketIdleTimeoutInMs() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultWebSocketTimeout(), 15 * 60 * 1000); + testIntegerSystemProperty("webSocketTimeout", "defaultWebSocketTimeout", "100"); + } + + public void testDefaultMaxConnectionLifeTimeInMs() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultConnectionTTL(), -1); + testIntegerSystemProperty("connectionTTL", "defaultConnectionTTL", "100"); + } + + public void testDefaultFollowRedirect() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultFollowRedirect()); + testBooleanSystemProperty("followRedirect", "defaultFollowRedirect", "true"); + } + + public void testDefaultMaxRedirects() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultMaxRedirects(), 5); + testIntegerSystemProperty("maxRedirects", "defaultMaxRedirects", "100"); + } + + public void testDefaultCompressionEnforced() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultCompressionEnforced()); + testBooleanSystemProperty("compressionEnforced", "defaultCompressionEnforced", "true"); + } + + public void testDefaultUserAgent() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultUserAgent(),"NING/1.0"); + testStringSystemProperty("userAgent", "defaultUserAgent", "MyAHC"); + } + + public void testDefaultIoThreadMultiplier() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultIoThreadMultiplier(), 2); + testIntegerSystemProperty("ioThreadMultiplier", "defaultIoThreadMultiplier", "100"); + } + + public void testDefaultUseProxySelector() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultUseProxySelector()); + testBooleanSystemProperty("useProxySelector", "defaultUseProxySelector", "true"); + } + + public void testDefaultUseProxyProperties() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultUseProxyProperties()); + testBooleanSystemProperty("useProxyProperties", "defaultUseProxyProperties", "true"); + } + + public void testDefaultStrict302Handling() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultStrict302Handling()); + testBooleanSystemProperty("strict302Handling", "defaultStrict302Handling", "true"); + } + + public void testDefaultAllowPoolingConnection() { + Assert.assertTrue(AsyncHttpClientConfigDefaults.defaultAllowPoolingConnections()); + testBooleanSystemProperty("allowPoolingConnections", "defaultAllowPoolingConnections", "false"); + } + + public void testDefaultUseRelativeURIsWithConnectProxies() { + Assert.assertTrue(AsyncHttpClientConfigDefaults.defaultUseRelativeURIsWithConnectProxies()); + testBooleanSystemProperty("useRelativeURIsWithConnectProxies", "defaultUseRelativeURIsWithConnectProxies", "false"); + } + + public void testDefaultMaxRequestRetry() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultMaxRequestRetry(), 5); + testIntegerSystemProperty("maxRequestRetry", "defaultMaxRequestRetry", "100"); + } + + public void testDefaultAllowSslConnectionPool() { + Assert.assertTrue(AsyncHttpClientConfigDefaults.defaultAllowPoolingSslConnections()); + testBooleanSystemProperty("allowPoolingSslConnections", "defaultAllowPoolingSslConnections", "false"); + } + + public void testDefaultDisableUrlEncodingForBoundRequests() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultDisableUrlEncodingForBoundRequests()); + testBooleanSystemProperty("disableUrlEncodingForBoundRequests", "defaultDisableUrlEncodingForBoundRequests", "true"); + } + + public void testDefaultRemoveQueryParamOnRedirect() { + Assert.assertTrue(AsyncHttpClientConfigDefaults.defaultRemoveQueryParamOnRedirect()); + testBooleanSystemProperty("removeQueryParamOnRedirect", "defaultRemoveQueryParamOnRedirect", "false"); + } + + public void testDefaultSpdyEnabled() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultSpdyEnabled()); + testBooleanSystemProperty("spdyEnabled", "defaultSpdyEnabled", "true"); + } + + public void testDefaultSpdyInitialWindowSize() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultSpdyInitialWindowSize(), 10 * 1024 * 1024); + testIntegerSystemProperty("spdyInitialWindowSize", "defaultSpdyInitialWindowSize", "100"); + } + + public void testDefaultSpdyMaxConcurrentStreams() { + Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultSpdyMaxConcurrentStreams(), 100); + testIntegerSystemProperty("spdyMaxConcurrentStreams", "defaultSpdyMaxConcurrentStreams", "100"); + } + + public void testDefaultAcceptAnyCertificate() { + Assert.assertFalse(AsyncHttpClientConfigDefaults.defaultAcceptAnyCertificate()); + testBooleanSystemProperty("acceptAnyCertificate", "defaultAcceptAnyCertificate", "true"); + } + + private void testIntegerSystemProperty(String propertyName,String methodName,String value){ + String previous = System.getProperty(ASYNC_CLIENT + propertyName); + System.setProperty(ASYNC_CLIENT + propertyName, value); + AsyncPropertiesHelper.reloadProperties(); + try { + Method method = AsyncHttpClientConfigDefaults.class.getMethod(methodName, new Class[]{}); + Assert.assertEquals(method.invoke(null, new Object[]{}),Integer.parseInt(value)); + } catch (Exception e) { + Assert.fail("Couldn't find or execute method : " + methodName,e); + } + if(previous != null) + System.setProperty(ASYNC_CLIENT + propertyName, previous); + else + System.clearProperty(ASYNC_CLIENT + propertyName); + } + + private void testBooleanSystemProperty(String propertyName,String methodName,String value){ + String previous = System.getProperty(ASYNC_CLIENT + propertyName); + System.setProperty(ASYNC_CLIENT + propertyName, value); + AsyncPropertiesHelper.reloadProperties(); + try { + Method method = AsyncHttpClientConfigDefaults.class.getMethod(methodName, new Class[]{}); + Assert.assertEquals(method.invoke(null, new Object[]{}),Boolean.parseBoolean(value)); + } catch (Exception e) { + Assert.fail("Couldn't find or execute method : " + methodName,e); + } + if(previous != null) + System.setProperty(ASYNC_CLIENT + propertyName, previous); + else + System.clearProperty(ASYNC_CLIENT + propertyName); + } + + private void testStringSystemProperty(String propertyName,String methodName,String value){ + String previous = System.getProperty(ASYNC_CLIENT + propertyName); + System.setProperty(ASYNC_CLIENT + propertyName, value); + AsyncPropertiesHelper.reloadProperties(); + try { + Method method = AsyncHttpClientConfigDefaults.class.getMethod(methodName, new Class[]{}); + Assert.assertEquals(method.invoke(null, new Object[]{}),value); + } catch (Exception e) { + Assert.fail("Couldn't find or execute method : " + methodName,e); + } + if(previous != null) + System.setProperty(ASYNC_CLIENT + propertyName, previous); + else + System.clearProperty(ASYNC_CLIENT + propertyName); + } +} diff --git a/extras/registry/src/main/java/org/asynchttpclient/extras/registry/AsyncImplHelper.java b/extras/registry/src/main/java/org/asynchttpclient/extras/registry/AsyncImplHelper.java index 0e827c0114..6d3803c643 100644 --- a/extras/registry/src/main/java/org/asynchttpclient/extras/registry/AsyncImplHelper.java +++ b/extras/registry/src/main/java/org/asynchttpclient/extras/registry/AsyncImplHelper.java @@ -12,30 +12,21 @@ */ package org.asynchttpclient.extras.registry; + import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.util.AsyncPropertiesHelper; + +import com.typesafe.config.ConfigException; -import java.io.IOException; -import java.io.InputStream; import java.security.AccessController; -import java.security.PrivilegedAction; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.util.Properties; public class AsyncImplHelper { public static final String ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY = "org.async.http.client.impl"; public static final String ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY = "org.async.http.client.registry.impl"; - public static final String ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE = "asynchttpclient.properties"; - - private static String getSystemProperty(final String systemProperty) { - return AccessController.doPrivileged(new PrivilegedAction() { - public String run() { - return System.getProperty(systemProperty); - } - }); - } - + /* * Returns the class specified by either a system property or a properties * file as the class to instantiated for the AsyncHttpClient. Returns null @@ -43,41 +34,12 @@ public String run() { * the specified class couldn't be created. */ public static Class getAsyncImplClass(String propertyName) { - String asyncHttpClientImplClassName = getSystemProperty(propertyName); - if (asyncHttpClientImplClassName == null) { - Properties properties = AsyncImplHelper.getAsyncImplProperties(); - if (properties != null) - asyncHttpClientImplClassName = properties.getProperty(propertyName); - } - - if (asyncHttpClientImplClassName == null) - return null; - - Class asyncHttpClientImplClass = AsyncImplHelper.getClass(asyncHttpClientImplClassName); - return asyncHttpClientImplClass; - } - - private static Properties getAsyncImplProperties() { try { - return AccessController.doPrivileged(new PrivilegedExceptionAction() { - public Properties run() throws IOException { - InputStream stream = null; - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - if (cl != null) - stream = cl.getResourceAsStream(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE); - if (stream == null) - stream = ClassLoader.getSystemClassLoader().getResourceAsStream( - AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_PROPERTIES_FILE); - if (stream != null) { - Properties properties = new Properties(); - properties.load(stream); - return properties; - } - return null; - } - }); - } catch (PrivilegedActionException e) { - throw new AsyncHttpClientImplException("Unable to read properties file because of exception : " + e.getMessage(), e); + String asyncHttpClientImplClassName = AsyncPropertiesHelper.getAsyncHttpClientConfig().getString(propertyName); + Class asyncHttpClientImplClass = AsyncImplHelper.getClass(asyncHttpClientImplClassName); + return asyncHttpClientImplClass; + }catch(ConfigException configException) { + return null; } } diff --git a/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AbstractAsyncHttpClientFactoryTest.java b/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AbstractAsyncHttpClientFactoryTest.java index b4c05cc8be..b39946085b 100644 --- a/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AbstractAsyncHttpClientFactoryTest.java +++ b/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AbstractAsyncHttpClientFactoryTest.java @@ -22,6 +22,7 @@ import org.asynchttpclient.extras.registry.AsyncHttpClientFactory; import org.asynchttpclient.extras.registry.AsyncHttpClientImplException; import org.asynchttpclient.extras.registry.AsyncImplHelper; +import org.asynchttpclient.util.AsyncPropertiesHelper; import org.eclipse.jetty.server.Server; import org.testng.Assert; import org.testng.annotations.AfterClass; @@ -47,6 +48,7 @@ public void setUp() { PA.setValue(AsyncHttpClientFactory.class, "instantiated", false); PA.setValue(AsyncHttpClientFactory.class, "asyncHttpClientImplClass", null); System.clearProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY); + AsyncPropertiesHelper.reloadProperties(); } @BeforeClass(alwaysRun = true) @@ -118,12 +120,14 @@ public void testGetAsyncHttpClientStringConfig() { @Test(groups = "fast") public void testFactoryWithSystemProperty() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); Assert.assertTrue(AsyncHttpClientFactory.getAsyncHttpClient().getClass().equals(TestAsyncHttpClient.class)); } @Test(groups = "fast") public void testGetAsyncHttpClientConfigWithSystemProperty() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient(new AsyncHttpClientConfig.Builder().build()); Assert.assertTrue(asyncHttpClient.getClass().equals(TestAsyncHttpClient.class)); } @@ -131,6 +135,7 @@ public void testGetAsyncHttpClientConfigWithSystemProperty() { @Test(groups = "fast") public void testGetAsyncHttpClientProviderWithSystemProperty() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient(getAsyncHttpProvider(null)); Assert.assertTrue(asyncHttpClient.getClass().equals(TestAsyncHttpClient.class)); } @@ -138,6 +143,7 @@ public void testGetAsyncHttpClientProviderWithSystemProperty() { @Test(groups = "fast") public void testGetAsyncHttpClientConfigAndProviderWithSystemProperty() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient(getAsyncHttpProvider(null), new AsyncHttpClientConfig.Builder().build()); Assert.assertTrue(asyncHttpClient.getClass().equals(TestAsyncHttpClient.class)); @@ -146,6 +152,7 @@ public void testGetAsyncHttpClientConfigAndProviderWithSystemProperty() { @Test(groups = "fast") public void testGetAsyncHttpClientStringConfigWithSystemProperty() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient(getAsyncHttpProvider(null).getClass().getName(), new AsyncHttpClientConfig.Builder().build()); Assert.assertTrue(asyncHttpClient.getClass().equals(TestAsyncHttpClient.class)); @@ -161,6 +168,7 @@ public void testGetAsyncHttpClientStringConfigWithSystemProperty() { @Test(groups = "fast", expectedExceptions = BadAsyncHttpClientException.class) public void testFactoryWithBadAsyncHttpClient() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClientFactory.getAsyncHttpClient(); Assert.fail("BadAsyncHttpClientException should have been thrown before this point"); } @@ -168,6 +176,7 @@ public void testFactoryWithBadAsyncHttpClient() { @Test(groups = "fast") public void testGetAsyncHttpClientConfigWithBadAsyncHttpClient() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); try { AsyncHttpClientFactory.getAsyncHttpClient(new AsyncHttpClientConfig.Builder().build()); } catch (AsyncHttpClientImplException e) { @@ -179,6 +188,7 @@ public void testGetAsyncHttpClientConfigWithBadAsyncHttpClient() { @Test(groups = "fast") public void testGetAsyncHttpClientProviderWithBadAsyncHttpClient() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); try { AsyncHttpClientFactory.getAsyncHttpClient(getAsyncHttpProvider(null)); } catch (AsyncHttpClientImplException e) { @@ -190,6 +200,7 @@ public void testGetAsyncHttpClientProviderWithBadAsyncHttpClient() { @Test(groups = "fast") public void testGetAsyncHttpClientConfigAndProviderWithBadAsyncHttpClient() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); try { AsyncHttpClientFactory.getAsyncHttpClient(getAsyncHttpProvider(null), new AsyncHttpClientConfig.Builder().build()); } catch (AsyncHttpClientImplException e) { @@ -201,6 +212,7 @@ public void testGetAsyncHttpClientConfigAndProviderWithBadAsyncHttpClient() { @Test(groups = "fast") public void testGetAsyncHttpClientStringConfigWithBadAsyncHttpClient() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); try { AsyncHttpClientFactory.getAsyncHttpClient(getAsyncHttpProvider(null).getClass().getName(), new AsyncHttpClientConfig.Builder().build()); @@ -219,6 +231,7 @@ public void testGetAsyncHttpClientStringConfigWithBadAsyncHttpClient() { @Test(groups = "fast", expectedExceptions = AsyncHttpClientImplException.class) public void testFactoryWithNonExistentAsyncHttpClient() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, NON_EXISTENT_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClientFactory.getAsyncHttpClient(); Assert.fail("AsyncHttpClientImplException should have been thrown before this point"); } @@ -231,6 +244,7 @@ public void testFactoryWithNonExistentAsyncHttpClient() { public void testRepeatedCallsToBadAsyncHttpClient() { boolean exceptionCaught = false; System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, NON_EXISTENT_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); try { AsyncHttpClientFactory.getAsyncHttpClient(); } catch (AsyncHttpClientImplException e) { diff --git a/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AsyncHttpClientRegistryTest.java b/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AsyncHttpClientRegistryTest.java index c4e94ed128..b250a9d65b 100644 --- a/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AsyncHttpClientRegistryTest.java +++ b/extras/registry/src/test/java/org/asynchttpclient/extras/registry/AsyncHttpClientRegistryTest.java @@ -17,6 +17,7 @@ import org.asynchttpclient.extras.registry.AsyncHttpClientImplException; import org.asynchttpclient.extras.registry.AsyncHttpClientRegistryImpl; import org.asynchttpclient.extras.registry.AsyncImplHelper; +import org.asynchttpclient.util.AsyncPropertiesHelper; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -32,6 +33,7 @@ public class AsyncHttpClientRegistryTest { @BeforeMethod public void setUp() { System.clearProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClientRegistryImpl.getInstance().clearAllInstances(); PA.setValue(AsyncHttpClientRegistryImpl.class, "_instance", null); } @@ -95,12 +97,14 @@ public void testClearAllInstances() { @Test(groups = "fast") public void testCustomAsyncHttpClientRegistry() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY, TestAsyncHttpClientRegistry.class.getName()); + AsyncPropertiesHelper.reloadProperties(); Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance() instanceof TestAsyncHttpClientRegistry); } @Test(groups = "fast", expectedExceptions = AsyncHttpClientImplException.class) public void testNonExistentAsyncHttpClientRegistry() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY, AbstractAsyncHttpClientFactoryTest.NON_EXISTENT_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClientRegistryImpl.getInstance(); Assert.fail("Should never have reached here"); } @@ -108,6 +112,7 @@ public void testNonExistentAsyncHttpClientRegistry() { @Test(groups = "fast", expectedExceptions = AsyncHttpClientImplException.class) public void testBadAsyncHttpClientRegistry() { System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_REGISTRY_SYSTEM_PROPERTY, AbstractAsyncHttpClientFactoryTest.BAD_CLIENT_CLASS_NAME); + AsyncPropertiesHelper.reloadProperties(); AsyncHttpClientRegistryImpl.getInstance(); Assert.fail("Should never have reached here"); }