FileDocCategorySizeDatePackage
StrutsXmlConfigurationProvider.javaAPI DocExample6319Mon Jul 23 13:26:54 BST 2007org.apache.struts2.config

StrutsXmlConfigurationProvider

public class StrutsXmlConfigurationProvider extends com.opensymphony.xwork2.config.providers.XmlConfigurationProvider
Override Xwork class so we can use an arbitrary config file

Fields Summary
private static final Log
LOG
private File
baseDir
private String
filename
private String
reloadKey
private ServletContext
servletContext
Constructors Summary
public StrutsXmlConfigurationProvider(boolean errorIfMissing)
Constructs the configuration provider

param
errorIfMissing If we should throw an exception if the file can't be found


                           
       
        this("struts.xml", errorIfMissing, null);
    
public StrutsXmlConfigurationProvider(String filename, boolean errorIfMissing, ServletContext ctx)
Constructs the configuration provider

param
filename The filename to look for
param
errorIfMissing If we should throw an exception if the file can't be found
param
ctx Our ServletContext

        super(filename, errorIfMissing);
        this.servletContext = ctx;
        this.filename = filename;
        reloadKey = "configurationReload-"+filename;
        Map<String,String> dtdMappings = new HashMap<String,String>(getDtdMappings());
        dtdMappings.put("-//Apache Software Foundation//DTD Struts Configuration 2.0//EN", "struts-2.0.dtd");
        setDtdMappings(dtdMappings);
        File file = new File(filename);
        if (file.getParent() != null) {
            this.baseDir = file.getParentFile();
        }
    
Methods Summary
protected java.net.URLfindInFileSystem(java.lang.String fileName)

        URL url = null;
        File file = new File(fileName);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Trying to load file " + file);
        }

        // Trying relative path to original file
        if (!file.exists()) {
            file = new File(baseDir, fileName);
        }
        if (file.exists()) {
            try {
                url = file.toURL();
            } catch (MalformedURLException e) {
                throw new IOException("Unable to convert "+file+" to a URL");
            }
        }
        return url;
    
protected java.util.IteratorgetConfigurationUrls(java.lang.String fileName)
Look for the configuration file on the classpath and in the file system

param
fileName The file name to retrieve
see
com.opensymphony.xwork2.config.providers.XmlConfigurationProvider#getConfigurationUrls

        URL url = null;
        if (baseDir != null) {
            url = findInFileSystem(fileName);
            if (url == null) {
                return super.getConfigurationUrls(fileName);
            }
        }
        if (url != null) {
            List<URL> list = new ArrayList<URL>();
            list.add(url);
            return list.iterator();
        } else {
            return super.getConfigurationUrls(fileName);
        }
    
public voidloadPackages()

        ActionContext ctx = ActionContext.getContext();
        ctx.put(reloadKey, Boolean.TRUE);
        super.loadPackages();
    
public booleanneedsReload()
Overrides needs reload to ensure it is only checked once per request

        ActionContext ctx = ActionContext.getContext();
        return ctx.get(reloadKey) == null && super.needsReload();

    
public voidregister(com.opensymphony.xwork2.inject.ContainerBuilder containerBuilder, com.opensymphony.xwork2.util.location.LocatableProperties props)

        if (servletContext != null && !containerBuilder.contains(ServletContext.class)) {
            containerBuilder.factory(ServletContext.class, new Factory() {
                public Object create(Context context) throws Exception {
                    return servletContext;
                }
                
            });
        }
        super.register(containerBuilder, props);
    
public java.lang.StringtoString()

        return ("Struts XML configuration provider ("+filename+")");