From: Leif Åstrand Date: Tue, 20 Nov 2012 14:53:49 +0000 (+0200) Subject: Remove AddonContext (#10254) X-Git-Tag: 7.0.0.beta10~62 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=feac70070091a3d823632a7396ec4eef0886f5b2;p=vaadin-framework.git Remove AddonContext (#10254) Change-Id: I12d3075a4808b004d7b891d1c4092131fa7cb1a2 --- diff --git a/server/src/com/vaadin/server/AddonContext.java b/server/src/com/vaadin/server/AddonContext.java deleted file mode 100644 index 088366c541..0000000000 --- a/server/src/com/vaadin/server/AddonContext.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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. - *

- * 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 bootstrapListeners = new ArrayList(); - - private List initedListeners = new ArrayList(); - - /** - * 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. - *

- * 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 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. - *

- * 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); - } -} diff --git a/server/src/com/vaadin/server/AddonContextEvent.java b/server/src/com/vaadin/server/AddonContextEvent.java deleted file mode 100644 index d83b1b0084..0000000000 --- a/server/src/com/vaadin/server/AddonContextEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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(); - } - -} diff --git a/server/src/com/vaadin/server/AddonContextListener.java b/server/src/com/vaadin/server/AddonContextListener.java deleted file mode 100644 index 602ba0ba66..0000000000 --- a/server/src/com/vaadin/server/AddonContextListener.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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); -} diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 05b6ac360a..3f7c4472e9 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -246,7 +246,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants { // when the portlet is removed? private VaadinPortletService vaadinService; - private AddonContext addonContext; @Override public void init(PortletConfig config) throws PortletException { @@ -275,9 +274,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants { // Sets current service even though there are no request and response vaadinService.setCurrentInstances(null, null); - addonContext = new AddonContext(vaadinService); - addonContext.init(); - portletInitialized(); CurrentInstance.clearAll(); } @@ -296,13 +292,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants { return new VaadinPortletService(this, deploymentConfiguration); } - @Override - public void destroy() { - super.destroy(); - - addonContext.destroy(); - } - /** * @author Vaadin Ltd * diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index 41115a3503..02463242f4 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -24,9 +24,7 @@ import java.net.MalformedURLException; 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; @@ -70,7 +68,6 @@ public abstract class VaadinService implements Serializable { @Deprecated public static final String URL_PARAMETER_CLOSE_APPLICATION = "closeApplication"; - private AddonContext addonContext; private final DeploymentConfiguration deploymentConfiguration; private final EventRouter eventRouter = new EventRouter(); @@ -190,22 +187,6 @@ public abstract class VaadinService implements Serializable { return deploymentConfiguration; } - public Iterator getAddonContextListeners() { - // Called once for init and then no more, so there's no point in caching - // the instance - ServiceLoader 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. diff --git a/server/src/com/vaadin/server/VaadinServlet.java b/server/src/com/vaadin/server/VaadinServlet.java index 3089eccc12..414e814ffd 100644 --- a/server/src/com/vaadin/server/VaadinServlet.java +++ b/server/src/com/vaadin/server/VaadinServlet.java @@ -74,8 +74,6 @@ public class VaadinServlet extends HttpServlet implements Constants { private VaadinServletService servletService; - private AddonContext addonContext; - /** * Called by the servlet container to indicate to a servlet that the servlet * is being placed into service. @@ -116,9 +114,6 @@ public class VaadinServlet extends HttpServlet implements Constants { // Sets current service even though there are no request and response servletService.setCurrentInstances(null, null); - addonContext = new AddonContext(servletService); - addonContext.init(); - servletInitialized(); CurrentInstance.clearAll(); @@ -177,13 +172,6 @@ public class VaadinServlet extends HttpServlet implements Constants { 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. diff --git a/uitest/src/META-INF/services/com.vaadin.server.AddonContextListener b/uitest/src/META-INF/services/com.vaadin.server.AddonContextListener deleted file mode 100644 index 9b3d9eb082..0000000000 --- a/uitest/src/META-INF/services/com.vaadin.server.AddonContextListener +++ /dev/null @@ -1 +0,0 @@ -com.vaadin.tests.vaadincontext.TestAddonContextListener \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.html b/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.html index e6fafb14fa..83db83b5f7 100644 --- a/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.html +++ b/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.html @@ -16,6 +16,16 @@ /run/com.vaadin.tests.vaadincontext.BootstrapModifyUI?restartApplication + + click + vaadin=runcomvaadintestsvaadincontextBootstrapModifyUI::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VButton[0]/domChild[0]/domChild[0] + + + + open + /run/com.vaadin.tests.vaadincontext.BootstrapModifyUI + + assertText vaadin=runcomvaadintestsvaadincontextBootstrapModifyUI::/VVerticalLayout[0]/VLabel[0] diff --git a/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.java b/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.java index 35f7aeb044..512d89a381 100644 --- a/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.java +++ b/uitest/src/com/vaadin/tests/vaadincontext/BootstrapModifyUI.java @@ -16,15 +16,66 @@ 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 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("

Added by modifyBootstrapPage
"); + } + } + }; } @Override diff --git a/uitest/src/com/vaadin/tests/vaadincontext/TestAddonContextListener.java b/uitest/src/com/vaadin/tests/vaadincontext/TestAddonContextListener.java deleted file mode 100644 index 87a0f5fa51..0000000000 --- a/uitest/src/com/vaadin/tests/vaadincontext/TestAddonContextListener.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 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("
Added by modifyBootstrapPage
"); - } - } - }); - } - - @Override - public void contextDestroyed(AddonContextEvent event) { - // Nothing to do - } - -}