+++ /dev/null
-/*
- * Copyright 2011 Vaadin Ltd.
- *
- * Licensed 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 com.vaadin.server;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ServiceLoader;
-
-/**
- * Point of entry for add-ons for integrating into various aspects of the
- * framework. One add-on context is initialized for each Vaadin Servlet or
- * Portlet instance and upon initialization, every {@link AddonContextListener}
- * that can be found is notified to let it add listeners to the context.
- * <p>
- * By default, AddonContextListeners are loaded using {@link ServiceLoader},
- * which means that the file
- * META-INF/services/com.vaadin.server.AddonContextListener will be checked for
- * lines containing fully qualified names of classes to use. This behavior can
- * however be overridden for custom deployment situations (e.g. to use CDI or
- * OSGi) by overriding {@link VaadinService#getAddonContextListeners()}.
- *
- * @author Vaadin Ltd
- * @since 7.0.0
- */
-public class AddonContext implements Serializable {
- private final VaadinService vaadinService;
-
- private List<BootstrapListener> bootstrapListeners = new ArrayList<BootstrapListener>();
-
- private List<AddonContextListener> initedListeners = new ArrayList<AddonContextListener>();
-
- /**
- * Creates a new context using a given vaadin service. Only the framework
- * itself should typically create AddonContext instances.
- *
- * @param vaadinService
- * the vaadin service for the associated servlet or portlet.
- */
- public AddonContext(VaadinService vaadinService) {
- this.vaadinService = vaadinService;
- vaadinService.addSessionInitListener(new SessionInitListener() {
- @Override
- public void sessionInit(SessionInitEvent event)
- throws ServiceException {
- for (BootstrapListener l : bootstrapListeners) {
- event.getSession().addBootstrapListener(l);
- }
- }
- });
- vaadinService.setAddonContext(this);
- }
-
- /**
- * Gets the vaadin service for this context.
- *
- * @return the vaadin service
- */
- public VaadinService getService() {
- return vaadinService;
- }
-
- /**
- * Initializes this context, causing all found listeners to be notified.
- * Listeners are by default found using {@link ServiceLoader}, but the
- * {@link VaadinService} can provide an alternative implementation.
- * <p>
- * This method is not intended to be used by add-ons, but instead by the
- * part of the framework that created this context object.
- */
- public void init() {
- AddonContextEvent event = new AddonContextEvent(this);
- Iterator<AddonContextListener> listeners = vaadinService
- .getAddonContextListeners();
- while (listeners.hasNext()) {
- AddonContextListener listener = listeners.next();
- listener.contextCreated(event);
- initedListeners.add(listener);
- }
- }
-
- /**
- * Destroys this context, causing all initialized listeners to be invoked.
- * <p>
- * This method is not intended to be used by add-ons, but instead by the
- * part of the framework that created this context object.
- */
- public void destroy() {
- AddonContextEvent event = new AddonContextEvent(this);
- for (AddonContextListener listener : initedListeners) {
- listener.contextDestroyed(event);
- }
- }
-
- /**
- * Shorthand for adding a bootstrap listener that will be added to every new
- * service session.
- *
- * @see VaadinSession#addBootstrapListener(BootstrapListener)
- *
- * @param listener
- * the bootstrap listener that should be added to all new
- * applications.
- */
- public void addBootstrapListener(BootstrapListener listener) {
- bootstrapListeners.add(listener);
- }
-}
+++ /dev/null
-/*
- * Copyright 2011 Vaadin Ltd.
- *
- * Licensed 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 com.vaadin.server;
-
-import java.util.EventObject;
-
-/**
- * Event used when an {@link AddonContext} is created and destroyed.
- *
- * @see AddonContextListener
- *
- * @author Vaadin Ltd
- * @since 7.0.0
- */
-public class AddonContextEvent extends EventObject {
-
- /**
- * Creates a new event for the given add-on context.
- *
- * @param source
- * the add-on context that created the event
- */
- public AddonContextEvent(AddonContext source) {
- super(source);
- }
-
- /**
- * Gets the add-on context that created this event.
- *
- * @return the add-on context that created this event.
- */
- public AddonContext getAddonContext() {
- return (AddonContext) getSource();
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2011 Vaadin Ltd.
- *
- * Licensed 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 com.vaadin.server;
-
-import java.io.Serializable;
-import java.util.EventListener;
-
-/**
- * Listener that gets notified then the {@link AddonContext} is initialized,
- * allowing an add-on to add listeners to various parts of the framework. In a
- * default configuration, add-ons can register their listeners by including a
- * file named META-INF/services/com.vaadin.server.AddonContextListener
- * containing the fully qualified class names of classes implementing this
- * interface.
- *
- * @author Vaadin Ltd
- * @since 7.0.0
- */
-public interface AddonContextListener extends EventListener, Serializable {
- /**
- * Notifies the listener that the add-on context has been created and
- * initialized. An add-on can use this method to get access to an
- * {@link AddonContext} object to which listeners can be added.
- *
- * @param event
- * the add-on context event
- */
- public void contextCreated(AddonContextEvent event);
-
- /**
- * Notifies the listener that the add-on context has been closed. An add-on
- * can use this method to e.g. close resources that have been opened in
- * {@link #contextCreated(AddonContextEvent)}.
- *
- * @param event
- * the add-on context event
- */
- public void contextDestroyed(AddonContextEvent event);
-}
// when the portlet is removed?
private VaadinPortletService vaadinService;
- private AddonContext addonContext;
@Override
public void init(PortletConfig config) throws PortletException {
// Sets current service even though there are no request and response
vaadinService.setCurrentInstances(null, null);
- addonContext = new AddonContext(vaadinService);
- addonContext.init();
-
portletInitialized();
CurrentInstance.clearAll();
}
return new VaadinPortletService(this, deploymentConfiguration);
}
- @Override
- public void destroy() {
- super.destroy();
-
- addonContext.destroy();
- }
-
/**
* @author Vaadin Ltd
*
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Locale;
-import java.util.ServiceLoader;
import javax.portlet.PortletContext;
import javax.servlet.ServletContext;
@Deprecated
public static final String URL_PARAMETER_CLOSE_APPLICATION = "closeApplication";
- private AddonContext addonContext;
private final DeploymentConfiguration deploymentConfiguration;
private final EventRouter eventRouter = new EventRouter();
return deploymentConfiguration;
}
- public Iterator<AddonContextListener> getAddonContextListeners() {
- // Called once for init and then no more, so there's no point in caching
- // the instance
- ServiceLoader<AddonContextListener> contextListenerLoader = ServiceLoader
- .load(AddonContextListener.class, getClassLoader());
- return contextListenerLoader.iterator();
- }
-
- public AddonContext getAddonContext() {
- return addonContext;
- }
-
- public void setAddonContext(AddonContext addonContext) {
- this.addonContext = addonContext;
- }
-
/**
* Sets the system messages provider to use for getting system messages to
* display to users of this service.
private VaadinServletService servletService;
- private AddonContext addonContext;
-
/**
* Called by the servlet container to indicate to a servlet that the servlet
* is being placed into service.
// Sets current service even though there are no request and response
servletService.setCurrentInstances(null, null);
- addonContext = new AddonContext(servletService);
- addonContext.init();
-
servletInitialized();
CurrentInstance.clearAll();
return new VaadinServletService(this, deploymentConfiguration);
}
- @Override
- public void destroy() {
- super.destroy();
-
- addonContext.destroy();
- }
-
/**
* Receives standard HTTP requests from the public service method and
* dispatches them.
+++ /dev/null
-com.vaadin.tests.vaadincontext.TestAddonContextListener
\ No newline at end of file
<td>/run/com.vaadin.tests.vaadincontext.BootstrapModifyUI?restartApplication</td>
<td></td>
</tr>
+<tr>
+ <td>click</td>
+ <td>vaadin=runcomvaadintestsvaadincontextBootstrapModifyUI::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VButton[0]/domChild[0]/domChild[0]</td>
+ <td></td>
+</tr>
+<tr>
+ <td>open</td>
+ <td>/run/com.vaadin.tests.vaadincontext.BootstrapModifyUI</td>
+ <td></td>
+</tr>
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestsvaadincontextBootstrapModifyUI::/VVerticalLayout[0]/VLabel[0]</td>
package com.vaadin.tests.vaadincontext;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+
+import com.vaadin.server.BootstrapFragmentResponse;
+import com.vaadin.server.BootstrapListener;
+import com.vaadin.server.BootstrapPageResponse;
+import com.vaadin.server.BootstrapResponse;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.UI;
public class BootstrapModifyUI extends AbstractTestUI {
+ private static final String INSTALLED_ATRIBUTE_NAME = BootstrapModifyUI.class
+ .getName() + ".installed";
@Override
protected void setup(VaadinRequest request) {
- // TODO Auto-generated method stub
+ Button c = new Button("Add bootstrap listener",
+ new Button.ClickListener() {
+ @Override
+ public void buttonClick(ClickEvent event) {
+ getSession().addBootstrapListener(
+ createBootstrapListener());
+ event.getButton().setEnabled(false);
+ getSession().setAttribute(INSTALLED_ATRIBUTE_NAME,
+ Boolean.TRUE);
+ }
+ });
+ addComponent(c);
+ c.setEnabled(getSession().getAttribute(INSTALLED_ATRIBUTE_NAME) == null);
+ }
+
+ private static BootstrapListener createBootstrapListener() {
+ return new BootstrapListener() {
+ @Override
+ public void modifyBootstrapFragment(
+ BootstrapFragmentResponse response) {
+ if (shouldModify(response)) {
+ Element heading = new Element(Tag.valueOf("div"), "")
+ .text("Added by modifyBootstrapFragment");
+ response.getFragmentNodes().add(0, heading);
+ }
+ }
+
+ private boolean shouldModify(BootstrapResponse response) {
+ Class<? extends UI> uiClass = response.getUiClass();
+ boolean shouldModify = uiClass == BootstrapModifyUI.class;
+ return shouldModify;
+ }
+ @Override
+ public void modifyBootstrapPage(BootstrapPageResponse response) {
+ if (shouldModify(response)) {
+ response.getDocument().body().child(0)
+ .before("<div>Added by modifyBootstrapPage</div>");
+ }
+ }
+ };
}
@Override
+++ /dev/null
-/*
- * Copyright 2011 Vaadin Ltd.
- *
- * Licensed 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 com.vaadin.tests.vaadincontext;
-
-import org.jsoup.nodes.Element;
-import org.jsoup.parser.Tag;
-
-import com.vaadin.server.AddonContextEvent;
-import com.vaadin.server.AddonContextListener;
-import com.vaadin.server.BootstrapFragmentResponse;
-import com.vaadin.server.BootstrapListener;
-import com.vaadin.server.BootstrapPageResponse;
-import com.vaadin.server.BootstrapResponse;
-import com.vaadin.ui.UI;
-
-public class TestAddonContextListener implements AddonContextListener {
- @Override
- public void contextCreated(AddonContextEvent event) {
- event.getAddonContext().addBootstrapListener(new BootstrapListener() {
- @Override
- public void modifyBootstrapFragment(
- BootstrapFragmentResponse response) {
- if (shouldModify(response)) {
- Element heading = new Element(Tag.valueOf("div"), "")
- .text("Added by modifyBootstrapFragment");
- response.getFragmentNodes().add(0, heading);
- }
- }
-
- private boolean shouldModify(BootstrapResponse response) {
- Class<? extends UI> uiClass = response.getUiClass();
- boolean shouldModify = uiClass == BootstrapModifyUI.class;
- return shouldModify;
- }
-
- @Override
- public void modifyBootstrapPage(BootstrapPageResponse response) {
- if (shouldModify(response)) {
- response.getDocument().body().child(0)
- .before("<div>Added by modifyBootstrapPage</div>");
- }
- }
- });
- }
-
- @Override
- public void contextDestroyed(AddonContextEvent event) {
- // Nothing to do
- }
-
-}