PageRenderTime 774ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/demos/gae/src/main/java/org/jclouds/chef/demo/config/GuiceServletConfig.java

http://github.com/jclouds/jclouds-chef
Java | 96 lines | 59 code | 11 blank | 26 comment | 0 complexity | 8fd4145d673c4ff003f4912e3ac36241 MD5 | raw file
Possible License(s): Apache-2.0
  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.chef.demo.config;
  20. import static com.google.common.base.Preconditions.checkNotNull;
  21. import static com.google.common.collect.Iterables.concat;
  22. import static com.google.inject.name.Names.bindProperties;
  23. import static org.jclouds.chef.reference.ChefConstants.CHEF_NODE;
  24. import static org.jclouds.chef.reference.ChefConstants.CHEF_SERVICE_CLIENT;
  25. import static org.jclouds.util.Utils.modulesForProviderInProperties;
  26. import java.util.Properties;
  27. import javax.servlet.ServletContextEvent;
  28. import org.jclouds.PropertiesBuilder;
  29. import org.jclouds.chef.ChefService;
  30. import org.jclouds.chef.demo.controllers.GetNodeStatusController;
  31. import org.jclouds.chef.domain.Node;
  32. import org.jclouds.chef.servlet.functions.InitParamsToProperties;
  33. import org.jclouds.json.config.GsonModule;
  34. import org.jclouds.logging.Logger;
  35. import org.jclouds.logging.jdk.JDKLogger;
  36. import com.google.common.collect.ImmutableSet;
  37. import com.google.inject.Guice;
  38. import com.google.inject.Injector;
  39. import com.google.inject.servlet.GuiceServletContextListener;
  40. import com.google.inject.servlet.ServletModule;
  41. /**
  42. * Finds the nodename and client connection in the servlet context and binds
  43. * them to guice.
  44. *
  45. * Guice then registers the servlets associated with this demo.
  46. *
  47. * @author Adrian Cole
  48. */
  49. public class GuiceServletConfig extends GuiceServletContextListener {
  50. private Logger logger = new JDKLogger.JDKLoggerFactory().getLogger(GuiceServletConfig.class.getName());
  51. private Properties properties;
  52. private Node node;
  53. private ChefService clientConnection;
  54. @Override
  55. public void contextInitialized(ServletContextEvent servletContextEvent) {
  56. try {
  57. logger.debug("starting initialization");
  58. properties = InitParamsToProperties.INSTANCE.apply(servletContextEvent.getServletContext());
  59. node = getContextAttribute(servletContextEvent, CHEF_NODE);
  60. clientConnection = getContextAttribute(servletContextEvent, CHEF_SERVICE_CLIENT);
  61. super.contextInitialized(servletContextEvent);
  62. logger.debug("initialized");
  63. } catch (RuntimeException e) {
  64. logger.error(e, "error initializing");
  65. throw e;
  66. }
  67. }
  68. @Override
  69. protected Injector getInjector() {
  70. return Guice.createInjector(concat(modulesForProviderInProperties("chef", properties), ImmutableSet.of(
  71. new GsonModule(), new ServletModule() {
  72. @Override
  73. protected void configureServlets() {
  74. bindProperties(binder(), new PropertiesBuilder().build());
  75. bind(Node.class).toInstance(node);
  76. bind(ChefService.class).toInstance(clientConnection);
  77. serve("/nodes/do").with(GetNodeStatusController.class);
  78. }
  79. })));
  80. }
  81. @SuppressWarnings("unchecked")
  82. private static <T> T getContextAttribute(ServletContextEvent servletContextEvent, String name) {
  83. return (T) checkNotNull(servletContextEvent.getServletContext().getAttribute(name), name);
  84. }
  85. }