• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Readin struts-config.xml from Action

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic