I have the following file structure in the war for my web application.
WEB-INF/classes/com/olf/servlets/AppServlet.class
WEB-INF/classes/com/olf/util/CommonUtil.class
WEB-INF/classes/com/olf/util/JndiUtil.class
WEB-INF/classes/com/olf/util/jndi.xml
When the JndiUtil is invoked by the CommonUtil , the JndiUtil has no problem to
locate the jndi.xml; however, when
the JndiUtil class is invoked by the AppServlet. An exception thrown from the
JndiUtil states that it can't locate
the XML file from WebLogic Home directory. Does anyone know why the search is
done on WebLogic Home or even got up there?
Any help is greatly appreciate!
kenny
In AppServlet.class
===================
public void init(ServletConfig config) throws ServletException
{
...
InitialContext ic = JndiUtil.getInstance().getInitialContext();
...
}
In JndiUtil.class
public final static JndiUtil getInstance()
{
...
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File("jnid.xml"), handler);
...
catch (SAXException e){
e.printStackTrace();
System.out.println("SAXException:" + e.getMessage());
}
...
}
Below is the exception thrown by the SAX parser
org.xml.sax.SAXParseException: File "file:C:/bea/wlserver6.1/jndi.xml" not found.
at weblogic.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1088)
at weblogic.apache.xerces.readers.DefaultEntityHandler.startReadingFromDocument(DefaultEntityHandler.java:512)
at weblogic.apache.xerces.framework.XMLParser.parseSomeSetup(XMLParser.java:310)
at weblogic.apache.xerces.framework.XMLParser.parse(XMLParser.java:966)
at weblogic.xml.jaxp.WebLogicXMLReader.parse(WebLogicXMLReader.java:123)
at weblogic.xml.jaxp.RegistryXMLReader.parse(RegistryXMLReader.java:125)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:346)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:286)
at com.olf.util.JndiUtil.getInstance(JndiUtil.java:59)
at com.olf.servlets.AppServlet.init(AppServlet.java:60)
at weblogic.servlet.internal.ServletStubImpl.createServlet(ServletStubImpl.java:700)
at weblogic.servlet.internal.ServletStubImpl.createInstances(ServletStubImpl.java:643)
at weblogic.servlet.internal.ServletStubImpl.prepareServlet(ServletStubImpl.java:588)
at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:368)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:242)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2495)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2204)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
Thanks for the thought! However, by using the ServletContext, the JndiUtil class
would be tighted to the Servlet Container because it expects the caller passing
in a ServletContext. I would like to make the JndiUtil class shared by other
components in the system instead of only serving Servlets.
Do you know if there is a way to locate a file in the system regardless which
container the call is in?
Thanks!
kenny
interface ResourceHandler {
public InputStream getStream();
}
and make our method take ResourceHandler as an argument.
which can be used as follows
JndiUtil.someMethod(new ResourceHandler(){
public InputStream getStream(String name){
return context.getResourceAsStream("name");
}
});
u can create a new inner class to serve a different purpose..
e.g. standalone
JndiUtil.someMethod(new ResourceHandler(){
public InputStream getStream(String name){
return new FileInputStream(name);
}
});
hope this helps
nagesh
PS: ooh yes, make this method throw an IOException
and also you can use an abstract class if u have any
common logic to be added to this class..