Use builder to create S3Proxy

Also hoist sanity checks out of main.
pull/42/head
Andrew Gaul 2015-02-19 14:06:17 -08:00
rodzic 2e82478f0e
commit 613da2f3ec
3 zmienionych plików z 115 dodań i 34 usunięć

Wyświetl plik

@ -55,7 +55,7 @@ public final class S3Proxy {
System.setProperty("org.eclipse.jetty.http.HttpParser.STRICT", "true");
}
public S3Proxy(BlobStore blobStore, URI endpoint, String identity,
S3Proxy(BlobStore blobStore, URI endpoint, String identity,
String credential, String keyStorePath, String keyStorePassword,
boolean forceMultiPartUpload, Optional<String> virtualHost) {
checkNotNull(blobStore);
@ -63,6 +63,15 @@ public final class S3Proxy {
// TODO: allow service paths?
checkArgument(endpoint.getPath().isEmpty(),
"endpoint path must be empty, was: " + endpoint.getPath());
checkArgument(Strings.isNullOrEmpty(identity) ^
!Strings.isNullOrEmpty(credential),
"Must provide both identity and credential");
if (endpoint.getScheme().equals("https:")) {
checkNotNull(keyStorePath,
"Must provide keyStorePath with HTTPS endpoint");
checkNotNull(keyStorePassword,
"Must provide keyStorePassword with HTTPS endpoint");
}
checkNotNull(virtualHost);
server = new Server();
@ -85,6 +94,62 @@ public final class S3Proxy {
forceMultiPartUpload, virtualHost));
}
public static final class Builder {
private BlobStore blobStore;
private URI endpoint;
private String identity;
private String credential;
private String keyStorePath;
private String keyStorePassword;
private boolean forceMultiPartUpload;
private String virtualHost;
Builder() {
}
public S3Proxy build() {
return new S3Proxy(blobStore, endpoint, identity, credential,
keyStorePath, keyStorePassword, forceMultiPartUpload,
Optional.fromNullable(virtualHost));
}
public Builder blobStore(BlobStore blobStore) {
this.blobStore = checkNotNull(blobStore);
return this;
}
public Builder endpoint(URI endpoint) {
this.endpoint = checkNotNull(endpoint);
return this;
}
public Builder awsAuthentication(String identity, String credential) {
this.identity = checkNotNull(identity);
this.credential = checkNotNull(credential);
return this;
}
public Builder keyStore(String keyStorePath, String keyStorePassword) {
this.keyStorePath = checkNotNull(keyStorePath);
this.keyStorePassword = checkNotNull(keyStorePassword);
return this;
}
public Builder forceMultiPartUpload(boolean forceMultiPartUpload) {
this.forceMultiPartUpload = forceMultiPartUpload;
return this;
}
public Builder virtualHost(String virtualHost) {
this.virtualHost = checkNotNull(virtualHost);
return this;
}
}
public static Builder builder() {
return new Builder();
}
public void start() throws Exception {
server.start();
}
@ -144,13 +209,6 @@ public final class S3Proxy {
S3ProxyConstants.PROPERTY_IDENTITY);
localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
if (localIdentity == null || localCredential == null) {
System.err.println(
"Both " + S3ProxyConstants.PROPERTY_IDENTITY +
" and " + S3ProxyConstants.PROPERTY_CREDENTIAL +
" must be set");
System.exit(1);
}
} else if (!s3ProxyAuthorization.equalsIgnoreCase("none")) {
System.err.println(S3ProxyConstants.PROPERTY_AUTHORIZATION +
" must be aws-v2 or none, was: " + s3ProxyAuthorization);
@ -161,22 +219,10 @@ public final class S3Proxy {
S3ProxyConstants.PROPERTY_KEYSTORE_PATH);
String keyStorePassword = properties.getProperty(
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
if (s3ProxyEndpointString.startsWith("https")) {
if (Strings.isNullOrEmpty(keyStorePath) ||
Strings.isNullOrEmpty(keyStorePassword)) {
System.err.println(
"Both " + S3ProxyConstants.PROPERTY_KEYSTORE_PATH +
" and " + S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD +
" must be set with an HTTPS endpoint");
System.exit(1);
}
}
String forceMultiPartUpload = properties.getProperty(
S3ProxyConstants.PROPERTY_FORCE_MULTI_PART_UPLOAD);
Optional<String> virtualHost = Optional.fromNullable(
properties.getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST));
String virtualHost = properties.getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
ContextBuilder builder = ContextBuilder
.newBuilder(provider)
@ -188,10 +234,30 @@ public final class S3Proxy {
}
BlobStoreContext context = builder.build(BlobStoreContext.class);
URI s3ProxyEndpoint = new URI(s3ProxyEndpointString);
S3Proxy s3Proxy = new S3Proxy(context.getBlobStore(), s3ProxyEndpoint,
localIdentity, localCredential, keyStorePath,
keyStorePassword,
"true".equalsIgnoreCase(forceMultiPartUpload), virtualHost);
S3Proxy s3Proxy;
try {
S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder()
.blobStore(context.getBlobStore())
.endpoint(s3ProxyEndpoint)
.forceMultiPartUpload("true".equalsIgnoreCase(
forceMultiPartUpload));
if (localIdentity != null || localCredential != null) {
s3ProxyBuilder.awsAuthentication(localIdentity,
localCredential);
}
if (keyStorePath != null || keyStorePassword != null) {
s3ProxyBuilder.keyStore(keyStorePath, keyStorePassword);
}
if (virtualHost != null) {
s3ProxyBuilder.virtualHost(virtualHost);
}
s3Proxy = s3ProxyBuilder.build();
} catch (IllegalArgumentException | IllegalStateException e) {
System.err.println(e.getMessage());
System.exit(1);
throw e;
}
s3Proxy.start();
}
}

