views:

713

answers:

1

I am using JBoss4.0.1 and Struts2.1.6

I have an application which have some configuration file(appConfig.xml in folder WEB-INF/config). This congiuration file contains the relative paths of other files to be read.(other.xml, some.xml etc). Application is deployed as .war inside default/deploy

I have a Utility package that reads the configuration files. I have deployed the utility.jar inside default/lib

In contextInitialized method of ServletContextListener i am reading the appConfig.xml and passing IOStream on appConfig to my Utility Class, which reads this file and loads all the context relative paths.

how can i read these files now? becuase if i try to create an input stream it is returned NULL.

I have tried following inside Utility Class.

read method is called from inside the contextInitialized method of ServletContextListener

public void read(){ 
this.getClass().getClassLoader.getResourceAsStream("/WEB-INF/config/some.xml");
}

public void read(ClassLoader cl){ 
  cl.getResourceAsStream("/WEB-INF/config/some.xml"); --> Null
  cl.getResourceAsStream("/../config/some.xml"); --> Null
  cl.getResourceAsStream("../config/some.xml"); --> Null
}

public void read(ServletContext ct){ 
 ct.getResourceAsStream("/WEB-INF/config/some.xml");.
 ct.getResourceAsStream("/../config/some.xml"); --> Null
 ct.getResourceAsStream("../config/some.xml"); --> Null
}

any help will highly be obliged.

A: 

None of following would work because /WEB-INF/config is not in classpath so classloader doesn't know anything about it,

public void read(){ 
this.getClass().getClassLoader.getResourceAsStream("/WEB-INF/config/some.xml");
}

public void read(ClassLoader cl){ 
  cl.getResourceAsStream("/WEB-INF/config/some.xml"); --> Null
  cl.getResourceAsStream("/../config/some.xml"); --> Null
  cl.getResourceAsStream("../config/some.xml"); --> Null
}

Those 2 doesn't work because it goes out of the context (.. can't be first element),

public void read(ServletContext ct){ 

 ct.getResourceAsStream("/../config/some.xml"); --> Null
 ct.getResourceAsStream("../config/some.xml"); --> Null
}

I don't quite understand your intention. If you know the file is in "/WEB-INF/config/some.xml", why do you want read it from "/../config/some.xml"?

It's not a good idea to store configuration file inside war. You should consider moving it somewhere else.

ZZ Coder
thanks for your comments, Just want to add a couple fo things for your understanding.1. WEB-INF/config is in added in class path2. this is one time configuration so i need it inside the .war.3. there can be multiuple application context to have the same configuration so i created one utility package which reads this kind of configuration. Now from within the context i want the utility to read my configuration file.i hope this will better help you understand the problemyour help is appreciated.
Muhammad Shahid