<commit_before>/*
 * 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.
 */
package org.codehaus.groovy.grails.web.servlet;

import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.spring.SpringConfig;
import org.springframework.beans.BeansException;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springmodules.beans.factory.drivers.xml.XmlWebApplicationContextDriver;

/**
 * <p>Servlet that handles incoming requests for Grails.
 * <p/>
 * <p>This servlet loads the Spring configuration based on the Grails application
 * in the parent application context.
 *
 * @author <NAME>
 * @since Jul 2, 2005
 */
public class GrailsDispatcherServlet extends DispatcherServlet {

    private static final String GRAILS_APPLICATION_ID = "grailsApplication";

    public GrailsDispatcherServlet() {
        super();
    }

    protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException {
        // use config file locations if available
        String[] locations = null;
        if (null != getContextConfigLocation()) {
            locations = StringUtils.tokenizeToStringArray(
                    getContextConfigLocation(),
                    ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
        }
        // construct the SpringConfig for the container managed application
        GrailsApplication application = (GrailsApplication) parent.getBean(GRAILS_APPLICATION_ID, GrailsApplication.class);
        SpringConfig springConfig = new SpringConfig(application);
        // return a context that obeys grails' settings
        return new XmlWebApplicationContextDriver().getWebApplicationContext(
                springConfig.getBeanReferences(),
                parent,
                getServletContext(),
                getNamespace(),
                locations);
    }

}<commit_msg>added check to see if the Grails context has already been loaded by a filter<commit_after>/*
 * 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.
 */
package org.codehaus.groovy.grails.web.servlet;

import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.spring.SpringConfig;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springmodules.beans.factory.drivers.xml.XmlWebApplicationContextDriver;

/**
 * <p>Servlet that handles incoming requests for Grails.
 * <p/>
 * <p>This servlet loads the Spring configuration based on the Grails application
 * in the parent application context.
 *
 * @author <NAME>
 * @since Jul 2, 2005
 */
public class GrailsDispatcherServlet extends DispatcherServlet {

    private static final String GRAILS_APPLICATION_ID = "grailsApplication";
    private static final String GRAILS_APPLICATION_CONTEXT = "grailsApplicationContext";

    public GrailsDispatcherServlet() {
        super();
    }

    protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException {
        // use config file locations if available
    	ApplicationContext grailsContext = (ApplicationContext)getServletContext().getAttribute(GRAILS_APPLICATION_CONTEXT );
    	
    	if(grailsContext != null) {
    		XmlWebApplicationContext webContext = new XmlWebApplicationContext();
    		webContext.setParent(grailsContext);    		
    		return webContext; 
    	}
    	else {
	        String[] locations = null;
	        if (null != getContextConfigLocation()) {
	            locations = StringUtils.tokenizeToStringArray(
	                    getContextConfigLocation(),
	                    ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
	        }
	        // construct the SpringConfig for the container managed application
	        GrailsApplication application = (GrailsApplication) parent.getBean(GRAILS_APPLICATION_ID, GrailsApplication.class);
	        SpringConfig springConfig = new SpringConfig(application);
	        // return a context that obeys grails' settings
	        return new XmlWebApplicationContextDriver().getWebApplicationContext(
	                springConfig.getBeanReferences(),
	                parent,
	                getServletContext(),
	                getNamespace(),
	                locations);
    	}
    }

}
{'fraction_non_alphanumeric': 0.07225288509784245, 'fraction_numerical': 0.005686569660478341, 'mean_word_length': 3.6106399383191983, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '261190', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1417, 'n_words': 441}