made rest context factory work with credentials present in properties

This commit is contained in:
Adrian Cole 2010-07-06 13:29:46 -07:00
parent c443309801
commit 504f93e8b1
3 changed files with 87 additions and 2 deletions

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.rest; 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.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.toArray; import static com.google.common.collect.Iterables.toArray;
import static org.jclouds.util.Utils.initContextBuilder; import static org.jclouds.util.Utils.initContextBuilder;
@ -97,6 +98,12 @@ public class RestContextFactory {
this.credential = credential; this.credential = credential;
this.sync = sync; this.sync = sync;
this.async = async; this.async = async;
checkArgument(RestContextBuilder.class.isAssignableFrom(contextBuilderClass),
contextBuilderClass.getName() + " is not a subclass of "
+ RestContextBuilder.class.getName());
checkArgument(PropertiesBuilder.class.isAssignableFrom(propertiesBuilderClass),
propertiesBuilderClass.getName() + " is not a subclass of "
+ PropertiesBuilder.class.getName());
this.propertiesBuilderClass = propertiesBuilderClass; this.propertiesBuilderClass = propertiesBuilderClass;
this.contextBuilderClass = contextBuilderClass; this.contextBuilderClass = contextBuilderClass;
} }
@ -182,7 +189,8 @@ public class RestContextFactory {
} }
/** /**
* @see RestContextFactory#createContextBuilder(String, Properties, Iterable<? extends Module>, Properties) * @see RestContextFactory#createContextBuilder(String, 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, overrides.getProperty("jclouds.identity"), overrides
@ -244,6 +252,32 @@ public class RestContextFactory {
return createContextBuilder(contextSpec, wiring, _overrides); return createContextBuilder(contextSpec, wiring, _overrides);
} }
public static Properties toProperties(ContextSpec<?, ?> contextSpec) {
checkNotNull(contextSpec, "contextSpec");
Properties props = new Properties();
props.setProperty(contextSpec.provider + ".endpoint", contextSpec.endpoint);
props.setProperty(contextSpec.provider + ".apiversion", contextSpec.apiVersion);
props.setProperty(contextSpec.provider + ".identity", contextSpec.identity);
if (contextSpec.credential != null)
props.setProperty(contextSpec.provider + ".credential", contextSpec.credential);
if (contextSpec.sync != null) {
props.setProperty(contextSpec.provider + ".sync", contextSpec.sync.getName());
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());
}
return props;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <A, S> ContextSpec<S, A> createContextSpec(String providerName, String identity, public <A, S> ContextSpec<S, A> createContextSpec(String providerName, String identity,
String credential, Properties _overrides) { String credential, Properties _overrides) {

View File

@ -121,4 +121,54 @@ public class RestContextImpl<S, A> implements RestContext<S, A> {
public String getProvider() { public String getProvider() {
return provider; return provider;
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((apiVersion == null) ? 0 : apiVersion.hashCode());
result = prime * result + ((endpoint == null) ? 0 : endpoint.hashCode());
result = prime * result + ((identity == null) ? 0 : identity.hashCode());
result = prime * result + ((provider == null) ? 0 : provider.hashCode());
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RestContextImpl other = (RestContextImpl) obj;
if (apiVersion == null) {
if (other.apiVersion != null)
return false;
} else if (!apiVersion.equals(other.apiVersion))
return false;
if (endpoint == null) {
if (other.endpoint != null)
return false;
} else if (!endpoint.equals(other.endpoint))
return false;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
if (provider == null) {
if (other.provider != null)
return false;
} else if (!provider.equals(other.provider))
return false;
return true;
}
@Override
public String toString() {
return "RestContextImpl [provider=" + provider + ", endpoint=" + endpoint + ", apiVersion="
+ apiVersion + ", identity=" + identity + "]";
}
} }

View File

@ -383,7 +383,8 @@ public class Utils {
return (Class<RestContextBuilder<S, A>>) (contextBuilderClassName != null ? Class return (Class<RestContextBuilder<S, A>>) (contextBuilderClassName != null ? Class
.forName(contextBuilderClassName) : RestContextBuilder.class); .forName(contextBuilderClassName) : RestContextBuilder.class);
} else { } else {
checkArgument(contextBuilderClassName != null, "please configure contextbuilder class"); checkArgument(contextBuilderClassName != null,
"please configure contextbuilder class for " + provider);
return (Class<RestContextBuilder<S, A>>) Class.forName(contextBuilderClassName); return (Class<RestContextBuilder<S, A>>) Class.forName(contextBuilderClassName);
} }
} }