Now determining the list of blobstores based on the available identity/credential property pairs, with optional override via the 'jclouds.tweetstore.blobstores' system property

This commit is contained in:
Andrew Phillips 2011-07-07 21:46:35 -04:00
parent 0b7bcb7b3f
commit 741920278b
4 changed files with 253 additions and 7 deletions

View File

@ -19,7 +19,12 @@
package org.jclouds.demo.tweetstore.config; package org.jclouds.demo.tweetstore.config;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.in;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Sets.filter;
import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.MINUTES;
import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_BLOBSTORES;
import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER; import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER;
import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN; import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN;
import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN_SECRET; import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN_SECRET;
@ -38,9 +43,10 @@ import javax.servlet.ServletContextEvent;
import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.BlobStoreContextFactory; import org.jclouds.blobstore.BlobStoreContextFactory;
import org.jclouds.demo.tweetstore.config.util.CredentialsCollector;
import org.jclouds.demo.tweetstore.config.util.HttpRequestTask; import org.jclouds.demo.tweetstore.config.util.HttpRequestTask;
import org.jclouds.demo.tweetstore.config.util.TaskQueue;
import org.jclouds.demo.tweetstore.config.util.HttpRequestTask.Factory; import org.jclouds.demo.tweetstore.config.util.HttpRequestTask.Factory;
import org.jclouds.demo.tweetstore.config.util.TaskQueue;
import org.jclouds.demo.tweetstore.controller.AddTweetsController; import org.jclouds.demo.tweetstore.controller.AddTweetsController;
import org.jclouds.demo.tweetstore.controller.StoreTweetsController; import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
@ -69,8 +75,6 @@ import com.google.inject.servlet.ServletModule;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class GuiceServletConfig extends GuiceServletContextListener { public class GuiceServletConfig extends GuiceServletContextListener {
public static final String PROPERTY_BLOBSTORE_CONTEXTS = "blobstore.contexts";
private Map<String, BlobStoreContext> providerTypeToBlobStoreMap; private Map<String, BlobStoreContext> providerTypeToBlobStoreMap;
private Twitter twitterClient; private Twitter twitterClient;
private String container; private String container;
@ -100,7 +104,7 @@ public class GuiceServletConfig extends GuiceServletContextListener {
// instantiate and store references to all blobstores by provider name // instantiate and store references to all blobstores by provider name
providerTypeToBlobStoreMap = Maps.newHashMap(); providerTypeToBlobStoreMap = Maps.newHashMap();
for (String hint : Splitter.on(',').split(checkNotNull(props.getProperty(PROPERTY_BLOBSTORE_CONTEXTS), PROPERTY_BLOBSTORE_CONTEXTS))) { for (String hint : getBlobstoreContexts(props)) {
providerTypeToBlobStoreMap.put(hint, blobStoreContextFactory.createContext(hint, modules, props)); providerTypeToBlobStoreMap.put(hint, blobStoreContextFactory.createContext(hint, modules, props));
} }
@ -118,6 +122,16 @@ public class GuiceServletConfig extends GuiceServletContextListener {
super.contextInitialized(servletContextEvent); super.contextInitialized(servletContextEvent);
} }
private static Iterable<String> getBlobstoreContexts(Properties props) {
Set<String> contexts = new CredentialsCollector().apply(props).keySet();
String explicitContexts = props.getProperty(PROPERTY_TWEETSTORE_BLOBSTORES);
if (explicitContexts != null) {
contexts = filter(contexts, in(copyOf(Splitter.on(',').split(explicitContexts))));
}
checkState(!contexts.isEmpty(), "no credentials available for any requested context");
return contexts;
}
private static URI withUrl(ServletContext servletContext, String url) { private static URI withUrl(ServletContext servletContext, String url) {
return URI.create("http://" + checkNotNull(servletContext.getInitParameter("application.host"), "application.host") return URI.create("http://" + checkNotNull(servletContext.getInitParameter("application.host"), "application.host")
+ servletContext.getContextPath() + url); + servletContext.getContextPath() + url);

View File

@ -0,0 +1,153 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.demo.tweetstore.config.util;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Maps.filterValues;
import static org.jclouds.util.Maps2.fromKeys;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jclouds.demo.tweetstore.config.util.CredentialsCollector.Credential;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
/**
* Reads provider credentials from a {@link Properties} bag.
*
* @author Andrew Phillips
*
*/
public class CredentialsCollector implements Function<Properties, Map<String, Credential>> {
private static final String IDENTITY_PROPERTY_SUFFIX = ".identity";
private static final String CREDENTIAL_PROPERTY_SUFFIX = ".credential";
// using the identity for provider name extraction
private static final Pattern IDENTITY_PROPERTY_PATTERN =
Pattern.compile("([a-zA-Z0-9-]+)" + Pattern.quote(IDENTITY_PROPERTY_SUFFIX));
@Override
public Map<String, Credential> apply(final Properties properties) {
Collection<String> providerNames = transform(
filter(properties.stringPropertyNames(), MatchesPattern.matches(IDENTITY_PROPERTY_PATTERN)),
new Function<String, String>() {
@Override
public String apply(String input) {
Matcher matcher = IDENTITY_PROPERTY_PATTERN.matcher(input);
// as a side-effect, sets the matching group!
checkState(matcher.matches(), "'%s' should match '%s'", input, IDENTITY_PROPERTY_PATTERN);
return matcher.group(1);
}
});
/*
* Providers without a credential property result in null values, which are
* removed from the returned map.
*/
return filterValues(fromKeys(copyOf(providerNames), new Function<String, Credential>() {
@Override
public Credential apply(String providerName) {
String identity = properties.getProperty(providerName + IDENTITY_PROPERTY_SUFFIX);
String credential = properties.getProperty(providerName + CREDENTIAL_PROPERTY_SUFFIX);
return (((identity != null) && (credential != null))
? new Credential(identity, credential)
: null);
}
}), notNull());
}
public static class Credential {
private final String identity;
private final String credential;
public Credential(String identity, String credential) {
this.identity = checkNotNull(identity, "identity");
this.credential = checkNotNull(credential, "credential");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((credential == null) ? 0 : credential.hashCode());
result = prime * result
+ ((identity == null) ? 0 : identity.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Credential other = (Credential) obj;
if (credential == null) {
if (other.credential != null)
return false;
} else if (!credential.equals(other.credential))
return false;
if (identity == null) {
if (other.identity != null)
return false;
} else if (!identity.equals(other.identity))
return false;
return true;
}
public String getIdentity() {
return identity;
}
public String getCredential() {
return credential;
}
}
@GwtIncompatible(value = "java.util.regex.Pattern")
private static class MatchesPattern implements Predicate<String> {
private final Pattern pattern;
private MatchesPattern(Pattern pattern) {
this.pattern = pattern;
}
@Override
public boolean apply(String input) {
return pattern.matcher(input).matches();
}
private static MatchesPattern matches(Pattern pattern) {
return new MatchesPattern(pattern);
}
}
}

View File

@ -0,0 +1,82 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.demo.tweetstore.config.util;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Map;
import java.util.Properties;
import org.jclouds.demo.tweetstore.config.util.CredentialsCollector;
import org.jclouds.demo.tweetstore.config.util.CredentialsCollector.Credential;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
/**
* Tests behavior of {@code CredentialsCollector}
*
* @author Andrew Phillips
*/
@Test(groups = "unit")
public class CredentialsCollectorTest {
private CredentialsCollector collector = new CredentialsCollector();
public void testEmptyProperties() {
assertTrue(collector.apply(new Properties()).isEmpty(),
"Expected returned map to be empty");
}
public void testNoCredentials() {
Properties properties = propertiesOf(ImmutableMap.of("not-an-identity",
"v1", "not-a-credential", "v2"));
assertTrue(collector.apply(properties).isEmpty(),
"Expected returned map to be empty");
}
private static Properties propertiesOf(Map<String, String> entries) {
Properties properties = new Properties();
properties.putAll(entries);
return properties;
}
public void testNonMatchingCredentials() {
Properties properties = propertiesOf(ImmutableMap.of("non_matching.identity", "v1",
"non_matching.credential", "v2"));
assertTrue(collector.apply(properties).isEmpty(),
"Expected returned map to be empty");
}
public void testIncompleteCredentials() {
Properties properties = propertiesOf(ImmutableMap.of("acme.identity", "v1",
"acme-2.credential", "v2"));
assertTrue(collector.apply(properties).isEmpty(),
"Expected returned map to be empty");
}
public void testCredentials() {
Properties properties = propertiesOf(ImmutableMap.of("acme.identity", "v1",
"acme.credential", "v2", "acme-2.identity", "v3",
"acme-2.credential", "v4"));
assertEquals(collector.apply(properties),
ImmutableMap.of("acme", new Credential("v1", "v2"),
"acme-2", new Credential("v3", "v4")));
}
}

View File

@ -40,7 +40,6 @@ import java.util.concurrent.TimeoutException;
import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.BlobStoreContextFactory; import org.jclouds.blobstore.BlobStoreContextFactory;
import org.jclouds.demo.tweetstore.config.GuiceServletConfig;
import org.jclouds.demo.tweetstore.controller.StoreTweetsController; import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.AuthorizationException;
@ -57,7 +56,6 @@ import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration; import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder; import twitter4j.conf.ConfigurationBuilder;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@ -86,7 +84,6 @@ public class TweetStoreLiveTest {
container = getRequiredSystemProperty(PROPERTY_TWEETSTORE_CONTAINER); container = getRequiredSystemProperty(PROPERTY_TWEETSTORE_CONTAINER);
props.setProperty(PROPERTY_TWEETSTORE_CONTAINER, container); props.setProperty(PROPERTY_TWEETSTORE_CONTAINER, container);
props.setProperty(GuiceServletConfig.PROPERTY_BLOBSTORE_CONTEXTS, Joiner.on(',').join(blobstores));
// put all identity/credential pairs into the client // put all identity/credential pairs into the client
addCredentialsForBlobStores(props); addCredentialsForBlobStores(props);