git-svn-id: http://jclouds.googlecode.com/svn/trunk@865 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
suprememoocow 2009-05-28 22:14:38 +00:00
parent 0f346ca1c2
commit 5b1f294068
7 changed files with 287 additions and 160 deletions

View File

@ -24,18 +24,20 @@
package org.jclouds.aws.s3;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.aws.s3.reference.S3Constants.PROPERTY_AWS_ACCESSKEYID;
import static org.jclouds.aws.s3.reference.S3Constants.PROPERTY_AWS_SECRETACCESSKEY;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_AWS_ACCESSKEYID;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_AWS_SECRETACCESSKEY;
import static org.jclouds.command.pool.PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS;
import static org.jclouds.command.pool.PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS;
import static org.jclouds.command.pool.PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE;
import static org.jclouds.command.pool.PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES;
import static org.jclouds.command.pool.PoolConstants.PROPERTY_POOL_REQUEST_INVOKER_THREADS;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_ADDRESS;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_MAX_RETRIES;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_PORT;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_SECURE;
import static org.jclouds.http.HttpConstants.PROPERTY_HTTP_MAX_RETRIES;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
@ -51,7 +53,6 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
@ -68,114 +69,189 @@ import com.google.inject.name.Names;
* If no <code>Module</code>s are specified, the default {@link JDKLoggingModule logging} and
* {@link JavaUrlHttpFutureCommandClientModule http transports} will be installed.
*
* @author Adrian Cole
* @author Adrian Cole, Andrew Newdigate
* @see S3Context
*/
public class S3ContextFactory {
public static final Properties DEFAULT_PROPERTIES;
private static final String DEFAULT_SECURE_HTTP_PORT = "443";
private static final String DEFAULT_NON_SECURE_HTTP_PORT = "80";
static {
DEFAULT_PROPERTIES = new Properties();
DEFAULT_PROPERTIES.setProperty(PROPERTY_HTTP_ADDRESS, "s3.amazonaws.com");
DEFAULT_PROPERTIES.setProperty(PROPERTY_HTTP_PORT, "443");
DEFAULT_PROPERTIES.setProperty(PROPERTY_HTTP_SECURE, "true");
DEFAULT_PROPERTIES.setProperty(PROPERTY_HTTP_MAX_RETRIES, "5");
DEFAULT_PROPERTIES.setProperty(PROPERTY_POOL_MAX_CONNECTION_REUSE, "75");
DEFAULT_PROPERTIES.setProperty(PROPERTY_POOL_MAX_SESSION_FAILURES, "2");
DEFAULT_PROPERTIES.setProperty(PROPERTY_POOL_REQUEST_INVOKER_THREADS, "1");
DEFAULT_PROPERTIES.setProperty(PROPERTY_POOL_IO_WORKER_THREADS, "2");
DEFAULT_PROPERTIES.setProperty(PROPERTY_POOL_MAX_CONNECTIONS, "12");
private final Properties properties;
private final List<Module> modules = new ArrayList<Module>(3);
private S3ContextFactory(Properties properties) {
this.properties = properties;
}
public static S3ContextFactory createContext(String awsAccessKeyId, String awsSecretAccessKey) {
Properties properties = new Properties();
properties.setProperty(PROPERTY_AWS_ACCESSKEYID, checkNotNull(awsAccessKeyId, "awsAccessKeyId"));
properties.setProperty(PROPERTY_AWS_SECRETACCESSKEY, checkNotNull(awsSecretAccessKey,"awsSecretAccessKey"));
properties.setProperty(PROPERTY_HTTP_ADDRESS, "s3.amazonaws.com");
properties.setProperty(PROPERTY_HTTP_SECURE, "true");
properties.setProperty(PROPERTY_HTTP_MAX_RETRIES, "5");
properties.setProperty(PROPERTY_POOL_MAX_CONNECTION_REUSE, "75");
properties.setProperty(PROPERTY_POOL_MAX_SESSION_FAILURES, "2");
properties.setProperty(PROPERTY_POOL_REQUEST_INVOKER_THREADS, "1");
properties.setProperty(PROPERTY_POOL_IO_WORKER_THREADS, "2");
properties.setProperty(PROPERTY_POOL_MAX_CONNECTIONS, "12");
return new S3ContextFactory(properties);
}
public S3Context build() {
return createInjector().getInstance(S3Context.class);
}
public static Injector createInjector(String awsAccessKeyId, String awsSecretAccessKey,
Module... modules) {
Properties properties = new Properties(DEFAULT_PROPERTIES);
properties.setProperty(PROPERTY_AWS_ACCESSKEYID, awsAccessKeyId);
properties.setProperty(PROPERTY_AWS_SECRETACCESSKEY, awsSecretAccessKey);
return createInjector(properties, modules);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.createInjector();
}
public static S3Context createS3Context(String awsAccessKeyId, String awsSecretAccessKey,
Module... modules) {
return createInjector(awsAccessKeyId, awsSecretAccessKey, modules).getInstance(
S3Context.class);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.build();
}
public static Injector createInjector(String awsAccessKeyId, String awsSecretAccessKey,
boolean isSecure, Module... modules) {
Properties properties = new Properties(DEFAULT_PROPERTIES);
properties.setProperty(PROPERTY_AWS_ACCESSKEYID, awsAccessKeyId);
properties.setProperty(PROPERTY_AWS_SECRETACCESSKEY, awsSecretAccessKey);
properties.setProperty(PROPERTY_HTTP_SECURE, Boolean.toString(isSecure));
if (!isSecure)
properties.setProperty(PROPERTY_HTTP_PORT, "80");
return createInjector(properties, modules);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.withHttpSecure(isSecure)
.createInjector();
}
public static S3Context createS3Context(String awsAccessKeyId, String awsSecretAccessKey,
boolean isSecure, Module... modules) {
return createInjector(awsAccessKeyId, awsSecretAccessKey, isSecure, modules).getInstance(
S3Context.class);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.withHttpSecure(isSecure)
.build();
}
public static Injector createInjector(String awsAccessKeyId, String awsSecretAccessKey,
boolean isSecure, String server, Module... modules) {
Properties properties = new Properties(DEFAULT_PROPERTIES);
properties.setProperty(PROPERTY_AWS_ACCESSKEYID, awsAccessKeyId);
properties.setProperty(PROPERTY_AWS_SECRETACCESSKEY, awsSecretAccessKey);
properties.setProperty(PROPERTY_HTTP_SECURE, Boolean.toString(isSecure));
properties.setProperty(PROPERTY_HTTP_ADDRESS, server);
if (!isSecure)
properties.setProperty(PROPERTY_HTTP_PORT, "80");
return createInjector(properties, modules);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.withHttpSecure(isSecure)
.withHttpAddress(server)
.createInjector();
}
public static S3Context createS3Context(String awsAccessKeyId, String awsSecretAccessKey,
boolean isSecure, String server, Module... modules) {
return createInjector(awsAccessKeyId, awsSecretAccessKey, isSecure, server, modules)
.getInstance(S3Context.class);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.withHttpSecure(isSecure)
.withHttpAddress(server)
.build();
}
public static S3Context createS3Context(String awsAccessKeyId, String awsSecretAccessKey,
boolean isSecure, String server, int port, Module... modules) {
return createInjector(awsAccessKeyId, awsSecretAccessKey, isSecure, server, port, modules)
.getInstance(S3Context.class);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.withHttpSecure(isSecure)
.withHttpAddress(server)
.withHttpPort(port)
.build();
}
public static Injector createInjector(String awsAccessKeyId, String awsSecretAccessKey,
boolean isSecure, String server, int port, Module... modules) {
Properties properties = new Properties(DEFAULT_PROPERTIES);
properties.setProperty(PROPERTY_AWS_ACCESSKEYID, awsAccessKeyId);
properties.setProperty(PROPERTY_AWS_SECRETACCESSKEY, awsSecretAccessKey);
properties.setProperty(PROPERTY_HTTP_SECURE, Boolean.toString(isSecure));
properties.setProperty(PROPERTY_HTTP_ADDRESS, server);
properties.setProperty(PROPERTY_HTTP_PORT, port + "");
return createInjector(properties, modules);
return createContext(awsAccessKeyId, awsSecretAccessKey)
.withModules(modules)
.withHttpSecure(isSecure)
.withHttpAddress(server)
.withHttpPort(port)
.createInjector();
}
public static S3Context createS3Context(Properties properties, Module... modules) {
return createInjector(properties, modules).getInstance(S3Context.class);
public S3ContextFactory withHttpAddress(String httpAddress) {
properties.setProperty(PROPERTY_HTTP_ADDRESS, httpAddress);
return this;
}
/**
* Bind the given properties and install the list of modules. If no modules are specified,
* install the default {@link JDKLoggingModule} {@link JavaUrlHttpFutureCommandClientModule}
*
* @param properties
* - contains constants used by jclouds {@link #DEFAULT_PROPERTIES}
* @param configModules
* - alternative configuration modules
*/
public static Injector createInjector(final Properties properties, Module... configModules) {
final List<Module> modules = Lists.newArrayList(configModules);
public S3ContextFactory withHttpMaxRetries(int httpMaxRetries) {
properties.setProperty(PROPERTY_HTTP_MAX_RETRIES, Integer.toString(httpMaxRetries));
return this;
}
public S3ContextFactory withHttpPort(int httpPort) {
properties.setProperty(PROPERTY_HTTP_PORT, Integer.toString(httpPort));
return this;
}
public S3ContextFactory withHttpSecure(boolean httpSecure) {
properties.setProperty(PROPERTY_HTTP_SECURE, Boolean.toString(httpSecure));
return this;
}
public S3ContextFactory withPoolMaxConnectionReuse(int poolMaxConnectionReuse) {
properties.setProperty(PROPERTY_POOL_MAX_CONNECTION_REUSE, Integer.toString(poolMaxConnectionReuse));
return this;
}
public S3ContextFactory withPoolMaxSessionFailures(int poolMaxSessionFailures) {
properties.setProperty(PROPERTY_POOL_MAX_SESSION_FAILURES, Integer.toString(poolMaxSessionFailures));
return this;
}
public S3ContextFactory withPoolRequestInvokerThreads(int poolRequestInvokerThreads) {
properties.setProperty(PROPERTY_POOL_REQUEST_INVOKER_THREADS, Integer.toString(poolRequestInvokerThreads));
return this;
}
public S3ContextFactory withPoolIoWorkerThreads(int poolIoWorkerThreads) {
properties.setProperty(PROPERTY_POOL_IO_WORKER_THREADS, Integer.toString(poolIoWorkerThreads));
return this;
}
public S3ContextFactory withPoolMaxConnections(int poolMaxConnections) {
properties.setProperty(PROPERTY_POOL_MAX_CONNECTIONS, Integer.toString(poolMaxConnections));
return this;
}
public S3ContextFactory withModule(Module module) {
modules.add(module);
return this;
}
public S3ContextFactory withModules(Module... modules) {
this.modules.addAll(Arrays.asList(modules));
return this;
}
private Injector createInjector() {
/* Use 80 or 443 as the default port if one hasn't been set? */
if(!properties.containsKey(PROPERTY_HTTP_PORT)) {
if(Boolean.parseBoolean(properties.getProperty(PROPERTY_HTTP_SECURE))) {
properties.setProperty(PROPERTY_HTTP_PORT, DEFAULT_SECURE_HTTP_PORT);
} else {
properties.setProperty(PROPERTY_HTTP_PORT, DEFAULT_NON_SECURE_HTTP_PORT);
}
}
addLoggingModuleIfNotPresent(modules);
addHttpModuleIfNeededAndNotPresent(modules);
addS3ConnectionModuleIfNotPresent(modules);
addHttpModuleIfNeededAndNotPresent(modules);
return Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
@ -186,6 +262,7 @@ public class S3ContextFactory {
}, new S3ContextModule());
}
@VisibleForTesting
static void addHttpModuleIfNeededAndNotPresent(final List<Module> modules) {
if (Iterables.any(modules, new Predicate<Module>() {
@ -219,4 +296,9 @@ public class S3ContextFactory {
if (!Iterables.any(modules, Predicates.instanceOf(LoggingModule.class)))
modules.add(new JDKLoggingModule());
}
@VisibleForTesting
Properties getProperties() {
return properties;
}
}

View File

@ -36,6 +36,7 @@ import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Tests behavior of modules configured in S3ContextFactory
@ -108,4 +109,46 @@ public class S3ContextFactoryTest {
assert modules.remove(0) instanceof JavaUrlHttpFutureCommandClientModule;
assert modules.remove(0) instanceof JDKLoggingModule;
}
public void testBuilder() {
String awsAccessKeyId = "awsAccessKeyId";
String awsSecretAccessKey = "awsSecretAccessKey";
String httpAddress = "httpAddress";
int httpMaxRetries = 9875;
int httpPort = 3827;
boolean httpSecure = false;
int poolIoWorkerThreads= 2727;
int poolMaxConnectionReuse = 3932;
int poolMaxConnections = 3382;
int poolMaxSessionFailures = 857;
int poolRequestInvokerThreads = 8362;
AbstractModule module1 = new AbstractModule() {
@Override
protected void configure() {
}
};
AbstractModule module2 = new AbstractModule() {
@Override
protected void configure() {
}
};
S3ContextFactory factory = S3ContextFactory.createContext(awsAccessKeyId, awsSecretAccessKey);
factory.withHttpAddress(httpAddress);
factory.withHttpMaxRetries(httpMaxRetries);
factory.withHttpPort(httpPort);
factory.withHttpSecure(httpSecure);
factory.withModule(module1);
factory.withModules(module2);
factory.withPoolIoWorkerThreads(poolIoWorkerThreads);
factory.withPoolMaxConnectionReuse(poolMaxConnectionReuse);
factory.withPoolMaxConnections(poolMaxConnections);
factory.withPoolMaxSessionFailures(poolMaxSessionFailures);
factory.withPoolRequestInvokerThreads(poolRequestInvokerThreads);
Properties properties = factory.getProperties();
}
}

View File

@ -166,15 +166,16 @@ public class S3IntegrationTest {
}
protected void createStubS3Context() {
Properties props = new Properties();
props.setProperty(S3Constants.PROPERTY_HTTP_ADDRESS, "stub");
context = S3ContextFactory.createS3Context(props, new StubS3ConnectionModule());
context = S3ContextFactory.createContext("stub", "stub")
.withHttpAddress("stub")
.withModule(new StubS3ConnectionModule())
.build();
}
protected void createLiveS3Context(String AWSAccessKeyId, String AWSSecretAccessKey) {
context = S3ContextFactory.createS3Context(buildS3Properties(checkNotNull(AWSAccessKeyId,
"AWSAccessKeyId"), checkNotNull(AWSSecretAccessKey, "AWSSecretAccessKey")),
createHttpModule());
context = buildS3ContextFactory(AWSAccessKeyId, AWSSecretAccessKey)
.withModule(createHttpModule())
.build();
}
@BeforeMethod(dependsOnMethods = "deleteBucket", groups = { "integration", "live" })
@ -195,15 +196,10 @@ public class S3IntegrationTest {
return false;
}
protected Properties buildS3Properties(String AWSAccessKeyId, String AWSSecretAccessKey) {
Properties properties = new Properties(S3ContextFactory.DEFAULT_PROPERTIES);
properties.setProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID, checkNotNull(AWSAccessKeyId,
"AWSAccessKeyId"));
properties.setProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY, checkNotNull(
AWSSecretAccessKey, "AWSSecretAccessKey"));
properties.setProperty(HttpConstants.PROPERTY_HTTP_SECURE, "false");
properties.setProperty(HttpConstants.PROPERTY_HTTP_PORT, "80");
return properties;
protected S3ContextFactory buildS3ContextFactory(String AWSAccessKeyId, String AWSSecretAccessKey) {
return S3ContextFactory.createContext(AWSAccessKeyId, AWSSecretAccessKey)
.withHttpSecure(false)
.withHttpPort(80);
}
protected Module createHttpModule() {

View File

@ -25,8 +25,6 @@ package org.jclouds.aws.s3;
import org.testng.annotations.Test;
import java.util.Properties;
/**
* This performs the same test as {@link S3ConnectionIntegrationTest}, except using SSL.
*
@ -35,13 +33,11 @@ import java.util.Properties;
@Test(groups = {"live"}, testName = "s3.SecureS3ConnectionIntegrationTest")
public class SecureS3ConnectionIntegrationTest extends S3ConnectionIntegrationTest {
@Override
protected Properties buildS3Properties(String AWSAccessKeyId,
protected S3ContextFactory buildS3ContextFactory(String AWSAccessKeyId,
String AWSSecretAccessKey) {
Properties properties = super.buildS3Properties(AWSAccessKeyId,
AWSSecretAccessKey);
properties.setProperty("jclouds.http.secure", Boolean.toString(true));
properties.setProperty("jclouds.http.port", "443");
return properties;
return S3ContextFactory.createContext(AWSAccessKeyId, AWSSecretAccessKey)
.withHttpSecure(true)
.withHttpPort(443);
}
}

View File

@ -103,6 +103,8 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
throws S3ServiceException, InterruptedException, ExecutionException
{
String bucketName = bucketPrefix + ".testCreateBucketImplStringStringAccessControlList";
bucketName = bucketName.toLowerCase();
S3Bucket bucket = service.createBucket(new S3Bucket(bucketName));
assertEquals(bucket.getName(), bucketName);
assertTrue(client.bucketExists(bucketName).get());
@ -114,6 +116,9 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
public void testDeleteBucketImplString() throws S3ServiceException,
InterruptedException, ExecutionException, TimeoutException {
String bucketName = bucketPrefix + ".testDeleteBucketImplString";
bucketName = bucketName.toLowerCase();
service.deleteBucket(bucketName);
assert !client.bucketExists(bucketName).get(10, TimeUnit.SECONDS);
}
@ -130,6 +135,8 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
String objectKey = "key";
String objectValue = "test";
bucketName = bucketName.toLowerCase();
addNewObject(bucketName, objectKey, objectValue);
service.deleteObject(bucketName, objectKey);
@ -187,6 +194,8 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
String objectKey = "key";
String objectValue = "test";
bucketName = bucketName.toLowerCase();
addNewObject(bucketName, objectKey, objectValue);
S3Object objectDetails = service.getObjectDetails(new S3Bucket(bucketName), objectKey);
@ -208,6 +217,8 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
String objectKey = "key";
String objectValue = "test";
bucketName = bucketName.toLowerCase();
addNewObject(bucketName, objectKey, objectValue);
S3Object object = service.getObject(new S3Bucket(bucketName), objectKey);
@ -239,6 +250,8 @@ public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
ExecutionException, TimeoutException, S3ServiceException {
// Ensure there is at least 1 bucket in S3 account to list and compare.
String bucketName = bucketPrefix + ".testListAllBucketsImplString";
bucketName = bucketName.toLowerCase();
createBucket(bucketName);
S3Bucket[] jsBuckets = service.listAllBuckets();

View File

@ -23,8 +23,6 @@
*/
package org.jclouds.aws.s3;
import java.util.Properties;
import org.jclouds.http.httpnio.config.HttpNioConnectionPoolClientModule;
import org.testng.annotations.Test;
@ -34,15 +32,14 @@ import com.google.inject.Module;
public class JCloudsNioPerformanceLiveTest extends BaseJCloudsPerformance {
@Override
protected Properties buildS3Properties(String AWSAccessKeyId,
protected S3ContextFactory buildS3ContextFactory(String AWSAccessKeyId,
String AWSSecretAccessKey) {
Properties properties = super.buildS3Properties(AWSAccessKeyId, AWSSecretAccessKey);
properties.setProperty("jclouds.http.pool.max_connection_reuse", "75");
properties.setProperty("jclouds.http.pool.max_session_failures", "2");
properties.setProperty("jclouds.http.pool.request_invoker_threads", "1");
properties.setProperty("jclouds.http.pool.io_worker_threads", "2");
properties.setProperty("jclouds.pool.max_connections", "12");
return properties;
return super.buildS3ContextFactory(AWSAccessKeyId, AWSSecretAccessKey)
.withPoolMaxConnectionReuse(75)
.withPoolMaxSessionFailures(2)
.withPoolRequestInvokerThreads(1)
.withPoolIoWorkerThreads(2)
.withPoolMaxConnections(12);
}
@Override