PageRenderTime 94ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/csamuel/jclouds
Java | 141 lines | 94 code | 19 blank | 28 comment | 3 complexity | 5db1ede7b42142ad724351cc7ecc414b MD5 | raw file
  1. /**
  2. *
  3. * Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
  4. *
  5. * ====================================================================
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.jclouds.demo.tweetstore.config;
  20. import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.url;
  21. import static com.google.common.base.Preconditions.checkNotNull;
  22. import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.Map;
  26. import java.util.Properties;
  27. import java.util.Set;
  28. import javax.servlet.ServletContextEvent;
  29. import org.jclouds.blobstore.BlobStoreContext;
  30. import org.jclouds.blobstore.BlobStoreContextFactory;
  31. import org.jclouds.demo.tweetstore.controller.AddTweetsController;
  32. import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
  33. import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
  34. import twitter4j.Twitter;
  35. import twitter4j.TwitterFactory;
  36. import com.google.appengine.api.labs.taskqueue.Queue;
  37. import com.google.appengine.api.labs.taskqueue.QueueFactory;
  38. import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
  39. import com.google.appengine.repackaged.com.google.common.base.Splitter;
  40. import com.google.common.collect.ImmutableSet;
  41. import com.google.common.collect.Maps;
  42. import com.google.common.io.Closeables;
  43. import com.google.inject.Guice;
  44. import com.google.inject.Injector;
  45. import com.google.inject.Module;
  46. import com.google.inject.TypeLiteral;
  47. import com.google.inject.name.Names;
  48. import com.google.inject.servlet.GuiceServletContextListener;
  49. import com.google.inject.servlet.ServletModule;
  50. /**
  51. * Setup Logging and create Injector for use in testing S3.
  52. *
  53. * @author Adrian Cole
  54. */
  55. public class GuiceServletConfig extends GuiceServletContextListener {
  56. public static final String PROPERTY_BLOBSTORE_CONTEXTS = "blobstore.contexts";
  57. private Map<String, BlobStoreContext> providerTypeToBlobStoreMap;
  58. private Twitter twitterClient;
  59. private String container;
  60. @Override
  61. public void contextInitialized(ServletContextEvent servletContextEvent) {
  62. BlobStoreContextFactory blobStoreContextFactory = new BlobStoreContextFactory();
  63. Properties props = loadJCloudsProperties(servletContextEvent);
  64. Module googleModule = new GoogleAppEngineConfigurationModule();
  65. Set<Module> modules = ImmutableSet.<Module> of(googleModule);
  66. // shared across all blobstores and used to retrieve tweets
  67. try {
  68. twitterClient = new TwitterFactory().getInstance(props.getProperty("twitter.identity"), props
  69. .getProperty("credential"));
  70. } catch (IllegalArgumentException e) {
  71. throw new IllegalArgumentException("properties for twitter not configured properly in " + props.toString(), e);
  72. }
  73. // common namespace for storing tweets
  74. container = checkNotNull(props.getProperty(PROPERTY_TWEETSTORE_CONTAINER), PROPERTY_TWEETSTORE_CONTAINER);
  75. // instantiate and store references to all blobstores by provider name
  76. providerTypeToBlobStoreMap = Maps.newHashMap();
  77. for (String hint : Splitter.on(',').split(
  78. checkNotNull(props.getProperty(PROPERTY_BLOBSTORE_CONTEXTS), PROPERTY_BLOBSTORE_CONTEXTS))) {
  79. providerTypeToBlobStoreMap.put(hint, blobStoreContextFactory.createContext(hint, modules, props));
  80. }
  81. // get a queue for submitting store tweet requests
  82. Queue queue = QueueFactory.getQueue("twitter");
  83. // submit a job to store tweets for each configured blobstore
  84. for (String name : providerTypeToBlobStoreMap.keySet()) {
  85. queue.add(url("/store/do").header("context", name).method(Method.GET));
  86. }
  87. super.contextInitialized(servletContextEvent);
  88. }
  89. private Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) {
  90. InputStream input = servletContextEvent.getServletContext().getResourceAsStream("/WEB-INF/jclouds.properties");
  91. Properties props = new Properties();
  92. try {
  93. props.load(input);
  94. } catch (IOException e) {
  95. throw new RuntimeException(e);
  96. } finally {
  97. Closeables.closeQuietly(input);
  98. }
  99. return props;
  100. }
  101. @Override
  102. protected Injector getInjector() {
  103. return Guice.createInjector(new ServletModule() {
  104. @Override
  105. protected void configureServlets() {
  106. bind(new TypeLiteral<Map<String, BlobStoreContext>>() {
  107. }).toInstance(providerTypeToBlobStoreMap);
  108. bind(Twitter.class).toInstance(twitterClient);
  109. bindConstant().annotatedWith(Names.named(PROPERTY_TWEETSTORE_CONTAINER)).to(container);
  110. serve("/store/*").with(StoreTweetsController.class);
  111. serve("/tweets/*").with(AddTweetsController.class);
  112. }
  113. });
  114. }
  115. @Override
  116. public void contextDestroyed(ServletContextEvent servletContextEvent) {
  117. for (BlobStoreContext context : providerTypeToBlobStoreMap.values()) {
  118. context.close();
  119. }
  120. super.contextDestroyed(servletContextEvent);
  121. }
  122. }