groovy
  1. groovy
  2. GROOVY-5125

GroovyScriptEngine load from jarURL fail

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.8.0, 1.8.4
    • Fix Version/s: 1.8.8, 2.0.2
    • Component/s: GroovyScriptEngine
    • Labels:
      None
    • Number of attachments :
      0

      Description

      GroovyScriptEngine loadScriptByName from jarURL, first time is ok, second time fail, GroovyScriptEngine attempts to load from a fileURL.
      For fix this, change this:

      String path = conn.getURL().getPath();
      

      to

      String path = conn.getURL().toString();
      

        Activity

        Hide
        Paul King added a comment -

        I presume you are talking about GroovyScriptEngine#loadScriptByName around about line 449?

        Can you describe in a little more detail an example that shows the failure?

        Thanks.

        Show
        Paul King added a comment - I presume you are talking about GroovyScriptEngine#loadScriptByName around about line 449? Can you describe in a little more detail an example that shows the failure? Thanks.
        Hide
        lwh added a comment - - edited

        for management, we put groovy scripts in jars. but when gse.loadScriptByName these scripts , failed.
        the code like this:

        List<URL> GSEROOTS = new ArrayList<URL>();
        URL jarURL = GroovyScriptUtils.class.getClassLoader().getResource("com/abc/bcd/scripts/");
        GSEROOTS.add(jarURL);
        URL fileUrl = new File("c:\scripts").toURI().toURL();
        GSEROOTS.add(fileUrl);
        gse = new GroovyScriptEngine(urlroots);
        

        then:

        gse.loadScriptByName('a.groovy') //a.groovy in a jar "com/abc/bcd/scripts/"
        

        this is ok, do this again:

        gse.loadScriptByName('a.groovy')
        

        failed, I traced the gse code, gse seek a.groovy in a path not in a jar. because "String path = conn.getURL().getPath();" only return a path, the protocol is lost.

        Show
        lwh added a comment - - edited for management, we put groovy scripts in jars. but when gse.loadScriptByName these scripts , failed. the code like this: List<URL> GSEROOTS = new ArrayList<URL>(); URL jarURL = GroovyScriptUtils.class.getClassLoader().getResource( "com/abc/bcd/scripts/" ); GSEROOTS.add(jarURL); URL fileUrl = new File( "c:\scripts" ).toURI().toURL(); GSEROOTS.add(fileUrl); gse = new GroovyScriptEngine(urlroots); then: gse.loadScriptByName('a.groovy') //a.groovy in a jar "com/abc/bcd/scripts/" this is ok, do this again: gse.loadScriptByName('a.groovy') failed, I traced the gse code, gse seek a.groovy in a path not in a jar. because "String path = conn.getURL().getPath();" only return a path, the protocol is lost.
        Guillaume Laforge made changes -
        Field Original Value New Value
        Fix Version/s 1.8.6 [ 18245 ]
        Fix Version/s 1.8.5 [ 18071 ]
        Guillaume Laforge made changes -
        Fix Version/s 1.8.7 [ 18317 ]
        Fix Version/s 1.8.6 [ 18245 ]
        Guillaume Laforge made changes -
        Description GroovyScriptEngine loadScriptByName from jarURL, first time is ok, second time fail, GroovyScriptEngine attempts to load from a fileURL.
        For fix this, change this:
        String path = conn.getURL().getPath();
        to
        String path = conn.getURL().toString();
        GroovyScriptEngine loadScriptByName from jarURL, first time is ok, second time fail, GroovyScriptEngine attempts to load from a fileURL.
        For fix this, change this:
        {code}
        String path = conn.getURL().getPath();
        {code}
        to
        {code}
        String path = conn.getURL().toString();
        {code}
        Hide
        blackdrag blackdrag added a comment -

        lwh, I was not able to reproduce the issue with your example. That your test can't be right is also shown by GSE not having a list based constructor. Could you please give a test case that fails at least under 1.8.6 and master?

        Show
        blackdrag blackdrag added a comment - lwh, I was not able to reproduce the issue with your example. That your test can't be right is also shown by GSE not having a list based constructor. Could you please give a test case that fails at least under 1.8.6 and master?
        Hide
        lwh added a comment - - edited

        I've tested in 1.8.6, it fails. the code is.

        import groovy.lang.Binding;
        import groovy.util.GroovyScriptEngine;
        import groovy.util.ResourceException;
        import groovy.util.ScriptException;
        
        import java.net.MalformedURLException;
        import java.net.URL;
        
        public class TestGSE {
        
        	public static void main(String[] args) throws ResourceException,
        			ScriptException, InterruptedException, MalformedURLException {
        
        		URL jarURL = TestGSE.class.getClassLoader().getResource("com/test/");  //test.groovy is in a jar. (like a test.zip contains com/test/test.groovy)
        		URL[] urlroots = {jarURL};
        		GroovyScriptEngine gse = new GroovyScriptEngine(urlroots);
        		System.out.println("roots="+jarURL);
        		//this is ok
        		gse.run("test.groovy", new Binding());
        		//this will fail:  groovy.util.ResourceException: Cannot open URL: file:/C:/test.zip!/com/test/test.groovy
        		gse.run("test.groovy", new Binding());
        	}
        }
        
        
        Show
        lwh added a comment - - edited I've tested in 1.8.6, it fails. the code is. import groovy.lang.Binding; import groovy.util.GroovyScriptEngine; import groovy.util.ResourceException; import groovy.util.ScriptException; import java.net.MalformedURLException; import java.net.URL; public class TestGSE { public static void main( String [] args) throws ResourceException, ScriptException, InterruptedException, MalformedURLException { URL jarURL = TestGSE.class.getClassLoader().getResource( "com/test/" ); //test.groovy is in a jar. (like a test.zip contains com/test/test.groovy) URL[] urlroots = {jarURL}; GroovyScriptEngine gse = new GroovyScriptEngine(urlroots); System .out.println( "roots=" +jarURL); // this is ok gse.run( "test.groovy" , new Binding()); // this will fail: groovy.util.ResourceException: Cannot open URL: file:/C:/test.zip!/com/test/test.groovy gse.run( "test.groovy" , new Binding()); } }
        Hide
        Manuel Mayer added a comment -

        I have exactly the same problem in 1.8.5.

        Show
        Manuel Mayer added a comment - I have exactly the same problem in 1.8.5.
        Guillaume Laforge made changes -
        Fix Version/s 1.8.8 [ 18684 ]
        Fix Version/s 1.8.7 [ 18317 ]
        Hide
        blackdrag blackdrag added a comment -

        Iwh, sorry for getting back to this so late... The test case you gave works for me. The question now is why does it not for you? I can only assume that getPath() behaves different for you and me. Assuming this is the case toString() or toExternalForm() might be a better choice, yes.

        Show
        blackdrag blackdrag added a comment - Iwh, sorry for getting back to this so late... The test case you gave works for me. The question now is why does it not for you? I can only assume that getPath() behaves different for you and me. Assuming this is the case toString() or toExternalForm() might be a better choice, yes.
        Hide
        blackdrag blackdrag added a comment -

        I made that change as well as another similar change in SourceUnit. I hope this issue is then fixed

        Show
        blackdrag blackdrag added a comment - I made that change as well as another similar change in SourceUnit. I hope this issue is then fixed
        blackdrag blackdrag made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee blackdrag blackdrag [ blackdrag ]
        Fix Version/s 2.0.2 [ 18685 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            lwh
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: