groovy extension module and grails

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

groovy extension module and grails

mperry

I have had trouble getting my groovy extension module to work within grails (2.2).  I have tried several different extension modules including my own and these two:

https://github.com/timyates/groovy-common-extensions
https://github.com/michal-mally/groovy-random-extension

In each case the method is not found.  

I have tried including the extension module in the lib directory and from BuildConfig.groovy.  Inspecting the war shows the jar is in the right location.  I am running the app using "run-app" from the Grails 2.2 console.  I am calling the extension module method from within a Grails controller and creating a model map to return as the result.

Running each of these extension modules from gradle (1.4) seems to work fine (including my own).

Am I missing something?  Is there an example you can point me to on Github or elsewhere where a groovy extension module is working with Grails?

Thanks in advance.

Mark
Reply | Threaded
Open this post in threaded view
|

Re: groovy extension module and grails

burtbeckwith
The Groovy search for extension modules is triggered very early during startup, before the dependency jars are added to the classpath. You can add this to BootStrap.groovy to do the search within your application's dependencies to find the rest:


import org.codehaus.groovy.reflection.CachedClass
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl

...

Map<CachedClass, List<MetaMethod>> map = [:]

ClassLoader classLoader = Thread.currentThread().contextClassLoader

try {
   Enumeration<URL> resources = classLoader.getResources(MetaClassRegistryImpl.MODULE_META_INF_FILE)
   for (URL url in resources) {
      if (url.path.contains('groovy-all')) {
         // already registered
         continue
      }
      Properties properties = new Properties()
      InputStream inStream
      try {
         inStream = url.openStream()
         properties.load(inStream)
         GroovySystem.metaClassRegistry.registerExtensionModuleFromProperties(properties, classLoader, map)
      }
      catch (IOException e) {
         throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e)
      }
      finally {
         inStream?.close()
      }
   }
}
catch (IOException ignored) {}

map.each { CachedClass cls, List<MetaMethod> methods ->
   cls.setNewMopMethods(methods)
}

Burt

mperry wrote
I have had trouble getting my groovy extension module to work within grails (2.2).  I have tried several different extension modules including my own and these two:

https://github.com/timyates/groovy-common-extensions
https://github.com/michal-mally/groovy-random-extension

In each case the method is not found.  

I have tried including the extension module in the lib directory and from BuildConfig.groovy.  Inspecting the war shows the jar is in the right location.  I am running the app using "run-app" from the Grails 2.2 console.  I am calling the extension module method from within a Grails controller and creating a model map to return as the result.

Running each of these extension modules from gradle (1.4) seems to work fine (including my own).

Am I missing something?  Is there an example you can point me to on Github or elsewhere where a groovy extension module is working with Grails?

Thanks in advance.

Mark
Reply | Threaded
Open this post in threaded view
|

Re: groovy extension module and grails

mperry
This post was updated on .
Burt, thanks for that info.  I downloaded Grails 2.2.1 and have confirmed your solution works, fantastic.  It would be nice if a workaround was needed for extension modules, but regardless...

For completeness here is the documentation of the problem and solution.  I added my groovy extension modules in the lib directory and at runtime got the error MissingMethodException as per below.

grails>
| Error 2013-03-10 21:27:47,697 [http-bio-8080-exec-1] ERROR errors.GrailsExcepti
onResolver  - MissingMethodException occurred when processing request: [GET] /ext
ensionModule/test/index
No signature of method: fj.data.Option$Some.bind() is applicable for argument typ
es: (perry.TestController$_index_closure1) values: [perry.TestController$_index_c
losure1@24c2a145]
Possible solutions: bind(), bind(fj.F), find(), bind(fj.data.Option, fj.F), find(
groovy.lang.Closure), bind(fj.data.Option, fj.data.Option, fj.F). Stacktrace foll
ows:
Message: No signature of method: fj.data.Option$Some.bind() is applicable for arg
ument types: (perry.TestController$_index_closure1) values: [perry.TestController
$_index_closure1@24c2a145]
Possible solutions: bind(), bind(fj.F), find(), bind(fj.data.Option, fj.F), find(
groovy.lang.Closure), bind(fj.data.Option, fj.data.Option, fj.F)
    Line | Method
->>   16 | index     in perry.TestController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    195 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread

The full BootStrap.groovy file is:

import org.codehaus.groovy.reflection.CachedClass
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl

class BootStrap {

    def init = { servletContext ->
                addExtensionModules()
        }

