Closure doWithSpring()

in grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/GroovyPagesGrailsPlugin.groovy [100:273]


    Closure doWithSpring() {{->
        def application = grailsApplication
        Config config = application.config
        boolean developmentMode = isDevelopmentMode()
        Environment env = Environment.current

        boolean enableReload = env.isReloadEnabled() ||
                                config.getProperty(GroovyPagesTemplateEngine.CONFIG_PROPERTY_GSP_ENABLE_RELOAD, Boolean, false) ||
                                    (developmentMode && env == Environment.DEVELOPMENT)

        boolean warDeployed = application.warDeployed
        boolean warDeployedWithReload = warDeployed && enableReload

        long gspCacheTimeout = config.getProperty(GSP_RELOAD_INTERVAL, Long,  (developmentMode && env == Environment.DEVELOPMENT) ? 0L : 5000L)
        boolean enableCacheResources = !config.getProperty(GroovyPagesTemplateEngine.CONFIG_PROPERTY_DISABLE_CACHING_RESOURCES, Boolean, false)
        String viewsDir = config.getProperty(GSP_VIEWS_DIR, '')

        RuntimeSpringConfiguration spring = springConfig

        // resolves JSP tag libraries
        boolean resolveJspTagLibraries = ClassUtils.isPresent('org.grails.gsp.jsp.TagLibraryResolverImpl', application.classLoader)
        if (resolveJspTagLibraries) {
            jspTagLibraryResolver(TagLibraryResolverImpl)
        }

        // resolves GSP tag libraries
        gspTagLibraryLookup(TagLibraryLookup) { bean ->
            bean.lazyInit = true
        }


        boolean customResourceLoader = false
        // If the development environment is used we need to load GSP files relative to the base directory
        // as oppose to in WAR deployment where views are loaded from /WEB-INF

        if (viewsDir) {
            log.info "Configuring GSP views directory as '${viewsDir}'"
            customResourceLoader = true
            groovyPageResourceLoader(GroovyPageResourceLoader) {
                baseResource = "file:${viewsDir}"
            }
        }
        else {
            if (developmentMode) {
                customResourceLoader = true
                groovyPageResourceLoader(GroovyPageResourceLoader) { bean ->
                    bean.lazyInit = true
                    def location = GroovyPagesGrailsPlugin.transformToValidLocation(BuildSettings.BASE_DIR.absolutePath)
                    baseResource = "file:$location"
                }
            }
            else {
                if (warDeployedWithReload && env.hasReloadLocation()) {
                    customResourceLoader = true
                    groovyPageResourceLoader(GroovyPageResourceLoader) {
                        def location = GroovyPagesGrailsPlugin.transformToValidLocation(env.reloadLocation)
                        baseResource = "file:${location}"
                    }
                }
            }
        }

        def deployed = !Metadata.getCurrent().isDevelopmentEnvironmentAvailable()
        groovyPageLocator(CachingGrailsConventionGroovyPageLocator) { bean ->
            bean.lazyInit = true
            if (customResourceLoader) {
                resourceLoader = groovyPageResourceLoader
            }
            if (deployed) {
                Resource defaultViews = applicationContext?.getResource('gsp/views.properties')

                if(defaultViews != null) {
                    if(!defaultViews.exists()) {
                        defaultViews = applicationContext?.getResource('classpath:gsp/views.properties')
                    }
                }

                if(defaultViews?.exists()) {
                    precompiledGspMap = { PropertiesFactoryBean pfb ->
                        ignoreResourceNotFound = true
                        locations = [defaultViews] as Resource[]
                    }
                }
            }
            if (enableReload) {
                cacheTimeout = gspCacheTimeout
            }
            reloadEnabled = enableReload
        }

        grailsResourceLocator(CachingGroovyPageStaticResourceLocator) { bean ->
            bean.parent = "abstractGrailsResourceLocator"
            if (enableReload) {
                cacheTimeout = gspCacheTimeout
            }
        }

        // Setup the main templateEngine used to render GSPs
        groovyPagesTemplateEngine(GroovyPagesTemplateEngine) {
            classLoader = ref("classLoader")
            groovyPageLocator = groovyPageLocator
            if (enableReload) {
                reloadEnabled = enableReload
            }
            tagLibraryLookup = gspTagLibraryLookup
            if (resolveJspTagLibraries) {
                jspTagLibraryResolver = jspTagLibraryResolver
            }
            cacheResources = enableCacheResources
        }

        spring.addAlias('groovyTemplateEngine', 'groovyPagesTemplateEngine')

        groovyPageRenderer(PageRenderer, ref("groovyPagesTemplateEngine")) { bean ->
            bean.lazyInit = true
            groovyPageLocator = groovyPageLocator
        }

        groovyPagesTemplateRenderer(GroovyPagesTemplateRenderer) { bean ->
            bean.autowire = true
            if (enableReload) {
                reloadEnabled = enableReload
            }
        }

        // Setup the GroovyPagesUriService
        groovyPagesUriService(DefaultGroovyPagesUriService) { bean ->
            bean.lazyInit = true
        }
        
        boolean jstlPresent = ClassUtils.isPresent(
            "jakarta.servlet.jsp.jstl.core.Config", InternalResourceViewResolver.class.getClassLoader())
        
        abstractViewResolver {
            prefix = GrailsApplicationAttributes.PATH_TO_VIEWS
            suffix = jstlPresent ? GroovyPageViewResolver.JSP_SUFFIX : GroovyPageViewResolver.GSP_SUFFIX
            resolveJspView = jstlPresent
            templateEngine = groovyPagesTemplateEngine
            groovyPageLocator = groovyPageLocator
            if (enableReload) {
                cacheTimeout = gspCacheTimeout
            }
        }
        // Configure a Spring MVC view resolver
        jspViewResolver(GroovyPageViewResolver) { bean ->
            bean.lazyInit = true
            bean.parent = "abstractViewResolver"
        }

        // Now go through tag libraries and configure them in Spring too. With AOP proxies and so on
        for (taglib in application.tagLibClasses) {

            final tagLibClass = taglib.clazz

            "${taglib.fullName}"(tagLibClass) { bean ->
                bean.autowire = true
                bean.lazyInit = true

                // Taglib scoping support could be easily added here. Scope could be based on a static field in the taglib class.
                //bean.scope = 'request'
            }
        }

        errorsViewStackTracePrinter(ErrorsViewStackTracePrinter, ref('grailsResourceLocator'))
        filteringCodecsByContentTypeSettings(FilteringCodecsByContentTypeSettings, application)

        groovyPagesServlet(ServletRegistrationBean, new GroovyPagesServlet(), "*.gsp") {
            if(Environment.isDevelopmentMode()) {
                initParameters = [showSource:"1"]
            }
        }

        grailsTagDateHelper(DefaultGrailsTagDateHelper)
    }}