Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-18972

About class loader of "Dynamic Choice Parameter (Scriptler)" (Version 0.2.0)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • Jenkins 1.478
      Jenkins 1.502

      When we were using "Dynamic Choice Parameter (Scriptler)", we found something is not right.We wanted to run a script, which would import classes of svnkit, to get a list of pathes in svn.
      The script was as follow:

      import java.util.*
      import org.tmatesoft.svn.core.ISVNDirEntryHandler
      import org.tmatesoft.svn.core.SVNDirEntry
      import org.tmatesoft.svn.core.SVNNodeKind
      import org.tmatesoft.svn.core.SVNException
      
      class SVNDirEntryHandlerImpl implements ISVNDirEntryHandler {
          List<SVNDirEntry> dirs = new ArrayList<SVNDirEntry>()
          void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {
              dirs.add(dirEntry)
          }
      }
      
      def h = new SVNDirEntryHandlerImpl()
      
      // get svn client, and list dir from svn
      // we do not show it, because it is not important
      
      def result = []
      h.dirs.each() { 
          def dirName = it.name
          if (null != dirName && dirName.length() > 0 && SVNNodeKind.DIR == it.kind) {
              result.add(dirName)
          }
          result.add(dirName)
      }
      return result
      

      The script was run well in Scriptler, but we got an empty list in job build parameter, without any errors.We read the log of tomcat, and we found "unable to resolve class" error.
      Log was as follow:

      2013-7-29 10:37:34 com.seitenbau.jenkins.plugins.dynamicparameter.BaseParameterDefinition executeScript
      严重: Error during executing script for parameter 'test'
      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Script1.groovy: 4: unable to resolve class org.tmatesoft.svn.core.SVNNodeKind
       @ line 4, column 1.
         import org.tmatesoft.svn.core.SVNNodeKind
         ^
      
      Script1.groovy: 5: unable to resolve class org.tmatesoft.svn.core.SVNException
       @ line 5, column 1.
         import org.tmatesoft.svn.core.SVNException
         ^
      
      Script1.groovy: 2: unable to resolve class org.tmatesoft.svn.core.ISVNDirEntryHandler
       @ line 2, column 1.
         import org.tmatesoft.svn.core.ISVNDirEntryHandler
         ^
      
      Script1.groovy: 3: unable to resolve class org.tmatesoft.svn.core.SVNDirEntry
       @ line 3, column 1.
         import org.tmatesoft.svn.core.SVNDirEntry
         ^
      
      4 errors
      
      	at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
      	at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:858)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:548)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:497)
      	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
      	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:731)
      	at groovy.lang.GroovyShell.parse(GroovyShell.java:743)
      	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:578)
      	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
      	at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
      	at com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils.execute(JenkinsUtils.java:88)
      	at com.seitenbau.jenkins.plugins.dynamicparameter.scriptler.ScriptlerParameterDefinition$ParameterizedScriptCall.call(ScriptlerParameterDefinition.java:170)
      	at com.seitenbau.jenkins.plugins.dynamicparameter.BaseParameterDefinition.executeScript(BaseParameterDefinition.java:221)
      	at com.seitenbau.jenkins.plugins.dynamicparameter.BaseParameterDefinition.getScriptResultAsList(BaseParameterDefinition.java:117)
      	at com.seitenbau.jenkins.plugins.dynamicparameter.scriptler.ScriptlerChoiceParameterDefinition.getChoices(ScriptlerChoiceParameterDefinition.java:81)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.apache.commons.jexl.util.PropertyExecutor.execute(PropertyExecutor.java:125)
      	at org.apache.commons.jexl.util.introspection.UberspectImpl$VelGetterImpl.invoke(UberspectImpl.java:314)
      	at org.apache.commons.jexl.parser.ASTArrayAccess.evaluateExpr(ASTArrayAccess.java:185)
      	at org.apache.commons.jexl.parser.ASTIdentifier.execute(ASTIdentifier.java:75)
      	at org.apache.commons.jexl.parser.ASTReference.execute(ASTReference.java:83)
      	at org.apache.commons.jexl.parser.ASTReference.value(ASTReference.java:57)
      	at org.apache.commons.jexl.parser.ASTReferenceExpression.value(ASTReferenceExpression.java:51)
      	at org.apache.commons.jexl.ExpressionImpl.evaluate(ExpressionImpl.java:80)
      	at hudson.ExpressionFactory2$JexlExpression.evaluate(ExpressionFactory2.java:72)
      	at org.apache.commons.jelly.expression.ExpressionSupport.evaluateRecurse(ExpressionSupport.java:61)
      	at org.apache.commons.jelly.expression.ExpressionSupport.evaluateAsIterator(ExpressionSupport.java:94)
      	at org.apache.commons.jelly.tags.core.ForEachTag.doTag(ForEachTag.java:89)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$1.run(CoreTagLibrary.java:98)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.kohsuke.stapler.jelly.CallTagLibScript$1.run(CallTagLibScript.java:98)
      	at org.apache.commons.jelly.tags.define.InvokeBodyTag.doTag(InvokeBodyTag.java:91)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$2.run(CoreTagLibrary.java:105)
      	at org.kohsuke.stapler.jelly.CallTagLibScript.run(CallTagLibScript.java:119)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$2.run(CoreTagLibrary.java:105)
      	at org.kohsuke.stapler.jelly.JellyViewScript.run(JellyViewScript.java:81)
      	at org.kohsuke.stapler.jelly.IncludeTag.doTag(IncludeTag.java:146)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:161)
      	at org.apache.commons.jelly.tags.core.ForEachTag.doTag(ForEachTag.java:150)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.CallTagLibScript$1.run(CallTagLibScript.java:98)
      	at org.apache.commons.jelly.tags.define.InvokeBodyTag.doTag(InvokeBodyTag.java:91)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$2.run(CoreTagLibrary.java:105)
      	at org.kohsuke.stapler.jelly.CallTagLibScript.run(CallTagLibScript.java:119)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.CallTagLibScript$1.run(CallTagLibScript.java:98)
      	at org.apache.commons.jelly.tags.define.InvokeBodyTag.doTag(InvokeBodyTag.java:91)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$1.run(CoreTagLibrary.java:98)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$2.run(CoreTagLibrary.java:105)
      	at org.kohsuke.stapler.jelly.CallTagLibScript.run(CallTagLibScript.java:119)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.CallTagLibScript$1.run(CallTagLibScript.java:98)
      	at org.apache.commons.jelly.tags.define.InvokeBodyTag.doTag(InvokeBodyTag.java:91)
      	at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:270)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.kohsuke.stapler.jelly.ReallyStaticTagLibrary$1.run(ReallyStaticTagLibrary.java:99)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$2.run(CoreTagLibrary.java:105)
      	at org.kohsuke.stapler.jelly.CallTagLibScript.run(CallTagLibScript.java:119)
      	at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:95)
      	at org.apache.commons.jelly.tags.core.CoreTagLibrary$2.run(CoreTagLibrary.java:105)
      	at org.kohsuke.stapler.jelly.JellyViewScript.run(JellyViewScript.java:81)
      	at org.kohsuke.stapler.jelly.DefaultScriptInvoker.invokeScript(DefaultScriptInvoker.java:63)
      	at org.kohsuke.stapler.jelly.DefaultScriptInvoker.invokeScript(DefaultScriptInvoker.java:53)
      	at org.kohsuke.stapler.jelly.JellyRequestDispatcher.forward(JellyRequestDispatcher.java:55)
      	at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinitionProperty.java:112)
      	at hudson.model.AbstractProject.doBuild(AbstractProject.java:1651)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:288)
      	at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:151)
      	at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:90)
      	at org.kohsuke.stapler.MetaClass$1.doDispatch(MetaClass.java:111)
      	at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
      	at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:574)
      	at org.kohsuke.stapler.Stapler.invoke(Stapler.java:659)
      	at org.kohsuke.stapler.MetaClass$6.doDispatch(MetaClass.java:241)
      	at org.kohsuke.stapler.NameBasedDispatcher.dispatch(NameBasedDispatcher.java:53)
      	at org.kohsuke.stapler.Stapler.tryInvoke(Stapler.java:574)
      	at org.kohsuke.stapler.Stapler.invoke(Stapler.java:659)
      	at org.kohsuke.stapler.Stapler.invoke(Stapler.java:488)
      	at org.kohsuke.stapler.Stapler.service(Stapler.java:162)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at hudson.util.PluginServletFilter$1.doFilter(PluginServletFilter.java:95)
      	at hudson.util.PluginServletFilter.doFilter(PluginServletFilter.java:87)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at hudson.security.csrf.CrumbFilter.doFilter(CrumbFilter.java:47)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:84)
      	at hudson.security.UnwrapSecurityExceptionFilter.doFilter(UnwrapSecurityExceptionFilter.java:51)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:166)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:271)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at org.acegisecurity.ui.basicauth.BasicProcessingFilter.doFilter(BasicProcessingFilter.java:173)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at jenkins.security.ApiTokenFilter.doFilter(ApiTokenFilter.java:63)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:249)
      	at hudson.security.HttpSessionContextIntegrationFilter2.doFilter(HttpSessionContextIntegrationFilter2.java:66)
      	at hudson.security.ChainedServletFilter$1.doFilter(ChainedServletFilter.java:87)
      	at hudson.security.ChainedServletFilter.doFilter(ChainedServletFilter.java:76)
      	at hudson.security.HudsonFilter.doFilter(HudsonFilter.java:164)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at org.kohsuke.stapler.compression.CompressionFilter.doFilter(CompressionFilter.java:50)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at hudson.util.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:81)
      	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      	at java.lang.Thread.run(Thread.java:662)
      

      After reading the code of "Jenkins Dynamic Parameter Plug-in"(TAG 0.2.0), "Scriptler Plugin" and Groovy, we found the reason of this error.
      The code of function execute(String script, Map<String, String> parameters) in class com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils is as follow:

      JenkinsUtils.java
        public static Object execute(String script, Map<String, String> parameters)
        {
          CompilerConfiguration config = new CompilerConfiguration();
          GroovyShell groovyShell = new GroovyShell(config);
      
          for (Entry<String, String> parameter : parameters.entrySet())
          {
            groovyShell.setVariable(parameter.getKey(), parameter.getValue());
          }
      
          Object evaluate = groovyShell.evaluate(script);
          return evaluate;
        }
      

      Here, groovyShell use the constructor GroovyShell(CompilerConfiguration config), so Groovy use the class loader of GroovyShell as the parent class loader of Groovy. The class GroovyShell is loaded by Jenkins(You can find the jars of goorvy in webapps\jenkins\WEB-INF\lib). The class loader of GroovyShell is same to Jenkins. The classes of svnkit are loaded by subversion plugin(You can find the jars of svnkit in the path of subversion plugin). The class loader of svnkit classes is same to subversion plugin. According to document of Jenkins, the class loader of plugin extends the class loader of Jenkins, so the class loader of Groovy can not find the classes of svnkit.

      We sugget that modify the function execute(String script, Map<String, String> parameters) to follow:

      JenkinsUtils.java
        public static Object execute(String script, Map<String, String> parameters)
        {
          //CompilerConfiguration config = new CompilerConfiguration();
          GroovyShell groovyShell = new GroovyShell(Thread.currentThread().getContextClassLoader());
      
          for (Entry<String, String> parameter : parameters.entrySet())
          {
            groovyShell.setVariable(parameter.getKey(), parameter.getValue());
          }
      
          Object evaluate = groovyShell.evaluate(script);
          return evaluate;
        }
      

      Then Groovy will use the context class loader of current thread as the parent class loader, and Groovy can find the classes of svnkit.
      We test this code in Jenkins, the script run well, and we get the list which we want.

            dimitarp Dimitar Popov
            druid0523 BingXi Li
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: