Thursday, February 22, 2007

Reading Property Files with Axis 1.x

From the past: here is a little code for loading a config.properties file into Axis 1.x. The first version is useful for debugging when you are running things outside the servlet container (i.e. firing from the command line rather than inside tomcat). The second method shows how to do this from within the servlet container.

//Needed to get the ServletContext to read the properties.
import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPConstants;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

...

//Do this if running from the command line.
if(useClassLoader) {
System.out.println("Using classloader");
//This is useful for command line clients but does not work
//inside Tomcat.
ClassLoader loader=ClassLoader.getSystemClassLoader();
properties=new Properties();

//This works if you are using the classloader but not inside
//Tomcat.
properties.load(loader.getResourceAsStream("geofestconfig.properties"));
}
else {
//Extract the Servlet Context
System.out.println("Using Servlet Context");
MessageContext msgC=MessageContext.getCurrentContext();
ServletContext
context=((HttpServlet)msgC.getProperty(HTTPConstants.MC_HTTP_SERVLET))
.getServletContext();

String propertyFile=context.getRealPath("/")
+"/WEB-INF/classes/config.properties";
System.out.println("Prop file location "+propertyFile);

properties=new Properties();
properties.load(new
FileInputStream(propertyFile));
}

serverUrl=properties.getProperty("service.url");
baseWorkDir=properties.getProperty("base.workdir");
baseDestDir=properties.getProperty("base.dest.dir");
....

No comments: