Issue 107: added proxy support, timeout parameters, and refactored ssl module

This commit is contained in:
Adrian Cole 2010-02-05 23:16:34 -08:00
parent 268bc2ec04
commit 7228d2ea04
30 changed files with 507 additions and 349 deletions

View File

@ -21,10 +21,9 @@ package org.jclouds.aws.ec2.config;
import static com.google.common.util.concurrent.Executors.sameThreadExecutor; import static com.google.common.util.concurrent.Executors.sameThreadExecutor;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.jclouds.Constants;
import org.jclouds.aws.ec2.EC2AsyncClient; import org.jclouds.aws.ec2.EC2AsyncClient;
import org.jclouds.aws.ec2.EC2Client; import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.reference.EC2Constants; import org.jclouds.aws.ec2.EC2PropertiesBuilder;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.logging.jdk.config.JDKLoggingModule; import org.jclouds.logging.jdk.config.JDKLoggingModule;
@ -52,22 +51,8 @@ public class EC2ContextModuleTest {
new EC2ContextModule() { new EC2ContextModule() {
@Override @Override
protected void configure() { protected void configure() {
bindConstant().annotatedWith( Jsr330.bindProperties(this.binder(), new EC2PropertiesBuilder("user", "key")
Jsr330.named(EC2Constants.PROPERTY_AWS_ACCESSKEYID)).to("user"); .build());
bindConstant().annotatedWith(
Jsr330.named(EC2Constants.PROPERTY_AWS_SECRETACCESSKEY)).to("key");
bindConstant().annotatedWith(Jsr330.named(EC2Constants.PROPERTY_EC2_ENDPOINT))
.to("http://localhost");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS)).to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS))
.to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST)).to("1");
bindConstant().annotatedWith(
Jsr330.named(EC2Constants.PROPERTY_AWS_EXPIREINTERVAL)).to(30);
super.configure(); super.configure();
} }
}); });

View File

@ -66,6 +66,11 @@
<artifactId>jets3t</artifactId> <artifactId>jets3t</artifactId>
<version>0.7.1</version> <version>0.7.1</version>
</dependency> </dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jclouds-enterprise</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>jclouds-httpnio</artifactId> <artifactId>jclouds-httpnio</artifactId>

View File

@ -0,0 +1,63 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.aws.s3;
import static org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT;
import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT;
import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST;
import static org.jclouds.Constants.PROPERTY_SO_TIMEOUT;
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import java.util.Properties;
import java.util.concurrent.Executors;
import org.jclouds.enterprise.config.EnterpriseConfigurationModule;
import org.jclouds.logging.config.NullLoggingModule;
import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@Test(sequential = true, testName = "perftest.JCloudsNioPerformanceLiveTest", groups = { "live" })
public class JCloudsEnterprisePerformanceLiveTest extends BaseJCloudsPerformanceLiveTest {
@Override
@BeforeClass(groups = { "integration", "live" })
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
exec = Executors.newCachedThreadPool();
String accesskeyid = System.getProperty("jclouds.test.user");
String secretkey = System.getProperty("jclouds.test.key");
Properties overrides = new Properties();
overrides.setProperty(PROPERTY_SO_TIMEOUT, 5000 + "");
overrides.setProperty(PROPERTY_CONNECTION_TIMEOUT, 5000 + "");
overrides.setProperty(PROPERTY_MAX_CONNECTIONS_PER_CONTEXT, 20 + "");
overrides.setProperty(PROPERTY_MAX_CONNECTIONS_PER_HOST, 0 + "");
overrides.setProperty(PROPERTY_IO_WORKER_THREADS, 50 + "");
overrides.setProperty(PROPERTY_USER_THREADS, 0 + "");
String contextName = "enterprise";
overrideWithSysPropertiesAndPrint(overrides, contextName);
context = S3ContextFactory.createContext(overrides, accesskeyid, secretkey,
new NullLoggingModule(), new EnterpriseConfigurationModule());
}
@Override
public S3AsyncClient getApi() {
return (S3AsyncClient) context.getProviderSpecificContext().getAsyncApi();
}
}

View File

@ -27,7 +27,8 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.enterprise.config.EnterpriseConfigurationModule; import org.jclouds.date.joda.config.JodaDateServiceModule;
import org.jclouds.encryption.bouncycastle.config.BouncyCastleEncryptionServiceModule;
import org.jclouds.gae.config.GoogleAppEngineConfigurationModule; import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
import org.jclouds.logging.config.NullLoggingModule; import org.jclouds.logging.config.NullLoggingModule;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
@ -191,8 +192,8 @@ public class JCloudsGaePerformanceLiveTest extends BaseJCloudsPerformanceLiveTes
String contextName = "gae"; String contextName = "gae";
overrideWithSysPropertiesAndPrint(overrides, contextName); overrideWithSysPropertiesAndPrint(overrides, contextName);
this.perfContext = S3ContextFactory.createContext(overrides, accesskeyid, secretkey, this.perfContext = S3ContextFactory.createContext(overrides, accesskeyid, secretkey,
new NullLoggingModule(), new EnterpriseConfigurationModule(), new NullLoggingModule(), new BouncyCastleEncryptionServiceModule(),
new GoogleAppEngineConfigurationModule()); new JodaDateServiceModule(), new GoogleAppEngineConfigurationModule());
} }
@AfterClass(groups = { "live" }) @AfterClass(groups = { "live" })

View File

@ -26,7 +26,8 @@ import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jclouds.enterprise.config.EnterpriseConfigurationModule; import org.jclouds.date.joda.config.JodaDateServiceModule;
import org.jclouds.encryption.bouncycastle.config.BouncyCastleEncryptionServiceModule;
import org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule; import org.jclouds.http.httpnio.config.NioTransformingHttpCommandExecutorServiceModule;
import org.jclouds.logging.config.NullLoggingModule; import org.jclouds.logging.config.NullLoggingModule;
import org.testng.ITestContext; import org.testng.ITestContext;
@ -50,8 +51,8 @@ public class JCloudsNioPerformanceLiveTest extends BaseJCloudsPerformanceLiveTes
String contextName = "nio"; String contextName = "nio";
overrideWithSysPropertiesAndPrint(overrides, contextName); overrideWithSysPropertiesAndPrint(overrides, contextName);
context = S3ContextFactory.createContext(overrides, accesskeyid, secretkey, context = S3ContextFactory.createContext(overrides, accesskeyid, secretkey,
new NullLoggingModule(), new EnterpriseConfigurationModule(), new NullLoggingModule(), new BouncyCastleEncryptionServiceModule(),
new NioTransformingHttpCommandExecutorServiceModule()); new JodaDateServiceModule(), new NioTransformingHttpCommandExecutorServiceModule());
} }
@Override @Override

View File

