2

I've created two groovy extension modules / methods on java.util.ArrayList(). It's all working very well inside my IDE. I use gradle to build the jar, and deploy it to a remote JVM. When it reaches the remote JVM, it fails.

Here is the extension method:

    static Map sumSelectedAttributes(final List self, List attributes) {
    Map resultsMap = [:]
    attributes.each { attr ->
        resultsMap[attr] = self.inject(0) { sum, obj ->
            sum + obj[attr]
        }
    }
    return resultsMap

Here is the code that invokes it:

outputMap[processName][iName] << kBeanList.sumSelectedAttributes([
    "messageCount", "userCount", "outstandingRequests",
    "cpuUsage", "memoryUsage", "threadCount", "cacheCount", "huserCount",
    "manualHuserCount", "dataPointerCount", "tableHandleCount",
    "jdbCacheRecordCount", "dbConnectionCount"])

Here is the error:

No signature of method: java.util.ArrayList.sumSelectedAttributes() is applicable for argument types: (java.util.ArrayList) values: [[messageCount, incomingConnectionsCount, outgoingConnectionsCount, ...]]

Again, it works fine in intellij with test cases. What is different on the remote JVM that would prevent this from working? Here are some things that came to my mind:

  1. The remote JVM uses Groovy 2.3 while I'm on 2.4.5
  2. We use a custom classloader on the remote JVM to load classes

Other than that, I could not find any other documentation about anything special I need to do to make extensions work on remote JVM's.

Any help is greatly appreciated.

Per a comment, seems like an issue with custom classloader, here is the class that handles the manipulation of a few classloaders.

class CustomLoader {
    static Map loaders = [:]
    static File loaderRoot = new File("../branches")

    static URLClassLoader getCustomLoader(String branchName) {
        if (!loaders[branchName]) {
            loaders[branchName] = new URLClassLoader(getUrls(branchName))
        } else {
            loaders[branchName]
        }
    }

    static URLClassLoader updateClassLoader(String branchName) {
        loaders[branchName] = null
        loaders[branchName] = new URLClassLoader(getUrls(branchName))
    }

    private static URL[] getUrls(String branchName) {
        def loaderDir = new File(loaderRoot, branchName)
        List<File> files = []
        loaderDir.eachFileRecurse{
            if (it.name.endsWith('.jar')) {
                files  << it
            }
        }
        List urls = files.sort{ it.name }.reverse().collect{it.toURI().toURL()}
        return urls
    }
}
2
  • When you say custom class loader, it rings the alarms ;) In general for Groovy to be able to load an extension module, this module must be visible to the loader of the Groovy runtime. That is different to how your application normally works, in that there the applications class loader needs to see the runtime, but the runtime does not see the application classes. So of course I am now wondering about your actual classloading setup on the remote JVM.
    – blackdrag
    Apr 21, 2016 at 8:25
  • Thank you for responding, and it sounds like you've immediately identified the issue. In this case, is it simply impossible to use extension modules. Based on my code, can you recommend some alternative approach? I kind of planned a lot of code around this. Just to confirm, and for posterity, I have just added the "classloader class" to the original post for your review. It's is on a dev box where we need to load different versions of jars at the same time, and need to flexibly load changes without restarting JVM. (dev/test/prod branches) .
    – solvingJ
    Apr 21, 2016 at 8:37

1 Answer 1

2

To manually register an extension method module you can use code similar to what is used in GrapeIvy. Because grapes have the same problem in that they make a jar visible in the wrong loader but still want to enable extension methods. The piece of code in question is this here:

JarFile jar = new JarFile(file)
def entry = jar.getEntry(ExtensionModuleScanner.MODULE_META_INF_FILE)
if (entry) {
  Properties props = new Properties()
  props.load(jar.getInputStream(entry))
  Map<CachedClass, List<MetaMethod>> metaMethods = new HashMap<CachedClass, List<MetaMethod>>()
  mcRegistry.registerExtensionModuleFromProperties(props, loader, metaMethods)
  // add old methods to the map
  metaMethods.each { CachedClass c, List<MetaMethod> methods ->
    // GROOVY-5543: if a module was loaded using grab, there are chances that subclasses
    // have their own ClassInfo, and we must change them as well!
    Set<CachedClass> classesToBeUpdated = [c]
    ClassInfo.onAllClassInfo { ClassInfo info ->
      if (c.theClass.isAssignableFrom(info.cachedClass.theClass)) {
        classesToBeUpdated << info.cachedClass
      }
    }
    classesToBeUpdated*.addNewMopMethods(methods)
  }
}

In this code file is a File representing the jar. In your case you will need to have something else here. Basically we first load the descriptor file into Properties, call registerExtensionModuleFromProperties to fill the map with MetaMethods depending on a given class loader. And that is the key part for the solution of your problem, the right class loader here is one that can load all the classes in your extension module and the groovy runtime!. After this any new meta class will know about the extension methods. The code that follows is needed only if there are already existing meta classes, you want to know about those new methods.

1
  • Wow, I did not believe there would be a solution for this. I really appreciate the insight and info! FYI, because supportability by others is a pretty big factor, I decided it would be simpler to move the functions to a trait, and just implement that whereever I need to. It took all of 20 minutes... The power and flexibility of groovy!
    – solvingJ
    Apr 21, 2016 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.