scripting/java/com/sun/star/script/framework/provider/ClassLoaderFactory.java  
       |    4 
 
scripting/java/com/sun/star/script/framework/provider/java/ScriptProviderForJava.java
 |   91 ++++------
 2 files changed, 43 insertions(+), 52 deletions(-)

New commits:
commit 2a263cbdaf16c723a93af020ebc11b3e07210242
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Tue May 31 09:27:20 2022 +0200
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Tue May 31 14:51:57 2022 +0200

    tdf#145527 Revert "Related tdf#116767: Call URLClassLoader.close"
    
    This reverts commit 418533f0af7cd303d559c8fb136c49e7e9fb0d79.  Turned out 
that
    closing the given URLClassLoader once the script's main class has been 
loaded
    prevents the script from loading further classes located next to that main
    class.  So abandon the Coverity suggestion that such class loaders should be
    closed promptly to avoid resource leaks.
    
    Change-Id: I1bcf83117836cfe317eac9850d413f2c3767bd8e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135158
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git 
a/scripting/java/com/sun/star/script/framework/provider/ClassLoaderFactory.java 
b/scripting/java/com/sun/star/script/framework/provider/ClassLoaderFactory.java
index 0974b8874110..b5e6e3085bd2 100644
--- 
a/scripting/java/com/sun/star/script/framework/provider/ClassLoaderFactory.java
+++ 
b/scripting/java/com/sun/star/script/framework/provider/ClassLoaderFactory.java
@@ -33,7 +33,7 @@ public class ClassLoaderFactory {
 
     private ClassLoaderFactory() {}
 
-    public static URLClassLoader getURLClassLoader(ScriptMetaData scriptData) {
+    public static ClassLoader getURLClassLoader(ScriptMetaData scriptData) {
         ClassLoader parent = scriptData.getClass().getClassLoader();
         URL[] classPath = scriptData.getClassPath();
         LogUtils.DEBUG("Classpath has length " + classPath.length);
@@ -45,7 +45,7 @@ public class ClassLoaderFactory {
         return getURLClassLoader(parent, classPath);
     }
 
-    public static URLClassLoader getURLClassLoader(final ClassLoader parent,
+    public static ClassLoader getURLClassLoader(final ClassLoader parent,
             final URL[] classpath) {
         return AccessController.doPrivileged(
             new PrivilegedAction<URLClassLoader>() {
diff --git 
a/scripting/java/com/sun/star/script/framework/provider/java/ScriptProviderForJava.java
 
b/scripting/java/com/sun/star/script/framework/provider/java/ScriptProviderForJava.java
index 63dc3169f759..eeea0d49ebe5 100644
--- 
a/scripting/java/com/sun/star/script/framework/provider/java/ScriptProviderForJava.java
+++ 
b/scripting/java/com/sun/star/script/framework/provider/java/ScriptProviderForJava.java
@@ -45,8 +45,6 @@ import com.sun.star.uno.Any;
 import com.sun.star.uno.Type;
 import com.sun.star.uno.XComponentContext;
 
-import java.io.IOException;
-import java.net.URLClassLoader;
 import java.util.ArrayList;
 
 public class ScriptProviderForJava {
@@ -184,63 +182,56 @@ class ScriptImpl implements XScript {
         ScriptDescriptor scriptDesc =
             new ScriptDescriptor(metaData.getLanguageName());
 
+        LogUtils.DEBUG("Classloader starting...");
+
+        ClassLoader scriptLoader =
+            ClassLoaderFactory.getURLClassLoader(metaData);
+
+        LogUtils.DEBUG("Classloader finished...");
+
+        ArrayList<Object> invocationArgList = new ArrayList<Object>();
         Object[] invocationArgs = null;
-        ScriptProxy script = null;
 
-        LogUtils.DEBUG("Classloader starting...");
+        LogUtils.DEBUG("Parameter Mapping...");
 
-        try (URLClassLoader scriptLoader = 
ClassLoaderFactory.getURLClassLoader(metaData)) {
-            LogUtils.DEBUG("Classloader finished...");
+        // Setup Context Object
+        XScriptContext xSc =
+            ScriptContext.createContext(m_xModel, m_xInvocContext,
+                                        m_xContext, m_xMultiComponentFactory);
 
-            ArrayList<Object> invocationArgList = new ArrayList<Object>();
+        scriptDesc.addArgumentType(XScriptContext.class);
+        invocationArgList.add(xSc);
 
-            LogUtils.DEBUG("Parameter Mapping...");
+        for (int i = 0; i < params.length; i++) {
+            scriptDesc.addArgumentType(params[ i ].getClass());
+            invocationArgList.add(params[ i ]);
+        }
 
-            // Setup Context Object
-            XScriptContext xSc =
-                ScriptContext.createContext(m_xModel, m_xInvocContext,
-                                            m_xContext, 
m_xMultiComponentFactory);
+        if (!invocationArgList.isEmpty()) {
+            invocationArgs = invocationArgList.toArray();
+        }
 
-            scriptDesc.addArgumentType(XScriptContext.class);
-            invocationArgList.add(xSc);
+        LogUtils.DEBUG("ScriptProxy starting... ");
+        ScriptProxy script = null;
 
-            for (int i = 0; i < params.length; i++) {
-                scriptDesc.addArgumentType(params[ i ].getClass());
-                invocationArgList.add(params[ i ]);
-            }
+        try {
 
-            if (!invocationArgList.isEmpty()) {
-                invocationArgs = invocationArgList.toArray();
-            }
+            String className = metaData.getLanguageName().substring(0,
+                               metaData.getLanguageName().lastIndexOf('.'));
 
-            LogUtils.DEBUG("ScriptProxy starting... ");
+            LogUtils.DEBUG("About to load Class " + className + " starting... 
");
 
-            try {
+            long start = new java.util.Date().getTime();
+            Class<?> c = scriptLoader.loadClass(className);
+            long end = new java.util.Date().getTime();
 
-                String className = metaData.getLanguageName().substring(0,
-                                   
metaData.getLanguageName().lastIndexOf('.'));
-
-                LogUtils.DEBUG("About to load Class " + className + " 
starting... ");
-
-                long start = new java.util.Date().getTime();
-                Class<?> c = scriptLoader.loadClass(className);
-                long end = new java.util.Date().getTime();
-
-                LogUtils.DEBUG("loadClass took: " + (end - start) + 
"milliseconds");
-
-                try {
-                    LogUtils.DEBUG("class loaded ... ");
-                    script = m_resolutionPolicy.getProxy(scriptDesc, c);
-                    LogUtils.DEBUG("script resolved ... ");
-                } catch (NoSuchMethodException e) {
-                    // Framework error
-                    ScriptFrameworkErrorException e2 = new 
ScriptFrameworkErrorException(
-                        e.toString(), null, metaData.getLanguageName(),
-                        metaData.getLanguage(), 
ScriptFrameworkErrorType.NO_SUCH_SCRIPT);
-                    e2.initCause(e);
-                    throw e2;
-                }
-            } catch (ClassNotFoundException e) {
+            LogUtils.DEBUG("loadClass took: " + (end - start) + 
"milliseconds");
+
+            try {
+                LogUtils.DEBUG("class loaded ... ");
+                script = m_resolutionPolicy.getProxy(scriptDesc, c);
+                LogUtils.DEBUG("script resolved ... ");
+            } catch (NoSuchMethodException e) {
                 // Framework error
                 ScriptFrameworkErrorException e2 = new 
ScriptFrameworkErrorException(
                     e.toString(), null, metaData.getLanguageName(),
@@ -248,11 +239,11 @@ class ScriptImpl implements XScript {
                 e2.initCause(e);
                 throw e2;
             }
-        } catch (IOException e) {
+        } catch (ClassNotFoundException e) {
             // Framework error
             ScriptFrameworkErrorException e2 = new 
ScriptFrameworkErrorException(
-                e.toString(), null, metaData.getLanguageName(), 
metaData.getLanguage(),
-                ScriptFrameworkErrorType.NO_SUCH_SCRIPT);
+                e.toString(), null, metaData.getLanguageName(),
+                metaData.getLanguage(), 
ScriptFrameworkErrorType.NO_SUCH_SCRIPT);
             e2.initCause(e);
             throw e2;
         }

Reply via email to