@ -32,7 +32,6 @@ import java.util.Map;
import javax.ws.rs.HttpMethod; import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import org.jclouds.Constants;
import org.jclouds.azure.storage.AzureBlob; import org.jclouds.azure.storage.AzureBlob;
import org.jclouds.azure.storage.blob.functions.ParseContainerPropertiesFromHeaders; import org.jclouds.azure.storage.blob.functions.ParseContainerPropertiesFromHeaders;
import org.jclouds.azure.storage.blob.functions.ReturnFalseIfContainerAlreadyExists; import org.jclouds.azure.storage.blob.functions.ReturnFalseIfContainerAlreadyExists;
@ -43,9 +42,7 @@ import org.jclouds.azure.storage.options.ListOptions;
import org.jclouds.azure.storage.reference.AzureStorageConstants; import org.jclouds.azure.storage.reference.AzureStorageConstants;
import org.jclouds.blobstore.functions.ReturnNullOnContainerNotFound; import org.jclouds.blobstore.functions.ReturnNullOnContainerNotFound;
import org.jclouds.blobstore.functions.ReturnVoidOnNotFoundOr404; import org.jclouds.blobstore.functions.ReturnVoidOnNotFoundOr404;
import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.http.functions.CloseContentAndReturn; import org.jclouds.http.functions.CloseContentAndReturn;
import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ParseSax;
@ -366,24 +363,10 @@ public class AzureBlobAsyncClientTest {
Injector injector = Guice.createInjector(new AbstractModule() { Injector injector = Guice.createInjector(new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
Jsr330.bindProperties(this.binder(), new AzureBlobPropertiesBuilder("user", "key")
.build());
bind(URI.class).annotatedWith(AzureBlob.class).toInstance( bind(URI.class).annotatedWith(AzureBlob.class).toInstance(
URI.create("http://myaccount.blob.core.windows.net")); URI.create("http://myaccount.blob.core.windows.net"));
bindConstant().annotatedWith(
Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)).to(
"myaccount");
bindConstant().annotatedWith(
Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to(
Base64.encodeBytes("key".getBytes()));
bindConstant().annotatedWith(
Jsr330.named(BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX)).to(
"x-ms-meta-");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS))
.to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -39,9 +39,9 @@ import org.jclouds.azure.storage.queue.options.PutMessageOptions;
import org.jclouds.azure.storage.reference.AzureStorageConstants; import org.jclouds.azure.storage.reference.AzureStorageConstants;
import org.jclouds.date.TimeStamp; import org.jclouds.date.TimeStamp;
import org.jclouds.encryption.internal.Base64; import org.jclouds.encryption.internal.Base64;
import org.jclouds.http.functions.CloseContentAndReturn;
import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ReturnTrueIf2xx; import org.jclouds.http.functions.ReturnTrueIf2xx;
import org.jclouds.http.functions.CloseContentAndReturn;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rest.RestClientTest; import org.jclouds.rest.RestClientTest;

View File

@ -37,7 +37,7 @@ public interface Constants {
* Amount of threads servicing the I/O of http connections. * Amount of threads servicing the I/O of http connections.
*/ */
public static final String PROPERTY_IO_WORKER_THREADS = "jclouds.io_worker_threads"; public static final String PROPERTY_IO_WORKER_THREADS = "jclouds.io_worker_threads";
/** /**
* Integer property. default (20) * Integer property. default (20)
* <p/> * <p/>
@ -46,9 +46,10 @@ public interface Constants {
public static final String PROPERTY_MAX_CONNECTIONS_PER_CONTEXT = "jclouds.max_connections_per_context"; public static final String PROPERTY_MAX_CONNECTIONS_PER_CONTEXT = "jclouds.max_connections_per_context";
/** /**
* Integer property. default (12) * Integer property. default (0)
* <p/> * <p/>
* Limits the amount of connections per host. * Limits the amount of connections per host. 0 means indirectly limited by
* {@link #PROPERTY_MAX_CONNECTIONS_PER_CONTEXT}.
*/ */
public static final String PROPERTY_MAX_CONNECTIONS_PER_HOST = "jclouds.max_connections_per_host"; public static final String PROPERTY_MAX_CONNECTIONS_PER_HOST = "jclouds.max_connections_per_host";
@ -66,6 +67,20 @@ public interface Constants {
*/ */
public static final String PROPERTY_MAX_CONNECTION_REUSE = "jclouds.max_connection_reuse"; public static final String PROPERTY_MAX_CONNECTION_REUSE = "jclouds.max_connection_reuse";
/**
* int property. default (60000)
* <p/>
* How many milliseconds to wait before a socket connection times out. 0 means infinity.
*/
public static final String PROPERTY_SO_TIMEOUT = "jclouds.so_timeout";
/**
* Long property. default (60000)
* <p/>
* How many milliseconds to wait before a connection times out. 0 means infinity.
*/
public static final String PROPERTY_CONNECTION_TIMEOUT = "jclouds.connection_timeout";
/** /**
* Boolean property. * Boolean property.
* <p/> * <p/>

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds; package org.jclouds;
import static org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT;
import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS; import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT; import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT;
import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST; import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST;
@ -27,6 +28,7 @@ import static org.jclouds.Constants.PROPERTY_MAX_RETRIES;
import static org.jclouds.Constants.PROPERTY_MAX_SESSION_FAILURES; import static org.jclouds.Constants.PROPERTY_MAX_SESSION_FAILURES;
import static org.jclouds.Constants.PROPERTY_PROXY_SYSTEM; import static org.jclouds.Constants.PROPERTY_PROXY_SYSTEM;
import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME; import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME;
import static org.jclouds.Constants.PROPERTY_SO_TIMEOUT;
import static org.jclouds.Constants.PROPERTY_USER_THREADS; import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import java.net.URI; import java.net.URI;
@ -57,10 +59,26 @@ public abstract class PropertiesBuilder {
return this; return this;
} }
/**
* @see org.jclouds.Constants.PROPERTY_SO_TIMEOUT
*/
public PropertiesBuilder withSOTimeout(long soTimeout) {
properties.setProperty(PROPERTY_SO_TIMEOUT, Long.toString(soTimeout));
return this;
}
/**
* @see org.jclouds.Constants.PROPERTY_CONNECTION_TIMEOUT
*/
public PropertiesBuilder withConnectionTimeout(long connectionTimeout) {
properties.setProperty(PROPERTY_CONNECTION_TIMEOUT, Long.toString(connectionTimeout));
return this;
}
/** /**
* @see org.jclouds.Constants.PROPERTY_MAX_RETRIES * @see org.jclouds.Constants.PROPERTY_MAX_RETRIES
*/ */
public PropertiesBuilder withHttpMaxRetries(int httpMaxRetries) { public PropertiesBuilder withMaxRetries(int httpMaxRetries) {
properties.setProperty(PROPERTY_MAX_RETRIES, Integer.toString(httpMaxRetries)); properties.setProperty(PROPERTY_MAX_RETRIES, Integer.toString(httpMaxRetries));
return this; return this;
} }
@ -68,7 +86,7 @@ public abstract class PropertiesBuilder {
/** /**
* @see org.jclouds.Constants.PROPERTY_MAX_REDIRECTS * @see org.jclouds.Constants.PROPERTY_MAX_REDIRECTS
*/ */
public PropertiesBuilder withHttpMaxRedirects(int httpMaxRedirects) { public PropertiesBuilder withMaxRedirects(int httpMaxRedirects) {
properties.setProperty(PROPERTY_MAX_REDIRECTS, Integer.toString(httpMaxRedirects)); properties.setProperty(PROPERTY_MAX_REDIRECTS, Integer.toString(httpMaxRedirects));
return this; return this;
} }
@ -133,7 +151,9 @@ public abstract class PropertiesBuilder {
protected Properties defaultProperties() { protected Properties defaultProperties() {
Properties props = new Properties(); Properties props = new Properties();
props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_CONTEXT, 20 + ""); props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_CONTEXT, 20 + "");
props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_HOST, 12 + ""); props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_HOST, 0 + "");
props.setProperty(PROPERTY_SO_TIMEOUT, 60000 + "");
props.setProperty(PROPERTY_CONNECTION_TIMEOUT, 60000 + "");
props.setProperty(PROPERTY_IO_WORKER_THREADS, 20 + ""); props.setProperty(PROPERTY_IO_WORKER_THREADS, 20 + "");
props.setProperty(PROPERTY_USER_THREADS, 0 + ""); props.setProperty(PROPERTY_USER_THREADS, 0 + "");
props.setProperty(PROPERTY_MAX_CONNECTION_REUSE, 75 + ""); props.setProperty(PROPERTY_MAX_CONNECTION_REUSE, 75 + "");

View File

@ -39,6 +39,9 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import javax.inject.Named;
import org.jclouds.Constants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.util.Utils; import org.jclouds.util.Utils;
@ -51,11 +54,61 @@ import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.TreeMultimap; import com.google.common.collect.TreeMultimap;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.inject.Inject;
/** /**
* @author Adrian Cole * @author Adrian Cole
*/ */
public class HttpUtils { public class HttpUtils {
@Inject(optional = true)
@Named(Constants.PROPERTY_RELAX_HOSTNAME)
private boolean relaxHostname = false;
@Inject(optional = true)
@Named(Constants.PROPERTY_PROXY_SYSTEM)
private boolean systemProxies = System.getProperty("java.net.useSystemProxies") != null ? Boolean
.parseBoolean(System.getProperty("java.net.useSystemProxies"))
: false;
private final int globalMaxConnections;
private final int globalMaxConnectionsPerHost;
private final int connectionTimeout;
private final int soTimeout;
@Inject
public HttpUtils(@Named(Constants.PROPERTY_CONNECTION_TIMEOUT) int connectionTimeout,
@Named(Constants.PROPERTY_SO_TIMEOUT) int soTimeout,
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT) int globalMaxConnections,
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST) int globalMaxConnectionsPerHost) {
this.soTimeout = soTimeout;
this.connectionTimeout = connectionTimeout;
this.globalMaxConnections = globalMaxConnections;
this.globalMaxConnectionsPerHost = globalMaxConnectionsPerHost;
}
public int getSocketOpenTimeout() {
return soTimeout;
}
public int getConnectionTimeout() {
return connectionTimeout;
}
public boolean relaxHostname() {
return relaxHostname;
}
public boolean useSystemProxies() {
return systemProxies;
}
public int getMaxConnections() {
return globalMaxConnections;
}
public int getMaxConnectionsPerHost() {
return globalMaxConnectionsPerHost;
}
/** /**
* keys to the map are only used for socket information, not path. In this case, you should * keys to the map are only used for socket information, not path. In this case, you should

View File

@ -18,11 +18,20 @@
*/ */
package org.jclouds.http.config; package org.jclouds.http.config;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Singleton;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import org.jclouds.http.HttpCommandExecutorService; import org.jclouds.http.HttpCommandExecutorService;
import org.jclouds.http.TransformingHttpCommandExecutorService; import org.jclouds.http.TransformingHttpCommandExecutorService;
import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl; import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl;
import org.jclouds.http.internal.JavaUrlHttpCommandExecutorService; import org.jclouds.http.internal.JavaUrlHttpCommandExecutorService;
import org.jclouds.logging.Logger;
import com.google.common.collect.Maps;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Scopes; import com.google.inject.Scopes;
@ -44,9 +53,27 @@ public class JavaUrlHttpCommandExecutorServiceModule extends AbstractModule {
protected void bindClient() { protected void bindClient() {
bind(HttpCommandExecutorService.class).to(JavaUrlHttpCommandExecutorService.class).in( bind(HttpCommandExecutorService.class).to(JavaUrlHttpCommandExecutorService.class).in(
Scopes.SINGLETON); Scopes.SINGLETON);
bind(HostnameVerifier.class).to(LogToMapHostnameVerifier.class);
bind(TransformingHttpCommandExecutorService.class).to( bind(TransformingHttpCommandExecutorService.class).to(
TransformingHttpCommandExecutorServiceImpl.class).in(Scopes.SINGLETON); TransformingHttpCommandExecutorServiceImpl.class).in(Scopes.SINGLETON);
} }
/**
*
* Used to get more information about HTTPS hostname wrong errors.
*
* @author Adrian Cole
*/
@Singleton
static class LogToMapHostnameVerifier implements HostnameVerifier {
@Resource
private Logger logger = Logger.NULL;
private final Map<String, String> sslMap = Maps.newHashMap();;
public boolean verify(String hostname, SSLSession session) {
logger.warn("hostname was %s while session was %s", hostname, session.getPeerHost());
sslMap.put(hostname, session.getPeerHost());
return true;
}
}
} }

View File

@ -58,7 +58,7 @@ public abstract class BaseHttpCommandExecutorService<Q> implements HttpCommandEx
@Named(Constants.LOGGER_HTTP_HEADERS) @Named(Constants.LOGGER_HTTP_HEADERS)
protected Logger headerLog = Logger.NULL; protected Logger headerLog = Logger.NULL;
private final HttpWire wire; protected final HttpWire wire;
@Inject @Inject
protected BaseHttpCommandExecutorService( protected BaseHttpCommandExecutorService(

View File

@ -27,26 +27,26 @@ import java.net.HttpURLConnection;
import java.net.Proxy; import java.net.Proxy;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.net.URL; import java.net.URL;
import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.http.HttpCommandExecutorService; import org.jclouds.http.HttpCommandExecutorService;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.handlers.DelegatingErrorHandler; import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler; import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.logging.Logger;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -61,44 +61,54 @@ public class JavaUrlHttpCommandExecutorService extends
BaseHttpCommandExecutorService<HttpURLConnection> { BaseHttpCommandExecutorService<HttpURLConnection> {
public static final String USER_AGENT = "jclouds/1.0 java/" + System.getProperty("java.version"); public static final String USER_AGENT = "jclouds/1.0 java/" + System.getProperty("java.version");
@Resource
@Inject(optional = true) protected Logger logger = Logger.NULL;
@Named(Constants.PROPERTY_RELAX_HOSTNAME) private final HostnameVerifier verifier;
private boolean relaxHostname = false; private final HttpUtils utils;
private final Map<String, String> sslMap;
@Inject(optional = true)
@Named(Constants.PROPERTY_PROXY_SYSTEM)
private boolean systemProxies = System.getProperty("java.net.useSystemProxies") != null ? Boolean
.parseBoolean(System.getProperty("java.net.useSystemProxies"))
: false;
@Inject @Inject
public JavaUrlHttpCommandExecutorService( public JavaUrlHttpCommandExecutorService(
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor,
DelegatingRetryHandler retryHandler, DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler,
DelegatingErrorHandler errorHandler, HttpWire wire, HttpUtils utils, HostnameVerifier verifier) {
HttpWire wire,
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT) final int globalMaxConnections,
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST) final int globalMaxConnectionsPerHost) {
super(ioWorkerExecutor, retryHandler, errorHandler, wire); super(ioWorkerExecutor, retryHandler, errorHandler, wire);
sslMap = Maps.newHashMap(); if (utils.getMaxConnections() > 0)
if (globalMaxConnections > 0) System.setProperty("http.maxConnections", String.valueOf(utils.getMaxConnections()));
System.setProperty("http.maxConnections", String.valueOf(globalMaxConnections)); this.utils = utils;
this.verifier = verifier;
} }
/** @Override
* protected HttpResponse invoke(HttpURLConnection connection) throws IOException,
* Used to get more information about HTTPS hostname wrong errors. InterruptedException {
* HttpResponse response = new HttpResponse();
* @author Adrian Cole InputStream in = null;
*/ try {
class LogToMapHostnameVerifier implements HostnameVerifier { in = consumeOnClose(connection.getInputStream());
public boolean verify(String hostname, SSLSession session) { } catch (IOException e) {
logger.warn("hostname was %s while session was %s", hostname, session.getPeerHost()); in = bufferAndCloseStream(connection.getErrorStream());
sslMap.put(hostname, session.getPeerHost()); } catch (RuntimeException e) {
return true; Closeables.closeQuietly(in);
Throwables.propagate(e);
assert false : "should have propagated exception";
} }
for (String header : connection.getHeaderFields().keySet()) {
response.getHeaders().putAll(header, connection.getHeaderFields().get(header));
}
if (connection.getResponseCode() == 204) {
Closeables.closeQuietly(in);
in = null;
} else if (in != null) {
response.setContent(in);
}
response.setStatusCode(connection.getResponseCode());
response.setMessage(connection.getResponseMessage());
return response;
}
public InputStream consumeOnClose(InputStream in) {
return new ConsumeOnCloseInputStream(in);
} }
class ConsumeOnCloseInputStream extends FilterInputStream { class ConsumeOnCloseInputStream extends FilterInputStream {
@ -128,33 +138,6 @@ public class JavaUrlHttpCommandExecutorService extends
} }
@Override
protected HttpResponse invoke(HttpURLConnection connection) throws IOException,
InterruptedException {
HttpResponse response = new HttpResponse();
InputStream in = null;
try {
in = new ConsumeOnCloseInputStream(connection.getInputStream());
} catch (IOException e) {
in = bufferAndCloseStream(connection.getErrorStream());
} catch (RuntimeException e) {
Closeables.closeQuietly(in);
Throwables.propagate(e);
assert false : "should have propagated exception";
}
for (String header : connection.getHeaderFields().keySet()) {
response.getHeaders().putAll(header, connection.getHeaderFields().get(header));
}
if (connection.getResponseCode() == 204) {
Closeables.closeQuietly(in);
in = null;
} else if (in != null) {
response.setContent(in);
}
response.setStatusCode(connection.getResponseCode());
response.setMessage(connection.getResponseMessage());
return response;
}
private InputStream bufferAndCloseStream(InputStream inputStream) throws IOException { private InputStream bufferAndCloseStream(InputStream inputStream) throws IOException {
InputStream in = null; InputStream in = null;
@ -173,7 +156,7 @@ public class JavaUrlHttpCommandExecutorService extends
InterruptedException { InterruptedException {
URL url = request.getEndpoint().toURL(); URL url = request.getEndpoint().toURL();
HttpURLConnection connection; HttpURLConnection connection;
if (systemProxies) { if (utils.useSystemProxies()) {
System.setProperty("java.net.useSystemProxies", "true"); System.setProperty("java.net.useSystemProxies", "true");
Iterable<Proxy> proxies = ProxySelector.getDefault().select(request.getEndpoint()); Iterable<Proxy> proxies = ProxySelector.getDefault().select(request.getEndpoint());
Proxy proxy = Iterables.getLast(proxies); Proxy proxy = Iterables.getLast(proxies);
@ -181,9 +164,9 @@ public class JavaUrlHttpCommandExecutorService extends
} else { } else {
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
} }
if (relaxHostname && connection instanceof HttpsURLConnection) { if (utils.relaxHostname() && connection instanceof HttpsURLConnection) {
HttpsURLConnection sslCon = (HttpsURLConnection) connection; HttpsURLConnection sslCon = (HttpsURLConnection) connection;
sslCon.setHostnameVerifier(new LogToMapHostnameVerifier()); sslCon.setHostnameVerifier(verifier);
} }
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setAllowUserInteraction(false); connection.setAllowUserInteraction(false);

View File

@ -51,7 +51,7 @@ public class HttpPropertiesBuilderTest {
return this; return this;
} }
}; };
builder.withHttpMaxRetries(httpMaxRetries); builder.withMaxRetries(httpMaxRetries);
builder.limitIoWorkerThreadsTo(poolIoWorkerThreads); builder.limitIoWorkerThreadsTo(poolIoWorkerThreads);
builder.withMaxClientReuse(poolMaxClientReuse); builder.withMaxClientReuse(poolMaxClientReuse);

View File

@ -32,6 +32,7 @@ import org.jclouds.PropertiesBuilder;
import org.jclouds.http.BaseJettyTest; import org.jclouds.http.BaseJettyTest;
import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.IntegrationTestAsyncClient; import org.jclouds.http.IntegrationTestAsyncClient;
import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl; import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl;
import org.jclouds.http.TransformingHttpCommandImpl; import org.jclouds.http.TransformingHttpCommandImpl;
@ -93,7 +94,7 @@ public class BackoffLimitedRetryHandlerTest {
ExecutorService execService = Executors.newCachedThreadPool(); ExecutorService execService = Executors.newCachedThreadPool();
JavaUrlHttpCommandExecutorService httpService = new JavaUrlHttpCommandExecutorService( JavaUrlHttpCommandExecutorService httpService = new JavaUrlHttpCommandExecutorService(
execService, new DelegatingRetryHandler(), new DelegatingErrorHandler(), execService, new DelegatingRetryHandler(), new DelegatingErrorHandler(),
new HttpWire(), 1, 1); new HttpWire(), new HttpUtils(0, 500, 1, 1), null);
executorService = new TransformingHttpCommandExecutorServiceImpl(httpService); executorService = new TransformingHttpCommandExecutorServiceImpl(httpService);
} }

View File

@ -19,31 +19,16 @@
package org.jclouds.http.apachehc; package org.jclouds.http.apachehc;
import static com.google.common.base.Preconditions.checkState;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import javax.annotation.PreDestroy;
import javax.inject.Named; import javax.inject.Named;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnManagerParams; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
@ -52,7 +37,6 @@ import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.internal.BaseHttpCommandExecutorService; import org.jclouds.http.internal.BaseHttpCommandExecutorService;
import org.jclouds.http.internal.HttpWire; import org.jclouds.http.internal.HttpWire;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject; import com.google.inject.Inject;
/** /**
@ -62,78 +46,39 @@ import com.google.inject.Inject;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class ApacheHCHttpCommandExecutorService extends public class ApacheHCHttpCommandExecutorService extends
BaseHttpCommandExecutorService<HttpEntityEnclosingRequest> { BaseHttpCommandExecutorService<HttpUriRequest> {
@VisibleForTesting private final HttpClient client;
boolean isOpen = true;
private final BasicHttpParams params;
private final ThreadSafeClientConnManager cm;
private final ApacheHCUtils utils;
@Inject @Inject
ApacheHCHttpCommandExecutorService( ApacheHCHttpCommandExecutorService(
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor,
DelegatingRetryHandler retryHandler, DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler,
DelegatingErrorHandler errorHandler, HttpWire wire, HttpClient client) {
HttpWire wire,
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT) final int globalMaxConnections,
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST) final int globalMaxConnectionsPerHost,
ApacheHCUtils utils) {
super(ioWorkerExecutor, retryHandler, errorHandler, wire); super(ioWorkerExecutor, retryHandler, errorHandler, wire);
this.utils = utils; this.client = client;
params = new BasicHttpParams();
// TODO: have this use our executor service, if possible
if (globalMaxConnections > 0)
ConnManagerParams.setMaxTotalConnections(params, globalMaxConnections);
if (globalMaxConnectionsPerHost > 0) {
ConnPerRoute connectionsPerRoute = new ConnPerRouteBean(globalMaxConnectionsPerHost);
ConnManagerParams.setMaxConnectionsPerRoute(params, connectionsPerRoute);
}
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
cm = new ThreadSafeClientConnManager(params, schemeRegistry);
} }
@Override @Override
protected HttpEntityEnclosingRequest convert(HttpRequest request) throws IOException { protected HttpUriRequest convert(HttpRequest request) throws IOException {
return utils.convertToApacheRequest(request); return ApacheHCUtils.convertToApacheRequest(request);
} }
@Override @Override
protected HttpResponse invoke(HttpEntityEnclosingRequest nativeRequest) throws IOException { protected HttpResponse invoke(HttpUriRequest nativeRequest) throws IOException {
checkState(isOpen, "http executor not open");
org.apache.http.HttpResponse nativeResponse = executeRequest(nativeRequest); org.apache.http.HttpResponse nativeResponse = executeRequest(nativeRequest);
return utils.convertToJCloudsResponse(nativeResponse); return ApacheHCUtils.convertToJCloudsResponse(nativeResponse);
} }
private org.apache.http.HttpResponse executeRequest(HttpEntityEnclosingRequest nativeRequest) private org.apache.http.HttpResponse executeRequest(HttpUriRequest nativeRequest)
throws IOException, ClientProtocolException { throws IOException, ClientProtocolException {
URI endpoint = URI.create(nativeRequest.getRequestLine().getUri()); URI endpoint = URI.create(nativeRequest.getRequestLine().getUri());
HttpClient client = new DefaultHttpClient(cm, params);
HttpHost host = new HttpHost(endpoint.getHost(), endpoint.getPort(), endpoint.getScheme()); HttpHost host = new HttpHost(endpoint.getHost(), endpoint.getPort(), endpoint.getScheme());
org.apache.http.HttpResponse nativeResponse = client.execute(host, nativeRequest); org.apache.http.HttpResponse nativeResponse = client.execute(host, nativeRequest);
return nativeResponse; return nativeResponse;
} }
@PreDestroy
public void close() {
// TODO test
isOpen = false;
cm.shutdown();
}
@Override @Override
protected void finalize() throws Throwable { protected void cleanup(HttpUriRequest nativeResponse) {
try {
close();
} finally {
super.finalize();
}
}
@Override
protected void cleanup(HttpEntityEnclosingRequest nativeResponse) {
// No cleanup necessary // No cleanup necessary
} }
} }

View File

@ -24,22 +24,26 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpVersion; import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.FileEntity; import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.Payload; import org.jclouds.http.Payload;
import org.jclouds.logging.Logger;
/** /**
* *
@ -47,31 +51,42 @@ import org.jclouds.logging.Logger;
*/ */
@Singleton @Singleton
public class ApacheHCUtils { public class ApacheHCUtils {
public static final String USER_AGENT = "jclouds/1.0 httpclient/4.0.1"; public static final String USER_AGENT = "jclouds/1.0 httpclient/4.0.1";
@Resource public static HttpUriRequest convertToApacheRequest(HttpRequest request) {
protected Logger logger = Logger.NULL; HttpUriRequest apacheRequest;
if (request.getMethod().equals(HttpMethod.HEAD)) {
public HttpEntityEnclosingRequest convertToApacheRequest(HttpRequest request) { apacheRequest = new HttpHead(request.getEndpoint());
} else if (request.getMethod().equals(HttpMethod.GET)) {
String uri = request.getEndpoint().toASCIIString(); apacheRequest = new HttpGet(request.getEndpoint());
if (request.getEndpoint().getQuery() != null) } else if (request.getMethod().equals(HttpMethod.DELETE)) {
uri += "?" + request.getEndpoint().getQuery(); apacheRequest = new HttpDelete(request.getEndpoint());
BasicHttpEntityEnclosingRequest apacheRequest = new BasicHttpEntityEnclosingRequest(request } else if (request.getMethod().equals(HttpMethod.PUT)) {
.getMethod(), uri, HttpVersion.HTTP_1_1); apacheRequest = new HttpPut(request.getEndpoint());
} else if (request.getMethod().equals(HttpMethod.POST)) {
apacheRequest = new HttpPost(request.getEndpoint());
} else {
throw new UnsupportedOperationException(request.getMethod());
}
Payload payload = request.getPayload(); Payload payload = request.getPayload();
// Since we may remove headers, ensure they are added to the apache // Since we may remove headers, ensure they are added to the apache
// request after this block // request after this block
if (payload != null) { if (apacheRequest instanceof HttpEntityEnclosingRequest) {
String lengthString = request.getFirstHeaderOrNull(HttpHeaders.CONTENT_LENGTH); if (payload != null) {
if (lengthString == null) { String lengthString = request.getFirstHeaderOrNull(HttpHeaders.CONTENT_LENGTH);
throw new IllegalStateException("no Content-Length header on request: " + apacheRequest); if (lengthString == null) {
throw new IllegalStateException("no Content-Length header on request: "
+ apacheRequest);
}
long contentLength = Long.parseLong(lengthString);
String contentType = request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE);
addEntityForContent(HttpEntityEnclosingRequest.class.cast(apacheRequest), payload
.getRawContent(), contentType, contentLength);
} }
long contentLength = Long.parseLong(lengthString); } else {
String contentType = request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE); apacheRequest.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
addEntityForContent(apacheRequest, payload.getRawContent(), contentType, contentLength);
} }
for (String header : request.getHeaders().keySet()) { for (String header : request.getHeaders().keySet()) {
@ -81,11 +96,10 @@ public class ApacheHCUtils {
apacheRequest.addHeader(header, value); apacheRequest.addHeader(header, value);
} }
apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT); apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
return apacheRequest; return apacheRequest;
} }
public void addEntityForContent(HttpEntityEnclosingRequest apacheRequest, Object content, public static void addEntityForContent(HttpEntityEnclosingRequest apacheRequest, Object content,
String contentType, long length) { String contentType, long length) {
if (content instanceof InputStream) { if (content instanceof InputStream) {
InputStream inputStream = (InputStream) content; InputStream inputStream = (InputStream) content;
@ -117,12 +131,13 @@ public class ApacheHCUtils {
assert (apacheRequest.getEntity() != null); assert (apacheRequest.getEntity() != null);
} }
public HttpResponse convertToJCloudsResponse(org.apache.http.HttpResponse apacheResponse) public static HttpResponse convertToJCloudsResponse(org.apache.http.HttpResponse apacheResponse)
throws IOException { throws IOException {
HttpResponse response = new HttpResponse(); HttpResponse response = new HttpResponse();
if (apacheResponse.getEntity() != null) { if (apacheResponse.getEntity() != null) {
response // response.setContent(consumeOnClose(apacheResponse.getEntity()));
.setContent(new ConsumeOnCloseInputStream(apacheResponse.getEntity().getContent())); response.setContent(apacheResponse.getEntity().getContent());
} }
for (Header header : apacheResponse.getAllHeaders()) { for (Header header : apacheResponse.getAllHeaders()) {
response.getHeaders().put(header.getName(), header.getValue()); response.getHeaders().put(header.getName(), header.getValue());
@ -132,10 +147,19 @@ public class ApacheHCUtils {
return response; return response;
} }
class ConsumeOnCloseInputStream extends FilterInputStream { public static InputStream consumeOnClose(HttpEntity httpEntity) throws IllegalStateException,
IOException {
return new ConsumeOnCloseInputStream(httpEntity);
}
protected ConsumeOnCloseInputStream(InputStream in) { static class ConsumeOnCloseInputStream extends FilterInputStream {
super(in);
private final HttpEntity httpEntity;
protected ConsumeOnCloseInputStream(HttpEntity httpEntity) throws IllegalStateException,
IOException {
super(httpEntity.getContent());
this.httpEntity = httpEntity;
} }
boolean closed; boolean closed;
@ -144,13 +168,8 @@ public class ApacheHCUtils {
public void close() throws IOException { public void close() throws IOException {
try { try {
if (!closed) { if (!closed) {
int result = 0; httpEntity.consumeContent();
while (result != -1) {
result = read();
}
} }
} catch (IOException e) {
logger.warn(e, "error reading stream");
} finally { } finally {
closed = true; closed = true;
super.close(); super.close();

View File

@ -18,13 +18,44 @@
*/ */
package org.jclouds.http.apachehc.config; package org.jclouds.http.apachehc.config;
import java.io.Closeable;
import java.io.IOException;
import java.net.ProxySelector;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.inject.Singleton;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRoute;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.jclouds.http.HttpCommandExecutorService; import org.jclouds.http.HttpCommandExecutorService;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.TransformingHttpCommandExecutorService; import org.jclouds.http.TransformingHttpCommandExecutorService;
import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl; import org.jclouds.http.TransformingHttpCommandExecutorServiceImpl;
import org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService; import org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService;
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService; import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
import org.jclouds.lifecycle.Closer;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Scopes; import com.google.inject.Scopes;
/** /**
@ -43,6 +74,89 @@ public class ApacheHCHttpCommandExecutorServiceModule extends AbstractModule {
bindClient(); bindClient();
} }
@Singleton
@Provides
HttpParams newBasicHttpParams(HttpUtils utils) {
BasicHttpParams params = new BasicHttpParams();
params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true)
.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true).setParameter(
CoreProtocolPNames.ORIGIN_SERVER, "jclouds/1.0");
if (utils.getConnectionTimeout() > 0) {
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, utils
.getConnectionTimeout());
}
if (utils.getSocketOpenTimeout() > 0) {
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, utils.getSocketOpenTimeout());
}
if (utils.getMaxConnections() > 0)
ConnManagerParams.setMaxTotalConnections(params, utils.getMaxConnections());
if (utils.getMaxConnectionsPerHost() > 0) {
ConnPerRoute connectionsPerRoute = new ConnPerRouteBean(utils.getMaxConnectionsPerHost());
ConnManagerParams.setMaxConnectionsPerRoute(params, connectionsPerRoute);
}
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
return params;
}
@Singleton
@Provides
X509HostnameVerifier newHostnameVerifier(HttpUtils utils) {
return utils.relaxHostname() ? SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
: SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
}
@Singleton
@Provides
ClientConnectionManager newClientConnectionManager(HttpParams params,
X509HostnameVerifier verifier, Closer closer) throws NoSuchAlgorithmException,
KeyManagementException {
SchemeRegistry schemeRegistry = new SchemeRegistry();
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
SSLSocketFactory sf = new SSLSocketFactory(context);
sf.setHostnameVerifier(verifier);
Scheme https = new Scheme("https", sf, 443);
SchemeRegistry sr = new SchemeRegistry();
sr.register(http);
sr.register(https);
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
final ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
closer.addToClose(new Closeable() {
@Override
public void close() throws IOException {
cm.shutdown();
}
});
return cm;
}
@Provides
@Singleton
HttpClient newDefaultHttpClient(HttpUtils utils, BasicHttpParams params,
ClientConnectionManager cm) {
DefaultHttpClient client = new DefaultHttpClient(cm, params);
if (utils.useSystemProxies()) {
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(client
.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
client.setRoutePlanner(routePlanner);
}
return client;
}
protected void bindClient() { protected void bindClient() {
bind(HttpCommandExecutorService.class).to(ApacheHCHttpCommandExecutorService.class).in( bind(HttpCommandExecutorService.class).to(ApacheHCHttpCommandExecutorService.class).in(
Scopes.SINGLETON); Scopes.SINGLETON);

View File

@ -18,6 +18,11 @@
*/ */
package org.jclouds.http.apachehc; package org.jclouds.http.apachehc;
import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
import static org.jclouds.Constants.*;
import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST;
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import java.util.Properties; import java.util.Properties;
import org.jclouds.http.BaseHttpCommandExecutorServiceTest; import org.jclouds.http.BaseHttpCommandExecutorServiceTest;
@ -33,13 +38,21 @@ import com.google.inject.Module;
*/ */
@Test @Test
public class ApacheHCHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorServiceTest { public class ApacheHCHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorServiceTest {
static {
System.setProperty("http.conn-manager.timeout", 1000 + "");
}
protected Module createConnectionModule() { protected Module createConnectionModule() {
return new ApacheHCHttpCommandExecutorServiceModule(); return new ApacheHCHttpCommandExecutorServiceModule();
} }
protected void addConnectionProperties(Properties props) { protected void addConnectionProperties(Properties props) {
props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_CONTEXT, 20 + "");
props.setProperty(PROPERTY_MAX_CONNECTIONS_PER_HOST, 0 + "");
props.setProperty(PROPERTY_CONNECTION_TIMEOUT, 100 + "");
props.setProperty(PROPERTY_SO_TIMEOUT, 100 + "");
props.setProperty(PROPERTY_IO_WORKER_THREADS, 3 + "");
props.setProperty(PROPERTY_USER_THREADS, 0 + "");
} }
} }

View File

@ -18,7 +18,7 @@
*/ */
package org.jclouds.http.httpnio.config; package org.jclouds.http.httpnio.config;
import static com.google.common.base.Preconditions.*; import static com.google.common.base.Preconditions.checkState;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
@ -36,6 +36,7 @@ import javax.inject.Singleton;
import org.apache.http.ConnectionReuseStrategy; import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpVersion;
import org.apache.http.impl.DefaultConnectionReuseStrategy; import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor; import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.nio.NHttpConnection; import org.apache.http.nio.NHttpConnection;
@ -50,6 +51,7 @@ import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpProcessor; import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.RequestConnControl; import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent; import org.apache.http.protocol.RequestContent;
@ -58,6 +60,7 @@ import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent; import org.apache.http.protocol.RequestUserAgent;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.http.HttpCommandRendezvous; import org.jclouds.http.HttpCommandRendezvous;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.TransformingHttpCommandExecutorService; import org.jclouds.http.TransformingHttpCommandExecutorService;
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService; import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
import org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool; import org.jclouds.http.httpnio.pool.NioHttpCommandConnectionPool;
@ -101,15 +104,26 @@ public class NioTransformingHttpCommandExecutorServiceModule extends
return httpproc; return httpproc;
} }
@Provides
@Singleton @Singleton
public HttpParams provideHttpParams() { @Provides
HttpParams params = new BasicHttpParams(); HttpParams newBasicHttpParams(HttpUtils utils) {
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000).setIntParameter( BasicHttpParams params = new BasicHttpParams();
CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024).setBooleanParameter(
CoreConnectionPNames.STALE_CONNECTION_CHECK, false).setBooleanParameter( params.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
CoreConnectionPNames.TCP_NODELAY, true).setParameter( .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true)
CoreProtocolPNames.ORIGIN_SERVER, "jclouds/1.0"); .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true).setParameter(
CoreProtocolPNames.ORIGIN_SERVER, "jclouds/1.0");
if (utils.getConnectionTimeout() > 0) {
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, utils
.getConnectionTimeout());
}
if (utils.getSocketOpenTimeout() > 0) {
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, utils.getSocketOpenTimeout());
}
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
return params; return params;
} }
@ -198,9 +212,9 @@ public class NioTransformingHttpCommandExecutorServiceModule extends
} }
@Override @Override
public BlockingQueue<NHttpConnection> provideAvailablePool( public BlockingQueue<NHttpConnection> provideAvailablePool(HttpUtils utils) throws Exception {
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST) int maxConnectionsPerHost) throws Exception { return new ArrayBlockingQueue<NHttpConnection>(utils.getMaxConnectionsPerHost() != 0 ? utils
return new ArrayBlockingQueue<NHttpConnection>(maxConnectionsPerHost, true); .getMaxConnectionsPerHost() : utils.getMaxConnections(), true);
} }
@Provides @Provides

View File

@ -22,9 +22,7 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Named; import org.jclouds.http.HttpUtils;
import org.jclouds.Constants;
import org.jclouds.lifecycle.config.LifeCycleModule; import org.jclouds.lifecycle.config.LifeCycleModule;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
@ -36,7 +34,6 @@ import com.google.inject.Provides;
*/ */
public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends AbstractModule { public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends AbstractModule {
protected void configure() { protected void configure() {
install(new LifeCycleModule()); install(new LifeCycleModule());
bind(AtomicInteger.class).toInstance(new AtomicInteger());// max errors bind(AtomicInteger.class).toInstance(new AtomicInteger());// max errors
@ -45,9 +42,7 @@ public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends Abst
@Provides @Provides
// @Singleton per uri... // @Singleton per uri...
public abstract BlockingQueue<C> provideAvailablePool( public abstract BlockingQueue<C> provideAvailablePool(HttpUtils utils) throws Exception;
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST) int maxConnectionsPerHost)
throws Exception;
/** /**
* controls production and destruction of real connections. * controls production and destruction of real connections.
@ -59,9 +54,8 @@ public abstract class ConnectionPoolCommandExecutorServiceModule<C> extends Abst
*/ */
@Provides @Provides
// @Singleton per uri... // @Singleton per uri...
public Semaphore provideTotalConnectionSemaphore( public Semaphore provideTotalConnectionSemaphore(HttpUtils utils) throws Exception {
@Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST) int maxConnectionsPerHost) return new Semaphore(utils.getMaxConnectionsPerHost() != 0 ? utils.getMaxConnectionsPerHost()
throws Exception { : utils.getMaxConnections(), true);
return new Semaphore(maxConnectionsPerHost, true);
} }
} }

View File

@ -27,7 +27,6 @@ import java.net.URI;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.encryption.EncryptionService; import org.jclouds.encryption.EncryptionService;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -84,13 +83,8 @@ public class PCSCloudTest {
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(PCS.class) bind(URI.class).annotatedWith(PCS.class)
.toInstance(URI.create("http://localhost:8080")); .toInstance(URI.create("http://localhost:8080"));
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS)) Jsr330.bindProperties(this.binder(), new PCSPropertiesBuilder(URI
.to("1"); .create("http://localhost:8080"), "user", "key").build());
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -21,7 +21,8 @@ package org.jclouds.mezeo.pcs2.config;
import static com.google.common.util.concurrent.Executors.sameThreadExecutor; import static com.google.common.util.concurrent.Executors.sameThreadExecutor;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.jclouds.Constants; import java.net.URI;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.http.functions.config.ParserModule; import org.jclouds.http.functions.config.ParserModule;
@ -31,8 +32,8 @@ import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.handlers.RedirectionRetryHandler; import org.jclouds.http.handlers.RedirectionRetryHandler;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.mezeo.pcs2.PCSPropertiesBuilder;
import org.jclouds.mezeo.pcs2.handlers.PCSClientErrorRetryHandler; import org.jclouds.mezeo.pcs2.handlers.PCSClientErrorRetryHandler;
import org.jclouds.mezeo.pcs2.reference.PCSConstants;
import org.jclouds.util.Jsr330; import org.jclouds.util.Jsr330;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -49,18 +50,8 @@ public class PCSContextModuleTest {
return Guice.createInjector(new PCSRestClientModule(), new PCSContextModule() { return Guice.createInjector(new PCSRestClientModule(), new PCSContextModule() {
@Override @Override
protected void configure() { protected void configure() {
bindConstant().annotatedWith(Jsr330.named(PCSConstants.PROPERTY_PCS2_USER)).to("user"); Jsr330.bindProperties(this.binder(), new PCSPropertiesBuilder(URI
bindConstant().annotatedWith(Jsr330.named(PCSConstants.PROPERTY_PCS2_PASSWORD)).to( .create("http://localhost:8080"), "user", "key").build());
"key");
bindConstant().annotatedWith(Jsr330.named(PCSConstants.PROPERTY_PCS2_ENDPOINT)).to(
"http://localhost");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS))
.to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -26,7 +26,6 @@ import java.net.URI;
import javax.ws.rs.HttpMethod; import javax.ws.rs.HttpMethod;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -77,13 +76,8 @@ public class SDNAuthenticationTest {
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(SDN.class) bind(URI.class).annotatedWith(SDN.class)
.toInstance(URI.create("http://localhost:8080")); .toInstance(URI.create("http://localhost:8080"));
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS)) Jsr330.bindProperties(this.binder(), new SDNPropertiesBuilder("user", "key", "key",
.to("1"); "key").build());
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;
@ -96,5 +90,4 @@ public class SDNAuthenticationTest {
.get(new TypeLiteral<RestAnnotationProcessor<SDNAuthentication>>() { .get(new TypeLiteral<RestAnnotationProcessor<SDNAuthentication>>() {
})); }));
} }
} }

View File

@ -27,13 +27,13 @@ import java.net.URI;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.ext.RuntimeDelegate; import javax.ws.rs.ext.RuntimeDelegate;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.nirvanix.sdn.SDNPropertiesBuilder;
import org.jclouds.nirvanix.sdn.SessionToken; import org.jclouds.nirvanix.sdn.SessionToken;
import org.jclouds.rest.annotations.EndpointParam; import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.config.RestModule; import org.jclouds.rest.config.RestModule;
@ -109,14 +109,8 @@ public class AddSessionTokenToRequestTest {
protected void configure() { protected void configure() {
RuntimeDelegate.setInstance(new RuntimeDelegateImpl()); RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
bind(DateService.class); bind(DateService.class);
bindConstant().annotatedWith( Jsr330.bindProperties(this.binder(), new SDNPropertiesBuilder("appkey",
Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS)).to("1"); "appname", "username", "password").build());
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS))
.to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST)).to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -30,12 +30,11 @@ import java.net.URI;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.ext.RuntimeDelegate; import javax.ws.rs.ext.RuntimeDelegate;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.nirvanix.sdn.reference.SDNConstants; import org.jclouds.nirvanix.sdn.SDNPropertiesBuilder;
import org.jclouds.rest.annotations.EndpointParam; import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.config.RestModule; import org.jclouds.rest.config.RestModule;
import org.jclouds.rest.internal.GeneratedHttpRequest; import org.jclouds.rest.internal.GeneratedHttpRequest;
@ -107,20 +106,8 @@ public class InsertUserContextIntoPathTest {
expect(sessionManager.getSessionToken()).andReturn("token").anyTimes(); expect(sessionManager.getSessionToken()).andReturn("token").anyTimes();
replay(sessionManager); replay(sessionManager);
bind(AddSessionTokenToRequest.class).toInstance(sessionManager); bind(AddSessionTokenToRequest.class).toInstance(sessionManager);
bindConstant().annotatedWith(Jsr330.named(SDNConstants.PROPERTY_SDN_APPKEY)) Jsr330.bindProperties(this.binder(), new SDNPropertiesBuilder("appkey",
.to("appKey"); "appname", "username", "password").build());
bindConstant().annotatedWith(Jsr330.named(SDNConstants.PROPERTY_SDN_USERNAME))
.to("username");
bindConstant().annotatedWith(Jsr330.named(SDNConstants.PROPERTY_SDN_APPNAME))
.to("appname");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS)).to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS))
.to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST)).to("1");
} }
}); });

