import grails.util.GrailsUtil import org.codehaus.groovy.grails.commons.ControllerArtefactHandler import org.codehaus.groovy.grails.commons.GrailsClassUtils import org.codehaus.groovy.grails.plugins.support.GrailsPluginUtils import de.lynorics.grails.skinnable.SkinArtefactHandler import de.lynorics.grails.skinnable.SkinWrapper import de.lynorics.grails.skinnable.Skin import org.springframework.aop.framework.ProxyFactoryBean import org.springframework.aop.target.HotSwappableTargetSource import org.springframework.beans.factory.config.MethodInvokingFactoryBean /** * @author Marc Pompl * @created February, 23rd 2008 */ class SkinnableGrailsPlugin { def version = 0.2 def dependsOn = [:] def author = "Marc Pompl" def authorEmail = "marc.pompl lynorics de" def title = "This plugin adds skinnable support to Grails application." def description = '''\ Skinnable plugin brings simple support for user selectable skins. The selected skin is stored in a cookie. ''' def documentation = "http://grails.org/Skinnable+plugin" def grailsVersion = GrailsPluginUtils.grailsVersion def loadAfter = [ 'services', 'controllers' ] def observe = [ ] def watchedResources = 'file:./grails-app/skin/*Skin.groovy' def artefacts = [ de.lynorics.grails.skinnable.SkinArtefactHandler ] def skinBeans = [] def doWithSpring = { // Configure the skins defined in the project. def skinClasses = application.skinClasses application.skinClasses.each{ skinClass -> log.info "Registering skin: ${skinClass.fullName}" //configureSkin.delegate = delegate //skinBeans << configureSkin(skinClass) //skinBeans.each( //{skin-> // println "${skin}"; //} //); } } def doWithApplicationContext = { applicationContext -> // TODO Implement post initialization spring config (optional) } def doWithWebDescriptor = { xml -> // TODO Implement additions to web.xml (optional) } def doWithDynamicMethods = { ctx -> // TODO Implement registering dynamic methods to classes (optional) } def onChange = { event -> // TODO Implement code that is executed when this class plugin class is changed // the event contains: event.application and event.applicationContext objects } def onApplicationChange = { event -> // TODO Implement code that is executed when any class in a GrailsApplication changes // the event contain: event.source, event.application and event.applicationContext objects } def configureSkin = { grailsClass -> def skinName = grailsClass.shortName // Create the skin bean. "${skinName}Class"(MethodInvokingFactoryBean) { targetObject = ref('grailsApplication', true) targetMethod = 'getArtefact' arguments = [ SkinArtefactHandler.TYPE, grailsClass.fullName] } def ret = "${skinName}Instance"(ref("${skinName}Class")) { bean -> bean.factoryMethod = 'newInstance' bean.singleton = true bean.autowire = 'byName' } // Wrap each skin with an adapter that implements the // Skin interface. def wrapperName = "${skinName}Wrapper".toString() ret = "${wrapperName}"(SkinWrapper) { skin = ref("${skinName}Instance") } if (GrailsUtil.isDevelopmentEnv()) { // Configure a hotswappable bean if we're in development mode. "${skinName}TargetSource"(HotSwappableTargetSource, ref(wrapperName)) "${skinName}Proxy"(ProxyFactoryBean) { targetSource = ref("${skinName}TargetSource") proxyInterfaces = [ Skin ] } } return ret } }