Browse Source

Move common code to AbstractDeploymentConfiguration (#8574)

tags/7.0.0.beta1
Leif Åstrand 12 years ago
parent
commit
569a82a7bf

+ 9
- 0
src/com/vaadin/terminal/DeploymentConfiguration.java View 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();
}

+ 12
- 109
src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java View 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
*

+ 17
- 135
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java View 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;
}

+ 128
- 0
src/com/vaadin/terminal/gwt/server/AbstractDeploymentConfiguration.java View File

@@ -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;
}
}

+ 2
- 4
src/com/vaadin/terminal/gwt/server/ApplicationPortlet2.java View 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);
}

+ 4
- 6
src/com/vaadin/terminal/gwt/server/ApplicationServlet.java View 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);
}

+ 10
- 2
src/com/vaadin/terminal/gwt/server/ServletPortletHelper.java View 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

+ 2
- 1
tests/testbench/com/vaadin/launcher/ApplicationRunnerServlet.java View 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();

Loading…
Cancel
Save