grails - Replacing invokeMethod to intercept Controller methods -
grails - Replacing invokeMethod to intercept Controller methods -
i intercept methods in controllers replacing invokemethod closure of expandometaclass.
i created new grails project (2.3.5). created new controller, called accountcontroller:
package com.invoke.test class accountcontroller { def index() { system.out.println("index called") } }
then created additional bootstrap called securitybootstrap:
class securitybootstrap { def grailsapplication def verifymethodaccess = { string name, args -> log.info "called method ${name}" def method = delegate.metaclass.getmetamethod(name, args) if (method) { homecoming method.invoke(delegate, args) } homecoming null } def init = { servletcontext -> grailsapplication.controllerclasses.each { controllerclass -> controllerclass.clazz.metaclass.invokemethod = verifymethodaccess log.info "replaced invokemethod closure on ${controllerclass.clazz}" } } }
after starting application, log looking promising:
|running grails application 2014-06-26 15:07:26,476 [localhost-startstop-1] info conf.securitybootstrap - replaced invokemethod closure on class grails.plugin.databasemigration.dbdoccontroller 2014-06-26 15:07:26,477 [localhost-startstop-1] info conf.securitybootstrap - replaced invokemethod closure on class com.invoke.test.accountcontroller |server running. browse http://localhost:8080/invoketest
but, when calling http://localhost:8080/invoketest/account/index
log like:
index called
i expected be:
called method index index called
thanks in advance
you compile time meta programming can't runtime metaprogramming. limitation of groovy's runtime metaprogramming applies method calls initiated groovy. much of grails framework written in java.
example:
class widget { def gettheanswer() { 42 } } widget.metaclass.gettheanswer = { -> 2112 }
when groovy code invokes new widget().gettheanswer()
result 2112. when java code invokes new widget().gettheanswer();
result 42.
grails groovy
Comments
Post a Comment