PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/demos/tweetstore/gae-tweetstore/src/main/java/org/jclouds/demo/tweetstore/config/GuiceServletConfig.java

https://github.com/regularfry/jclouds
Java | 169 lines | 121 code | 19 blank | 29 comment | 5 complexity | d47b8cd03cd0e136473a81ffc5dcdcde MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.demo.tweetstore.config;
  20. import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
  21. import static com.google.common.base.Preconditions.checkNotNull;
  22. import static com.google.common.base.Preconditions.checkState;
  23. import static com.google.common.base.Predicates.in;
  24. import static com.google.common.collect.ImmutableSet.copyOf;
  25. import static com.google.common.collect.Sets.filter;
  26. import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_BLOBSTORES;
  27. import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER;
  28. import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN;
  29. import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_ACCESSTOKEN_SECRET;
  30. import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_CONSUMER_KEY;
  31. import static org.jclouds.demo.tweetstore.reference.TwitterConstants.PROPERTY_TWITTER_CONSUMER_SECRET;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.util.Map;
  35. import java.util.Properties;
  36. import java.util.Set;
  37. import javax.servlet.ServletContextEvent;
  38. import org.jclouds.blobstore.BlobStoreContext;
  39. import org.jclouds.blobstore.BlobStoreContextFactory;
  40. import org.jclouds.demo.tweetstore.config.util.CredentialsCollector;
  41. import org.jclouds.demo.tweetstore.controller.AddTweetsController;
  42. import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
  43. import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
  44. import twitter4j.Twitter;
  45. import twitter4j.TwitterFactory;
  46. import twitter4j.conf.Configuration;
  47. import twitter4j.conf.ConfigurationBuilder;
  48. import com.google.appengine.api.taskqueue.Queue;
  49. import com.google.appengine.api.taskqueue.QueueFactory;
  50. import com.google.appengine.api.taskqueue.TaskOptions.Method;
  51. import com.google.appengine.repackaged.com.google.common.base.Splitter;
  52. import com.google.common.collect.ImmutableSet;
  53. import com.google.common.collect.Maps;
  54. import com.google.common.io.Closeables;
  55. import com.google.inject.Guice;
  56. import com.google.inject.Injector;
  57. import com.google.inject.Module;
  58. import com.google.inject.TypeLiteral;
  59. import com.google.inject.name.Names;
  60. import com.google.inject.servlet.GuiceServletContextListener;
  61. import com.google.inject.servlet.ServletModule;
  62. /**
  63. * Setup Logging and create Injector for use in testing S3.
  64. *
  65. * @author Adrian Cole
  66. */
  67. public class GuiceServletConfig extends GuiceServletContextListener {
  68. public static final String PROPERTY_BLOBSTORE_CONTEXTS = "blobstore.contexts";
  69. private Map<String, BlobStoreContext> providerTypeToBlobStoreMap;
  70. private Twitter twitterClient;
  71. private String container;
  72. private Queue queue;
  73. @Override
  74. public void contextInitialized(ServletContextEvent servletContextEvent) {
  75. BlobStoreContextFactory blobStoreContextFactory = new BlobStoreContextFactory();
  76. Properties props = loadJCloudsProperties(servletContextEvent);
  77. Module googleModule = new GoogleAppEngineConfigurationModule();
  78. Set<Module> modules = ImmutableSet.<Module> of(googleModule);
  79. // shared across all blobstores and used to retrieve tweets
  80. try {
  81. Configuration twitterConf = new ConfigurationBuilder()
  82. .setOAuthConsumerKey(props.getProperty(PROPERTY_TWITTER_CONSUMER_KEY))
  83. .setOAuthConsumerSecret(props.getProperty(PROPERTY_TWITTER_CONSUMER_SECRET))
  84. .setOAuthAccessToken(props.getProperty(PROPERTY_TWITTER_ACCESSTOKEN))
  85. .setOAuthAccessTokenSecret(props.getProperty(PROPERTY_TWITTER_ACCESSTOKEN_SECRET))
  86. .build();
  87. twitterClient = new TwitterFactory(twitterConf).getInstance();
  88. } catch (IllegalArgumentException e) {
  89. throw new IllegalArgumentException("properties for twitter not configured properly in " + props.toString(), e);
  90. }
  91. // common namespace for storing tweets
  92. container = checkNotNull(props.getProperty(PROPERTY_TWEETSTORE_CONTAINER), PROPERTY_TWEETSTORE_CONTAINER);
  93. // instantiate and store references to all blobstores by provider name
  94. // instantiate and store references to all blobstores by provider name
  95. providerTypeToBlobStoreMap = Maps.newHashMap();
  96. for (String hint : getBlobstoreContexts(props)) {
  97. providerTypeToBlobStoreMap.put(hint, blobStoreContextFactory.createContext(hint, modules, props));
  98. }
  99. // get a queue for submitting store tweet requests
  100. queue = QueueFactory.getQueue("twitter");
  101. // submit a job to store tweets for each configured blobstore
  102. for (String name : providerTypeToBlobStoreMap.keySet()) {
  103. queue.add(withUrl("/store/do").header("context", name).method(Method.GET));
  104. }
  105. super.contextInitialized(servletContextEvent);
  106. }
  107. private static Iterable<String> getBlobstoreContexts(Properties props) {
  108. Set<String> contexts = new CredentialsCollector().apply(props).keySet();
  109. String explicitContexts = props.getProperty(PROPERTY_TWEETSTORE_BLOBSTORES);
  110. if (explicitContexts != null) {
  111. contexts = filter(contexts, in(copyOf(Splitter.on(',').split(explicitContexts))));
  112. }
  113. checkState(!contexts.isEmpty(), "no credentials available for any requested context");
  114. return contexts;
  115. }
  116. private Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) {
  117. InputStream input = servletContextEvent.getServletContext().getResourceAsStream("/WEB-INF/jclouds.properties");
  118. Properties props = new Properties();
  119. try {
  120. props.load(input);
  121. } catch (IOException e) {
  122. throw new RuntimeException(e);
  123. } finally {
  124. Closeables.closeQuietly(input);
  125. }
  126. return props;
  127. }
  128. @Override
  129. protected Injector getInjector() {
  130. return Guice.createInjector(new ServletModule() {
  131. @Override
  132. protected void configureServlets() {
  133. bind(new TypeLiteral<Map<String, BlobStoreContext>>() {
  134. }).toInstance(providerTypeToBlobStoreMap);
  135. bind(Twitter.class).toInstance(twitterClient);
  136. bindConstant().annotatedWith(Names.named(PROPERTY_TWEETSTORE_CONTAINER)).to(container);
  137. serve("/store/*").with(StoreTweetsController.class);
  138. serve("/tweets/*").with(AddTweetsController.class);
  139. }
  140. });
  141. }
  142. @Override
  143. public void contextDestroyed(ServletContextEvent servletContextEvent) {
  144. for (BlobStoreContext context : providerTypeToBlobStoreMap.values()) {
  145. context.close();
  146. }
  147. queue.purge();
  148. super.contextDestroyed(servletContextEvent);
  149. }
  150. }