Malki
Malki

Reputation: 2385

Struts Java: how to read struts-config.xml from my java code?

I have a java web application working on struts. I want to access the data in the struts-config.xml file in runtime.

I tried to access it like a simple file, but it's unreachable to the application because it's outside the root directory of the app.

How does struts itself read the file? And how can I immitate it in runtime? I just need to read it like a simple xml file.

Thanks.

Upvotes: 0

Views: 2575

Answers (2)

Buhake Sindi
Buhake Sindi

Reputation: 89169

Struts does this simply by using ServletContext.

Because Struts ActionServlet extends HttpServlet, they simply do:

URL resource = getServletContext().getResource("/WEB-INF/struts-config.xml");

From there, You can get an InputStream and read the data from the resource.

Should the resource be null, the other alternative would be:

ClassLoader loader = Thread.currentThread().getContextClassLoader();

if (loader == null) {
    loader = this.getClass().getClassLoader();
}

Enumeration e = loader.getResources(path);
if (e != null && e.hasMoreElements()) {
    resource = (URL)e.nextElement();
}

The codes above is just simplified.

Upvotes: 2

caoxudong
caoxudong

Reputation: 373

In Stuts2, method init_TraditionalXmlConfigurations in class org.apache.struts2.dispatcher.Dispatcher is responsible to init xml configurations. It will search 3 files, struts-default.xml,struts-plugin.xml,struts.xml(they are defined in constant variant DEFAULT_CONFIGURATION_PATHS).

private void init_TraditionalXmlConfigurations() {
    String configPaths = initParams.get("config");
    if (configPaths == null) {
        configPaths = DEFAULT_CONFIGURATION_PATHS;
    }
    String[] files = configPaths.split("\\s*[,]\\s*");
    for (String file : files) {
        if (file.endsWith(".xml")) {
            if ("xwork.xml".equals(file)) {
                configurationManager.addContainerProvider(createXmlConfigurationProvider(file, false));
            } else {
                configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file, false, servletContext));
            }
        } else {
            throw new IllegalArgumentException("Invalid configuration file name");
        }
    }
}

Then, in method loadConfigurationFiles, it will get all configuration files url:

try {
    urls = getConfigurationUrls(fileName);
} catch (IOException ex) {
    ioException = ex;
}

And the following implementation is how to get configuration files' url:

protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException {
    return ClassLoaderUtil.getResources(fileName, XmlConfigurationProvider.class, false);
}
public static Iterator<URL> getResources(String resourceName, Class callingClass, boolean aggregate) throws IOException {

     AggregateIterator<URL> iterator = new AggregateIterator<URL>();

     iterator.addEnumeration(Thread.currentThread().getContextClassLoader().getResources(resourceName));

     if (!iterator.hasNext() || aggregate) {
         iterator.addEnumeration(ClassLoaderUtil.class.getClassLoader().getResources(resourceName));
     }

     if (!iterator.hasNext() || aggregate) {
         ClassLoader cl = callingClass.getClassLoader();

         if (cl != null) {
             iterator.addEnumeration(cl.getResources(resourceName));
         }
     }

     if (!iterator.hasNext() && (resourceName != null) && ((resourceName.length() == 0) || (resourceName.charAt(0) != '/'))) { 
         return getResources('/' + resourceName, callingClass, aggregate);
     }

     return iterator;
 }

The code above is how struts loads configuration.

For you, if you want to load struts-config.xml manually, you can use the following code:

String filePath = "your struts-config.xml file path";
URL resource = Thread.currentThread().getContextClassLoader().getResource(filePath);

Then, you can read the file like a simple xml file.

Upvotes: 1

Related Questions