Issue 324: added properties for (jclouds|provider).modules, (jclouds|provider).credential.resource, and (jclouds|provider).credential.file in order to support rewiring jclouds http and other components via properties files, and extract out where credentials are obtained from

This commit is contained in:
Adrian Cole 2010-07-28 00:41:13 -07:00
parent aed3bb7e74
commit ca247efaed
11 changed files with 396 additions and 203 deletions

View File

@ -24,7 +24,6 @@ import java.util.List;
import java.util.NoSuchElementException;
import java.util.Properties;
import org.jclouds.aws.ec2.compute.config.EC2ComputeServiceContextModule;
import org.jclouds.aws.ec2.compute.config.EC2ResolveImagesModule;
import org.jclouds.aws.ec2.config.EC2RestClientModule;
@ -42,14 +41,17 @@ import com.google.inject.Injector;
import com.google.inject.Module;
/**
* Creates {@link EC2ComputeServiceContext} or {@link Injector} instances based on the most commonly
* requested arguments.
* Creates {@link EC2ComputeServiceContext} or {@link Injector} instances based
* on the most commonly requested arguments.
* <p/>
* Note that Threadsafe objects will be bound as singletons to the Injector or Context provided.
* Note that Threadsafe objects will be bound as singletons to the Injector or
* Context provided.
* <p/>
* <p/>
* If no <code>Module</code>s are specified, the default {@link JDKLoggingModule logging} and
* {@link JavaUrlHttpCommandExecutorServiceModule http transports} will be installed.
* If no <code>Module</code>s are specified, the default
* {@link JDKLoggingModule logging} and
* {@link JavaUrlHttpCommandExecutorServiceModule http transports} will be
* installed.
*
* @author Adrian Cole
* @see EC2ComputeServiceContext
@ -61,7 +63,7 @@ public class EC2ContextBuilder extends ComputeServiceContextBuilder<EC2Client, E
}
@Override
public EC2ContextBuilder withModules(Module... modules) {
public EC2ContextBuilder withModules(Iterable<Module> modules) {
return (EC2ContextBuilder) super.withModules(modules);
}
@ -88,8 +90,7 @@ public class EC2ContextBuilder extends ComputeServiceContextBuilder<EC2Client, E
Iterable<Module> infra = Iterables.filter(modules, new Predicate<Module>() {
public boolean apply(Module input) {
return input.getClass().isAnnotationPresent(ConfiguresExecutorService.class)
|| input.getClass().isAnnotationPresent(
ConfiguresHttpCommandExecutorService.class)
|| input.getClass().isAnnotationPresent(ConfiguresHttpCommandExecutorService.class)
|| instanceOf(LoggingModule.class).apply(input);
}

View File

@ -30,7 +30,7 @@ import com.google.inject.Module;
public abstract class BlobStoreContextBuilder<S, A> extends RestContextBuilder<S, A> {
@Override
public BlobStoreContextBuilder<S, A> withModules(Module... modules) {
public BlobStoreContextBuilder<S, A> withModules(Iterable<Module> modules) {
return (BlobStoreContextBuilder<S, A>) super.withModules(modules);
}
@ -38,8 +38,7 @@ public abstract class BlobStoreContextBuilder<S, A> extends RestContextBuilder<S
this(syncClientType, asyncClientType, new Properties());
}
public BlobStoreContextBuilder(Class<S> syncClientType, Class<A> asyncClientType,
Properties properties) {
public BlobStoreContextBuilder(Class<S> syncClientType, Class<A> asyncClientType, Properties properties) {
super(syncClientType, asyncClientType, properties);
}

View File

@ -40,8 +40,8 @@ public class BlobStoreContextFactory {
private final RestContextFactory contextFactory;
/**
* Initializes with the default properties built-in to jclouds. This is typically stored in the
* classpath resource {@code rest.properties}
* Initializes with the default properties built-in to jclouds. This is
* typically stored in the classpath resource {@code rest.properties}
*
* @see RestContextFactory#getPropertiesFromResource
*/
@ -64,8 +64,7 @@ public class BlobStoreContextFactory {
this.contextFactory = restContextFactory;
}
public static <S, A> BlobStoreContext buildContextUnwrappingExceptions(
BlobStoreContextBuilder<S, A> builder) {
public static <S, A> BlobStoreContext buildContextUnwrappingExceptions(BlobStoreContextBuilder<S, A> builder) {
try {
return builder.buildBlobStoreContext();
} catch (Exception e) {
@ -77,8 +76,8 @@ public class BlobStoreContextFactory {
* @see RestContextFactory#createContextBuilder(String, String, String)
*/
public BlobStoreContext createContext(String provider, String identity, String credential) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential));
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory.createContextBuilder(
provider, identity, credential));
return buildContextUnwrappingExceptions(builder);
}
@ -86,39 +85,40 @@ public class BlobStoreContextFactory {
* @see RestContextFactory#createContextBuilder(String, Properties)
*/
public BlobStoreContext createContext(String provider, Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, overrides));
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory.createContextBuilder(
provider, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, Iterable)
*/
public BlobStoreContext createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, modules, overrides));
public BlobStoreContext createContext(String provider, Iterable<? extends Module> modules, Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory.createContextBuilder(
provider, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable)
* @see RestContextFactory#createContextBuilder(String, String,String,
* Iterable)
*/
public BlobStoreContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential, modules));
public BlobStoreContext createContext(String provider, @Nullable String identity, @Nullable String credential,
Iterable<? extends Module> modules) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory.createContextBuilder(
provider, identity, credential, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
* @see RestContextFactory#createContextBuilder(String, String,String,
* Iterable, Properties)
*/
public BlobStoreContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential, modules, overrides));
public BlobStoreContext createContext(String provider, @Nullable String identity, @Nullable String credential,
Iterable<? extends Module> modules, Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(contextFactory.createContextBuilder(
provider, identity, credential, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
@ -126,38 +126,17 @@ public class BlobStoreContextFactory {
* @see RestContextFactory#createContextBuilder(ContextSpec)
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec));
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable)
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec, Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable, Properties)
*/
public <S, A> BlobStoreContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) {
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class
.cast(createContextBuilder(contextSpec, modules, overrides));
BlobStoreContextBuilder<?, ?> builder = BlobStoreContextBuilder.class.cast(createContextBuilder(contextSpec,
overrides));
return buildContextUnwrappingExceptions(builder);
}
}

