PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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