Friday, September 17, 2010

Load the xml file from the class which is in the same Jar file

Lets assume you have a class called samle and xml file called sample.xml file. Both class and xml files are in the jar file.

If you want to load the sample.xml from the sample.class file, you need to use the following code.

this.getClass().getClassLoader().getResource("sample.xml")

Sample examples:

public class Sample {
public void callXmlMapping() {
URL url = this.getClass().getClassLoader().getResource("sample.xml");
}
}


You can create jar file with these both files and add this jar file in class path of your webapplication.

Log4j setup and configuration specific to module

Lets assume you would like to enable logging for the modules which starts with the package called com.sample.*.*.*. You need to create a log4j.properties file in \web-inf\classes folder. And place foloowing entries in the log4j.properties file. And make sure that log4j.jar file needs to be there in your class path (Ex: web-inf\lib folder)

log4j.logger.com.sample=DEBUG, sample
log4j.appender.sample.File=${user.dir}/sample/logs/sample.log
log4j.appender.sample.MaxFileSize=10MB
log4j.appender.sample.MaxBackupIndex=10
log4j.appender.sample.Append=false
log4j.appender.sample=org.apache.log4j.RollingFileAppender
log4j.appender.sample.layout=org.apache.log4j.PatternLayout
log4j.appender.sample.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c - %m%n


Now, the log messages will redirect to the location ${user.dir}/sample/logs/sample.log.