PageRenderTime 69ms CodeModel.GetById 36ms app.highlight 22ms RepoModel.GetById 0ms app.codeStats 1ms

/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
 20package org.jclouds.demo.tweetstore.config;
 21
 22import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.url;
 23import static com.google.common.base.Preconditions.checkNotNull;
 24import static org.jclouds.demo.tweetstore.reference.TweetStoreConstants.PROPERTY_TWEETSTORE_CONTAINER;
 25
 26import java.io.IOException;
 27import java.io.InputStream;
 28import java.util.Map;
 29import java.util.Properties;
 30import java.util.Set;
 31
 32import javax.servlet.ServletContextEvent;
 33
 34import org.jclouds.blobstore.BlobStoreContext;
 35import org.jclouds.blobstore.BlobStoreContextFactory;
 36import org.jclouds.demo.tweetstore.controller.AddTweetsController;
 37import org.jclouds.demo.tweetstore.controller.StoreTweetsController;
 38import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
 39
 40import twitter4j.Twitter;
 41import twitter4j.TwitterFactory;
 42
 43import com.google.appengine.api.labs.taskqueue.Queue;
 44import com.google.appengine.api.labs.taskqueue.QueueFactory;
 45import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
 46import com.google.appengine.repackaged.com.google.common.base.Splitter;
 47import com.google.common.collect.ImmutableSet;
 48import com.google.common.collect.Maps;
 49import com.google.common.io.Closeables;
 50import com.google.inject.Guice;
 51import com.google.inject.Injector;
 52import com.google.inject.Module;
 53import com.google.inject.TypeLiteral;
 54import com.google.inject.name.Names;
 55import com.google.inject.servlet.GuiceServletContextListener;
 56import com.google.inject.servlet.ServletModule;
 57
 58/**
 59 * Setup Logging and create Injector for use in testing S3.
 60 * 
 61 * @author Adrian Cole
 62 */
 63public class GuiceServletConfig extends GuiceServletContextListener {
 64   public static final String PROPERTY_BLOBSTORE_CONTEXTS = "blobstore.contexts";
 65
 66   private Map<String, BlobStoreContext> providerTypeToBlobStoreMap;
 67   private Twitter twitterClient;
 68   private String container;
 69
 70   @Override
 71   public void contextInitialized(ServletContextEvent servletContextEvent) {
 72
 73      BlobStoreContextFactory blobStoreContextFactory = new BlobStoreContextFactory();
 74
 75      Properties props = loadJCloudsProperties(servletContextEvent);
 76
 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         twitterClient = new TwitterFactory().getInstance(props.getProperty("twitter.identity"), props
 82                  .getProperty("credential"));
 83      } catch (IllegalArgumentException e) {
 84         throw new IllegalArgumentException("properties for twitter not configured properly in " + props.toString(), e);
 85      }
 86      // common namespace for storing tweets
 87      container = checkNotNull(props.getProperty(PROPERTY_TWEETSTORE_CONTAINER), PROPERTY_TWEETSTORE_CONTAINER);
 88
 89      // instantiate and store references to all blobstores by provider name
 90      providerTypeToBlobStoreMap = Maps.newHashMap();
 91      for (String hint : Splitter.on(',').split(
 92               checkNotNull(props.getProperty(PROPERTY_BLOBSTORE_CONTEXTS), PROPERTY_BLOBSTORE_CONTEXTS))) {
 93         providerTypeToBlobStoreMap.put(hint, blobStoreContextFactory.createContext(hint, modules, props));
 94      }
 95
 96      // get a queue for submitting store tweet requests
 97      Queue queue = QueueFactory.getQueue("twitter");
 98      // submit a job to store tweets for each configured blobstore
 99      for (String name : providerTypeToBlobStoreMap.keySet()) {
100         queue.add(url("/store/do").header("context", name).method(Method.GET));
101      }
102
103      super.contextInitialized(servletContextEvent);
104   }
105
106   private Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) {
107      InputStream input = servletContextEvent.getServletContext().getResourceAsStream("/WEB-INF/jclouds.properties");
108      Properties props = new Properties();
109      try {
110         props.load(input);
111      } catch (IOException e) {
112         throw new RuntimeException(e);
113      } finally {
114         Closeables.closeQuietly(input);
115      }
116      return props;
117   }
118
119   @Override
120   protected Injector getInjector() {
121      return Guice.createInjector(new ServletModule() {
122         @Override
123         protected void configureServlets() {
124            bind(new TypeLiteral<Map<String, BlobStoreContext>>() {
125            }).toInstance(providerTypeToBlobStoreMap);
126            bind(Twitter.class).toInstance(twitterClient);
127            bindConstant().annotatedWith(Names.named(PROPERTY_TWEETSTORE_CONTAINER)).to(container);
128            serve("/store/*").with(StoreTweetsController.class);
129            serve("/tweets/*").with(AddTweetsController.class);
130         }
131      });
132   }
133
134   @Override
135   public void contextDestroyed(ServletContextEvent servletContextEvent) {
136      for (BlobStoreContext context : providerTypeToBlobStoreMap.values()) {
137         context.close();
138      }
139      super.contextDestroyed(servletContextEvent);
140   }
141}