allowed endpoint property to be passed on createContext

This commit is contained in:
Adrian Cole 2010-06-15 22:16:56 -04:00
parent cace0ce00e
commit 80e22b0322
1 changed files with 59 additions and 41 deletions

View File

@ -39,7 +39,8 @@ import com.google.inject.Module;
import com.google.inject.ProvisionException; import com.google.inject.ProvisionException;
/** /**
* Helper class to instantiate {@code RestContext} instances. "blobstore.properties" * Helper class to instantiate {@code RestContext} instances.
* "blobstore.properties"
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@ -48,8 +49,8 @@ public abstract class RestContextFactory<T, B extends RestContextBuilder<?, ?>>
private final Properties properties; private final Properties properties;
/** /**
* 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 filename} * typically stored in the classpath resource {@code filename}
* *
* @param filename * @param filename
* name of the properties file to initialize from * name of the properties file to initialize from
@ -62,8 +63,8 @@ public abstract class RestContextFactory<T, B extends RestContextBuilder<?, ?>>
} }
/** /**
* Loads the default properties that define the {@code RestContext} objects. <h3>properties file * Loads the default properties that define the {@code RestContext} objects.
* format</h3> * <h3>properties file format</h3>
* *
* Two properties are needed per context: * Two properties are needed per context:
* <ul> * <ul>
@ -83,54 +84,65 @@ public abstract class RestContextFactory<T, B extends RestContextBuilder<?, ?>>
* @throws IOException * @throws IOException
* if {@code filename} cannot load. * if {@code filename} cannot load.
*/ */
public static Properties getPropertiesFromResource(String filename) throws IOException { public static Properties getPropertiesFromResource(String filename)
throws IOException {
Properties properties = new Properties(); Properties properties = new Properties();
properties.load(Resources.newInputStreamSupplier(Resources.getResource(filename)).getInput()); properties.load(Resources.newInputStreamSupplier(
Resources.getResource(filename)).getInput());
properties.putAll(System.getProperties()); properties.putAll(System.getProperties());
return properties; return properties;
} }
/** /**
* Initializes the {@code RestContext} definitions from the specified properties. * Initializes the {@code RestContext} definitions from the specified
* properties.
*/ */
@Inject @Inject
public RestContextFactory(Properties properties) { public RestContextFactory(Properties properties) {
this.properties = properties; this.properties = properties;
} }
public T createContext(URI contextUri, Iterable<? extends Module> modules, Properties overrides) { public T createContext(URI contextUri, Iterable<? extends Module> modules,
return createContext(contextUri, Credentials.parse(contextUri), modules, overrides); Properties overrides) {
return createContext(contextUri, Credentials.parse(contextUri), modules,
overrides);
} }
public T createContext(URI contextUri) { public T createContext(URI contextUri) {
return createContext(contextUri, ImmutableSet.<Module> of(), NO_PROPERTIES); return createContext(contextUri, ImmutableSet.<Module> of(),
NO_PROPERTIES);
} }
public T createContext(URI contextUri, Credentials creds, Iterable<? extends Module> modules, public T createContext(URI contextUri, Credentials creds,
Properties overrides) { Iterable<? extends Module> modules, Properties overrides) {
return createContext(checkNotNull(contextUri.getHost(), "host"), checkNotNull(creds.account, return createContext(checkNotNull(contextUri.getHost(), "host"),
"account"), checkNotNull(creds.key, "key"), checkNotNull(modules, "modules"), checkNotNull(creds.account, "account"), checkNotNull(creds.key,
checkNotNull(overrides, "overrides")); "key"), checkNotNull(modules, "modules"), checkNotNull(
overrides, "overrides"));
} }
public T createContext(URI contextUri, Credentials creds) { public T createContext(URI contextUri, Credentials creds) {
return createContext(contextUri, creds, ImmutableSet.<Module> of(), NO_PROPERTIES); return createContext(contextUri, creds, ImmutableSet.<Module> of(),
NO_PROPERTIES);
} }
public T createContext(String provider, String account, String key) { public T createContext(String provider, String account, String key) {
return createContext(provider, account, key, ImmutableSet.<Module> of(), NO_PROPERTIES); return createContext(provider, account, key, ImmutableSet.<Module> of(),
NO_PROPERTIES);
} }
public T createContext(String provider, Properties overrides) { public T createContext(String provider, Properties overrides) {
return createContext(provider, null, null, ImmutableSet.<Module> of(), overrides); return createContext(provider, null, null, ImmutableSet.<Module> of(),
overrides);
} }
public T createContext(String provider, Iterable<? extends Module> modules, Properties overrides) { public T createContext(String provider, Iterable<? extends Module> modules,
Properties overrides) {
return createContext(provider, null, null, modules, overrides); return createContext(provider, null, null, modules, overrides);
} }
public T createContext(String provider, @Nullable String account, @Nullable String key, public T createContext(String provider, @Nullable String account,
Iterable<? extends Module> modules) { @Nullable String key, Iterable<? extends Module> modules) {
return createContext(provider, account, key, modules, new Properties()); return createContext(provider, account, key, modules, new Properties());
} }
@ -143,40 +155,46 @@ public abstract class RestContextFactory<T, B extends RestContextBuilder<?, ?>>
* @param key * @param key
* nullable, if credentials are present in the overrides * nullable, if credentials are present in the overrides
* @param modules * @param modules
* Configuration you'd like to pass to the context. Ex. ImmutableSet.<Module>of(new * Configuration you'd like to pass to the context. Ex.
* ExecutorServiceModule(myexecutor)) * ImmutableSet.<Module>of(new ExecutorServiceModule(myexecutor))
* @param overrides * @param overrides
* properties to override defaults with. * properties to override defaults with.
* @return initialized context ready for use * @return initialized context ready for use
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T createContext(String provider, @Nullable String account, @Nullable String key, public T createContext(String provider, @Nullable String account,
Iterable<? extends Module> modules, Properties overrides) { @Nullable String key, Iterable<? extends Module> modules,
Properties overrides) {
checkNotNull(provider, "provider"); checkNotNull(provider, "provider");
checkNotNull(modules, "modules"); checkNotNull(modules, "modules");
checkNotNull(overrides, "overrides"); checkNotNull(overrides, "overrides");
String propertiesBuilderKey = String.format("%s.propertiesbuilder", provider); String propertiesBuilderKey = String.format("%s.propertiesbuilder",
String propertiesBuilderClassName = checkNotNull( provider);
properties.getProperty(propertiesBuilderKey), provider + " service not supported"); String propertiesBuilderClassName = checkNotNull(properties
.getProperty(propertiesBuilderKey), provider
+ " service not supported");
String contextBuilderKey = String.format("%s.contextbuilder", provider); String contextBuilderKey = String.format("%s.contextbuilder", provider);
String contextBuilderClassName = checkNotNull(properties.getProperty(contextBuilderKey), String contextBuilderClassName = checkNotNull(properties
contextBuilderKey); .getProperty(contextBuilderKey), contextBuilderKey);
String endpointKey = String.format("%s.endpoint", provider); String endpointKey = String.format("%s.endpoint", provider);
String endpoint = properties.getProperty(endpointKey); String endpoint = overrides.containsKey(endpointKey) ? overrides
.getProperty(endpointKey) : properties.getProperty(endpointKey);
try { try {
Class<PropertiesBuilder> propertiesBuilderClass = (Class<PropertiesBuilder>) Class Class<PropertiesBuilder> propertiesBuilderClass = (Class<PropertiesBuilder>) Class
.forName(propertiesBuilderClassName); .forName(propertiesBuilderClassName);
Class<B> contextBuilderClass = (Class<B>) Class.forName(contextBuilderClassName); Class<B> contextBuilderClass = (Class<B>) Class
PropertiesBuilder builder = propertiesBuilderClass.getConstructor(Properties.class) .forName(contextBuilderClassName);
.newInstance(overrides); PropertiesBuilder builder = propertiesBuilderClass.getConstructor(
Properties.class).newInstance(overrides);
if (key != null) if (key != null)
builder.withCredentials(account, key); builder.withCredentials(account, key);
if (endpoint != null) if (endpoint != null)
builder.withEndpoint(URI.create(endpoint)); builder.withEndpoint(URI.create(endpoint));
B contextBuilder = (B) contextBuilderClass.getConstructor(String.class, Properties.class) B contextBuilder = (B) contextBuilderClass.getConstructor(
.newInstance(provider, builder.build()).withModules( String.class, Properties.class).newInstance(provider,
builder.build()).withModules(
Iterables.toArray(modules, Module.class)); Iterables.toArray(modules, Module.class));
return build(contextBuilder); return build(contextBuilder);
} catch (ProvisionException e) { } catch (ProvisionException e) {
@ -192,8 +210,8 @@ public abstract class RestContextFactory<T, B extends RestContextBuilder<?, ?>>
} }
/** /**
* Hook so that you can specify how to create an object using the contextBuilder produced with * Hook so that you can specify how to create an object using the
* this factory. * contextBuilder produced with this factory.
*/ */
protected abstract T build(B contextBuilder); protected abstract T build(B contextBuilder);
} }