/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
- /**
- * Licensed to jclouds, Inc. (jclouds) under one or more
- * contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. jclouds licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- package org.jclouds.demo.paas.config;
- import static java.util.concurrent.TimeUnit.SECONDS;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import org.cloudfoundry.runtime.env.ApplicationInstanceInfo;
- import org.cloudfoundry.runtime.env.CloudEnvironment;
- import org.jclouds.concurrent.config.ExecutorServiceModule;
- import org.jclouds.demo.paas.PlatformServices;
- import org.jclouds.demo.paas.service.scheduler.Scheduler;
- import org.jclouds.demo.paas.service.taskqueue.TaskQueue;
- import org.jclouds.http.HttpCommandExecutorService;
- import org.jclouds.http.config.JavaUrlHttpCommandExecutorServiceModule;
- import com.google.common.collect.ImmutableMap;
- import com.google.common.collect.ImmutableMap.Builder;
- import com.google.inject.Guice;
- /**
- * @author Andrew Phillips
- */
- public class PlatformServicesInitializer implements ServletContextListener {
- public static final String PLATFORM_SERVICES_ATTRIBUTE_NAME = PlatformServices.class.getName();
- @Override
- public void contextInitialized(ServletContextEvent contextEvent) {
- ServletContext context = contextEvent.getServletContext();
- context.setAttribute(PLATFORM_SERVICES_ATTRIBUTE_NAME, createServices(context));
- }
- protected static PlatformServices createServices(ServletContext context) {
- HttpCommandExecutorService httpClient = createHttpClient(context);
- return new PlatformServices(getBaseUrl(context), new Scheduler(httpClient),
- createTaskQueues(httpClient));
- }
- protected static HttpCommandExecutorService createHttpClient(
- final ServletContext context) {
- return Guice.createInjector(new ExecutorServiceModule(),
- new JavaUrlHttpCommandExecutorServiceModule(),
- new HttpClientModule(context))
- .getInstance(HttpCommandExecutorService.class);
- }
- protected static String getBaseUrl(ServletContext context) {
- ApplicationInstanceInfo instanceInfo = new CloudEnvironment().getInstanceInfo();
- // using localhost instead of instanceInfo.getHost(). See http://support.cloudfoundry.com/requests/102117
- return "http://127.0.0.1:" + instanceInfo.getPort() + context.getContextPath();
- }
- // TODO: make the number and names of queues configurable
- protected static ImmutableMap<String, TaskQueue> createTaskQueues(HttpCommandExecutorService httpClient) {
- Builder<String, TaskQueue> taskQueues = ImmutableMap.builder();
- taskQueues.put("twitter", TaskQueue.builder(httpClient)
- .name("twitter").period(SECONDS.toMillis(30))
- .build());
- return taskQueues.build();
- }
- @Override
- public void contextDestroyed(ServletContextEvent servletContextEvent) {
- ServletContext context = servletContextEvent.getServletContext();
- context.removeAttribute(PLATFORM_SERVICES_ATTRIBUTE_NAME);
- }
- }