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.NoSuchElementException;
import java.util.Properties; import java.util.Properties;
import org.jclouds.aws.ec2.compute.config.EC2ComputeServiceContextModule; import org.jclouds.aws.ec2.compute.config.EC2ComputeServiceContextModule;
import org.jclouds.aws.ec2.compute.config.EC2ResolveImagesModule; import org.jclouds.aws.ec2.compute.config.EC2ResolveImagesModule;
import org.jclouds.aws.ec2.config.EC2RestClientModule; import org.jclouds.aws.ec2.config.EC2RestClientModule;
@ -42,14 +41,17 @@ import com.google.inject.Injector;
import com.google.inject.Module; import com.google.inject.Module;
/** /**
* Creates {@link EC2ComputeServiceContext} or {@link Injector} instances based on the most commonly * Creates {@link EC2ComputeServiceContext} or {@link Injector} instances based
* requested arguments. * on the most commonly requested arguments.
* <p/> * <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/>
* <p/> * <p/>
* If no <code>Module</code>s are specified, the default {@link JDKLoggingModule logging} and * If no <code>Module</code>s are specified, the default
* {@link JavaUrlHttpCommandExecutorServiceModule http transports} will be installed. * {@link JDKLoggingModule logging} and
* {@link JavaUrlHttpCommandExecutorServiceModule http transports} will be
* installed.
* *
* @author Adrian Cole * @author Adrian Cole
* @see EC2ComputeServiceContext * @see EC2ComputeServiceContext
@ -61,7 +63,7 @@ public class EC2ContextBuilder extends ComputeServiceContextBuilder<EC2Client, E
} }
@Override @Override
public EC2ContextBuilder withModules(Module... modules) { public EC2ContextBuilder withModules(Iterable<Module> modules) {
return (EC2ContextBuilder) super.withModules(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>() { Iterable<Module> infra = Iterables.filter(modules, new Predicate<Module>() {
public boolean apply(Module input) { public boolean apply(Module input) {
return input.getClass().isAnnotationPresent(ConfiguresExecutorService.class) return input.getClass().isAnnotationPresent(ConfiguresExecutorService.class)
|| input.getClass().isAnnotationPresent( || input.getClass().isAnnotationPresent(ConfiguresHttpCommandExecutorService.class)
ConfiguresHttpCommandExecutorService.class)
|| instanceOf(LoggingModule.class).apply(input); || 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> { public abstract class BlobStoreContextBuilder<S, A> extends RestContextBuilder<S, A> {
@Override @Override
public BlobStoreContextBuilder<S, A> withModules(Module... modules) { public BlobStoreContextBuilder<S, A> withModules(Iterable<Module> modules) {
return (BlobStoreContextBuilder<S, A>) super.withModules(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()); this(syncClientType, asyncClientType, new Properties());
} }
public BlobStoreContextBuilder(Class<S> syncClientType, Class<A> asyncClientType, public BlobStoreContextBuilder(Class<S> syncClientType, Class<A> asyncClientType, Properties properties) {
Properties properties) {
super(syncClientType, asyncClientType, properties); super(syncClientType, asyncClientType, properties);
} }

View File

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

View File

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

View File

@ -40,8 +40,8 @@ public class ComputeServiceContextFactory {
private final RestContextFactory contextFactory; private final RestContextFactory contextFactory;
/** /**
* Initializes with the default properties built-in to jclouds. This is typically stored in the * Initializes with the default properties built-in to jclouds. This is
* classpath resource {@code rest.properties} * typically stored in the classpath resource {@code rest.properties}
* *
* @see RestContextFactory#getPropertiesFromResource * @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) { public ComputeServiceContextFactory(RestContextFactory restContextFactory) {
this.contextFactory = restContextFactory; this.contextFactory = restContextFactory;
@ -77,8 +78,8 @@ public class ComputeServiceContextFactory {
* @see RestContextFactory#createContextBuilder(String, String, String) * @see RestContextFactory#createContextBuilder(String, String, String)
*/ */
public ComputeServiceContext createContext(String provider, String identity, String credential) { public ComputeServiceContext createContext(String provider, String identity, String credential) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.cast(contextFactory.createContextBuilder(provider, identity, credential)); .createContextBuilder(provider, identity, credential));
return buildContextUnwrappingExceptions(builder); return buildContextUnwrappingExceptions(builder);
} }
@ -86,40 +87,40 @@ public class ComputeServiceContextFactory {
* @see RestContextFactory#createContextBuilder(String, Properties) * @see RestContextFactory#createContextBuilder(String, Properties)
*/ */
public ComputeServiceContext createContext(String provider, Properties overrides) { public ComputeServiceContext createContext(String provider, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.cast(contextFactory.createContextBuilder(provider, overrides)); .createContextBuilder(provider, overrides));
return buildContextUnwrappingExceptions(builder); return buildContextUnwrappingExceptions(builder);
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, Iterable) * @see RestContextFactory#createContextBuilder(String, Iterable)
*/ */
public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules, public ComputeServiceContext createContext(String provider, Iterable<? extends Module> modules, Properties overrides) {
Properties overrides) { ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class .createContextBuilder(provider, modules, overrides));
.cast(contextFactory.createContextBuilder(provider, modules, overrides));
return buildContextUnwrappingExceptions(builder); 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, public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential,
@Nullable String credential, Iterable<? extends Module> modules) { Iterable<? extends Module> modules) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.cast(contextFactory.createContextBuilder(provider, identity, credential, modules)); .createContextBuilder(provider, identity, credential, modules));
return buildContextUnwrappingExceptions(builder); 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, public ComputeServiceContext createContext(String provider, @Nullable String identity, @Nullable String credential,
@Nullable String credential, Iterable<? extends Module> modules, Properties overrides) { Iterable<? extends Module> modules, Properties overrides) {
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(contextFactory
.cast(contextFactory.createContextBuilder(provider, identity, credential, modules, .createContextBuilder(provider, identity, credential, modules, overrides));
overrides));
return buildContextUnwrappingExceptions(builder); 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) * @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/ */
public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec, public <S, A> ComputeServiceContext createContext(ContextSpec<S, A> contextSpec, Properties overrides) {
Properties overrides) { ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class.cast(createContextBuilder(
ComputeServiceContextBuilder<?, ?> builder = ComputeServiceContextBuilder.class contextSpec, overrides));
.cast(createContextBuilder(contextSpec, overrides));
return buildContextUnwrappingExceptions(builder); 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.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -125,8 +124,8 @@ public class RestContextBuilder<S, A> {
this.properties = checkNotNull(properties, "properties"); this.properties = checkNotNull(properties, "properties");
} }
public RestContextBuilder<S, A> withModules(Module... modules) { public RestContextBuilder<S, A> withModules(Iterable<Module> modules) {
this.modules.addAll(Arrays.asList(modules)); Iterables.addAll(this.modules, modules);
return this; 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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; 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.initContextBuilder;
import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException; import static org.jclouds.util.Utils.propagateAuthorizationOrOriginalException;
import static org.jclouds.util.Utils.resolveContextBuilderClass; import static org.jclouds.util.Utils.resolveContextBuilderClass;
import static org.jclouds.util.Utils.resolvePropertiesBuilderClass; import static org.jclouds.util.Utils.resolvePropertiesBuilderClass;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
@ -34,8 +40,12 @@ import javax.inject.Inject;
import org.jclouds.PropertiesBuilder; 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.collect.ImmutableSet;
import com.google.common.io.Files;
import com.google.inject.Module; 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, public static <S, A> ContextSpec<S, A> contextSpec(String provider, String endpoint, String apiVersion,
String identity, String credential, Class<S> sync, Class<A> async, 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, 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, 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); 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> { public static class ContextSpec<S, A> {
final String provider; final String provider;
final String endpoint; final String endpoint;
@ -85,10 +103,11 @@ public class RestContextFactory {
final Class<A> async; final Class<A> async;
final Class<PropertiesBuilder> propertiesBuilderClass; final Class<PropertiesBuilder> propertiesBuilderClass;
final Class<RestContextBuilder<S, A>> contextBuilderClass; final Class<RestContextBuilder<S, A>> contextBuilderClass;
final Iterable<Module> modules;
ContextSpec(String provider, String endpoint, String apiVersion, String identity, String credential, ContextSpec(String provider, String endpoint, String apiVersion, String identity, String credential,
Class<S> sync, Class<A> async, Class<PropertiesBuilder> propertiesBuilderClass, 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.provider = checkNotNull(provider, "provider");
this.endpoint = endpoint; this.endpoint = endpoint;
this.apiVersion = apiVersion; this.apiVersion = apiVersion;
@ -103,13 +122,14 @@ public class RestContextFactory {
+ " is not a subclass of " + PropertiesBuilder.class.getName()); + " is not a subclass of " + PropertiesBuilder.class.getName());
this.propertiesBuilderClass = propertiesBuilderClass; this.propertiesBuilderClass = propertiesBuilderClass;
this.contextBuilderClass = contextBuilderClass; this.contextBuilderClass = contextBuilderClass;
this.modules = modules;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ContextSpec(String provider, String endpoint, String apiVersion, String identity, String credential, public ContextSpec(String provider, String endpoint, String apiVersion, String identity, String credential,
Class<S> sync, Class<A> async) { Class<S> sync, Class<A> async) {
this(provider, endpoint, apiVersion, identity, credential, sync, async, PropertiesBuilder.class, this(provider, endpoint, apiVersion, identity, credential, sync, async, PropertiesBuilder.class,
(Class) RestContextBuilder.class); (Class) RestContextBuilder.class, EMPTY_LIST);
} }
@Override @Override
@ -244,7 +264,7 @@ public class RestContextFactory {
try { try {
properties.load(RestContextFactory.class.getResourceAsStream(filename)); properties.load(RestContextFactory.class.getResourceAsStream(filename));
} catch (IOException e) { } catch (IOException e) {
Throwables.propagate(e); propagate(e);
} }
properties.putAll(System.getProperties()); properties.putAll(System.getProperties());
return properties; return properties;
@ -268,8 +288,15 @@ public class RestContextFactory {
* Iterable<? extends Module>, Properties) * Iterable<? extends Module>, Properties)
*/ */
public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, Properties overrides) { public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, Properties overrides) {
return createContextBuilder(provider, overrides.getProperty("jclouds.identity"), overrides return createContextBuilder(provider, null, null, ImmutableSet.<Module> of(), overrides);
.getProperty("jclouds.credential"), 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, public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, Iterable<? extends Module> wiring,
Properties overrides) { Properties overrides) {
return createContextBuilder(provider, overrides.getProperty("jclouds.identity"), overrides return createContextBuilder(provider, null, null, wiring, overrides);
.getProperty("jclouds.credential"), 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, public <S, A> RestContextBuilder<S, A> createContextBuilder(String provider, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> wiring) { @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, public <S, A> RestContextBuilder<S, A> createContextBuilder(String providerName, @Nullable String identity,
@Nullable String credential, Iterable<? extends Module> wiring, Properties _overrides) { @Nullable String credential, Iterable<? extends Module> wiring, Properties _overrides) {
checkNotNull(wiring, "wiring"); checkNotNull(wiring, "wiring");
ContextSpec<S, A> contextSpec = createContextSpec(providerName, identity, credential, _overrides); ContextSpec<S, A> contextSpec = createContextSpec(providerName, identity, credential, wiring, _overrides);
return createContextBuilder(contextSpec, wiring, _overrides); return createContextBuilder(contextSpec, _overrides);
} }
public static Properties toProperties(ContextSpec<?, ?> contextSpec) { public static Properties toProperties(ContextSpec<?, ?> contextSpec) {
@ -340,19 +372,35 @@ public class RestContextFactory {
props.setProperty(contextSpec.provider + ".async", checkNotNull(contextSpec.async, "contextSpec.async") props.setProperty(contextSpec.provider + ".async", checkNotNull(contextSpec.async, "contextSpec.async")
.getName()); .getName());
} else { } else {
props.setProperty(contextSpec.provider + ".contextbuilder", checkNotNull(contextSpec.contextBuilderClass, props.setProperty(contextSpec.provider + ".contextbuilder", checkNotNull(contextSpec.contextBuilderClass,
"contextSpec.contextBuilderClass").getName()); "contextSpec.contextBuilderClass").getName());
props.setProperty(contextSpec.provider + ".propertiesbuilder", checkNotNull( props.setProperty(contextSpec.provider + ".propertiesbuilder", checkNotNull(
contextSpec.propertiesBuilderClass, "contextSpec.propertiesBuilderClass").getName()); 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; return props;
} }
@SuppressWarnings("unchecked") @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) { 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(providerName, "providerName");
checkNotNull(_overrides, "overrides"); checkNotNull(_overrides, "overrides");
@ -362,10 +410,13 @@ public class RestContextFactory {
String endpoint = props.getProperty(providerName + ".endpoint", null); String endpoint = props.getProperty(providerName + ".endpoint", null);
String apiVersion = props.getProperty(providerName + ".apiversion", null); String apiVersion = props.getProperty(providerName + ".apiversion", null);
identity = props.getProperty(providerName + ".identity", identity); identity = props.getProperty(providerName + ".identity", props.getProperty("jclouds.identity", identity));
credential = props.getProperty(providerName + ".credential", credential); credential = loadCredentialOrDefault(props, providerName + ".credential", loadCredentialOrDefault(props,
"jclouds.credential", credential));
String syncClassName = props.getProperty(providerName + ".sync", null); String syncClassName = props.getProperty(providerName + ".sync", null);
String asyncClassName = props.getProperty(providerName + ".async", 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<RestContextBuilder<S, A>> contextBuilderClass;
Class<PropertiesBuilder> propertiesBuilderClass; Class<PropertiesBuilder> propertiesBuilderClass;
@ -377,32 +428,77 @@ public class RestContextFactory {
sync = (Class<S>) (syncClassName != null ? Class.forName(syncClassName) : null); sync = (Class<S>) (syncClassName != null ? Class.forName(syncClassName) : null);
async = (Class<A>) (syncClassName != null ? Class.forName(asyncClassName) : null); async = (Class<A>) (syncClassName != null ? Class.forName(asyncClassName) : null);
} catch (Exception e) { } catch (Exception e) {
Throwables.propagate(e); propagate(e);
assert false : "exception should have propogated " + e; assert false : "exception should have propogated " + e;
return null; return null;
} }
ContextSpec<S, A> contextSpec = new ContextSpec<S, A>(providerName, endpoint, apiVersion, identity, credential, ContextSpec<S, A> contextSpec = new ContextSpec<S, A>(providerName, endpoint, apiVersion, identity, credential,
sync, async, propertiesBuilderClass, contextBuilderClass); sync, async, propertiesBuilderClass, contextBuilderClass, modules);
return contextSpec; return contextSpec;
} }
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec) { static String loadCredentialOrDefault(Properties properties, String property, String credential) {
return createContextBuilder(contextSpec, new Properties()); 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, public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec,
Properties overrides) { 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, public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> wiring) { Iterable<Module> modules) {
return createContextBuilder(contextSpec, wiring, new Properties()); return createContextBuilder(contextSpec, modules, NO_PROPERTIES);
} }
public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec, public static <S, A> RestContextBuilder<S, A> createContextBuilder(ContextSpec<S, A> contextSpec,
Iterable<? extends Module> wiring, Properties overrides) { Iterable<Module> modules, Properties overrides) {
try { try {
PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor(Properties.class).newInstance( PropertiesBuilder builder = contextSpec.propertiesBuilderClass.getConstructor(Properties.class).newInstance(
overrides); overrides);
@ -418,7 +514,7 @@ public class RestContextFactory {
RestContextBuilder<S, A> contextBuilder = initContextBuilder(contextSpec.contextBuilderClass, RestContextBuilder<S, A> contextBuilder = initContextBuilder(contextSpec.contextBuilderClass,
contextSpec.sync, contextSpec.async, builder.build()); contextSpec.sync, contextSpec.async, builder.build());
contextBuilder.withModules(toArray(wiring, Module.class)); contextBuilder.withModules(concat(modules, contextSpec.modules));
return contextBuilder; return contextBuilder;
} catch (Exception e) { } catch (Exception e) {
@ -459,6 +555,16 @@ public class RestContextFactory {
return buildContextUnwrappingExceptions(builder); 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, * @see RestContextFactory#createContextBuilder(String, String,String,
* Iterable) * Iterable)
@ -479,16 +585,6 @@ public class RestContextFactory {
return buildContextUnwrappingExceptions(builder); 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) * @see RestContextFactory#createContextBuilder(ContextSpec)
*/ */
@ -497,14 +593,6 @@ public class RestContextFactory {
return buildContextUnwrappingExceptions(builder); 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) * @see RestContextFactory#createContextBuilder(ContextSpec, Properties)
*/ */
@ -512,4 +600,22 @@ public class RestContextFactory {
RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, overrides); RestContextBuilder<S, A> builder = createContextBuilder(contextSpec, overrides);
return buildContextUnwrappingExceptions(builder); 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", ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost:" + testPort, "1", "identity", null, IntegrationTestClient.class, "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 @AfterTest

View File

@ -21,6 +21,7 @@ package org.jclouds.rest;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -82,8 +83,8 @@ public class RestContextBuilderTest {
modules.add(loggingModule); modules.add(loggingModule);
HttpModule httpModule = new HttpModule(); HttpModule httpModule = new HttpModule();
modules.add(httpModule); modules.add(httpModule);
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>( RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
String.class, String.class, new Properties()); new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules); builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules); builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 2); assertEquals(modules.size(), 2);
@ -95,8 +96,8 @@ public class RestContextBuilderTest {
public void testAddBothWhenDoesntRequireHttp() { public void testAddBothWhenDoesntRequireHttp() {
List<Module> modules = new ArrayList<Module>(); List<Module> modules = new ArrayList<Module>();
modules.add(new ConfiguresRestClientModule()); modules.add(new ConfiguresRestClientModule());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>( RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
String.class, String.class, new Properties()); new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules); builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules); builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 2); assertEquals(modules.size(), 2);
@ -115,8 +116,8 @@ public class RestContextBuilderTest {
@Test @Test
public void testAddBothWhenDefault() { public void testAddBothWhenDefault() {
List<Module> modules = new ArrayList<Module>(); List<Module> modules = new ArrayList<Module>();
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>( RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
String.class, String.class, new Properties()); new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules); builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules); builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 2); assertEquals(modules.size(), 2);
@ -136,8 +137,8 @@ public class RestContextBuilderTest {
public void testAddBothWhenLive() { public void testAddBothWhenLive() {
List<Module> modules = new ArrayList<Module>(); List<Module> modules = new ArrayList<Module>();
modules.add(new RequiresHttpModule()); modules.add(new RequiresHttpModule());
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>( RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
String.class, String.class, new Properties()); new Properties());
builder.addHttpModuleIfNeededAndNotPresent(modules); builder.addHttpModuleIfNeededAndNotPresent(modules);
builder.addLoggingModuleIfNotPresent(modules); builder.addLoggingModuleIfNotPresent(modules);
assertEquals(modules.size(), 3); assertEquals(modules.size(), 3);
@ -148,21 +149,21 @@ public class RestContextBuilderTest {
public void testBuilder() { public void testBuilder() {
AbstractModule module1 = new AbstractModule() { Module module1 = new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
} }
}; };
AbstractModule module2 = new AbstractModule() { Module module2 = new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
} }
}; };
RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>( RestContextBuilder<String, String> builder = new RestContextBuilder<String, String>(String.class, String.class,
String.class, String.class, new Properties()); new Properties());
builder.withModules(module1, module2); 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.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.testng.Assert.assertEquals; 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.List;
import java.util.Properties; import java.util.Properties;
@ -33,6 +37,10 @@ import org.jclouds.rest.RestContextFactory.ContextSpec;
import org.jclouds.rest.config.RestClientModule; import org.jclouds.rest.config.RestClientModule;
import org.testng.annotations.Test; 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; import com.google.inject.Module;
/** /**
@ -43,17 +51,15 @@ import com.google.inject.Module;
public class RestContextFactoryTest { public class RestContextFactoryTest {
public void testBuilder() { public void testBuilder() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec( ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"test", "http://localhost", "1", "dummy", null, IntegrationTestClient.class, "http://localhost", "1", "dummy", null, IntegrationTestClient.class, IntegrationTestAsyncClient.class);
IntegrationTestAsyncClient.class);
createContextBuilder(contextSpec); createContextBuilder(contextSpec);
} }
public void testBuilderProperties() { public void testBuilderProperties() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec( ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"test", "http://localhost", "1", "dummy", null, IntegrationTestClient.class, "http://localhost", "1", "dummy", null, IntegrationTestClient.class, IntegrationTestAsyncClient.class);
IntegrationTestAsyncClient.class);
Properties props = RestContextFactory.toProperties(contextSpec); Properties props = RestContextFactory.toProperties(contextSpec);
assertEquals(props.getProperty("test.endpoint"), "http://localhost"); 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.async"), IntegrationTestAsyncClient.class.getName());
assertEquals(props.getProperty("test.propertiesbuilder"), null); assertEquals(props.getProperty("test.propertiesbuilder"), null);
assertEquals(props.getProperty("test.contextbuilder"), null); assertEquals(props.getProperty("test.contextbuilder"), null);
assertEquals(props.getProperty("test.modules"), null);
new RestContextFactory().createContext("test", props); new RestContextFactory().createContext("test", props);
} }
public void testBuilderPropertiesWithCredential() { public void testBuilderPropertiesWithCredential() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec( ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"test", "http://localhost", "1", "dummy", "credential", IntegrationTestClient.class, "http://localhost", "1", "dummy", "credential", IntegrationTestClient.class,
IntegrationTestAsyncClient.class); IntegrationTestAsyncClient.class);
Properties props = RestContextFactory.toProperties(contextSpec); Properties props = RestContextFactory.toProperties(contextSpec);
@ -82,15 +89,16 @@ public class RestContextFactoryTest {
assertEquals(props.getProperty("test.async"), IntegrationTestAsyncClient.class.getName()); assertEquals(props.getProperty("test.async"), IntegrationTestAsyncClient.class.getName());
assertEquals(props.getProperty("test.propertiesbuilder"), null); assertEquals(props.getProperty("test.propertiesbuilder"), null);
assertEquals(props.getProperty("test.contextbuilder"), null); assertEquals(props.getProperty("test.contextbuilder"), null);
assertEquals(props.getProperty("test.modules"), null);
new RestContextFactory().createContext("test", props); new RestContextFactory().createContext("test", props);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBuilderPropertiesWithContextBuilder() { public void testBuilderPropertiesWithContextBuilder() {
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec( ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"test", "http://localhost", "1", "dummy", null, (Class) null, (Class) null, "http://localhost", "1", "dummy", null, (Class) null, (Class) null, PropertiesBuilder.class,
PropertiesBuilder.class, (Class) IntegrationTestContextBuilder.class); (Class) IntegrationTestContextBuilder.class, Collections.EMPTY_LIST);
Properties props = RestContextFactory.toProperties(contextSpec); Properties props = RestContextFactory.toProperties(contextSpec);
assertEquals(props.getProperty("test.endpoint"), "http://localhost"); assertEquals(props.getProperty("test.endpoint"), "http://localhost");
@ -100,19 +108,138 @@ public class RestContextFactoryTest {
assertEquals(props.getProperty("test.sync"), null); assertEquals(props.getProperty("test.sync"), null);
assertEquals(props.getProperty("test.async"), null); assertEquals(props.getProperty("test.async"), null);
assertEquals(props.getProperty("test.propertiesbuilder"), PropertiesBuilder.class.getName()); assertEquals(props.getProperty("test.propertiesbuilder"), PropertiesBuilder.class.getName());
assertEquals(props.getProperty("test.contextbuilder"), IntegrationTestContextBuilder.class assertEquals(props.getProperty("test.contextbuilder"), IntegrationTestContextBuilder.class.getName());
.getName()); assertEquals(props.getProperty("test.modules"), null);
new RestContextFactory().createContext("test", props); 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") @SuppressWarnings("unchecked")
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testBuilderPropertiesWithWrongConfig() { public void testBuilderPropertiesWithWrongConfig() {
@SuppressWarnings("unused") @SuppressWarnings("unused")
ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec( ContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"test", "http://localhost", "1", "dummy", null, (Class) null, (Class) null, "http://localhost", "1", "dummy", null, (Class) null, (Class) null,
(Class) IntegrationTestContextBuilder.class, (Class) PropertiesBuilder.class); (Class) IntegrationTestContextBuilder.class, (Class) PropertiesBuilder.class, Collections.EMPTY_LIST);
} }
@RequiresHttp @RequiresHttp

View File

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