This is an automated email from the ASF dual-hosted git repository.

fhanik pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new 7cd2aaa  Add in an ability to configure a custom class loader
7cd2aaa is described below

commit 7cd2aaacdbf20879b04e4ec57a4dbfa6adee2636
Author: Filip Hanik <fha...@pivotal.io>
AuthorDate: Wed Aug 12 10:46:09 2020 -0700

    Add in an ability to configure a custom class loader
    
    without using reflection
    beneficial for programmatic usage
    and building native images using GraalVM
---
 java/org/apache/catalina/loader/WebappLoader.java  | 15 ++++++++++++
 .../catalina/loader/TestVirtualWebappLoader.java   | 27 ++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/java/org/apache/catalina/loader/WebappLoader.java 
b/java/org/apache/catalina/loader/WebappLoader.java
index 3c3026b..38292be 100644
--- a/java/org/apache/catalina/loader/WebappLoader.java
+++ b/java/org/apache/catalina/loader/WebappLoader.java
@@ -189,6 +189,17 @@ public class WebappLoader extends LifecycleMBeanBase 
implements Loader{
         this.loaderClass = loaderClass;
     }
 
+    /**
+     * Set the ClassLoader instance, without relying on reflection
+     * This method will also invoke {@link #setLoaderClass(String)} with
+     * {@code loaderInstance.getClass().getName()} as an argument
+     *
+     * @param loaderInstance The new ClassLoader instance to use
+     */
+    public void setLoaderInstance(WebappClassLoaderBase loaderInstance) {
+        this.classLoader = loaderInstance;
+        this.loaderClass = loaderInstance.getClass().getName();
+    }
 
     // --------------------------------------------------------- Public Methods
 
@@ -398,6 +409,10 @@ public class WebappLoader extends LifecycleMBeanBase 
implements Loader{
     private WebappClassLoaderBase createClassLoader()
         throws Exception {
 
+        if (classLoader != null) {
+            return classLoader;
+        }
+
         if (ParallelWebappClassLoader.class.getName().equals(loaderClass)) {
             return new 
ParallelWebappClassLoader(context.getParentClassLoader());
         }
diff --git a/test/org/apache/catalina/loader/TestVirtualWebappLoader.java 
b/test/org/apache/catalina/loader/TestVirtualWebappLoader.java
index afcd8a3..11d840e 100644
--- a/test/org/apache/catalina/loader/TestVirtualWebappLoader.java
+++ b/test/org/apache/catalina/loader/TestVirtualWebappLoader.java
@@ -37,6 +37,33 @@ public class TestVirtualWebappLoader extends TomcatBaseTest {
     }
 
     @Test
+    public void testLoaderInstance() throws Exception {
+        WebappLoader loader = new WebappLoader();
+        Assert.assertNull(loader.getClassLoader());
+        WebappClassLoader cl = new WebappClassLoader();
+        loader.setLoaderInstance(cl);
+        Assert.assertSame(cl, loader.getClassLoader());
+        Assert.assertEquals(WebappClassLoader.class.getName(), 
loader.getLoaderClass());
+
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File("test/webapp");
+        StandardContext ctx = (StandardContext) tomcat.addContext("",
+                appDir.getAbsolutePath());
+
+        loader.setContext(ctx);
+        ctx.setLoader(loader);
+
+
+        loader.start();
+        Assert.assertSame(cl, loader.getClassLoader());
+        Assert.assertEquals(WebappClassLoader.class.getName(), 
loader.getLoaderClass());
+        loader.stop();
+        Assert.assertNull(loader.getClassLoader());
+        Assert.assertEquals(WebappClassLoader.class.getName(), 
loader.getLoaderClass());
+    }
+
+    @Test
     public void testStartInternal() throws Exception {
         Tomcat tomcat = getTomcatInstance();
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to