I have written a static method in java class to read application specific properties from property file. This static method can be used by JSPs and other java classes. In order to load properties, I have to provide real path of the properties file. Since, this path is different on dev, staging and production m/c.

Christopher Schultz

I've had occation to write code like this, too. I wrote a simple interface to get an InputStream for a file:

public interface ResourceResolver
{
   public InputStream getResource(String filename)
      throws IOException;
}
You can create one of these and pass it to your utility method, so it can be called-back:

public void someMethod(String filename, ResourceResolver rr)
{
   InputStream is = rr.getResource(filename);

   // .. use the input stream
}
It can be created on-the-fly from your servlet, too:

public void doGet(...)
{
   ResourceResolver rr = new ResourceResolver()
   {
      public InputStream getResource(String filename)
         throws IOException
      {
          return getServletContext().getResourceAsStream(filename);
      }
   };

   UtilClass.someMethod(filename, rr);
}
You could adapt this technique to just resolve the filename, too, if you want.

Hope that helps,

-chris
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact