From 5dfec038cd191101a9e918f8f886b3a9b45e0179 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Mon, 13 Aug 2012 15:14:45 +0300 Subject: [PATCH] Register BootstrapListener to Application (#9274) --- src/com/vaadin/Application.java | 34 +++++++++++++++++ .../server/AbstractApplicationPortlet.java | 1 + .../server/AbstractApplicationServlet.java | 1 + .../gwt/server/ApplicationStartedEvent.java | 28 ++++++++++++++ .../server/ApplicationStartedListener.java | 11 ++++++ .../terminal/gwt/server/BootstrapHandler.java | 14 +++---- .../terminal/gwt/server/VaadinContext.java | 38 +++++++++++++------ 7 files changed, 106 insertions(+), 21 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/server/ApplicationStartedEvent.java create mode 100644 src/com/vaadin/terminal/gwt/server/ApplicationStartedListener.java diff --git a/src/com/vaadin/Application.java b/src/com/vaadin/Application.java index 086caa5509..1d31410185 100644 --- a/src/com/vaadin/Application.java +++ b/src/com/vaadin/Application.java @@ -7,6 +7,7 @@ package com.vaadin; import java.io.IOException; import java.io.Serializable; import java.lang.annotation.Annotation; +import java.lang.reflect.Method; import java.net.SocketException; import java.net.URL; import java.util.ArrayList; @@ -35,6 +36,7 @@ import com.vaadin.annotations.Widgetset; import com.vaadin.data.util.converter.Converter; import com.vaadin.data.util.converter.ConverterFactory; import com.vaadin.data.util.converter.DefaultConverterFactory; +import com.vaadin.event.EventRouter; import com.vaadin.service.ApplicationContext; import com.vaadin.terminal.AbstractErrorMessage; import com.vaadin.terminal.ApplicationResource; @@ -48,9 +50,14 @@ import com.vaadin.terminal.WrappedRequest.BrowserDetails; import com.vaadin.terminal.WrappedResponse; import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.server.AbstractApplicationServlet; +import com.vaadin.terminal.gwt.server.BootstrapFragmentResponse; +import com.vaadin.terminal.gwt.server.BootstrapListener; +import com.vaadin.terminal.gwt.server.BootstrapPageResponse; +import com.vaadin.terminal.gwt.server.BootstrapResponse; import com.vaadin.terminal.gwt.server.ChangeVariablesErrorEvent; import com.vaadin.terminal.gwt.server.ClientConnector; import com.vaadin.terminal.gwt.server.WebApplicationContext; +import com.vaadin.tools.ReflectTools; import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.AbstractField; import com.vaadin.ui.Root; @@ -119,6 +126,13 @@ public class Application implements Terminal.ErrorListener, Serializable { */ public static final String ROOT_PARAMETER = "root"; + private static final Method BOOTSTRAP_FRAGMENT_METHOD = ReflectTools + .findMethod(BootstrapListener.class, "modifyBootstrapFragment", + BootstrapFragmentResponse.class); + private static final Method BOOTSTRAP_PAGE_METHOD = ReflectTools + .findMethod(BootstrapListener.class, "modifyBootstrapPage", + BootstrapPageResponse.class); + /** * A special application designed to help migrating applications from Vaadin * 6 to Vaadin 7. The legacy application supports setting a main window, @@ -492,6 +506,8 @@ public class Application implements Terminal.ErrorListener, Serializable { private final Map retainOnRefreshRoots = new HashMap(); + private final EventRouter eventRouter = new EventRouter(); + /** * Keeps track of which roots have been inited. *

@@ -2389,4 +2405,22 @@ public class Application implements Terminal.ErrorListener, Serializable { public Root getRootById(int rootId) { return roots.get(rootId); } + + public void addBootstrapListener(BootstrapListener listener) { + eventRouter.addListener(BootstrapFragmentResponse.class, listener, + BOOTSTRAP_FRAGMENT_METHOD); + eventRouter.addListener(BootstrapPageResponse.class, listener, + BOOTSTRAP_PAGE_METHOD); + } + + public void removeBootstrapListener(BootstrapListener listener) { + eventRouter.removeListener(BootstrapFragmentResponse.class, listener, + BOOTSTRAP_FRAGMENT_METHOD); + eventRouter.removeListener(BootstrapPageResponse.class, listener, + BOOTSTRAP_PAGE_METHOD); + } + + public void modifyBootstrapResponse(BootstrapResponse response) { + eventRouter.fireEvent(response); + } } diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java index bb4e8ba0a7..e5fb6afad2 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java @@ -801,6 +801,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet application.start(new ApplicationStartEvent(null, getDeploymentConfiguration().getInitParameters(), context, isProductionMode())); + vaadinContext.applicationStarted(application); } } diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java index c2e1d2d3e7..b8aba5c4b4 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java @@ -901,6 +901,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements application.start(new ApplicationStartEvent(applicationUrl, getDeploymentConfiguration().getInitParameters(), webApplicationContext, isProductionMode())); + vaadinContext.applicationStarted(application); } } diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationStartedEvent.java b/src/com/vaadin/terminal/gwt/server/ApplicationStartedEvent.java new file mode 100644 index 0000000000..33859f5605 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/ApplicationStartedEvent.java @@ -0,0 +1,28 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.server; + +import java.util.EventObject; + +import com.vaadin.Application; + +public class ApplicationStartedEvent extends EventObject { + private final Application application; + + public ApplicationStartedEvent(VaadinContext context, + Application application) { + super(context); + this.application = application; + } + + public VaadinContext getContext() { + return (VaadinContext) getSource(); + } + + public Application getApplication() { + return application; + } + +} diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationStartedListener.java b/src/com/vaadin/terminal/gwt/server/ApplicationStartedListener.java new file mode 100644 index 0000000000..87884a0fda --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/ApplicationStartedListener.java @@ -0,0 +1,11 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.server; + +import java.util.EventListener; + +public interface ApplicationStartedListener extends EventListener { + public void applicationStarted(ApplicationStartedEvent event); +} diff --git a/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java b/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java index bc3b7aa7a0..e89737337b 100644 --- a/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java +++ b/src/com/vaadin/terminal/gwt/server/BootstrapHandler.java @@ -131,13 +131,9 @@ public abstract class BootstrapHandler implements RequestHandler { application, rootId); setupMainDiv(context); - DeploymentConfiguration deploymentConfiguration = request - .getDeploymentConfiguration(); - - VaadinContext vContext = deploymentConfiguration.getVaadinContext(); BootstrapFragmentResponse fragmentResponse = context .getBootstrapResponse(); - vContext.fireModifyBootstrapEvent(fragmentResponse); + application.modifyBootstrapResponse(fragmentResponse); String html = getBootstrapHtml(context); @@ -154,7 +150,7 @@ public abstract class BootstrapHandler implements RequestHandler { WrappedResponse response = context.getResponse(); DeploymentConfiguration deploymentConfiguration = request .getDeploymentConfiguration(); - VaadinContext vContext = deploymentConfiguration.getVaadinContext(); + BootstrapFragmentResponse fragmentResponse = context .getBootstrapResponse(); @@ -171,7 +167,7 @@ public abstract class BootstrapHandler implements RequestHandler { } setupStandaloneDocument(context, pageResponse); - vContext.fireModifyBootstrapEvent(pageResponse); + context.getApplication().modifyBootstrapResponse(pageResponse); sendBootstrapHeaders(response, headers); @@ -266,8 +262,8 @@ public abstract class BootstrapHandler implements RequestHandler { public BootstrapContext createContext(WrappedRequest request, WrappedResponse response, Application application, Integer rootId) { BootstrapContext context = new BootstrapContext(response, - new BootstrapFragmentResponse(this, request, new ArrayList(), - application, rootId)); + new BootstrapFragmentResponse(this, request, + new ArrayList(), application, rootId)); return context; } diff --git a/src/com/vaadin/terminal/gwt/server/VaadinContext.java b/src/com/vaadin/terminal/gwt/server/VaadinContext.java index 3682c0ac4d..081f25bbfb 100644 --- a/src/com/vaadin/terminal/gwt/server/VaadinContext.java +++ b/src/com/vaadin/terminal/gwt/server/VaadinContext.java @@ -5,24 +5,26 @@ package com.vaadin.terminal.gwt.server; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import com.vaadin.Application; import com.vaadin.event.EventRouter; import com.vaadin.terminal.DeploymentConfiguration; import com.vaadin.tools.ReflectTools; public class VaadinContext { - private static final Method BOOTSTRAP_FRAGMENT_METHOD = ReflectTools - .findMethod(BootstrapListener.class, "modifyBootstrapFragment", - BootstrapFragmentResponse.class); - private static final Method BOOTSTRAP_PAGE_METHOD = ReflectTools - .findMethod(BootstrapListener.class, "modifyBootstrapPage", - BootstrapPageResponse.class); + private static final Method APPLICATION_STARTED_METHOD = ReflectTools + .findMethod(ApplicationStartedListener.class, "applicationStarted", + ApplicationStartedEvent.class); private final DeploymentConfiguration deploymentConfiguration; private final EventRouter eventRouter = new EventRouter(); + private List bootstrapListeners = new ArrayList(); + public VaadinContext(DeploymentConfiguration deploymentConfiguration) { this.deploymentConfiguration = deploymentConfiguration; deploymentConfiguration.setVaadinContext(this); @@ -53,14 +55,26 @@ public class VaadinContext { } public void addBootstrapListener(BootstrapListener listener) { - eventRouter.addListener(BootstrapFragmentResponse.class, listener, - BOOTSTRAP_FRAGMENT_METHOD); - eventRouter.addListener(BootstrapPageResponse.class, listener, - BOOTSTRAP_PAGE_METHOD); + bootstrapListeners.add(listener); + } + + public void applicationStarted(Application application) { + eventRouter.fireEvent(new ApplicationStartedEvent(this, application)); + for (BootstrapListener l : bootstrapListeners) { + application.addBootstrapListener(l); + } + } + + public void addApplicationStartedListener( + ApplicationStartedListener applicationStartListener) { + eventRouter.addListener(ApplicationStartedEvent.class, + applicationStartListener, APPLICATION_STARTED_METHOD); } - public void fireModifyBootstrapEvent(BootstrapResponse bootstrapResponse) { - eventRouter.fireEvent(bootstrapResponse); + public void removeApplicationStartedListener( + ApplicationStartedListener applicationStartListener) { + eventRouter.removeListener(ApplicationStartedEvent.class, + applicationStartListener, APPLICATION_STARTED_METHOD); } } -- 2.39.5