]> source.dussan.org Git - vaadin-framework.git/commitdiff
Move common code to AbstractDeploymentConfiguration (#8574)
authorLeif Åstrand <leif@vaadin.com>
Tue, 31 Jul 2012 08:50:29 +0000 (11:50 +0300)
committerLeif Åstrand <leif@vaadin.com>
Tue, 31 Jul 2012 08:50:29 +0000 (11:50 +0300)
src/com/vaadin/terminal/DeploymentConfiguration.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/server/ApplicationPortlet2.java
src/com/vaadin/terminal/gwt/server/ApplicationServlet.java
src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java
tests/testbench/com/vaadin/launcher/ApplicationRunnerServlet.java

index 5d622cac20fdc6e22ae497af657367e80bd0f656..ddb42349d8add86c7d18b182a00a96c8a7c79632 100644 (file)
@@ -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();
 }
index d6285008a5b48100bd544b6a8c95418dbc042fc6..363f7e486933a5bde1fb7e365178eb1a29ecb47d 100644 (file)
@@ -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
      * 
index c3e85fa50bf89cd0f3c13bf7475179b111ecebf7..a0e807801e44eb0035dee0e947d85c61560e75a5 100644 (file)
@@ -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 (file)
index 0000000..7402f8f
--- /dev/null
@@ -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;
+    }
+ }
index 7a46a07e6c0f17f6ab0931ed0766b9c99d08af7d..788c48267e50d52dacedee6c6e48e33826fb9495 100644 (file)
@@ -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);
         }
index 2c4d38ef2454bd9839a7161004d02f94dc9d4df1..1af49e0da058f4adadddf9fe6c14797d4f1651f8 100644 (file)
@@ -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);
         }
index 91d9371d6a9954d529f5410a46eaace5fb48e025..b2b962b0fd96f8fe4d78340b05b5329da8fd691d 100644 (file)
@@ -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<? extends Application> 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
index dd93b1a8b64c776ec6dfaa7cf4534d812b2a5382..b2148c8f0835e5ab6be6a13b681b1e864cfbe0bd 100644 (file)
@@ -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();