From 569a82a7bfc4958a9ac164e16caecc3fb47fdbc7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 31 Jul 2012 11:50:29 +0300 Subject: [PATCH] Move common code to AbstractDeploymentConfiguration (#8574) --- .../terminal/DeploymentConfiguration.java | 9 ++ .../server/AbstractApplicationPortlet.java | 121 ++------------ .../server/AbstractApplicationServlet.java | 152 ++---------------- .../AbstractDeploymentConfiguration.java | 128 +++++++++++++++ .../gwt/server/ApplicationPortlet2.java | 6 +- .../gwt/server/ApplicationServlet.java | 10 +- .../gwt/server/ServletPortletHelper.java | 12 +- .../launcher/ApplicationRunnerServlet.java | 3 +- 8 files changed, 184 insertions(+), 257 deletions(-) create mode 100644 src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java diff --git a/src/com/vaadin/terminal/DeploymentConfiguration.java b/src/com/vaadin/terminal/DeploymentConfiguration.java index 5d622cac20..ddb42349d8 100644 --- a/src/com/vaadin/terminal/DeploymentConfiguration.java +++ b/src/com/vaadin/terminal/DeploymentConfiguration.java @@ -5,6 +5,7 @@ package com.vaadin.terminal; import java.io.Serializable; +import java.util.Properties; import javax.portlet.PortletContext; import javax.servlet.ServletContext; @@ -101,4 +102,12 @@ public interface DeploymentConfiguration extends Serializable { * @see PortletContext#getMimeType(String) */ public String getMimeType(String resourceName); + + /** + * Gets the properties configured for the deployment, e.g. as init + * parameters to the servlet or portlet. + * + * @return properties for the application. + */ + public Properties getInitParameters(); } diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java index d6285008a5..363f7e4869 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java @@ -205,12 +205,10 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet // TODO Can we close the application when the portlet is removed? Do we know // when the portlet is removed? - private Properties applicationProperties; - private boolean productionMode = false; - private DeploymentConfiguration deploymentConfiguration = new DeploymentConfiguration() { - + private DeploymentConfiguration deploymentConfiguration = new AbstractDeploymentConfiguration( + getClass()) { @Override public String getConfiguredWidgetset(WrappedRequest request) { @@ -218,7 +216,8 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet PARAMETER_WIDGETSET, null); if (widgetset == null) { - // If no widgetset defined for the application, check the portal + // If no widgetset defined for the application, check the + // portal // property widgetset = WrappedPortletRequest.cast(request) .getPortalProperty(PORTAL_PARAMETER_VAADIN_WIDGETSET); @@ -247,13 +246,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet return themeName; } - @Override - public String getApplicationOrSystemProperty(String propertyName, - String defaultValue) { - return AbstractApplicationPortlet.this - .getApplicationOrSystemProperty(propertyName, defaultValue); - } - @Override public boolean isStandalone(WrappedRequest request) { return false; @@ -292,13 +284,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet } } - @Override - public ClassLoader getClassLoader() { - // Custom class loaders not currently supported in portlets (see - // #8574) - return null; - } - @Override public String getMimeType(String resourceName) { return getPortletContext().getMimeType(resourceName); @@ -308,7 +293,8 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet @Override public void init(PortletConfig config) throws PortletException { super.init(config); - applicationProperties = new Properties(); + Properties applicationProperties = getDeploymentConfiguration() + .getInitParameters(); // Read default parameters from the context final PortletContext context = config.getPortletContext(); @@ -332,7 +318,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet } private void checkCrossSiteProtection() { - if (getApplicationOrSystemProperty( + if (getDeploymentConfiguration().getApplicationOrSystemProperty( SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION, "false").equals( "true")) { /* @@ -347,8 +333,8 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet // TODO Identical code in AbstractApplicationServlet -> refactor // Check if the application is in production mode. // We are in production mode if productionMode=true - if (getApplicationOrSystemProperty(SERVLET_PARAMETER_PRODUCTION_MODE, - "false").equals("true")) { + if (getDeploymentConfiguration().getApplicationOrSystemProperty( + SERVLET_PARAMETER_PRODUCTION_MODE, "false").equals("true")) { productionMode = true; } @@ -359,85 +345,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet } } - /** - * Gets an application property value. - * - * @param parameterName - * the Name or the parameter. - * @return String value or null if not found - */ - protected String getApplicationProperty(String parameterName) { - - String val = applicationProperties.getProperty(parameterName); - if (val != null) { - return val; - } - - // Try lower case application properties for backward compatibility with - // 3.0.2 and earlier - val = applicationProperties.getProperty(parameterName.toLowerCase()); - - return val; - } - - /** - * Gets an system property value. - * - * @param parameterName - * the Name or the parameter. - * @return String value or null if not found - */ - protected String getSystemProperty(String parameterName) { - String val = null; - - String pkgName; - final Package pkg = getClass().getPackage(); - if (pkg != null) { - pkgName = pkg.getName(); - } else { - final String className = getClass().getName(); - pkgName = new String(className.toCharArray(), 0, - className.lastIndexOf('.')); - } - val = System.getProperty(pkgName + "." + parameterName); - if (val != null) { - return val; - } - - // Try lowercased system properties - val = System.getProperty(pkgName + "." + parameterName.toLowerCase()); - return val; - } - - /** - * Gets an application or system property value. - * - * @param parameterName - * the Name or the parameter. - * @param defaultValue - * the Default to be used. - * @return String value or default if not found - */ - protected String getApplicationOrSystemProperty(String parameterName, - String defaultValue) { - - String val = null; - - // Try application properties - val = getApplicationProperty(parameterName); - if (val != null) { - return val; - } - - // Try system properties - val = getSystemProperty(parameterName); - if (val != null) { - return val; - } - - return defaultValue; - } - protected enum RequestType { FILE_UPLOAD, UIDL, RENDER, STATIC_FILE, APPLICATION_RESOURCE, DUMMY, EVENT, ACTION, UNKNOWN, BROWSER_DETAILS, CONNECTOR_RESOURCE; } @@ -736,7 +643,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet } - private DeploymentConfiguration getDeploymentConfiguration() { + protected DeploymentConfiguration getDeploymentConfiguration() { return deploymentConfiguration; } @@ -880,7 +787,8 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet application.setLocale(locale); // No application URL when running inside a portlet application.start(new ApplicationStartEvent(null, - applicationProperties, context, isProductionMode())); + getDeploymentConfiguration().getInitParameters(), + context, isProductionMode())); } } @@ -1004,11 +912,6 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet } } - protected ClassLoader getClassLoader() throws PortletException { - // TODO Add support for custom class loader - return getClass().getClassLoader(); - } - /** * Get system messages from the current application class * diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java index c3e85fa50b..a0e807801e 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java @@ -10,7 +10,6 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Serializable; -import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; @@ -88,15 +87,14 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements // TODO Move some (all?) of the constants to a separate interface (shared // with portlet) - private Properties applicationProperties; - private boolean productionMode = false; private final String resourcePath = null; private int resourceCacheTime = 3600; - private DeploymentConfiguration deploymentConfiguration = new DeploymentConfiguration() { + private DeploymentConfiguration deploymentConfiguration = new AbstractDeploymentConfiguration( + getClass()) { @Override public String getStaticFileLocation(WrappedRequest request) { @@ -119,27 +117,11 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements return AbstractApplicationServlet.getDefaultTheme(); } - @Override - public String getApplicationOrSystemProperty(String propertyName, - String defaultValue) { - return AbstractApplicationServlet.this - .getApplicationOrSystemProperty(propertyName, defaultValue); - } - @Override public boolean isStandalone(WrappedRequest request) { return true; } - @Override - public ClassLoader getClassLoader() { - try { - return AbstractApplicationServlet.this.getClassLoader(); - } catch (ServletException e) { - throw new RuntimeException(e); - } - } - @Override public String getMimeType(String resourceName) { return getServletContext().getMimeType(resourceName); @@ -158,11 +140,11 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements * servlet's normal operation. */ @Override - @SuppressWarnings("unchecked") public void init(javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException { super.init(servletConfig); - applicationProperties = new Properties(); + Properties applicationProperties = getDeploymentConfiguration() + .getInitParameters(); // Read default parameters from server.xml final ServletContext context = servletConfig.getServletContext(); @@ -187,7 +169,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements } private void checkCrossSiteProtection() { - if (getApplicationOrSystemProperty( + if (getDeploymentConfiguration().getApplicationOrSystemProperty( SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION, "false").equals( "true")) { /* @@ -201,8 +183,8 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements private void checkProductionMode() { // Check if the application is in production mode. // We are in production mode if productionMode=true - if (getApplicationOrSystemProperty(SERVLET_PARAMETER_PRODUCTION_MODE, - "false").equals("true")) { + if (getDeploymentConfiguration().getApplicationOrSystemProperty( + SERVLET_PARAMETER_PRODUCTION_MODE, "false").equals("true")) { productionMode = true; } @@ -216,8 +198,9 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements private void checkResourceCacheTime() { // Check if the browser caching time has been set in web.xml try { - String rct = getApplicationOrSystemProperty( - SERVLET_PARAMETER_RESOURCE_CACHE_TIME, "3600"); + String rct = getDeploymentConfiguration() + .getApplicationOrSystemProperty( + SERVLET_PARAMETER_RESOURCE_CACHE_TIME, "3600"); resourceCacheTime = Integer.parseInt(rct); } catch (NumberFormatException nfe) { // Default is 1h @@ -226,85 +209,6 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements } } - /** - * Gets an application property value. - * - * @param parameterName - * the Name or the parameter. - * @return String value or null if not found - */ - protected String getApplicationProperty(String parameterName) { - - String val = applicationProperties.getProperty(parameterName); - if (val != null) { - return val; - } - - // Try lower case application properties for backward compatibility with - // 3.0.2 and earlier - val = applicationProperties.getProperty(parameterName.toLowerCase()); - - return val; - } - - /** - * Gets an system property value. - * - * @param parameterName - * the Name or the parameter. - * @return String value or null if not found - */ - protected String getSystemProperty(String parameterName) { - String val = null; - - String pkgName; - final Package pkg = getClass().getPackage(); - if (pkg != null) { - pkgName = pkg.getName(); - } else { - final String className = getClass().getName(); - pkgName = new String(className.toCharArray(), 0, - className.lastIndexOf('.')); - } - val = System.getProperty(pkgName + "." + parameterName); - if (val != null) { - return val; - } - - // Try lowercased system properties - val = System.getProperty(pkgName + "." + parameterName.toLowerCase()); - return val; - } - - /** - * Gets an application or system property value. - * - * @param parameterName - * the Name or the parameter. - * @param defaultValue - * the Default to be used. - * @return String value or default if not found - */ - String getApplicationOrSystemProperty(String parameterName, - String defaultValue) { - - String val = null; - - // Try application properties - val = getApplicationProperty(parameterName); - if (val != null) { - return val; - } - - // Try system properties - val = getSystemProperty(parameterName); - if (val != null) { - return val; - } - - return defaultValue; - } - /** * Returns true if the servlet is running in production mode. Production * mode disables all debug facilities. @@ -567,30 +471,6 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements return true; } - protected ClassLoader getClassLoader() throws ServletException { - // Gets custom class loader - final String classLoaderName = getApplicationOrSystemProperty( - "ClassLoader", null); - ClassLoader classLoader; - if (classLoaderName == null) { - classLoader = getClass().getClassLoader(); - } else { - try { - final Class classLoaderClass = getClass().getClassLoader() - .loadClass(classLoaderName); - final Constructor c = classLoaderClass - .getConstructor(new Class[] { ClassLoader.class }); - classLoader = (ClassLoader) c - .newInstance(new Object[] { getClass().getClassLoader() }); - } catch (final Exception e) { - throw new ServletException( - "Could not find specified class loader: " - + classLoaderName, e); - } - } - return classLoader; - } - /** * Send a notification to client's application. Used to notify client of * critical errors, session expiration and more. Server has no knowledge of @@ -1007,8 +887,8 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements Locale locale = request.getLocale(); application.setLocale(locale); application.start(new ApplicationStartEvent(applicationUrl, - applicationProperties, webApplicationContext, - isProductionMode())); + getDeploymentConfiguration().getInitParameters(), + webApplicationContext, isProductionMode())); } } @@ -1070,7 +950,8 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements // strip leading "/" otherwise stream from JAR wont work filename = filename.substring(1); - resourceUrl = getClassLoader().getResource(filename); + resourceUrl = getDeploymentConfiguration().getClassLoader() + .getResource(filename); if (resourceUrl == null) { // cannot serve requested file @@ -1387,8 +1268,9 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements HttpServletRequest request) { String staticFileLocation; // if property is defined in configurations, use that - staticFileLocation = getApplicationOrSystemProperty( - PARAMETER_VAADIN_RESOURCES, null); + staticFileLocation = getDeploymentConfiguration() + .getApplicationOrSystemProperty(PARAMETER_VAADIN_RESOURCES, + null); if (staticFileLocation != null) { return staticFileLocation; } diff --git a/src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java b/src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java new file mode 100644 index 0000000000..7402f8fec3 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java @@ -0,0 +1,128 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.server; + +import java.lang.reflect.Constructor; +import java.util.Properties; + +import com.vaadin.terminal.DeploymentConfiguration; +import com.vaadin.terminal.WrappedRequest; + +public abstract class AbstractDeploymentConfiguration implements + DeploymentConfiguration { + + private final Class systemPropertyBaseClass; + private final Properties applicationProperties = new Properties(); + + public AbstractDeploymentConfiguration(Class systemPropertyBaseClass) { + this.systemPropertyBaseClass = systemPropertyBaseClass; + } + + @Override + public boolean isStandalone(WrappedRequest request) { + // TODO Auto-generated method stub + return false; + } + + @Override + public String getApplicationOrSystemProperty(String propertyName, + String defaultValue) { + + String val = null; + + // Try application properties + val = getApplicationProperty(propertyName); + if (val != null) { + return val; + } + + // Try system properties + val = getSystemProperty(propertyName); + if (val != null) { + return val; + } + + return defaultValue; + } + + /** + * Gets an system property value. + * + * @param parameterName + * the Name or the parameter. + * @return String value or null if not found + */ + protected String getSystemProperty(String parameterName) { + String val = null; + + String pkgName; + final Package pkg = systemPropertyBaseClass.getPackage(); + if (pkg != null) { + pkgName = pkg.getName(); + } else { + final String className = systemPropertyBaseClass.getName(); + pkgName = new String(className.toCharArray(), 0, + className.lastIndexOf('.')); + } + val = System.getProperty(pkgName + "." + parameterName); + if (val != null) { + return val; + } + + // Try lowercased system properties + val = System.getProperty(pkgName + "." + parameterName.toLowerCase()); + return val; + } + + @Override + public ClassLoader getClassLoader() { + final String classLoaderName = getApplicationOrSystemProperty( + "ClassLoader", null); + ClassLoader classLoader; + if (classLoaderName == null) { + classLoader = getClass().getClassLoader(); + } else { + try { + final Class classLoaderClass = getClass().getClassLoader() + .loadClass(classLoaderName); + final Constructor c = classLoaderClass + .getConstructor(new Class[] { ClassLoader.class }); + classLoader = (ClassLoader) c + .newInstance(new Object[] { getClass().getClassLoader() }); + } catch (final Exception e) { + throw new RuntimeException( + "Could not find specified class loader: " + + classLoaderName, e); + } + } + return classLoader; + } + + /** + * Gets an application property value. + * + * @param parameterName + * the Name or the parameter. + * @return String value or null if not found + */ + protected String getApplicationProperty(String parameterName) { + + String val = applicationProperties.getProperty(parameterName); + if (val != null) { + return val; + } + + // Try lower case application properties for backward compatibility with + // 3.0.2 and earlier + val = applicationProperties.getProperty(parameterName.toLowerCase()); + + return val; + } + + @Override + public Properties getInitParameters() { + return applicationProperties; + } + } diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationPortlet2.java b/src/com/vaadin/terminal/gwt/server/ApplicationPortlet2.java index 7a46a07e6c..788c48267e 100644 --- a/src/com/vaadin/terminal/gwt/server/ApplicationPortlet2.java +++ b/src/com/vaadin/terminal/gwt/server/ApplicationPortlet2.java @@ -23,10 +23,8 @@ public class ApplicationPortlet2 extends AbstractApplicationPortlet { public void init(PortletConfig config) throws PortletException { super.init(config); try { - applicationClass = ServletPortletHelper.getApplicationClass( - config.getInitParameter("application"), - config.getInitParameter(Application.ROOT_PARAMETER), - getClassLoader()); + applicationClass = ServletPortletHelper + .getApplicationClass(getDeploymentConfiguration()); } catch (ApplicationClassException e) { throw new PortletException(e); } diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java index 2c4d38ef24..1af49e0da0 100644 --- a/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/ApplicationServlet.java @@ -41,14 +41,12 @@ public class ApplicationServlet extends AbstractApplicationServlet { throws javax.servlet.ServletException { super.init(servletConfig); - // Loads the application class using the same class loader - // as the servlet itself + // Loads the application class using the classloader defined in the + // deployment configuration try { - applicationClass = ServletPortletHelper.getApplicationClass( - servletConfig.getInitParameter("application"), - servletConfig.getInitParameter(Application.ROOT_PARAMETER), - getClassLoader()); + applicationClass = ServletPortletHelper + .getApplicationClass(getDeploymentConfiguration()); } catch (ApplicationClassException e) { throw new ServletException(e); } diff --git a/src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java b/src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java index 91d9371d6a..b2b962b0fd 100644 --- a/src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java +++ b/src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java @@ -3,6 +3,7 @@ package com.vaadin.terminal.gwt.server; import java.io.Serializable; import com.vaadin.Application; +import com.vaadin.terminal.DeploymentConfiguration; import com.vaadin.terminal.WrappedRequest; import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.ui.Root; @@ -26,8 +27,15 @@ class ServletPortletHelper implements Serializable { } static Class getApplicationClass( - String applicationParameter, String rootParameter, - ClassLoader classLoader) throws ApplicationClassException { + DeploymentConfiguration deploymentConfiguration) + throws ApplicationClassException { + String applicationParameter = deploymentConfiguration + .getInitParameters().getProperty("application"); + String rootParameter = deploymentConfiguration + .getInitParameters().getProperty( + Application.ROOT_PARAMETER); + ClassLoader classLoader = deploymentConfiguration.getClassLoader(); + if (applicationParameter == null) { // Validate the parameter value diff --git a/tests/testbench/com/vaadin/launcher/ApplicationRunnerServlet.java b/tests/testbench/com/vaadin/launcher/ApplicationRunnerServlet.java index dd93b1a8b6..b2148c8f08 100644 --- a/tests/testbench/com/vaadin/launcher/ApplicationRunnerServlet.java +++ b/tests/testbench/com/vaadin/launcher/ApplicationRunnerServlet.java @@ -61,7 +61,8 @@ public class ApplicationRunnerServlet extends AbstractApplicationServlet { Collections.addAll(defaultPackages, initParameter.split(",")); } String str = TestBase.class.getName().replace('.', '/') + ".class"; - URL url = getClassLoader().getResource(str); + URL url = getDeploymentConfiguration().getClassLoader() + .getResource(str); if ("file".equals(url.getProtocol())) { File comVaadinTests = new File(url.getPath()).getParentFile() .getParentFile(); -- 2.39.5