<commit_before>package org.codehaus.xfire.spring;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireException;
import org.codehaus.xfire.XFireFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class XFireConfigLoader
{
    private Log log = LogFactory.getLog(XFireConfigLoader.class);
    
    public XFire loadConfig(String configPath, ServletContext servletCtx) throws XFireException
    {
    	if(configPath == null)
    	{
    		throw new XFireException("Configuration file required");
    	}
    	
    	ApplicationContext parent = null;
    	ClassPathXmlApplicationContext newCtx = null;
    	
    	if(servletCtx != null)
    	{
    		parent = (ApplicationContext)servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    	}

    	String configFiles[] = null;
    	
    	if((parent == null) || !parent.containsBean("xfire"))
		{
    		// Include all xfire definitions (including custom editors)
    		configPath = "org/codehaus/xfire/spring/xfire.xml," + configPath;
		} 
    	else 
    	{
    		// Include only custom editors, because they are nor inherited from springs parent defs.
			configPath = "org/codehaus/xfire/spring/customEditors.xml," + configPath;
    	}

        if(configPath.indexOf(",") != -1)
        {
            configFiles = configPath.split(",");
        } 
        else 
        {
        	configFiles = new String[]{configPath};
        }
        
		if(parent != null)
    	{
	       	newCtx = new ClassPathXmlApplicationContext(configFiles, parent);    		
    	} 
    	else 
    	{
    		newCtx = new ClassPathXmlApplicationContext(configFiles);
    	}
    				
    	if((servletCtx != null) && (servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) == null))
		{
    		servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, newCtx);
		}
        	
        XFire xfire = (XFire) newCtx.getBean("xfire");
        log.debug("Setting XFire instance: "+xfire);
        
        // TODO: don't like this
        XFireFactory.newInstance().setXFire(xfire);

        return xfire;
    }

}<commit_msg>refactor class to allow future extraction to cut down dependencies. Add single argument loadConfig() method to allow reuse of the code in a non Web environment.<commit_after>package org.codehaus.xfire.spring;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireException;
import org.codehaus.xfire.XFireFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * 
 */
public class XFireConfigLoader
{
    private Log log = LogFactory.getLog(XFireConfigLoader.class);
    
    // we might want to move this one out if we want to cut the dependency on the javax.servlet package
    public XFire loadConfig(String configPath, ServletContext servletCtx) throws XFireException
    {
        ApplicationContext parent = null;
        
        if(servletCtx != null)
        {
            parent = (ApplicationContext)servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        }

        ClassPathXmlApplicationContext newCtx = getXFireApplicationContext(configPath, parent);

        if((servletCtx != null) && (servletCtx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) == null))
        {
             servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, newCtx);
        }

        return getXFire(newCtx);
    }

    public XFire loadConfig(String configPath) throws XFireException
    {
        ClassPathXmlApplicationContext newCtx = getXFireApplicationContext(configPath, null);
        return getXFire(newCtx);
    }

    private XFire getXFire(ApplicationContext ctx)
    {
        XFire xfire = (XFire) ctx.getBean("xfire");
        log.debug("Setting XFire instance: "+xfire);
        
        // TODO: don't like this
        XFireFactory.newInstance().setXFire(xfire);

        return xfire;
    }

    /**
     * @param configPath may not be full
     * @param parent may be null
     * @throws XFireException  if the configuration file is missing
     */
    private ClassPathXmlApplicationContext getXFireApplicationContext(String configPath, ApplicationContext parent) throws XFireException
    {
        if(configPath == null)
        {
            throw new XFireException("Configuration file required");
        }
        
        ClassPathXmlApplicationContext newCtx = null;
        
        String configFiles[] = null;
        
        if((parent == null) || !parent.containsBean("xfire"))
        {
            // Include all xfire definitions (including custom editors)
            configPath = "org/codehaus/xfire/spring/xfire.xml," + configPath;
        } 
        else 
        {
            // Include only custom editors, because they are nor inherited from springs parent defs.
            configPath = "org/codehaus/xfire/spring/customEditors.xml," + configPath;
        }

        if(configPath.indexOf(",") != -1)
        {
            configFiles = configPath.split(",");
        } 
        else 
        {
            configFiles = new String[]{configPath};
        }
        
        if(parent != null)
        {
            newCtx = new ClassPathXmlApplicationContext(configFiles, parent);            
        } 
        else 
        {
            newCtx = new ClassPathXmlApplicationContext(configFiles);
        }

        return newCtx;
    }
}
{'fraction_non_alphanumeric': 0.09724473257698542, 'fraction_numerical': 0.00032414910858995135, 'mean_word_length': 2.8116121062384187, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5453012', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1763, 'n_words': 355}