PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

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