PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/groovy/com/onresolve/jira/groovy/JiraGroovyResourceLoader.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 48 lines | 32 code | 10 blank | 6 comment | 5 complexity | 316b607f3f329a16f713755f2f6e87d3 MD5 | raw file
  1. package com.onresolve.jira.groovy;
  2. import com.atlassian.jira.util.BuildUtils
  3. import java.util.regex.Matcher
  4. import org.apache.log4j.Category
  5. public class JiraGroovyResourceLoader implements GroovyResourceLoader {
  6. ClassLoader gcl
  7. ScriptManager scriptManager
  8. Category log = Category.getInstance(this.class)
  9. JiraGroovyResourceLoader(ClassLoader gcl, ScriptManager scriptManager) {
  10. this.gcl = gcl
  11. this.scriptManager = scriptManager
  12. }
  13. public URL loadGroovySource(String filename) throws MalformedURLException {
  14. // log.debug "load class with name $filename"
  15. // the way to make this whole process a bit faster is not to use strong typing, I think
  16. Properties props = scriptManager.getProperties()
  17. Long currentBuildNumber = BuildUtils.getCurrentBuildNumber() as Long
  18. String origFilename = filename
  19. props.each {
  20. Matcher matcher = (it.key =~ /${filename}\.(\d+)\.to\.(\d+)$/)
  21. if (matcher.matches()) {
  22. // log.debug ("Matched ${it.key}")
  23. Long minVersion = matcher[0][1] as Long
  24. Long maxVersion = matcher[0][2] as Long
  25. // todo: check the timestamp for files that don't match their class against cls.getField(Verifier.__TIMESTAMP);
  26. // see GroovyClassLoader.java
  27. // or write a custom classloader
  28. if (currentBuildNumber >= minVersion && (maxVersion == 0 || currentBuildNumber <= maxVersion)) {
  29. filename = it.value.replaceAll(/\.groovy$/, "").replaceAll("/", ".")
  30. log.debug ("Modifying resource from $origFilename to $filename for current build $currentBuildNumber - has range min: $minVersion max: $maxVersion")
  31. }
  32. }
  33. }
  34. def convertedName = filename.replaceAll(/\./, "/") + ".groovy"
  35. URL resource = gcl.getResource(convertedName)
  36. return resource;
  37. }
  38. }