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

https://github.com/danikov/jclouds · Java · 86 lines · 52 code · 11 blank · 23 comment · 0 complexity · c04ea6d199ce47e12dc3e2e228c3ae33 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 java.util.concurrent.TimeUnit.SECONDS;
  21. import javax.servlet.ServletContext;
  22. import javax.servlet.ServletContextEvent;
  23. import javax.servlet.ServletContextListener;
  24. import org.cloudfoundry.runtime.env.ApplicationInstanceInfo;
  25. import org.cloudfoundry.runtime.env.CloudEnvironment;
  26. import org.jclouds.concurrent.config.ExecutorServiceModule;
  27. import org.jclouds.demo.paas.PlatformServices;
  28. import org.jclouds.demo.paas.service.scheduler.Scheduler;
  29. import org.jclouds.demo.paas.service.taskqueue.TaskQueue;
  30. import org.jclouds.http.HttpCommandExecutorService;
  31. import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
  32. import com.google.common.collect.ImmutableMap;
  33. import com.google.common.collect.ImmutableMap.Builder;
  34. import com.google.inject.Guice;
  35. /**
  36. * @author Andrew Phillips
  37. */
  38. public class PlatformServicesInitializer implements ServletContextListener {
  39. public static final String PLATFORM_SERVICES_ATTRIBUTE_NAME = PlatformServices.class.getName();
  40. @Override
  41. public void contextInitialized(ServletContextEvent contextEvent) {
  42. ServletContext context = contextEvent.getServletContext();
  43. context.setAttribute(PLATFORM_SERVICES_ATTRIBUTE_NAME, createServices(context));
  44. }
  45. protected static PlatformServices createServices(ServletContext context) {
  46. HttpCommandExecutorService httpClient = createHttpClient(context);
  47. return new PlatformServices(getBaseUrl(context), new Scheduler(httpClient),
  48. createTaskQueues(httpClient));
  49. }
  50. protected static HttpCommandExecutorService createHttpClient(
  51. final ServletContext context) {
  52. return Guice.createInjector(new ExecutorServiceModule(),
  53. new JavaUrlHttpCommandExecutorServiceModule(),
  54. new HttpClientModule(context))
  55. .getInstance(HttpCommandExecutorService.class);
  56. }
  57. protected static String getBaseUrl(ServletContext context) {
  58. ApplicationInstanceInfo instanceInfo = new CloudEnvironment().getInstanceInfo();
  59. // using localhost instead of instanceInfo.getHost(). See http://support.cloudfoundry.com/requests/102117
  60. return "http://127.0.0.1:" + instanceInfo.getPort() + 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. }