You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddonContext.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.lang.reflect.Method;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.ServiceLoader;
  22. import com.vaadin.Application;
  23. import com.vaadin.event.EventRouter;
  24. import com.vaadin.tools.ReflectTools;
  25. /**
  26. * Point of entry for add-ons for integrating into various aspects of the
  27. * framework. One add-on context is initialized for each Vaadin Servlet or
  28. * Portlet instance and upon initialization, every {@link AddonContextListener}
  29. * that can be found is notified to let it add listeners to the context.
  30. * <p>
  31. * By default, AddonContextListeners are loaded using {@link ServiceLoader},
  32. * which means that the file
  33. * META-INF/services/com.vaadin.server.AddonContextListener will be checked for
  34. * lines containing fully qualified names of classes to use. This behavior can
  35. * however be overridden for custom deployment situations (e.g. to use CDI or
  36. * OSGi) by overriding
  37. * {@link DeploymentConfiguration#getAddonContextListeners()}.
  38. *
  39. * @author Vaadin Ltd
  40. * @since 7.0.0
  41. */
  42. public class AddonContext {
  43. private static final Method APPLICATION_STARTED_METHOD = ReflectTools
  44. .findMethod(ApplicationStartedListener.class, "applicationStarted",
  45. ApplicationStartedEvent.class);
  46. private final DeploymentConfiguration deploymentConfiguration;
  47. private final EventRouter eventRouter = new EventRouter();
  48. private List<BootstrapListener> bootstrapListeners = new ArrayList<BootstrapListener>();
  49. private List<AddonContextListener> initedListeners = new ArrayList<AddonContextListener>();
  50. /**
  51. * Creates a new context using a given deployment configuration. Only the
  52. * framework itself should typically create AddonContext methods.
  53. *
  54. * @param deploymentConfiguration
  55. * the deployment configuration for the associated servlet or
  56. * portlet.
  57. */
  58. public AddonContext(DeploymentConfiguration deploymentConfiguration) {
  59. this.deploymentConfiguration = deploymentConfiguration;
  60. deploymentConfiguration.setAddonContext(this);
  61. }
  62. /**
  63. * Gets the deployment configuration for this context.
  64. *
  65. * @return the deployment configuration
  66. */
  67. public DeploymentConfiguration getDeploymentConfiguration() {
  68. return deploymentConfiguration;
  69. }
  70. /**
  71. * Initializes this context, causing all found listeners to be notified.
  72. * Listeners are by default found using {@link ServiceLoader}, but the
  73. * {@link DeploymentConfiguration} can provide an alternative
  74. * implementation.
  75. * <p>
  76. * This method is not intended to be used by add-ons, but instead by the
  77. * part of the framework that created this context object.
  78. */
  79. public void init() {
  80. AddonContextEvent event = new AddonContextEvent(this);
  81. Iterator<AddonContextListener> listeners = deploymentConfiguration
  82. .getAddonContextListeners();
  83. while (listeners.hasNext()) {
  84. AddonContextListener listener = listeners.next();
  85. listener.contextCreated(event);
  86. initedListeners.add(listener);
  87. }
  88. }
  89. /**
  90. * Destroys this context, causing all initialized listeners to be invoked.
  91. * <p>
  92. * This method is not intended to be used by add-ons, but instead by the
  93. * part of the framework that created this context object.
  94. */
  95. public void destroy() {
  96. AddonContextEvent event = new AddonContextEvent(this);
  97. for (AddonContextListener listener : initedListeners) {
  98. listener.contextDestoryed(event);
  99. }
  100. }
  101. /**
  102. * Shorthand for adding a bootstrap listener that will be added to every new
  103. * application.
  104. *
  105. * @see #addApplicationStartedListener(ApplicationStartedListener)
  106. * @see Application#addBootstrapListener(BootstrapListener)
  107. *
  108. * @param listener
  109. * the bootstrap listener that should be added to all new
  110. * applications.
  111. */
  112. public void addBootstrapListener(BootstrapListener listener) {
  113. bootstrapListeners.add(listener);
  114. }
  115. /**
  116. * Fires an {@link ApplicationStartedEvent} to all registered listeners.
  117. * This method is not intended to be used by add-ons, but instead by the
  118. * part of the framework that creates new Application instances.
  119. *
  120. * @see #addApplicationStartedListener(ApplicationStartedListener)
  121. *
  122. * @param application
  123. * the newly started application
  124. */
  125. public void fireApplicationStarted(Application application) {
  126. eventRouter.fireEvent(new ApplicationStartedEvent(this, application));
  127. for (BootstrapListener l : bootstrapListeners) {
  128. application.addBootstrapListener(l);
  129. }
  130. }
  131. /**
  132. * Adds a listener that will be notified any time a new {@link Application}
  133. * instance is started or more precisely directly after
  134. * {@link Application#init()} has been invoked.
  135. *
  136. * @param applicationStartListener
  137. * the application start listener that should be added
  138. */
  139. public void addApplicationStartedListener(
  140. ApplicationStartedListener applicationStartListener) {
  141. eventRouter.addListener(ApplicationStartedEvent.class,
  142. applicationStartListener, APPLICATION_STARTED_METHOD);
  143. }
  144. /**
  145. * Removes an application start listener.
  146. *
  147. * @see #addApplicationStartedListener(ApplicationStartedListener)
  148. *
  149. * @param applicationStartListener
  150. * the application start listener to remove
  151. */
  152. public void removeApplicationStartedListener(
  153. ApplicationStartedListener applicationStartListener) {
  154. eventRouter.removeListener(ApplicationStartedEvent.class,
  155. applicationStartListener, APPLICATION_STARTED_METHOD);
  156. }
  157. }