Skip to content

Commit

Permalink
Cleanup i18n plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Dec 16, 2014
1 parent 4841479 commit 2c78e40
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 92 deletions.
9 changes: 6 additions & 3 deletions grails-core/src/main/groovy/grails/plugins/Plugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import grails.core.support.GrailsApplicationAware
import grails.spring.BeanBuilder
import grails.util.Environment
import groovy.transform.CompileStatic
import org.grails.spring.context.support.MapBasedSmartPropertyOverrideConfigurer
import org.springframework.beans.BeansException
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ConfigurableApplicationContext
Expand Down Expand Up @@ -144,14 +146,15 @@ abstract class Plugin implements GrailsApplicationLifeCycle, GrailsApplicationAw
}

/**
* Allows a plugin to define beans
* Allows a plugin to define beans at runtime. Used primarily for reloading in development mode
*
* @param beanDefinitions The bean definitions
* @return The BeanBuilder instance
*/
BeanBuilder beans(Closure beanDefinitions) {
void beans(Closure beanDefinitions) {
def bb = new BeanBuilder(null, grailsApplication.classLoader)
bb.beans beanDefinitions
return bb
bb.registerBeans((BeanDefinitionRegistry)applicationContext)
new MapBasedSmartPropertyOverrideConfigurer(grailsApplication).postProcessBeanFactory(((ConfigurableApplicationContext)applicationContext).beanFactory)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class ControllersGrailsPlugin extends Plugin implements GrailsApplicationAware{
def defaultScope = application.config.getProperty('grails.controllers.defaultScope', 'prototype')

GrailsControllerClass controllerClass = (GrailsControllerClass)application.addArtefact(ControllerArtefactHandler.TYPE, event.source)
def beanDefinitions = beans {
beans {
"${controllerClass.fullName}"(controllerClass.clazz) { bean ->
def beanScope = controllerClass.getPropertyValue("scope") ?: defaultScope
bean.scope = beanScope
Expand All @@ -182,7 +182,6 @@ class ControllersGrailsPlugin extends Plugin implements GrailsApplicationAware{
}
// now that we have a BeanBuilder calling registerBeans and passing the app ctx will
// register the necessary beans with the given app ctx
beanDefinitions.registerBeans(applicationContext)
controllerClass.initialize()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class DomainClassGrailsPlugin extends Plugin {
return
}

def beans = beans {
beans {
"${domainClass.fullName}"(domainClass.clazz) { bean ->
bean.singleton = false
bean.autowire = "byName"
Expand All @@ -147,7 +147,6 @@ class DomainClassGrailsPlugin extends Plugin {
application = ref("grailsApplication", true)
}
}
beans.registerBeans(event.ctx)
enhanceDomainClasses(application, applicationContext)
application.refreshConstraints()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,11 @@ class GroovyPagesGrailsPlugin extends Plugin {
if (taglibClass) {
// replace tag library bean
def beanName = taglibClass.fullName
def beans = beans {
beans {
"$beanName"(taglibClass.clazz) { bean ->
bean.autowire = true
}
}
beans.registerBeans(event.ctx)

// The tag library lookup class caches "tag -> taglib class"
// so we need to update it now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
package org.grails.plugins.i18n

import grails.config.Settings
import grails.core.GrailsApplication
import grails.core.support.GrailsApplicationAware
import grails.plugins.Plugin
import grails.util.BuildSettings
import grails.util.Environment
import grails.util.GrailsUtil
import org.apache.commons.logging.LogFactory
import org.grails.spring.context.support.PluginAwareResourceBundleMessageSource
import org.grails.web.i18n.ParamsAwareLocaleChangeInterceptor
import org.grails.web.servlet.context.GrailsConfigUtils
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.support.ReloadableResourceBundleMessageSource
import org.springframework.core.io.Resource
import org.springframework.web.context.support.ServletContextResourcePatternResolver
import org.springframework.web.servlet.i18n.SessionLocaleResolver

/**
Expand All @@ -38,27 +33,29 @@ import org.springframework.web.servlet.i18n.SessionLocaleResolver
* @author Graeme Rocher
* @since 0.4
*/
class I18nGrailsPlugin implements GrailsApplicationAware, ApplicationContextAware {
class I18nGrailsPlugin extends Plugin {

private static LOG = LogFactory.getLog(this)
public static final String I18N_CACHE_SECONDS = 'grails.i18n.cache.seconds'
public static final String I18N_FILE_CACHE_SECONDS = 'grails.i18n.filecache.seconds'

String baseDir = "grails-app/i18n"
String version = GrailsUtil.getGrailsVersion()
String watchedResources = "file:./${baseDir}/**/*.properties".toString()
ApplicationContext applicationContext
GrailsApplication grailsApplication

def doWithSpring = {
@Override
Closure doWithSpring() {{->
def application = grailsApplication
def config = application.config
boolean gspEnableReload = config.getProperty(Settings.GSP_ENABLE_RELOAD, Boolean, false)

messageSource(PluginAwareResourceBundleMessageSource) {
fallbackToSystemLocale = false
pluginManager = manager
if (Environment.current.isReloadEnabled() || GrailsConfigUtils.isConfigTrue(application, Settings.GSP_ENABLE_RELOAD)) {
def cacheSecondsSetting = application?.flatConfig?.get('grails.i18n.cache.seconds')
cacheSeconds = cacheSecondsSetting == null ? 5 : cacheSecondsSetting as Integer
def fileCacheSecondsSetting = application?.flatConfig?.get('grails.i18n.filecache.seconds')
fileCacheSeconds = fileCacheSecondsSetting == null ? 5 : fileCacheSecondsSetting as Integer

if (Environment.current.isReloadEnabled() || gspEnableReload) {
cacheSeconds = config.getProperty(I18N_CACHE_SECONDS, Integer, 5)
fileCacheSeconds = config.getProperty(I18N_FILE_CACHE_SECONDS, Integer, 5)
}
}

Expand All @@ -67,7 +64,7 @@ class I18nGrailsPlugin implements GrailsApplicationAware, ApplicationContextAwar
}

localeResolver(SessionLocaleResolver)
}
}}



Expand All @@ -93,18 +90,19 @@ class I18nGrailsPlugin implements GrailsApplicationAware, ApplicationContextAwar
}


def onChange = { event ->
def ctx = event.ctx
@Override
void onChange(Map<String, Object> event) {
def ctx = applicationContext
def application = grailsApplication
if (!ctx) {
LOG.debug("Application context not found. Can't reload")
return
}

def nativeascii = application.config.getProperty('grails.enable.native2ascii', Boolean, true)
def resourcesDir = BuildSettings.RESOURCES_DIR
if (resourcesDir.exists() && event.source instanceof Resource) {
def eventFile = event.source.file.canonicalFile
def nativeascii = event.application.config.grails.enable.native2ascii
nativeascii = (nativeascii instanceof Boolean) ? nativeascii : true
def ant = new AntBuilder()
File i18nDir = new File("${Environment.current.reloadLocation}/grails-app/i18n").canonicalFile
if (isChildOfFile(eventFile, i18nDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package org.grails.plugins.services
import grails.plugins.Plugin
import grails.util.GrailsUtil
import groovy.transform.CompileStatic
import org.grails.spring.context.support.MapBasedSmartPropertyOverrideConfigurer
import org.springframework.context.ConfigurableApplicationContext

import java.lang.reflect.Method
import java.lang.reflect.Modifier
Expand Down Expand Up @@ -155,7 +157,7 @@ class ServicesGrailsPlugin extends Plugin {
}
props."*" = attributes

def beans = beans {
beans {
"${serviceClass.fullName}ServiceClass"(MethodInvokingFactoryBean) {
targetObject = application
targetMethod = "getArtefact"
Expand All @@ -174,18 +176,18 @@ class ServicesGrailsPlugin extends Plugin {
transactionManager = ref("transactionManager$suffix")
}
}
beans.registerBeans(applicationContext)

}
else {
def beans = beans {
beans {
"$serviceName"(serviceClass.getClazz()) { bean ->
bean.autowire = true
if (scope) {
bean.scope = scope
}
}
}
beans.registerBeans(event.ctx)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ abstract class AbstractGrailsTagTests extends GroovyTestCase {
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.mapping.UrlMappingsGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.controllers.ControllersGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.web.GroovyPagesGrailsPlugin")
dependantPluginClasses << gcl.loadClass("org.grails.plugins.logging.log4j.LoggingGrailsPlugin")

def dependentPlugins = dependantPluginClasses.collect { new DefaultGrailsPlugin(it, grailsApplication)}

Expand Down

This file was deleted.

0 comments on commit 2c78e40

Please sign in to comment.