Tuesday, October 18, 2011

Classloader problem with diRMI and Grails

When trying to expose DiRMI services from a Grails-based web application, a fatal ClassNotFound exception may rise.

I have found the next caveat:

Extending DiRMI ClassResolver and Environment class.


class ClasspathResolver implements ClassResolver {

public Class resolveClass(String name)
throws ClassNotFoundException {
return Thread.currentThread().contextClassLoader.loadClass(name)
}

}

class GEnvironment extends Environment {

public Session newSession(ChannelBroker broker) throws IOException {
def session = super.newSession(broker)
session.classResolver = new ClasspathResolver()
return session
}

}


And that's all folks! It works!