PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/googleappengine/src/main/java/org/jclouds/samples/googleappengine/config/GuiceServletConfig.java

https://github.com/edwardt/jclouds
Java | 117 lines | 78 code | 15 blank | 24 comment | 2 complexity | 075acd06f5654232d3c5fbc184c8cbca 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 */
 19package org.jclouds.samples.googleappengine.config;
 20
 21import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_RUNNING;
 22import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_NODE_TERMINATED;
 23import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_PORT_OPEN;
 24import static org.jclouds.compute.reference.ComputeServiceConstants.PROPERTY_TIMEOUT_SCRIPT_COMPLETE;
 25
 26import java.io.IOException;
 27import java.io.InputStream;
 28import java.util.Properties;
 29
 30import javax.servlet.ServletContextEvent;
 31
 32import org.jclouds.blobstore.BlobStoreContext;
 33import org.jclouds.blobstore.BlobStoreContextFactory;
 34import org.jclouds.compute.ComputeServiceContext;
 35import org.jclouds.compute.ComputeServiceContextFactory;
 36import org.jclouds.gae.config.AsyncGoogleAppEngineConfigurationModule;
 37import org.jclouds.samples.googleappengine.GetAllStatusController;
 38
 39import com.google.common.collect.ImmutableSet;
 40import com.google.common.io.Closeables;
 41import com.google.inject.Guice;
 42import com.google.inject.Injector;
 43import com.google.inject.Module;
 44import com.google.inject.TypeLiteral;
 45import com.google.inject.servlet.GuiceServletContextListener;
 46import com.google.inject.servlet.ServletModule;
 47
 48/**
 49 * Setup Logging and create {@link Injector} for use in testing Amazon EC2 and S3.
 50 * 
 51 * @author Adrian Cole
 52 */
 53public class GuiceServletConfig extends GuiceServletContextListener {
 54
 55   private Iterable<BlobStoreContext> blobsStoreContexts;
 56   private Iterable<ComputeServiceContext> computeServiceContexts;
 57
 58   @Override
 59   public void contextInitialized(ServletContextEvent servletContextEvent) {
 60      Properties props = loadJCloudsProperties(servletContextEvent);
 61      props.setProperty(PROPERTY_TIMEOUT_NODE_TERMINATED, "25000");
 62      props.setProperty(PROPERTY_TIMEOUT_NODE_RUNNING, "25000");
 63      props.setProperty(PROPERTY_TIMEOUT_SCRIPT_COMPLETE, "25000");
 64      props.setProperty(PROPERTY_TIMEOUT_PORT_OPEN, "25000");
 65
 66      // note that this module hooks into the async urlfetchservice
 67      ImmutableSet<Module> modules = ImmutableSet.<Module> of(new AsyncGoogleAppEngineConfigurationModule());
 68
 69      blobsStoreContexts = ImmutableSet.<BlobStoreContext> of(new BlobStoreContextFactory().createContext("aws-s3",
 70            modules, props));
 71      computeServiceContexts = ImmutableSet.<ComputeServiceContext> of(new ComputeServiceContextFactory()
 72            .createContext("aws-ec2", modules, props));
 73
 74      super.contextInitialized(servletContextEvent);
 75   }
 76
 77   private Properties loadJCloudsProperties(ServletContextEvent servletContextEvent) {
 78      InputStream input = servletContextEvent.getServletContext().getResourceAsStream("/WEB-INF/jclouds.properties");
 79      Properties props = new Properties();
 80      try {
 81         props.load(input);
 82      } catch (IOException e) {
 83         throw new RuntimeException(e);
 84      } finally {
 85         Closeables.closeQuietly(input);
 86      }
 87      return props;
 88   }
 89
 90   @Override
 91   protected Injector getInjector() {
 92      return Guice.createInjector(new ServletModule() {
 93         @Override
 94         protected void configureServlets() {
 95            bind(new TypeLiteral<Iterable<BlobStoreContext>>() {
 96            }).toInstance(GuiceServletConfig.this.blobsStoreContexts);
 97            bind(new TypeLiteral<Iterable<ComputeServiceContext>>() {
 98            }).toInstance(GuiceServletConfig.this.computeServiceContexts);
 99            serve("*.check").with(GetAllStatusController.class);
100            requestInjection(this);
101         }
102      }
103
104      );
105   }
106
107   @Override
108   public void contextDestroyed(ServletContextEvent servletContextEvent) {
109      for (BlobStoreContext context : blobsStoreContexts) {
110         context.close();
111      }
112      for (ComputeServiceContext context : computeServiceContexts) {
113         context.close();
114      }
115      super.contextDestroyed(servletContextEvent);
116   }
117}