From 2d2418516ab34bc90de1f910dfa2e0c64f093431 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 5 Oct 2015 16:41:40 +0300 Subject: [PATCH] Detect Atmosphere in the same way for servlets and portlets (#11493) Change-Id: I6c153291045a35f253cd7e6987d7eb90dbc93a1e --- .../src/com/vaadin/server/VaadinService.java | 45 ++++++++++++++++--- .../vaadin/server/VaadinServletService.java | 43 +----------------- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index 8a6be63dd6..09704838b6 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -52,6 +52,7 @@ import com.vaadin.annotations.PreserveOnRefresh; import com.vaadin.event.EventRouter; import com.vaadin.server.VaadinSession.FutureAccess; import com.vaadin.server.VaadinSession.State; +import com.vaadin.server.communication.AtmospherePushConnection; import com.vaadin.server.communication.FileUploadHandler; import com.vaadin.server.communication.HeartbeatHandler; import com.vaadin.server.communication.PublishedFileHandler; @@ -133,6 +134,8 @@ public abstract class VaadinService implements Serializable { private Iterable requestHandlers; + private boolean atmosphereAvailable = checkAtmosphereSupport(); + /** * Keeps track of whether a warning about missing push support has already * been logged. This is used to avoid spamming the log with the same message @@ -1625,13 +1628,43 @@ public abstract class VaadinService implements Serializable { * is not available. */ public boolean ensurePushAvailable() { - if (!pushWarningEmitted) { - pushWarningEmitted = true; - getLogger().log(Level.WARNING, Constants.PUSH_NOT_SUPPORTED_ERROR, - getClass().getSimpleName()); + if (atmosphereAvailable) { + return true; + } else { + if (!pushWarningEmitted) { + pushWarningEmitted = true; + getLogger().log(Level.WARNING, + Constants.ATMOSPHERE_MISSING_ERROR); + } + return false; } - // Not supported by default for now, sublcasses may override - return false; + } + + private static boolean checkAtmosphereSupport() { + String rawVersion = AtmospherePushConnection.getAtmosphereVersion(); + if (rawVersion == null) { + return false; + } + + if (!Constants.REQUIRED_ATMOSPHERE_RUNTIME_VERSION.equals(rawVersion)) { + getLogger().log( + Level.WARNING, + Constants.INVALID_ATMOSPHERE_VERSION_WARNING, + new Object[] { + Constants.REQUIRED_ATMOSPHERE_RUNTIME_VERSION, + rawVersion }); + } + return true; + } + + /** + * Checks whether Atmosphere is avilable for use + * + * @since + * @return true if Atmosphere is available, false otherwise + */ + protected boolean isAtmosphereAvailable() { + return atmosphereAvailable; } /** diff --git a/server/src/com/vaadin/server/VaadinServletService.java b/server/src/com/vaadin/server/VaadinServletService.java index 22848c023c..5faea1ce6c 100644 --- a/server/src/com/vaadin/server/VaadinServletService.java +++ b/server/src/com/vaadin/server/VaadinServletService.java @@ -27,7 +27,6 @@ import java.util.logging.Logger; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; -import com.vaadin.server.communication.AtmospherePushConnection; import com.vaadin.server.communication.PushRequestHandler; import com.vaadin.server.communication.ServletBootstrapHandler; import com.vaadin.server.communication.ServletUIInitHandler; @@ -36,15 +35,6 @@ import com.vaadin.ui.UI; public class VaadinServletService extends VaadinService { private final VaadinServlet servlet; - private boolean atmosphereAvailable = checkAtmosphereSupport(); - - /** - * Keeps track of whether a warning about missing push support has already - * been logged. This is used to avoid spamming the log with the same message - * every time a new UI is bootstrapped. - */ - private boolean pushWarningLogged = false; - public VaadinServletService(VaadinServlet servlet, DeploymentConfiguration deploymentConfiguration) throws ServiceException { @@ -52,30 +42,13 @@ public class VaadinServletService extends VaadinService { this.servlet = servlet; } - private static boolean checkAtmosphereSupport() { - String rawVersion = AtmospherePushConnection.getAtmosphereVersion(); - if (rawVersion == null) { - return false; - } - - if (!Constants.REQUIRED_ATMOSPHERE_RUNTIME_VERSION.equals(rawVersion)) { - getLogger().log( - Level.WARNING, - Constants.INVALID_ATMOSPHERE_VERSION_WARNING, - new Object[] { - Constants.REQUIRED_ATMOSPHERE_RUNTIME_VERSION, - rawVersion }); - } - return true; - } - @Override protected List createRequestHandlers() throws ServiceException { List handlers = super.createRequestHandlers(); handlers.add(0, new ServletBootstrapHandler()); handlers.add(new ServletUIInitHandler()); - if (atmosphereAvailable) { + if (isAtmosphereAvailable()) { try { handlers.add(new PushRequestHandler(this)); } catch (ServiceException e) { @@ -86,7 +59,6 @@ public class VaadinServletService extends VaadinService { .log(Level.WARNING, "Error initializing Atmosphere. Push will not work.", e); - atmosphereAvailable = false; } } return handlers; @@ -271,17 +243,4 @@ public class VaadinServletService extends VaadinService { return Logger.getLogger(VaadinServletService.class.getName()); } - @Override - public boolean ensurePushAvailable() { - if (atmosphereAvailable) { - return true; - } else { - if (!pushWarningLogged) { - pushWarningLogged = true; - getLogger().log(Level.WARNING, - Constants.ATMOSPHERE_MISSING_ERROR); - } - return false; - } - } } -- 2.39.5