jQuery in Action, 3rd edition
The moose likes Struts and the fly likes Readin struts-config.xml from Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Readin struts-config.xml from Action" Watch "Readin struts-config.xml from Action" New topic

Readin struts-config.xml from Action

William Frederico

Joined: Oct 29, 2004
Posts: 9
Hi all,

Hoping someone can help.

The short story is:
I'm looking for a way to read/parse struts-config.xml from inside an action. Obviously I will use an XML parser library like Xerces ... the question is where do I look for the struts-config.xml file from within an action. Is it even available from an Action? Any help would be greatly appreciated!

The long story is:
I am configuring multiple data sources in the struts-config file (each installation requires X data sources, the number of data sources vary from installation to installation so struts-config.xml data sources are different in each installation.) I need to get a list of the data-sources from the struts-config.xml file so that I can display appropriate choices.

ActionServlet does have a dataSources HashMap variable with all of the keys and values, unfortunately it is protected so I don't have access to it (and there are no "getters" for it.)

ActionServlet exposes findDataSource(), but you need to know the key name of the data source (which I don't)

So... the goal is to just parse out struts-config directly and grab the info I need. The problem is if it's not in the web area I can't URL to it (and I can't put it in a public area because it contains passwords!) So, how do I get to it?

Thanks for any help/advice!

William Frederico<br />Logic Mountain Software<br /><a href="http://www.logicmountain.com" target="_blank" rel="nofollow">http://www.logicmountain.com</a>
William Frederico

Joined: Oct 29, 2004
Posts: 9
Well, I knew if I looked hard enough I would find it and I did! Beat yas all to the draw

Anyway, it looks like the web server (at least Tomcat) provides a jndi key to the struts-config file. So, to get a reference to struts-config that you can read and parse, you can do this:

strutsConfigFile = servlet.getServletContext().getResource( "/WEB-INF/struts-config.xml" ).toString();

This returns you something like :

jndi: localhost/[your_application_name]/WEB-INF/struts-config.xml

The trick is that you meed to know the resource name is WEB-INF/struts-config.xml (I looked at the source for ActionServlet and viola - there it was! )

So anyway, using something like JDOM (which BTW is a pretty nice XML parser) you can do something like:

SAXBuilder builder = new SAXBuilder();

strutsConfigFile = servlet.getServletContext().getResource( "/WEB-INF/struts-config.xml" ).toString();

Document strutsDocument = builder.build(strutsConfigFile);

and just use the nice features of JDOM to parse out whatcha need from there.

Ok, hope this was helpful for yuse as it was for me!
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
The whole approsach is too messy.

There are TWO other simpler approaches.

The simplest would be to declare another class in same package as the action servlet and access the protected map variable from there.
[ September 24, 2005: Message edited by: Swamy Nathan ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Readin struts-config.xml from Action
It's not a secret anymore!