        def addExtensionModules() {

                Map<CachedClass, List<MetaMethod>> map = [:]

                ClassLoader classLoader = Thread.currentThread().contextClassLoader

                try {
                        Enumeration<URL> resources = classLoader.getResources(MetaClassRegistryImpl.MODULE_META_INF_FILE)
                        for (URL url in resources) {
                                if (url.path.contains('groovy-all')) {
                                        // already registered
                                        continue
                                }
                                Properties properties = new Properties()
                                InputStream inStream
                                try {
                                        inStream = url.openStream()
                                        properties.load(inStream)
                                        GroovySystem.metaClassRegistry.registerExtensionModuleFromProperties(properties, classLoader, map)
                                }
                                catch (IOException e) {
                                        throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e)
                                }
                                finally {
                                        inStream?.close()
                                }
                        }
                }
                catch (IOException ignored) {}

                map.each { CachedClass cls, List<MetaMethod> methods ->
                        cls.setNewMopMethods(methods)
                }

        }

    def destroy = {
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: groovy extension module and grails

smaldini
Is there a JIRA for this ? We need it to be fixed for 2.3 as Reactor relies on that for groovy signatures (Closures etc) . Seems tests pass but Zyro has spotted this : https://groups.google.com/forum/?hl=en&fromgroups#!topic/reactor-framework/2qTTF_ihTVM.


On Sun, Mar 10, 2013 at 11:35 AM, mperry <[hidden email]> wrote:
Bert, thanks for that info.  I downloaded Grails 2.2.1 and have confirmed
your solution works, fantastic.  It would be nice if a workaround was needed
for extension modules, but regardless...

For completeness here is the documentation of the problem and solution.  I
added my groovy extension modules in the lib directory and at runtime got
the error MissingMethodException as per below.

grails>
| Error 2013-03-10 21:27:47,697 [http-bio-8080-exec-1] ERROR
errors.GrailsExcepti
onResolver  - MissingMethodException occurred when processing request: [GET]
/ext
ensionModule/test/index
No signature of method: fj.data.Option$Some.bind() is applicable for
argument typ
es: (perry.TestController$_index_closure1) values:
[perry.TestController$_index_c
losure1@24c2a145]
Possible solutions: bind(), bind(fj.F), find(), bind(fj.data.Option, fj.F),
find(
groovy.lang.Closure), bind(fj.data.Option, fj.data.Option, fj.F). Stacktrace
foll
ows:
Message: No signature of method: fj.data.Option$Some.bind() is applicable
for arg
ument types: (perry.TestController$_index_closure1) values:
[perry.TestController
$_index_closure1@24c2a145]
Possible solutions: bind(), bind(fj.F), find(), bind(fj.data.Option, fj.F),
find(
groovy.lang.Closure), bind(fj.data.Option, fj.data.Option, fj.F)
    Line | Method
->>   16 | index     in perry.TestController
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    195 | doFilter  in
grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run       in java.lang.Thread

The full BootStrap.groovy file is:

import org.codehaus.groovy.reflection.CachedClass
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl

class BootStrap {

    def init = { servletContext ->
                addExtensionModules()
        }

        def addExtensionModules() {

                Map<CachedClass, List<MetaMethod>> map = [:]

                ClassLoader classLoader = Thread.currentThread().contextClassLoader

                try {
                        Enumeration<URL> resources =
classLoader.getResources(MetaClassRegistryImpl.MODULE_META_INF_FILE)
                        for (URL url in resources) {
                                if (url.path.contains('groovy-all')) {
                                        // already registered
                                        continue
                                }
                                Properties properties = new Properties()
                                InputStream inStream
                                try {
                                        inStream = url.openStream()
                                        properties.load(inStream)

GroovySystem.metaClassRegistry.registerExtensionModuleFromProperties(properties,
classLoader, map)
                                }
                                catch (IOException e) {
                                        throw new GroovyRuntimeException("Unable to load module META-INF
descriptor", e)
                                }
                                finally {
                                        inStream?.close()
                                }
                        }
                }
                catch (IOException ignored) {}

                map.each { CachedClass cls, List<MetaMethod> methods ->
                        cls.setNewMopMethods(methods)
                }

        }

    def destroy = {
    }
}




--
View this message in context: http://grails.1312388.n4.nabble.com/groovy-extension-module-and-grails-tp4642249p4642285.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email





--
Stéphane
--

Reply | Threaded
Open this post in threaded view
|

Re: groovy extension module and grails

mperry
I did not raise a Jira issue for this.  I have no idea whether Bert did or not.
Reply | Threaded
Open this post in threaded view
|

Re: groovy extension module and grails

sergiomichels
Hi, I needed this too. Created http://jira.grails.org/browse/GRAILS-10652

--
Sérgio Michels


On Mon, May 27, 2013 at 3:46 AM, mperry <[hidden email]> wrote:
I did not raise a Jira issue for this.  I have no idea whether Bert did or
not.



--
View this message in context: http://grails.1312388.n4.nabble.com/groovy-extension-module-and-grails-tp4642249p4645171.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email