[Libreoffice-commits] core.git: scripting/java

Stephan Bergmann (via logerrit) logerrit at kemper.freedesktop.org
Thu Aug 8 11:29:06 UTC 2019


 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, 52 insertions(+), 43 deletions(-)

New commits:
commit 418533f0af7cd303d559c8fb136c49e7e9fb0d79
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Thu Aug 8 11:33:16 2019 +0200
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Thu Aug 8 13:27:52 2019 +0200

    Related tdf#116767: Call URLClassLoader.close
    
    ...which required the return type of
    com.sun.star.script.framework.provider.ClassLoaderFactory.getURLClassLoader to
    be changed.  (In theory, there could be 3rd-party scripting providers that hook
    into the framework and are affected by that incompatible change, but,
    practically, chances should be low.)
    
    Change-Id: I083c398726aaf62df7286d6da86b9d2056a5a8c1
    Reviewed-on: https://gerrit.libreoffice.org/77146
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at 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 b5e6e3085bd2..0974b8874110 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 ClassLoader getURLClassLoader(ScriptMetaData scriptData) {
+    public static URLClassLoader 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 ClassLoader getURLClassLoader(final ClassLoader parent,
+    public static URLClassLoader 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 eeea0d49ebe5..63dc3169f759 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,6 +45,8 @@ 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 {
@@ -182,56 +184,63 @@ 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("Parameter Mapping...");
+        LogUtils.DEBUG("Classloader starting...");
 
-        // Setup Context Object
-        XScriptContext xSc =
-            ScriptContext.createContext(m_xModel, m_xInvocContext,
-                                        m_xContext, m_xMultiComponentFactory);
+        try (URLClassLoader scriptLoader = ClassLoaderFactory.getURLClassLoader(metaData)) {
+            LogUtils.DEBUG("Classloader finished...");
 
-        scriptDesc.addArgumentType(XScriptContext.class);
-        invocationArgList.add(xSc);
+            ArrayList<Object> invocationArgList = new ArrayList<Object>();
 
-        for (int i = 0; i < params.length; i++) {
-            scriptDesc.addArgumentType(params[ i ].getClass());
-            invocationArgList.add(params[ i ]);
-        }
+            LogUtils.DEBUG("Parameter Mapping...");
 
-        if (!invocationArgList.isEmpty()) {
-            invocationArgs = invocationArgList.toArray();
-        }
+            // Setup Context Object
+            XScriptContext xSc =
+                ScriptContext.createContext(m_xModel, m_xInvocContext,
+                                            m_xContext, m_xMultiComponentFactory);
 
-        LogUtils.DEBUG("ScriptProxy starting... ");
-        ScriptProxy script = null;
+            scriptDesc.addArgumentType(XScriptContext.class);
+            invocationArgList.add(xSc);
 
-        try {
-
-            String className = metaData.getLanguageName().substring(0,
-                               metaData.getLanguageName().lastIndexOf('.'));
-
-            LogUtils.DEBUG("About to load Class " + className + " starting... ");
+            for (int i = 0; i < params.length; i++) {
+                scriptDesc.addArgumentType(params[ i ].getClass());
+                invocationArgList.add(params[ i ]);
+            }
 
-            long start = new java.util.Date().getTime();
-            Class<?> c = scriptLoader.loadClass(className);
-            long end = new java.util.Date().getTime();
+            if (!invocationArgList.isEmpty()) {
+                invocationArgs = invocationArgList.toArray();
+            }
 
-            LogUtils.DEBUG("loadClass took: " + (end - start) + "milliseconds");
+            LogUtils.DEBUG("ScriptProxy starting... ");
 
             try {
-                LogUtils.DEBUG("class loaded ... ");
-                script = m_resolutionPolicy.getProxy(scriptDesc, c);
-                LogUtils.DEBUG("script resolved ... ");
-            } catch (NoSuchMethodException e) {
+
+                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) {
                 // Framework error
                 ScriptFrameworkErrorException e2 = new ScriptFrameworkErrorException(
                     e.toString(), null, metaData.getLanguageName(),
@@ -239,11 +248,11 @@ class ScriptImpl implements XScript {
                 e2.initCause(e);
                 throw e2;
             }
-        } catch (ClassNotFoundException e) {
+        } catch (IOException 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;
         }


More information about the Libreoffice-commits mailing list