View File

@ -56,7 +56,7 @@ public abstract class ComputeServiceContextBuilder<S, A> extends RestContextBuil
* {@inheritDoc}
*/
@Override
public ComputeServiceContextBuilder<S, A> withModules(Module... modules) {
public ComputeServiceContextBuilder<S, A> withModules(Iterable<Module> modules) {
return (ComputeServiceContextBuilder<S, A>) super.withModules(modules);
}

View File

@ -40,8 +40,8 @@ public class ComputeServiceContextFactory {
private final RestContextFactory contextFactory;
/**
* Initializes with the default properties built-in to jclouds. This is typically stored in the
* classpath resource {@code rest.properties}
* Initializes with the default properties built-in to jclouds. This is
* typically stored in the classpath resource {@code rest.properties}
*
* @see RestContextFactory#getPropertiesFromResource
*/
@ -58,7 +58,8 @@ public class ComputeServiceContextFactory {
/**
*
* Uses the supplied RestContextFactory to create {@link ComputeServiceContext}s
* Uses the supplied RestContextFactory to create
* {@link ComputeServiceContext}s
*/
public ComputeServiceContextFactory(RestContextFactory restContextFactory) {
this.contextFactory = restContextFactory;
@ -77,8 +78,8 @@ public class ComputeServiceContextFactory {
* @see RestContextFactory#createContextBuilder(String, String, String)
*/
public ComputeServiceContext createContext(String provider, String identity, String credential) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, identity, credential));
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential));
return buildContextUnwrappingExceptions(builder);
}
@ -86,40 +87,40 @@ public class ComputeServiceContextFactory {
* @see RestContextFactory#createContextBuilder(String, Properties)
*/
public ComputeServiceContext createContext(String provider, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, overrides));
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, Iterable)
*/
public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, modules, overrides));
public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable)
* @see RestContextFactory#createContextBuilder(String, String,String,
* Iterable)
*/
public ComputeServiceContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, identity, credential, modules));
public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential,
Iterable<? extends Module> modules) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String, Iterable, Properties)
* @see RestContextFactory#createContextBuilder(String, String,String,
* Iterable, Properties)
*/
public ComputeServiceContext createContext(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(contextFactory.createContextBuilder(provider, identity, credential, modules,
overrides));
public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential,
Iterable<? extends Module> modules, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.createContextBuilder(provider, identity, credential, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
@ -133,33 +134,13 @@ public class ComputeServiceContextFactory {
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable)
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec, modules));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec, overrides));
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(createContextBuilder(
contextSpec, overrides));
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable, Properties)
*/
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> modules, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class
.cast(createContextBuilder(contextSpec, modules, overrides));
return buildContextUnwrappingExceptions(builder);
}
}

