diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-11-22 18:57:44 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-11-23 11:38:54 +0000 |
commit | 1c400f042b32046a4becacfb856db4829bd0515d (patch) | |
tree | 2f6fb6b92d4d16f5bd636d0269e79e10caa81965 /server | |
parent | 62a43b95f0d0247fed0550180c706692a4a41a84 (diff) | |
download | vaadin-framework-1c400f042b32046a4becacfb856db4829bd0515d.tar.gz vaadin-framework-1c400f042b32046a4becacfb856db4829bd0515d.zip |
Add VaadinService.setClassLoader (#9819)
* Set default value in constructor
* Remove support for getting a null class loader
Change-Id: Ib2ac914e0832395bb25cb1cff1a9caffbe09631e
Diffstat (limited to 'server')
4 files changed, 64 insertions, 25 deletions
diff --git a/server/src/com/vaadin/server/DefaultUIProvider.java b/server/src/com/vaadin/server/DefaultUIProvider.java index 919f781d3d..530b56c2a3 100644 --- a/server/src/com/vaadin/server/DefaultUIProvider.java +++ b/server/src/com/vaadin/server/DefaultUIProvider.java @@ -34,9 +34,6 @@ public class DefaultUIProvider extends UIProvider { String uiClassName = uiClassNameObj.toString(); ClassLoader classLoader = request.getService().getClassLoader(); - if (classLoader == null) { - classLoader = getClass().getClassLoader(); - } try { Class<? extends UI> uiClass = Class.forName(uiClassName, true, classLoader).asSubclass(UI.class); diff --git a/server/src/com/vaadin/server/VaadinPortletService.java b/server/src/com/vaadin/server/VaadinPortletService.java index 38813ec212..bb0ad4ca22 100644 --- a/server/src/com/vaadin/server/VaadinPortletService.java +++ b/server/src/com/vaadin/server/VaadinPortletService.java @@ -34,6 +34,16 @@ public class VaadinPortletService extends VaadinService { DeploymentConfiguration deploymentConfiguration) { super(deploymentConfiguration); this.portlet = portlet; + + // Set default class loader if not already set + if (getClassLoader() == null) { + /* + * The portlet is most likely to be loaded with a class loader + * specific to the application instead of some generic system class + * loader that loads the Vaadin classes. + */ + setClassLoader(portlet.getClass().getClassLoader()); + } } protected VaadinPortlet getPortlet() { diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index 50fca54cf7..eaa5b51b30 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -75,6 +75,8 @@ public abstract class VaadinService implements Serializable { private SystemMessagesProvider systemMessagesProvider = DefaultSystemMessagesProvider .get(); + private ClassLoader classLoader; + /** * Creates a new vaadin service based on a deployment configuration * @@ -83,6 +85,23 @@ public abstract class VaadinService implements Serializable { */ public VaadinService(DeploymentConfiguration deploymentConfiguration) { this.deploymentConfiguration = deploymentConfiguration; + + final String classLoaderName = getDeploymentConfiguration() + .getApplicationOrSystemProperty("ClassLoader", null); + if (classLoaderName != null) { + try { + final Class<?> classLoaderClass = getClass().getClassLoader() + .loadClass(classLoaderName); + final Constructor<?> c = classLoaderClass + .getConstructor(new Class[] { ClassLoader.class }); + setClassLoader((ClassLoader) c + .newInstance(new Object[] { getClass().getClassLoader() })); + } catch (final Exception e) { + throw new RuntimeException( + "Could not find specified class loader: " + + classLoaderName, e); + } + } } /** @@ -134,36 +153,39 @@ public abstract class VaadinService implements Serializable { public abstract boolean isStandalone(VaadinRequest request); /** - * Get the class loader to use for loading classes loaded by name, e.g. - * custom UI classes. <code>null</code> indicates that the default class - * loader should be used. + * Gets the class loader to use for loading classes loaded by name, e.g. + * custom UI classes. This is by default the class loader that was used to + * load the Servlet or Portlet class to which this service belongs. * * @return the class loader to use, or <code>null</code> + * + * @see #setClassLoader(ClassLoader) */ public ClassLoader getClassLoader() { - final String classLoaderName = getDeploymentConfiguration() - .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; } /** + * Sets the class loader to use for loading classes loaded by name, e.g. + * custom UI classes. Invokers of this method should be careful to not break + * any existing class loader hierarchy, e.g. by ensuring that a class loader + * set for this service delegates to the previously set class loader if the + * class is not found. + * + * @param classLoader + * the new class loader to set, not <code>null</code>. + * + * @see #getClassLoader() + */ + public void setClassLoader(ClassLoader classLoader) { + if (classLoader == null) { + throw new IllegalArgumentException( + "Can not set class loader to null"); + } + this.classLoader = classLoader; + } + + /** * Returns the MIME type of the specified file, or null if the MIME type is * not known. The MIME type is determined by the configuration of the * container, and may be specified in a deployment descriptor. Common MIME diff --git a/server/src/com/vaadin/server/VaadinServletService.java b/server/src/com/vaadin/server/VaadinServletService.java index aaa2ce327e..7a15c0cc19 100644 --- a/server/src/com/vaadin/server/VaadinServletService.java +++ b/server/src/com/vaadin/server/VaadinServletService.java @@ -32,6 +32,16 @@ public class VaadinServletService extends VaadinService { DeploymentConfiguration deploymentConfiguration) { super(deploymentConfiguration); this.servlet = servlet; + + // Set default class loader if not already set + if (getClassLoader() == null) { + /* + * The servlet is most likely to be loaded with a class loader + * specific to the application instead of some generic system class + * loader that loads the Vaadin classes. + */ + setClassLoader(servlet.getClass().getClassLoader()); + } } protected VaadinServlet getServlet() { |