/samples/samples-httpservice/wc-helloworld/src/main/java/org/ops4j/pax/web/samples/helloworld/wc/internal/Activator.java

https://github.com/ops4j/org.ops4j.pax.web · Java · 188 lines · 96 code · 31 blank · 61 comment · 4 complexity · 5e6b95de12f341207463364f40a8dc84 MD5 · raw file

  1. /* Copyright 2008 Alin Dreghiciu.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. * implied.
  13. *
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.ops4j.pax.web.samples.helloworld.wc.internal;
  18. import java.util.Dictionary;
  19. import java.util.Hashtable;
  20. import org.ops4j.pax.web.service.WebContainer;
  21. import org.osgi.framework.BundleActivator;
  22. import org.osgi.framework.BundleContext;
  23. import org.osgi.framework.ServiceReference;
  24. import org.osgi.service.http.HttpContext;
  25. import org.osgi.util.tracker.ServiceTracker;
  26. import org.osgi.util.tracker.ServiceTrackerCustomizer;
  27. /**
  28. * Hello World Activator.
  29. *
  30. * @author Alin Dreghiciu
  31. * @since 0.3.0, January 02, 2007
  32. */
  33. public final class Activator implements BundleActivator, ServiceTrackerCustomizer<WebContainer, WebContainer> {
  34. private BundleContext bundleContext;
  35. private ServiceTracker<WebContainer, WebContainer> tracker;
  36. private HelloWorldServlet helloWorldServlet;
  37. private HelloWorldFilter helloWorldFilter;
  38. private HelloWorldListener helloWorldListener;
  39. private HelloWorldSessionListener sessionListener;
  40. private HttpContext httpContext;
  41. private HelloWorldServlet worldServlet;
  42. private HelloWorldErrorServlet errorServlet;
  43. private HelloWorldErrorMakerServlet errorMakerServlet;
  44. /**
  45. * Called when the OSGi framework starts our bundle.
  46. */
  47. public void start(BundleContext bc) throws Exception {
  48. bundleContext = bc;
  49. tracker = new ServiceTracker<>(bc, WebContainer.class, this);
  50. tracker.open();
  51. }
  52. /**
  53. * Called when the OSGi framework stops our bundle
  54. */
  55. public void stop(BundleContext bc) throws Exception {
  56. WebContainer webContainer = tracker.getService();
  57. if (webContainer != null) {
  58. webContainer.unregisterServlet(helloWorldServlet);
  59. webContainer.unregisterFilter(helloWorldFilter);
  60. webContainer.unregisterFilter("HelloWorldFilter");
  61. webContainer.unregisterServlet(worldServlet);
  62. webContainer.unregisterEventListener(helloWorldListener);
  63. webContainer.unregisterEventListener(sessionListener);
  64. webContainer.unregister("/images");
  65. webContainer.unregister("/html");
  66. webContainer.unregisterServlet(errorServlet);
  67. webContainer.unregisterServlet(errorMakerServlet);
  68. webContainer.unregisterErrorPage("java.lang.Exception", httpContext);
  69. webContainer.unregisterErrorPage("404", httpContext);
  70. webContainer.unregisterWelcomeFiles(new String[] { "index.html" }, httpContext);
  71. }
  72. tracker.close();
  73. }
  74. @Override
  75. public WebContainer addingService(ServiceReference<WebContainer> reference) {
  76. final WebContainer webContainer = bundleContext.getService(reference);
  77. if (webContainer != null) {
  78. try {
  79. // not actually needed, because passing null to registerXXX() methods will do the same
  80. httpContext = webContainer.createDefaultHttpContext();
  81. // set a session timeout of 10 minutes
  82. webContainer.setSessionTimeout(10, httpContext);
  83. // register the hello world servlet for filtering with url pattern
  84. // no name is passed, so FQCN will be used as servlet name
  85. final Dictionary<String, String> initParamsServlet = new Hashtable<>();
  86. initParamsServlet.put("from", "WebContainer");
  87. helloWorldServlet = new HelloWorldServlet();
  88. webContainer.registerServlet(helloWorldServlet, new String[] { "/helloworld/wc" },
  89. initParamsServlet, httpContext);
  90. // register the hello world filter based on url paterns
  91. final Dictionary<String, String> initParamsFilter = new Hashtable<>();
  92. initParamsFilter.put("title", "Hello World (url pattern)");
  93. helloWorldFilter = new HelloWorldFilter();
  94. webContainer.registerFilter(helloWorldFilter, new String[] { "/helloworld/wc" }, null,
  95. initParamsFilter, httpContext);
  96. worldServlet = new HelloWorldServlet();
  97. webContainer.registerServlet(worldServlet, "HelloWorld", new String[] { "/helloworld/wc/sn" },
  98. initParamsServlet, httpContext);
  99. // register the hello world filter based on servlet name
  100. // (we need name, otherwise it'd be registered as disabled, because there's already
  101. // a filter with same FQCN)
  102. initParamsFilter.put("title", "Hello World (servlet name)");
  103. webContainer.registerFilter(new HelloWorldFilter(), "HelloWorldFilter", null,
  104. new String[] { "HelloWorld" }, initParamsFilter, true, httpContext);
  105. helloWorldListener = new HelloWorldListener();
  106. webContainer.registerEventListener(helloWorldListener, httpContext);
  107. sessionListener = new HelloWorldSessionListener();
  108. webContainer.registerEventListener(sessionListener, httpContext);
  109. // register images as resources
  110. webContainer.registerResources("/images", "/images", httpContext);
  111. // register a welcome file - should be used for ALL resource servlets - default and non default
  112. webContainer.registerWelcomeFiles(new String[] { "index.html" }, true, httpContext);
  113. // register static htmls
  114. webContainer.registerResources("/html", "/html", httpContext);
  115. errorServlet = new HelloWorldErrorServlet();
  116. webContainer.registerServlet(errorServlet, new String[] { "/helloworld/wc/error" },
  117. null, httpContext);
  118. errorMakerServlet = new HelloWorldErrorMakerServlet();
  119. webContainer.registerServlet(errorMakerServlet, new String[] { "/helloworld/wc/error/create" },
  120. null, httpContext);
  121. // register error page for any Exception
  122. webContainer.registerErrorPage("java.lang.Exception", "/helloworld/wc/error", httpContext);
  123. // register error page for 404 (Page not found)
  124. webContainer.registerErrorPage("404", "/helloworld/wc/error", httpContext);
  125. } catch (Exception e) {
  126. throw new RuntimeException(e.getMessage(), e);
  127. }
  128. }
  129. return webContainer;
  130. }
  131. @Override
  132. public void modifiedService(ServiceReference<WebContainer> reference, WebContainer service) {
  133. // ignore
  134. }
  135. @Override
  136. public void removedService(ServiceReference<WebContainer> reference, WebContainer webContainer) {
  137. // we don't have to unregister in removedService(), because it'll be cleaned anyway
  138. // webContainer.unregisterServlet(helloWorldServlet);
  139. // webContainer.unregisterFilter(helloWorldFilter);
  140. //
  141. // webContainer.unregisterFilter("HelloWorldFilter");
  142. // webContainer.unregisterServlet(worldServlet);
  143. //
  144. // webContainer.unregisterEventListener(helloWorldListener);
  145. // webContainer.unregisterEventListener(sessionListener);
  146. //
  147. // webContainer.unregister("/images");
  148. // webContainer.unregister("/html");
  149. // webContainer.unregisterServlet(errorServlet);
  150. // webContainer.unregisterServlet(errorMakerServlet);
  151. //
  152. // webContainer.unregisterErrorPage("java.lang.Exception", httpContext);
  153. // webContainer.unregisterErrorPage("404", httpContext);
  154. //
  155. // webContainer.unregisterWelcomeFiles(new String[] { "index.html" }, httpContext);
  156. }
  157. }