View File

@ -27,7 +27,6 @@ import java.util.Collections;
import javax.ws.rs.HttpMethod; import javax.ws.rs.HttpMethod;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -79,15 +78,10 @@ public class RackspaceAuthenticationTest {
Injector injector = Guice.createInjector(new AbstractModule() { Injector injector = Guice.createInjector(new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
Jsr330.bindProperties(this.binder(), new RackspacePropertiesBuilder("user", "key")
.build());
bind(URI.class).annotatedWith(Authentication.class).toInstance( bind(URI.class).annotatedWith(Authentication.class).toInstance(
URI.create("http://localhost:8080")); URI.create("http://localhost:8080"));
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS))
.to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -39,7 +39,6 @@ import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.date.TimeStamp; import org.jclouds.date.TimeStamp;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -49,6 +48,7 @@ import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rackspace.Authentication; import org.jclouds.rackspace.Authentication;
import org.jclouds.rackspace.CloudServers; import org.jclouds.rackspace.CloudServers;
import org.jclouds.rackspace.RackspacePropertiesBuilder;
import org.jclouds.rackspace.cloudservers.domain.BackupSchedule; import org.jclouds.rackspace.cloudservers.domain.BackupSchedule;
import org.jclouds.rackspace.cloudservers.domain.DailyBackup; import org.jclouds.rackspace.cloudservers.domain.DailyBackup;
import org.jclouds.rackspace.cloudservers.domain.RebootType; import org.jclouds.rackspace.cloudservers.domain.RebootType;
@ -999,15 +999,10 @@ public class CloudServersClientTest {
Injector injector = Guice.createInjector(new AbstractModule() { Injector injector = Guice.createInjector(new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
Jsr330.bindProperties(this.binder(), new RackspacePropertiesBuilder("user", "key")
.build());
bind(URI.class).annotatedWith(CloudServers.class).toInstance( bind(URI.class).annotatedWith(CloudServers.class).toInstance(
URI.create("http://localhost:8080")); URI.create("http://localhost:8080"));
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS))
.to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -21,7 +21,6 @@ package org.jclouds.rimuhosting.miro.config;
import static com.google.common.util.concurrent.Executors.sameThreadExecutor; import static com.google.common.util.concurrent.Executors.sameThreadExecutor;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.HttpRetryHandler; import org.jclouds.http.HttpRetryHandler;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -34,7 +33,7 @@ import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.handlers.RedirectionRetryHandler; import org.jclouds.http.handlers.RedirectionRetryHandler;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.rimuhosting.miro.reference.RimuHostingConstants; import org.jclouds.rimuhosting.miro.RimuHostingPropertiesBuilder;
import org.jclouds.util.Jsr330; import org.jclouds.util.Jsr330;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -48,25 +47,12 @@ import com.google.inject.Injector;
public class RimuHostingContextModuleTest { public class RimuHostingContextModuleTest {
Injector createInjector() { Injector createInjector() {
return Guice.createInjector( return Guice.createInjector(new RimuHostingRestClientModule(),
new RimuHostingRestClientModule(),
new RimuHostingContextModule() { new RimuHostingContextModule() {
@Override @Override
protected void configure() { protected void configure() {
bindConstant().annotatedWith( Jsr330.bindProperties(this.binder(),
Jsr330.named(RimuHostingConstants.PROPERTY_RIMUHOSTING_APIKEY)).to( new RimuHostingPropertiesBuilder("apikey").build());
"apikey");
bindConstant().annotatedWith(
Jsr330.named(RimuHostingConstants.PROPERTY_RIMUHOSTING_ENDPOINT)).to(
"http://localhost");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS)).to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS))
.to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST)).to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;

View File

@ -21,7 +21,6 @@ package org.jclouds.twitter.config;
import static com.google.common.util.concurrent.Executors.sameThreadExecutor; import static com.google.common.util.concurrent.Executors.sameThreadExecutor;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.jclouds.Constants;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.http.HttpRetryHandler; import org.jclouds.http.HttpRetryHandler;
import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule; import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
@ -34,7 +33,7 @@ import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.handlers.RedirectionRetryHandler; import org.jclouds.http.handlers.RedirectionRetryHandler;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.logging.Logger.LoggerFactory; import org.jclouds.logging.Logger.LoggerFactory;
import org.jclouds.twitter.reference.TwitterConstants; import org.jclouds.twitter.TwitterPropertiesBuilder;
import org.jclouds.util.Jsr330; import org.jclouds.util.Jsr330;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -51,19 +50,8 @@ public class TwitterContextModuleTest {
return Guice.createInjector(new TwitterRestClientModule(), new TwitterContextModule() { return Guice.createInjector(new TwitterRestClientModule(), new TwitterContextModule() {
@Override @Override
protected void configure() { protected void configure() {
bindConstant().annotatedWith(Jsr330.named(TwitterConstants.PROPERTY_TWITTER_USER)).to( Jsr330.bindProperties(this.binder(), new TwitterPropertiesBuilder("user", "pass")
"user"); .build());
bindConstant().annotatedWith(Jsr330.named(TwitterConstants.PROPERTY_TWITTER_PASSWORD))
.to("password");
bindConstant().annotatedWith(Jsr330.named(TwitterConstants.PROPERTY_TWITTER_ENDPOINT))
.to("http://localhost");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_IO_WORKER_THREADS))
.to("1");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_USER_THREADS)).to("1");
bindConstant().annotatedWith(
Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)).to("0");
bindConstant().annotatedWith(Jsr330.named(Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST))
.to("1");
bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() { bind(Logger.LoggerFactory.class).toInstance(new LoggerFactory() {
public Logger getLogger(String category) { public Logger getLogger(String category) {
return Logger.NULL; return Logger.NULL;