mirror of https://github.com/apache/jclouds.git
Ported the CredentialsCollector (see commit 741920278
) to gae-tweetstore
This commit is contained in:
parent
0477443a00
commit
2eb7d77bb7
|
@ -20,6 +20,11 @@ package org.jclouds.demo.tweetstore.config;
|
|||
|
||||
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
|
||||
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 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.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN;
|
||||
import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN_SECRET;
|
||||
|
@ -36,6 +41,7 @@ import javax.servlet.ServletContextEvent;
|
|||
|
||||
import org.jclouds.blobstore.BlobStoreContext;
|
||||
import org.jclouds.blobstore.BlobStoreContextFactory;
|
||||
import org.jclouds.demo.tweetstore.config.util.CredentialsCollector;
|
||||
import org.jclouds.demo.tweetstore.controller.AddTweetsController;
|
||||
import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
|
||||
import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
|
||||
|
@ -96,10 +102,10 @@ public class GuiceServletConfig extends GuiceServletContextListener {
|
|||
// common namespace for storing tweets
|
||||
container = checkNotNull(props.getProperty(PROPERTY_TWEETSTORE_CONTAINER), PROPERTY_TWEETSTORE_CONTAINER);
|
||||
|
||||
// instantiate and store references to all blobstores by provider name
|
||||
// instantiate and store references to all blobstores by provider name
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -113,6 +119,16 @@ public class GuiceServletConfig extends GuiceServletContextListener {
|
|||
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 Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) {
|
||||
InputStream input = servletContextEvent.getServletContext().getResourceAsStream("/WEB-INF/jclouds.properties");
|
||||
Properties props = new Properties();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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")));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue