Pārlūkot izejas kodu

Derive current servlet/portlet from the current service (#11779)

Implement VaadinPortlet.getCurrent() to use VaadinService.getCurrent()
instead of having a separate thread local variable. This is done to
avoid classloading issues when determining which instances to preserve
in CurrentInstance.setThreadLocals. The two current instances have
previously been kept in sync in all cases except during
VaadinPortlet.init where VaadinService has not yet been created.

VaadinPortlet.setCurrent() is removed as no way of preserving its
semantics has been found. This breaks API compatibility, but is probably
better than having a deprecated implementation that can not work as
expected in all situations.

The same changes have also been made to VaadinServlet to maintain the
symmetry.

Change-Id: I0a1ccc07a4aeecec558a9aaae211bd56207313d8
tags/7.1.0
Leif Åstrand pirms 11 gadiem
vecāks
revīzija
778de066b1

+ 10
- 25
server/src/com/vaadin/server/VaadinPortlet.java Parādīt failu

@@ -287,7 +287,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants,
@Override
public void init(PortletConfig config) throws PortletException {
CurrentInstance.clearAll();
setCurrent(this);
super.init(config);
Properties initParameters = new Properties();

@@ -407,7 +406,6 @@ public class VaadinPortlet extends GenericPortlet implements Constants,
PortletResponse response) throws PortletException, IOException {

CurrentInstance.clearAll();
setCurrent(this);
try {
getService().handleRequest(createVaadinRequest(request),
createVaadinResponse(response));
@@ -495,36 +493,23 @@ public class VaadinPortlet extends GenericPortlet implements Constants,
* portlet is defined (see {@link InheritableThreadLocal}). In other cases,
* (e.g. from background threads started in some other way), the current
* portlet is not automatically defined.
* <p>
* The current portlet is derived from the current service using
* {@link VaadinService#getCurrent()}
*
* @return the current vaadin portlet instance if available, otherwise
* <code>null</code>
*
* @see #setCurrent(VaadinPortlet)
*
* @since 7.0
*/
public static VaadinPortlet getCurrent() {
return CurrentInstance.get(VaadinPortlet.class);
}

/**
* Sets the current Vaadin portlet. This method is used by the framework to
* set the current portlet whenever a new request is processed and it is
* cleared when the request has been processed.
* <p>
* The application developer can also use this method to define the current
* portlet outside the normal request handling, e.g. when initiating custom
* background threads.
* </p>
*
* @param portlet
* the Vaadin portlet to register as the current portlet
*
* @see #getCurrent()
* @see InheritableThreadLocal
*/
public static void setCurrent(VaadinPortlet portlet) {
CurrentInstance.setInheritable(VaadinPortlet.class, portlet);
VaadinService vaadinService = CurrentInstance.get(VaadinService.class);
if (vaadinService instanceof VaadinPortletService) {
VaadinPortletService vps = (VaadinPortletService) vaadinService;
return vps.getPortlet();
} else {
return null;
}
}

}

+ 18
- 27
server/src/com/vaadin/server/VaadinServlet.java Parādīt failu

@@ -63,7 +63,6 @@ public class VaadinServlet extends HttpServlet implements Constants {
public void init(javax.servlet.ServletConfig servletConfig)
throws ServletException {
CurrentInstance.clearAll();
setCurrent(this);
super.init(servletConfig);
Properties initParameters = new Properties();

@@ -108,36 +107,23 @@ public class VaadinServlet extends HttpServlet implements Constants {
* servlet is defined (see {@link InheritableThreadLocal}). In other cases,
* (e.g. from background threads started in some other way), the current
* servlet is not automatically defined.
* <p>
* The current servlet is derived from the current service using
* {@link VaadinService#getCurrent()}
*
* @return the current Vaadin servlet instance if available, otherwise
* <code>null</code>
*
* @see #setCurrent(VaadinServlet)
*
* @since 7.0
*/
public static VaadinServlet getCurrent() {
return CurrentInstance.get(VaadinServlet.class);
}

/**
* Sets the current Vaadin servlet. This method is used by the framework to
* set the current servlet whenever a new request is processed and it is
* cleared when the request has been processed.
* <p>
* The application developer can also use this method to define the current
* servlet outside the normal request handling, e.g. when initiating custom
* background threads.
* </p>
*
* @param servlet
* the Vaadin servlet to register as the current servlet
*
* @see #getCurrent()
* @see InheritableThreadLocal
*/
public static void setCurrent(VaadinServlet servlet) {
CurrentInstance.setInheritable(VaadinServlet.class, servlet);
VaadinService vaadinService = CurrentInstance.get(VaadinService.class);
if (vaadinService instanceof VaadinServletService) {
VaadinServletService vss = (VaadinServletService) vaadinService;
return vss.getServlet();
} else {
return null;
}
}

protected DeploymentConfiguration createDeploymentConfiguration(
@@ -179,7 +165,6 @@ public class VaadinServlet extends HttpServlet implements Constants {
return;
}
CurrentInstance.clearAll();
setCurrent(this);

VaadinServletRequest vaadinRequest = createVaadinRequest(request);
VaadinServletResponse vaadinResponse = createVaadinResponse(response);
@@ -188,8 +173,14 @@ public class VaadinServlet extends HttpServlet implements Constants {
}

if (isStaticResourceRequest(request)) {
serveStaticResources(request, response);
return;
// Define current servlet and service, but no request and response
getService().setCurrentInstances(null, null);
try {
serveStaticResources(request, response);
return;
} finally {
CurrentInstance.clearAll();
}
}
try {
getService().handleRequest(vaadinRequest, vaadinResponse);

+ 0
- 26
server/src/com/vaadin/util/CurrentInstance.java Parādīt failu

@@ -22,12 +22,10 @@ import java.util.Map;
import java.util.Map.Entry;

import com.vaadin.server.VaadinPortlet;
import com.vaadin.server.VaadinPortletService;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinResponse;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.VaadinServletService;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.UI;

@@ -52,18 +50,6 @@ public class CurrentInstance implements Serializable {
private final Object instance;
private final boolean inheritable;

private static boolean portletAvailable = false;
{
try {
/*
* VaadinPortlet depends on portlet API which is available only if
* running in a portal.
*/
portletAvailable = (VaadinPortlet.class.getName() != null);
} catch (Throwable t) {
}
}

private static InheritableThreadLocal<Map<Class<?>, CurrentInstance>> instances = new InheritableThreadLocal<Map<Class<?>, CurrentInstance>>() {
@Override
protected Map<Class<?>, CurrentInstance> childValue(
@@ -236,18 +222,6 @@ public class CurrentInstance implements Serializable {
VaadinSession.setCurrent(session);
VaadinService.setCurrent(service);

if (service instanceof VaadinServletService) {
old.put(VaadinServlet.class,
new CurrentInstance(VaadinServlet.getCurrent(), true));
VaadinServlet.setCurrent(((VaadinServletService) service)
.getServlet());
} else if (portletAvailable && service instanceof VaadinPortletService) {
old.put(VaadinPortlet.class,
new CurrentInstance(VaadinPortlet.getCurrent(), true));
VaadinPortlet.setCurrent(((VaadinPortletService) service)
.getPortlet());
}

return old;
}
}

Notiek ielāde…
Atcelt
Saglabāt