Browse Source

Register BootstrapListener to Application (#9274)

tags/7.0.0.beta1
Leif Åstrand 12 years ago
parent
commit
5dfec038cd

+ 34
- 0
src/com/vaadin/Application.java View File

@@ -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<String, Integer> retainOnRefreshRoots = new HashMap<String, Integer>();

private final EventRouter eventRouter = new EventRouter();

/**
* Keeps track of which roots have been inited.
* <p>
@@ -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);
}
}

+ 1
- 0
src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java View File

@@ -801,6 +801,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
application.start(new ApplicationStartEvent(null,
getDeploymentConfiguration().getInitParameters(), context,
isProductionMode()));
vaadinContext.applicationStarted(application);
}
}


+ 1
- 0
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java View File

@@ -901,6 +901,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
application.start(new ApplicationStartEvent(applicationUrl,
getDeploymentConfiguration().getInitParameters(),
webApplicationContext, isProductionMode()));
vaadinContext.applicationStarted(application);
}
}


+ 28
- 0
src/com/vaadin/terminal/gwt/server/ApplicationStartedEvent.java View File

@@ -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;
}

}

+ 11
- 0
src/com/vaadin/terminal/gwt/server/ApplicationStartedListener.java View File

@@ -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);
}

+ 5
- 9
src/com/vaadin/terminal/gwt/server/BootstrapHandler.java View File

@@ -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<Node>(),
application, rootId));
new BootstrapFragmentResponse(this, request,
new ArrayList<Node>(), application, rootId));
return context;
}


+ 26
- 12
src/com/vaadin/terminal/gwt/server/VaadinContext.java View File

@@ -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<BootstrapListener> bootstrapListeners = new ArrayList<BootstrapListener>();

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);
}

}

Loading…
Cancel
Save