All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.grails.web.context.ServletEnvironmentGrailsApplicationDiscoveryStrategy.groovy Maven / Gradle / Ivy

/*
 * Copyright 2012-2023 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
 *
 *      https://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.
 */
package org.grails.web.context

import javax.servlet.ServletContext

import groovy.transform.CompileStatic
import org.springframework.context.ApplicationContext
import org.springframework.web.context.ContextLoader
import org.springframework.web.context.WebApplicationContext
import org.springframework.web.context.support.WebApplicationContextUtils

import grails.core.GrailsApplication

import org.grails.core.support.GrailsApplicationDiscoveryStrategy
import org.grails.web.servlet.mvc.GrailsWebRequest

/**
 * Strategy for discovering the GrailsApplication and ApplicationContext instances in the Servlet environment
 *
 * @author Graeme Rocher
 * @since 2.4
 */
@CompileStatic
class ServletEnvironmentGrailsApplicationDiscoveryStrategy implements GrailsApplicationDiscoveryStrategy {

    ServletContext servletContext
    ApplicationContext applicationContext

    ServletEnvironmentGrailsApplicationDiscoveryStrategy(ServletContext servletContext, ApplicationContext applicationContext = null) {
        this.servletContext = servletContext
        this.applicationContext = applicationContext
    }

    @Override
    GrailsApplication findGrailsApplication() {
        ApplicationContext context = findApplicationContext()
        if (context) {
            return context.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication)
        }

        GrailsWebRequest webReq = GrailsWebRequest.lookup()
        webReq?.applicationContext?.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication)
    }

    @Override
    ApplicationContext findApplicationContext() {
        if (applicationContext != null) {
            return applicationContext
        }
        if (servletContext == null) {
            return ContextLoader.currentWebApplicationContext
        }
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext)
        if (context) {
            return context
        }
        GrailsWebRequest.lookup()?.applicationContext
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy