Browse Source

Add VaadinService.setClassLoader (#9819)

* Set default value in constructor
* Remove support for getting a null class loader

Change-Id: Ib2ac914e0832395bb25cb1cff1a9caffbe09631e
tags/7.0.0.beta10
Leif Åstrand 11 years ago
parent
commit
1c400f042b

+ 0
- 3
server/src/com/vaadin/server/DefaultUIProvider.java View File

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

+ 10
- 0
server/src/com/vaadin/server/VaadinPortletService.java View File

@@ -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() {

+ 44
- 22
server/src/com/vaadin/server/VaadinService.java View File

@@ -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,35 +153,38 @@ 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

+ 10
- 0
server/src/com/vaadin/server/VaadinServletService.java View File

@@ -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() {

Loading…
Cancel
Save