Tuesday, March 12, 2013

Grails bootstrap environment

Every now and then we'd really like to know what Grails has mystically configured for us in that strange little world of the Bootstrap.

The following is a chunk you can paste into Bootstrap.groovy to see what is under the hood and available to you.

One of the more useful purposes I have found for this information is pulling out service classes for some sort of startup initialization.


        println "------- ServletContext Attributes -----------------------"
        def names = servletContext.getAttributeNames()
        names.each() {println "$it"}
 
        def ctx = servletContext.getAttribute('org.codehaus.groovy.grails.APPLICATION_CONTEXT')
        def osiv
        if (ctx) {
            def beans = ctx.beanDefinitionNames
            beans.sort()
            println "------- AppContext Beans -----------------------"
            beans.each() {
                if (it.indexOf("Interceptor") > 0 || it.indexOf('interceptor') > 0 || it.indexOf('Handler') > 0) {
                   println "$it"
                }
                if (it == 'openSessionInViewInterceptor') {
                    // Get the interceptor, check it state
                    osiv = ctx.getBean(it)
                    println "\t\t--> OSIV enabled?? ${osiv.isSingleSession()}"
                }
           }
 
           // Get the private interceptors field from the request handlerMapping class
           def field = org.springframework.web.servlet.handler.AbstractHandlerMapping.class.getDeclaredField('interceptors')
           field.accessible = true
           println "------- Interceptors via controllerHandlerMappings -----------------------"
           // Get this Field on the given object, the actual HandlerMapping that declares the interceptors
           def interceptors = field.get(ctx.controllerHandlerMappings)
           if (interceptors) {
               println "Got interceptors class: ${interceptors.class.name}"
               interceptors.each() {
                   println "$it"
               }
           }
           else {
               println "Could not get interceptors class"
           }
        }
        else {
            println "No AppContext"
        }
        
        def app = servletContext.getAttribute('grailsApplication')
        def messageManagerService
        
        if (app) {
            println "\n-------------------------------------------------"
            println "------- grailsApplication -----------------------"
            println "-------------------------------------------------"
            println "\n------- Properties -----------------------"
            app.properties.each { key ->
                println "### $key"
            }
            println "\n------- All Artefact Classes -----------------------"
            def cz = app.allArtefactClasses
            cz.each {
                println it
            }
            println "\n------- Domain Classes -----------------------"
            cz = app.getArtefacts(DomainClassArtefactHandler.TYPE)
            cz = app.domainClasses
            cz.each {
                println "$it (${it.propertyName})"
            }
            println "\n------- Controller Classes -----------------------"
            //cz = app.getArtefacts(ControllerArtefactHandler.TYPE)
            cz = app.controllerClasses
            cz.each {
                println "$it (${it.propertyName})"
            }
            println "\n------- Service Classes -----------------------"
            //cz = app.getArtefacts(ServiceArtefactHandler.TYPE)
            cz = app.serviceClasses
            cz.each {
                println "$it (${it.propertyName})"
            }
            println "\n------- UrlMappings Classes -----------------------"
            //cz = app.getArtefacts(UrlMappingsArtefactHandler.TYPE)
            cz = app.urlMappingsClasses
            cz.each {
                println "$it (${it.propertyName})"
            }
            
            // Pull out my service class by registered bean name
            messageManagerService = app.mainContext['messageManagerService']
        }
        else {
          println "No grailsApplication"  
        }
        



No comments:

Post a Comment