/demos/tweetstore/rhcloud-tweetstore/src/main/java/org/jclouds/demo/paas/config/PlatformServicesInitializer.java

https://github.com/danikov/jclouds · Java · 87 lines · 52 code · 12 blank · 23 comment · 0 complexity · df86f1c68344f360511021d1af8a0098 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.demo.paas.config;
  20. import static com.google.common.base.Preconditions.checkNotNull;
  21. import static java.lang.String.format;
  22. import static java.util.concurrent.TimeUnit.SECONDS;
  23. import javax.servlet.ServletContext;
  24. import javax.servlet.ServletContextEvent;
  25. import javax.servlet.ServletContextListener;
  26. import org.jclouds.concurrent.config.ExecutorServiceModule;
  27. import org.jclouds.demo.paas.PlatformServices;
  28. import org.jclouds.demo.paas.service.taskqueue.TaskQueue;
  29. import org.jclouds.http.HttpCommandExecutorService;
  30. import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
  31. import com.google.common.collect.ImmutableMap;
  32. import com.google.common.collect.ImmutableMap.Builder;
  33. import com.google.inject.Guice;
  34. /**
  35. * @author Andrew Phillips
  36. */
  37. public class PlatformServicesInitializer implements ServletContextListener {
  38. public static final String PLATFORM_SERVICES_ATTRIBUTE_NAME = PlatformServices.class.getName();
  39. // from .openshift/config/standalone.xml
  40. protected static final String HOST_VARIABLE = "OPENSHIFT_INTERNAL_IP";
  41. protected static final String PORT_VARIABLE = "OPENSHIFT_INTERNAL_PORT";
  42. @Override
  43. public void contextInitialized(ServletContextEvent contextEvent) {
  44. ServletContext context = contextEvent.getServletContext();
  45. context.setAttribute(PLATFORM_SERVICES_ATTRIBUTE_NAME, createServices(context));
  46. }
  47. protected static PlatformServices createServices(ServletContext context) {
  48. HttpCommandExecutorService httpClient = createHttpClient(context);
  49. return new PlatformServices(getBaseUrl(context), createTaskQueues(httpClient));
  50. }
  51. protected static HttpCommandExecutorService createHttpClient(
  52. final ServletContext context) {
  53. return Guice.createInjector(new ExecutorServiceModule(),
  54. new JavaUrlHttpCommandExecutorServiceModule(),
  55. new HttpClientModule(context))
  56. .getInstance(HttpCommandExecutorService.class);
  57. }
  58. protected static String getBaseUrl(ServletContext context) {
  59. return format("http://%s:%s%s", checkNotNull(System.getenv(HOST_VARIABLE), HOST_VARIABLE),
  60. checkNotNull(System.getenv(PORT_VARIABLE), PORT_VARIABLE), context.getContextPath());
  61. }
  62. // TODO: make the number and names of queues configurable
  63. protected static ImmutableMap<String, TaskQueue> createTaskQueues(HttpCommandExecutorService httpClient) {
  64. Builder<String, TaskQueue> taskQueues = ImmutableMap.builder();
  65. taskQueues.put("twitter", TaskQueue.builder(httpClient)
  66. .name("twitter").period(SECONDS.toMillis(30))
  67. .build());
  68. return taskQueues.build();
  69. }
  70. @Override
  71. public void contextDestroyed(ServletContextEvent servletContextEvent) {
  72. ServletContext context = servletContextEvent.getServletContext();
  73. context.removeAttribute(PLATFORM_SERVICES_ATTRIBUTE_NAME);
  74. }
  75. }