/* * Copyright 2004-2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU import org.springframework.beans.factory.config.MethodInvokingFactoryBean import org.codehaus.groovy.grails.plugins.echo2.Echo2ArtefactHandler class Echo2GrailsPlugin { def version = 0.1 def dependsOn = [:] def author = "Reginaldo Delfino" def authorEmail = "rdelfino@gmail.com" def title = 'Echo2 capabilities to Grails applications' def description = ''' Echo2 capabilities to Grails applications ''' def documentation = "http://grails.org/Echo2+Plugin" def loadAfter = ['core'] def watchedResources = "file:./grails-app/echo2/*ApplicationInstance.groovy" def artefacts = [new Echo2ArtefactHandler()] def doWithSpring = { def x = application application.applicationInstanceClasses.each { applicationInstance -> configureEcho2Beans.delegate = delegate configureEcho2Beans(applicationInstance) } } def configureEcho2Beans = { applicationInstanceClass -> def fullName = applicationInstanceClass.fullName "${fullName}ApplicationInstanceClass"(MethodInvokingFactoryBean) { targetObject = ref("grailsApplication", true) targetMethod = "getArtefact" arguments = [Echo2ArtefactHandler.TYPE, fullName] } "${fullName}"(ref("${fullName}ApplicationInstanceClass")) { bean -> bean.factoryMethod = "newInstance" bean.autowire = "byName" bean.singleton = false } } def doWithApplicationContext = { applicationContext -> } def doWithWebDescriptor = { xml -> def applications = [:] plugin.watchedResources.each { def match = (it.filename =~ /(\w+)(ApplicationInstance.groovy)$/) if(match) { def applicationName = match[0][1] def fullName = applicationName applicationName = GCU.getPropertyName(applicationName) applications[applicationName] = "${fullName}ApplicationInstance" } } def servletElement = xml.servlet applications.each { name, fullName -> servletElement[servletElement.size()-1] + { servlet { 'servlet-name'("${fullName}.echo2") 'servlet-class'("org.codehaus.groovy.grails.plugins.echo2.GrailsWebContainerServlet") 'init-param'{ 'param-name'('prototype') 'param-value'(fullName) } } } } def mappingElement = xml.'servlet-mapping' applications.each { name, fullName-> mappingElement[0] + { 'servlet-mapping' { 'servlet-name'("${fullName}.echo2") 'url-pattern'("/${name}.echo2") } } } } def doWithDynamicMethods = { ctx -> } def onChange = {event -> if(!application.isArtefactOfType(Echo2ArtefactHandler.TYPE, event.source)) { return } if(!context) { log.debug("Application context not found. Can't reload") return } log.debug("Application Instance ${event.source} changed. Reloading...") def context = event.ctx // add ApplicationInstance artefact to application def applicationInstanceClass = application.addArtefact(Echo2ArtefactHandler.TYPE, event.source) // configure and register ApplicationInstance beans def fullName = applicationInstanceClass.fullName def beans = beans { configureEcho2Beans.delegate = delegate configureEcho2Beans(applicationInstanceClass) } event.ctx.registerBeanDefinition("${fullName}ApplicationInstanceClass", beans.getBeanDefinition("${fullName}ApplicationInstanceClass")) event.ctx.registerBeanDefinition("${fullName}", beans.getBeanDefinition("${fullName}")) log.debug("Application Instance ${applicationInstanceClass.fullName} reloaded") } def onApplicationChange = { event -> } }