View File

@ -29,7 +29,6 @@ import static org.jclouds.Constants.PROPERTY_PROVIDER;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
@ -125,8 +124,8 @@ public class RestContextBuilder<S, A> {
this.properties = checkNotNull(properties, "properties");
}
public RestContextBuilder<S, A> withModules(Module... modules) {
this.modules.addAll(Arrays.asList(modules));
public RestContextBuilder<S, A> withModules(Iterable<Module> modules) {
Iterables.addAll(this.modules, modules);
return this;
}

View File

@ -20,12 +20,18 @@ package org.jclouds.rest;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.toArray;
import static com.google.common.base.Splitter.on;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.size;
import static com.google.common.collect.Iterables.transform;
import static java.util.Collections.EMPTY_LIST;
import static org.jclouds.util.Utils.initContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import static org.jclouds.util.Utils.resolveContextBuilderClass;
import static org.jclouds.util.Utils.resolvePropertiesBuilderClass;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
@ -34,8 +40,12 @@ import javax.inject.Inject;
import org.jclouds.PropertiesBuilder;
import com.google.common.base.Throwables;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.google.inject.Module;
/**
@ -65,9 +75,10 @@ public class RestContextFactory {
public static <S, A> ContextSpec<S, A> contextSpec(String provider, String endpoint, String apiVersion,
String identity, String credential, Class<S> sync, Class<A> async,
Class<PropertiesBuilder> propertiesBuilderClass, Class<RestContextBuilder<S, A>> contextBuilderClass) {
Class<PropertiesBuilder> propertiesBuilderClass, Class<RestContextBuilder<S, A>> contextBuilderClass,
Iterable<Module> modules) {
return new ContextSpec<S, A>(provider, endpoint, apiVersion, identity, credential, sync, async,
propertiesBuilderClass, contextBuilderClass);
propertiesBuilderClass, contextBuilderClass, modules);
}
public static <S, A> ContextSpec<S, A> contextSpec(String provider, String endpoint, String apiVersion,
@ -75,6 +86,13 @@ public class RestContextFactory {
return new ContextSpec<S, A>(provider, endpoint, apiVersion, identity, credential, sync, async);
}
@SuppressWarnings("unchecked")
public static <S, A> ContextSpec<S, A> contextSpec(String provider, String endpoint, String apiVersion,
String identity, String credential, Class<S> sync, Class<A> async, Iterable<Module> modules) {
return new ContextSpec<S, A>(provider, endpoint, apiVersion, identity, credential, sync, async,
PropertiesBuilder.class, (Class) RestContextBuilder.class, modules);
}
public static class ContextSpec<S, A> {
final String provider;
final String endpoint;
@ -85,10 +103,11 @@ public class RestContextFactory {
final Class<A> async;
final Class<PropertiesBuilder> propertiesBuilderClass;
final Class<RestContextBuilder<S, A>> contextBuilderClass;
final Iterable<Module> modules;
ContextSpec(String provider, String endpoint, String apiVersion, String identity, String credential,
Class<S> sync, Class<A> async, Class<PropertiesBuilder> propertiesBuilderClass,
Class<RestContextBuilder<S, A>> contextBuilderClass) {
Class<RestContextBuilder<S, A>> contextBuilderClass, Iterable<Module> modules) {
this.provider = checkNotNull(provider, "provider");
this.endpoint = endpoint;
this.apiVersion = apiVersion;
@ -103,13 +122,14 @@ public class RestContextFactory {
+ " is not a subclass of " + PropertiesBuilder.class.getName());
this.propertiesBuilderClass = propertiesBuilderClass;
this.contextBuilderClass = contextBuilderClass;
this.modules = modules;
}
@SuppressWarnings("unchecked")
public ContextSpec(String provider, String endpoint, String apiVersion, String identity, String credential,
Class<S> sync, Class<A> async) {
this(provider, endpoint, apiVersion, identity, credential, sync, async, PropertiesBuilder.class,
(Class) RestContextBuilder.class);
(Class) RestContextBuilder.class, EMPTY_LIST);
}
@Override
@ -244,7 +264,7 @@ public class RestContextFactory {
try {
properties.load(RestContextFactory.class.getResourceAsStream(filename));
} catch (IOException e) {
Throwables.propagate(e);
propagate(e);
}
properties.putAll(System.getProperties());
return properties;
@ -268,8 +288,15 @@ public class RestContextFactory {
* Iterable<? extends Module>, Properties)
*/
public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, Properties overrides) {
return createContextBuilder(provider, overrides.getProperty("jclouds.identity"), overrides
.getProperty("jclouds.credential"), ImmutableSet.<Module> of(), overrides);
return createContextBuilder(provider, null, null, ImmutableSet.<Module> of(), overrides);
}
/**
* @see RestContextFactory#createContextBuilder(String, Properties,
* Iterable<? extends Module>, Properties)
*/
public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, Iterable<Module> modules) {
return createContextBuilder(provider, null, null, modules, NO_PROPERTIES);
}
/**
@ -294,13 +321,18 @@ public class RestContextFactory {
*/
public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, Iterable<? extends Module> wiring,
Properties overrides) {
return createContextBuilder(provider, overrides.getProperty("jclouds.identity"), overrides
.getProperty("jclouds.credential"), wiring, overrides);
return createContextBuilder(provider, null, null, wiring, overrides);
}
@SuppressWarnings("unchecked")
public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, @Nullable String identity,
@Nullable String credential, Properties properties) {
return createContextBuilder(provider, identity, credential, EMPTY_LIST, properties);
}
public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> wiring) {
return createContextBuilder(provider, identity, credential, wiring, new Properties());
return createContextBuilder(provider, identity, credential, wiring, NO_PROPERTIES);
}
/**
@ -321,8 +353,8 @@ public class RestContextFactory {
public <S, A> RestContextBuilder<S, A> createContextBuilder(String providerName, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> wiring, Properties _overrides) {
checkNotNull(wiring, "wiring");
ContextSpec<S, A> contextSpec = createContextSpec(providerName, identity, credential, _overrides);
return createContextBuilder(contextSpec, wiring, _overrides);
ContextSpec<S, A> contextSpec = createContextSpec(providerName, identity, credential, wiring, _overrides);
return createContextBuilder(contextSpec, _overrides);
}
public static Properties toProperties(ContextSpec<?, ?> contextSpec) {
@ -340,19 +372,35 @@ public class RestContextFactory {
props.setProperty(contextSpec.provider + ".async", checkNotNull(contextSpec.async, "contextSpec.async")
.getName());
} else {
props.setProperty(contextSpec.provider + ".contextbuilder", checkNotNull(contextSpec.contextBuilderClass,
"contextSpec.contextBuilderClass").getName());
props.setProperty(contextSpec.provider + ".propertiesbuilder", checkNotNull(
contextSpec.propertiesBuilderClass, "contextSpec.propertiesBuilderClass").getName());
}
if (size(contextSpec.modules) > 0) {
props.setProperty(contextSpec.provider + ".modules", Joiner.on(',').join(
transform(contextSpec.modules, new Function<Module, String>() {
@Override
public String apply(Module from) {
return from.getClass().getName();
}
})));
}
return props;
}
@SuppressWarnings("unchecked")
public <A, S> ContextSpec<S, A> createContextSpec(String providerName, String identity, String credential,
public <S, A> ContextSpec<S, A> createContextSpec(String providerName, String identity, String credential,
Properties _overrides) {
return createContextSpec(providerName, identity, credential, EMPTY_LIST, _overrides);
}
@SuppressWarnings("unchecked")
public <S, A> ContextSpec<S, A> createContextSpec(String providerName, String identity, String credential,
Iterable<? extends Module> wiring, Properties _overrides) {
checkNotNull(providerName, "providerName");
checkNotNull(_overrides, "overrides");
@ -362,10 +410,13 @@ public class RestContextFactory {
String endpoint = props.getProperty(providerName + ".endpoint", null);
String apiVersion = props.getProperty(providerName + ".apiversion", null);
identity = props.getProperty(providerName + ".identity", identity);
credential = props.getProperty(providerName + ".credential", credential);
identity = props.getProperty(providerName + ".identity", props.getProperty("jclouds.identity", identity));
credential = loadCredentialOrDefault(props, providerName + ".credential", loadCredentialOrDefault(props,
"jclouds.credential", credential));
String syncClassName = props.getProperty(providerName + ".sync", null);
String asyncClassName = props.getProperty(providerName + ".async", null);
Iterable<Module> modules = concat(modulesFromProperty(props, "jclouds.modules"), modulesFromProperty(props,
providerName + ".modules"), wiring);
Class<RestContextBuilder<S, A>> contextBuilderClass;
Class<PropertiesBuilder> propertiesBuilderClass;
@ -377,32 +428,77 @@ public class RestContextFactory {
sync = (Class<S>) (syncClassName != null ? Class.forName(syncClassName) : null);
async = (Class<A>) (syncClassName != null ? Class.forName(asyncClassName) : null);
} catch (Exception e) {
Throwables.propagate(e);
propagate(e);
assert false : "exception should have propogated " + e;
return null;
}
ContextSpec<S, A> contextSpec = new ContextSpec<S, A>(providerName, endpoint, apiVersion, identity, credential,
sync, async, propertiesBuilderClass, contextBuilderClass);
sync, async, propertiesBuilderClass, contextBuilderClass, modules);
return contextSpec;
}
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec) {
return createContextBuilder(contextSpec, new Properties());
static String loadCredentialOrDefault(Properties properties, String property, String credential) {
if (properties.containsKey(property))
return properties.getProperty(property);
else if (properties.containsKey(property + ".resource"))
try {
return org.jclouds.util.Utils.toStringAndClose(RestContextFactory.class.getResourceAsStream(properties
.getProperty(property + ".resource")));
} catch (IOException e) {
throw new RuntimeException("error reading resource: " + properties.getProperty(property + ".resource"));
}
else if (properties.containsKey(property + ".file"))
try {
return Files.toString(new File(properties.getProperty(property + ".file")), Charsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("error reading file: " + properties.getProperty(property + ".file"));
}
else
return credential;
}
static Iterable<Module> modulesFromProperty(Properties props, String property) {
String moduleClasses = props.getProperty(property, null);
Iterable<Module> modules = ImmutableSet.of();
if (moduleClasses != null) {
Iterable<String> transformer = ImmutableList.copyOf(on(',').split(moduleClasses));
modules = transform(transformer, new Function<String, Module>() {
@Override
public Module apply(String from) {
try {
return (Module) Class.forName(from).newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("error instantiating " + from, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("error instantiating " + from, e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("error instantiating " + from, e);
}
}
});
}
return modules;
}
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec) {
return createContextBuilder(contextSpec, NO_PROPERTIES);
}
@SuppressWarnings("unchecked")
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec,
Properties overrides) {
return createContextBuilder(contextSpec, ImmutableSet.<Module> of(), overrides);
return createContextBuilder(contextSpec, EMPTY_LIST, overrides);
}
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> wiring) {
return createContextBuilder(contextSpec, wiring, new Properties());
Iterable<Module> modules) {
return createContextBuilder(contextSpec, modules, NO_PROPERTIES);
}
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> wiring, Properties overrides) {
Iterable<Module> modules, Properties overrides) {
try {
PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor(Properties.class).newInstance(
overrides);
@ -418,7 +514,7 @@ public class RestContextFactory {
RestContextBuilder<S, A> contextBuilder = initContextBuilder(contextSpec.contextBuilderClass,
contextSpec.sync, contextSpec.async, builder.build());
contextBuilder.withModules(toArray(wiring, Module.class));
contextBuilder.withModules(concat(modules, contextSpec.modules));
return contextBuilder;
} catch (Exception e) {
@ -459,6 +555,16 @@ public class RestContextFactory {
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String,
* Properties)
*/
public <S, A> RestContext<S, A> createContext(String provider, @Nullable String identity,
@Nullable String credential, Properties properties) {
RestContextBuilder<S, A> builder = createContextBuilder(provider, identity, credential, properties);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(String, String,String,
* Iterable)
@ -479,16 +585,6 @@ public class RestContextFactory {
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable,
* Properties)
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> wiring, Properties overrides) {
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, wiring, overrides);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec)
*/
@ -497,14 +593,6 @@ public class RestContextFactory {
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable)
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec, Iterable<? extends Module> wiring) {
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, wiring);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/
@ -512,4 +600,22 @@ public class RestContextFactory {
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, overrides);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable)
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec, Iterable<Module> modules) {
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, modules);
return buildContextUnwrappingExceptions(builder);
}
/**
* @see RestContextFactory#createContextBuilder(ContextSpec, Iterable,
* Properties)
*/
public static <S, A> RestContext<S, A> createContext(ContextSpec<S, A> contextSpec, Iterable<Module> modules,
Properties overrides) {
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, modules, overrides);
return buildContextUnwrappingExceptions(builder);
}
}

View File

@ -244,9 +244,9 @@ public abstract class BaseJettyTest {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost:" + testPort, "1", "identity", null, IntegrationTestClient.class,
IntegrationTestAsyncClient.class);
IntegrationTestAsyncClient.class, ImmutableSet.<Module> copyOf(connectionModules));
return createContextBuilder(contextSpec, ImmutableSet.<Module> copyOf(connectionModules), properties);
return createContextBuilder(contextSpec, properties);
}
@AfterTest

View File

@ -21,6 +21,7 @@ package org.jclouds.rest;
import static org.testng.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
@ -82,8 +83,8 @@ public class RestContextBuilderTest {
modules.add(loggingModule);
HttpModule httpModule = new HttpModule();
modules.add(httpModule);
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(
String.class, String.class, new Properties());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 2);
@ -95,8 +96,8 @@ public class RestContextBuilderTest {
public void testAddBothWhenDoesntRequireHttp() {
List<Module> modules = new ArrayList<Module>();
modules.add(new ConfiguresRestClientModule());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(
String.class, String.class, new Properties());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 2);
@ -115,8 +116,8 @@ public class RestContextBuilderTest {
@Test
public void testAddBothWhenDefault() {
List<Module> modules = new ArrayList<Module>();
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(
String.class, String.class, new Properties());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 2);
@ -136,8 +137,8 @@ public class RestContextBuilderTest {
public void testAddBothWhenLive() {
List<Module> modules = new ArrayList<Module>();
modules.add(new RequiresHttpModule());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(
String.class, String.class, new Properties());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 3);
@ -148,21 +149,21 @@ public class RestContextBuilderTest {
public void testBuilder() {
AbstractModule module1 = new AbstractModule() {
Module module1 = new AbstractModule() {
@Override
protected void configure() {
}
};
AbstractModule module2 = new AbstractModule() {
Module module2 = new AbstractModule() {
@Override
protected void configure() {
}
};
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(
String.class, String.class, new Properties());
builder.withModules(module1, module2);
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
new Properties());
builder.withModules(Arrays.asList(module1, module2));
}
}

View File

@ -22,6 +22,10 @@ import static org.jclouds.rest.RestContextFactory.contextSpec;
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
@ -33,6 +37,10 @@ import org.jclouds.rest.RestContextFactory.ContextSpec;
import org.jclouds.rest.config.RestClientModule;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
/**
@ -43,17 +51,15 @@ import com.google.inject.Module;
public class RestContextFactoryTest {
public void testBuilder() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec(
"test", "http://localhost", "1", "dummy", null, IntegrationTestClient.class,
IntegrationTestAsyncClient.class);
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", null, IntegrationTestClient.class, IntegrationTestAsyncClient.class);
createContextBuilder(contextSpec);
}
public void testBuilderProperties() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec(
"test", "http://localhost", "1", "dummy", null, IntegrationTestClient.class,
IntegrationTestAsyncClient.class);
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", null, IntegrationTestClient.class, IntegrationTestAsyncClient.class);
Properties props = RestContextFactory.toProperties(contextSpec);
assertEquals(props.getProperty("test.endpoint"), "http://localhost");
@ -64,13 +70,14 @@ public class RestContextFactoryTest {
assertEquals(props.getProperty("test.async"), IntegrationTestAsyncClient.class.getName());
assertEquals(props.getProperty("test.propertiesbuilder"), null);
assertEquals(props.getProperty("test.contextbuilder"), null);
assertEquals(props.getProperty("test.modules"), null);
new RestContextFactory().createContext("test", props);
}
public void testBuilderPropertiesWithCredential() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec(
"test", "http://localhost", "1", "dummy", "credential", IntegrationTestClient.class,
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", "credential", IntegrationTestClient.class,
IntegrationTestAsyncClient.class);
Properties props = RestContextFactory.toProperties(contextSpec);
@ -82,15 +89,16 @@ public class RestContextFactoryTest {
assertEquals(props.getProperty("test.async"), IntegrationTestAsyncClient.class.getName());
assertEquals(props.getProperty("test.propertiesbuilder"), null);
assertEquals(props.getProperty("test.contextbuilder"), null);
assertEquals(props.getProperty("test.modules"), null);
new RestContextFactory().createContext("test", props);
}
@SuppressWarnings("unchecked")
public void testBuilderPropertiesWithContextBuilder() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec(
"test", "http://localhost", "1", "dummy", null, (Class) null, (Class) null,
PropertiesBuilder.class, (Class) IntegrationTestContextBuilder.class);
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", null, (Class) null, (Class) null, PropertiesBuilder.class,
(Class) IntegrationTestContextBuilder.class, Collections.EMPTY_LIST);
Properties props = RestContextFactory.toProperties(contextSpec);
assertEquals(props.getProperty("test.endpoint"), "http://localhost");
@ -100,19 +108,138 @@ public class RestContextFactoryTest {
assertEquals(props.getProperty("test.sync"), null);
assertEquals(props.getProperty("test.async"), null);
assertEquals(props.getProperty("test.propertiesbuilder"), PropertiesBuilder.class.getName());
assertEquals(props.getProperty("test.contextbuilder"), IntegrationTestContextBuilder.class
.getName());
assertEquals(props.getProperty("test.contextbuilder"), IntegrationTestContextBuilder.class.getName());
assertEquals(props.getProperty("test.modules"), null);
new RestContextFactory().createContext("test", props);
}
@SuppressWarnings("unchecked")
public void testBuilderPropertiesWithModule() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", null, (Class) null, (Class) null, PropertiesBuilder.class,
(Class) IntegrationTestContextBuilder.class, Collections.<Module> singleton(new A()));
Properties props = RestContextFactory.toProperties(contextSpec);
assertEquals(props.getProperty("test.endpoint"), "http://localhost");
assertEquals(props.getProperty("test.apiversion"), "1");
assertEquals(props.getProperty("test.identity"), "dummy");
assertEquals(props.getProperty("test.credential"), null);
assertEquals(props.getProperty("test.sync"), null);
assertEquals(props.getProperty("test.async"), null);
assertEquals(props.getProperty("test.propertiesbuilder"), PropertiesBuilder.class.getName());
assertEquals(props.getProperty("test.contextbuilder"), IntegrationTestContextBuilder.class.getName());
assertEquals(props.getProperty("test.modules"), "org.jclouds.rest.RestContextFactoryTest$A");
new RestContextFactory().createContext("test", props);
}
@SuppressWarnings("unchecked")
public void testBuilderPropertiesWithModules() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", null, (Class) null, (Class) null, PropertiesBuilder.class,
(Class) IntegrationTestContextBuilder.class, Arrays.<Module> asList(new A(), new B()));
Properties props = RestContextFactory.toProperties(contextSpec);
assertEquals(props.getProperty("test.endpoint"), "http://localhost");
assertEquals(props.getProperty("test.apiversion"), "1");
assertEquals(props.getProperty("test.identity"), "dummy");
assertEquals(props.getProperty("test.credential"), null);
assertEquals(props.getProperty("test.sync"), null);
assertEquals(props.getProperty("test.async"), null);
assertEquals(props.getProperty("test.propertiesbuilder"), PropertiesBuilder.class.getName());
assertEquals(props.getProperty("test.contextbuilder"), IntegrationTestContextBuilder.class.getName());
assertEquals(props.getProperty("test.modules"),
"org.jclouds.rest.RestContextFactoryTest$A,org.jclouds.rest.RestContextFactoryTest$B");
new RestContextFactory().createContext("test", props);
}
public void testBuilderPropertiesJCloudsScope() {
Properties props = new Properties();
props.setProperty("test.endpoint", "http://localhost");
props.setProperty("test.apiversion", "1");
props.setProperty("jclouds.identity", "foo");
props.setProperty("jclouds.credential", "bar");
props.setProperty("test.propertiesbuilder", PropertiesBuilder.class.getName());
props.setProperty("test.contextbuilder", IntegrationTestContextBuilder.class.getName());
props.setProperty("jclouds.modules",
"org.jclouds.rest.RestContextFactoryTest$A,org.jclouds.rest.RestContextFactoryTest$B");
new RestContextFactory() {
@SuppressWarnings("hiding")
@Override
public <S, A> ContextSpec<S, A> createContextSpec(String providerName, String identity, String credential,
Iterable<? extends Module> wiring, Properties _overrides) {
ContextSpec<S, A> spec = super.createContextSpec(providerName, identity, credential, wiring, _overrides);
assertEquals(spec.identity, "foo");
assertEquals(spec.credential, "bar");
assertEquals(Iterables.size(spec.modules), 2);
return spec;
}
}.createContext("test", props);
}
public void testBuilderPropertiesJCloudsScopeWithProviderIdentityAndFileCredential() throws IOException {
File file = File.createTempFile("foo", "bar");
file.deleteOnExit();
Files.write("bar", file, Charsets.UTF_8);
Properties props = new Properties();
props.setProperty("test.endpoint", "http://localhost");
props.setProperty("test.apiversion", "1");
props.setProperty("test.identity", "foo");
props.setProperty("test.credential.file", file.getAbsolutePath());
props.setProperty("test.propertiesbuilder", PropertiesBuilder.class.getName());
props.setProperty("test.contextbuilder", IntegrationTestContextBuilder.class.getName());
props.setProperty("jclouds.modules",
"org.jclouds.rest.RestContextFactoryTest$A,org.jclouds.rest.RestContextFactoryTest$B");
new RestContextFactory() {
@SuppressWarnings("hiding")
@Override
public <S, A> ContextSpec<S, A> createContextSpec(String providerName, String identity, String credential,
Iterable<? extends Module> wiring, Properties _overrides) {
ContextSpec<S, A> spec = super.createContextSpec(providerName, identity, credential, wiring, _overrides);
assertEquals(spec.identity, "foo");
assertEquals(spec.credential, "bar");
assertEquals(Iterables.size(spec.modules), 2);
return spec;
}
}.createContext("test", props);
}
static class A extends AbstractModule {
@Override
protected void configure() {
}
}
static class B extends AbstractModule {
@Override
protected void configure() {
}
}
@SuppressWarnings("unchecked")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testBuilderPropertiesWithWrongConfig() {
@SuppressWarnings("unused")
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec(
"test", "http://localhost", "1", "dummy", null, (Class) null, (Class) null,
(Class) IntegrationTestContextBuilder.class, (Class) PropertiesBuilder.class);
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost", "1", "dummy", null, (Class) null, (Class) null,
(Class) IntegrationTestContextBuilder.class, (Class) PropertiesBuilder.class, Collections.EMPTY_LIST);
}
@RequiresHttp

View File

@ -288,10 +288,10 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
private Injector injectorForClient() {
ContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null,
Caller.class, AsyncCaller.class);
Caller.class, AsyncCaller.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(),
new CallerCalleeModule()));
return createContextBuilder(contextSpec,
ImmutableSet.of(new MockModule(), new NullLoggingModule(), new CallerCalleeModule())).buildInjector();
return createContextBuilder(contextSpec).buildInjector();
}
@ -1975,17 +1975,17 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@BeforeClass
void setupFactory() {
ContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null,
String.class, Integer.class);
injector = createContextBuilder(contextSpec,
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
String.class, Integer.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(),
new AbstractModule() {
@Override
protected void configure() {
bind(URI.class).annotatedWith(Localhost2.class).toInstance(URI.create("http://localhost:1111"));
}
})).buildInjector();
}));
injector = createContextBuilder(contextSpec).buildInjector();
parserFactory = injector.getInstance(ParseSax.Factory.class);
encryptionService = injector.getInstance(EncryptionService.class);
}