Wyświetl plik

@ -34,10 +34,14 @@
<module name="FallThrough"/>
<module name="FinalClass"/>
<module name="GenericWhitespace"/>
<!--
// TODO: Checkstyle dislikes builders without set prefix
https://github.com/checkstyle/checkstyle/issues/619
<module name="HiddenField">
<property name="ignoreConstructorParameter" value="true"/>
<property name="ignoreSetter" value="true"/>
</module>
-->
<module name="HideUtilityClassConstructor"/>
<module name="IllegalImport"/>
<module name="IllegalInstantiation">

Wyświetl plik

@ -26,7 +26,6 @@ import java.util.Random;
import javax.servlet.http.HttpServletResponse;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@ -95,9 +94,8 @@ public final class S3ProxyTest {
S3ProxyConstants.PROPERTY_KEYSTORE_PASSWORD);
String forceMultiPartUpload = s3ProxyProperties.getProperty(
S3ProxyConstants.PROPERTY_FORCE_MULTI_PART_UPLOAD);
Optional<String> virtualHost = Optional.fromNullable(
s3ProxyProperties.getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST));
String virtualHost = s3ProxyProperties.getProperty(
S3ProxyConstants.PROPERTY_VIRTUAL_HOST);
Properties properties = new Properties();
ContextBuilder builder = ContextBuilder
@ -113,10 +111,23 @@ public final class S3ProxyTest {
containerName = createRandomContainerName();
blobStore.createContainerInLocation(null, containerName);
s3Proxy = new S3Proxy(blobStore, s3Endpoint, s3Identity, s3Credential,
Resources.getResource(keyStorePath).toString(),
keyStorePassword,
"true".equalsIgnoreCase(forceMultiPartUpload), virtualHost);
S3Proxy.Builder s3ProxyBuilder = S3Proxy.builder()
.blobStore(blobStore)
.endpoint(s3Endpoint)
.forceMultiPartUpload("true".equalsIgnoreCase(
forceMultiPartUpload));
if (s3Identity != null || s3Credential != null) {
s3ProxyBuilder.awsAuthentication(s3Identity, s3Credential);
}
if (keyStorePath != null || keyStorePassword != null) {
s3ProxyBuilder.keyStore(
Resources.getResource(keyStorePath).toString(),
keyStorePassword);
}
if (virtualHost != null) {
s3ProxyBuilder.virtualHost(virtualHost);
}
s3Proxy = s3ProxyBuilder.build();
s3Proxy.start();
while (!s3Proxy.getState().equals(AbstractLifeCycle.STARTED)) {
Thread.sleep(1);