Skip to content

Commit

Permalink
Make SpringUIProvider serializable (#19462)
Browse files Browse the repository at this point in the history
Change-Id: Ie4ab1e3ff226e34c9c1a3f404890b28c8a018b45
  • Loading branch information
Artur- committed Sep 1, 2016
1 parent e7acb97 commit 5a34f67
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.vaadin.server.UICreateEvent;
import com.vaadin.server.UIProvider;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinSession;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.internal.UIID;
import com.vaadin.ui.UI;
Expand All @@ -45,16 +46,22 @@ public class SpringUIProvider extends UIProvider {

protected final Logger logger = LoggerFactory.getLogger(getClass());

private final WebApplicationContext webApplicationContext;
private final VaadinSession vaadinSession;

/**
* Temporary cache for webApplicationContext, cleared if the session is serialized.
*/
private transient WebApplicationContext webApplicationContext = null;
private final Map<String, Class<? extends UI>> pathToUIMap = new ConcurrentHashMap<String, Class<? extends UI>>();
private final Map<String, Class<? extends UI>> wildcardPathToUIMap = new ConcurrentHashMap<String, Class<? extends UI>>();

public SpringUIProvider(WebApplicationContext webApplicationContext) {
if (webApplicationContext == null) {
public SpringUIProvider(VaadinSession vaadinSession) {
this.vaadinSession = vaadinSession;

if (getWebApplicationContext() == null) {
throw new IllegalStateException(
"Spring WebApplicationContext not initialized for UI provider. Use e.g. ContextLoaderListener to initialize it.");
}
this.webApplicationContext = webApplicationContext;
detectUIs();
if (pathToUIMap.isEmpty()) {
logger.warn("Found no Vaadin UIs in the application context");
Expand Down Expand Up @@ -144,6 +151,11 @@ private String extractUIPathFromRequest(VaadinRequest request) {
}

protected WebApplicationContext getWebApplicationContext() {
if (webApplicationContext == null) {
webApplicationContext = ((SpringVaadinServletService) vaadinSession.getService())
.getWebApplicationContext();
}

return webApplicationContext;
}

Expand All @@ -169,7 +181,7 @@ public UI createInstance(UICreateEvent event) {
logger.debug(
"Creating a new UI bean of class [{}] with identifier [{}]",
event.getUIClass().getCanonicalName(), identifier);
return webApplicationContext.getBean(event.getUIClass());
return getWebApplicationContext().getBean(event.getUIClass());
} finally {
CurrentInstance.set(key, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ public void sessionInit(SessionInitEvent sessionInitEvent)
}

// add Spring UI provider
SpringUIProvider uiProvider = new SpringUIProvider(
webApplicationContext);
SpringUIProvider uiProvider = new SpringUIProvider(session);
session.addUIProvider(uiProvider);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import java.util.List;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.server.DeploymentConfiguration;
import com.vaadin.server.RequestHandler;
import com.vaadin.server.ServiceException;
Expand Down Expand Up @@ -72,4 +75,12 @@ protected String getServiceUrl(BootstrapContext context) {
return handlers;
}

/**
* Find the Spring web application context related to the servlet context.
*
*/
public WebApplicationContext getWebApplicationContext() {
return WebApplicationContextUtils
.getWebApplicationContext(getServlet().getServletContext());
}
}

0 comments on commit 5a34f67

Please sign in to comment.