From 504f93e8b18f080e878ae90739ed1caf7777220b Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 6 Jul 2010 13:29:46 -0700 Subject: [PATCH] made rest context factory work with credentials present in properties --- .../org/jclouds/rest/RestContextFactory.java | 36 ++++++++++++- .../rest/internal/RestContextImpl.java | 50 +++++++++++++++++++ .../src/main/java/org/jclouds/util/Utils.java | 3 +- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jclouds/rest/RestContextFactory.java b/core/src/main/java/org/jclouds/rest/RestContextFactory.java index 55803d117b..3509934815 100644 --- a/core/src/main/java/org/jclouds/rest/RestContextFactory.java +++ b/core/src/main/java/org/jclouds/rest/RestContextFactory.java @@ -18,6 +18,7 @@ */ 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 org.jclouds.util.Utils.initContextBuilder; @@ -97,6 +98,12 @@ public class RestContextFactory { this.credential = credential; this.sync = sync; 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.contextBuilderClass = contextBuilderClass; } @@ -182,7 +189,8 @@ public class RestContextFactory { } /** - * @see RestContextFactory#createContextBuilder(String, Properties, Iterable, Properties) + * @see RestContextFactory#createContextBuilder(String, Properties, Iterable, + * Properties) */ public RestContextBuilder createContextBuilder(String provider, Properties overrides) { return createContextBuilder(provider, overrides.getProperty("jclouds.identity"), overrides @@ -244,6 +252,32 @@ public class RestContextFactory { 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") public ContextSpec createContextSpec(String providerName, String identity, String credential, Properties _overrides) { diff --git a/core/src/main/java/org/jclouds/rest/internal/RestContextImpl.java b/core/src/main/java/org/jclouds/rest/internal/RestContextImpl.java index 637cfa3032..aaba8ad0d0 100644 --- a/core/src/main/java/org/jclouds/rest/internal/RestContextImpl.java +++ b/core/src/main/java/org/jclouds/rest/internal/RestContextImpl.java @@ -121,4 +121,54 @@ public class RestContextImpl implements RestContext { public String getProvider() { 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 + "]"; + } } diff --git a/core/src/main/java/org/jclouds/util/Utils.java b/core/src/main/java/org/jclouds/util/Utils.java index a25c9ceaa0..f5a6652bd8 100644 --- a/core/src/main/java/org/jclouds/util/Utils.java +++ b/core/src/main/java/org/jclouds/util/Utils.java @@ -383,7 +383,8 @@ public class Utils { return (Class>) (contextBuilderClassName != null ? Class .forName(contextBuilderClassName) : RestContextBuilder.class); } else { - checkArgument(contextBuilderClassName != null, "please configure contextbuilder class"); + checkArgument(contextBuilderClassName != null, + "please configure contextbuilder class for " + provider); return (Class>) Class.forName(contextBuilderClassName); } }