XML configuration of Log4j for the ServletHelper

The original ServletHelper was introduced in Separate a J2EE application from data/configuration. In short it is a generic way to make a J2EE application configurable. Just use the ServletHelper class to have easy log and application configuration and have the application data outside your deployed J2EE application. So redeploying your .war file won’t destroy your data or configuration.

The previous ServletHelper only supported a Log4j .properties file. This new version of the ServletHelper supports XML configuration of Log4j too. If the data/configuration directory contains a file name [servletname]-log4j.xml, it is used to configure Log4j for the J2EE application using the new version of the ServletHelper.

Check the original article for more information on how to use the ServletHelper.

package nl.vanhulzenonline.servlethelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;

public class ServletHelper
{
	public static Logger logger = null;
	public static Properties properties = null;

	private static ServletContext _context = null;

	private static String getServletName()
	{
		return _context.getContextPath().substring(1);
	}

	private static String getServletHomePathVariableName()
	{
		return getServletName().toUpperCase() + "_HOME";
	}

	private static String getServletHomePath()
	{
		String path = System.getenv(getServletHomePathVariableName());
		return (path == null) ? "" : path;
	}

	private static String getPropertiesFilePrefix()
	{
		return getServletHomePath() + File.separatorChar + getServletName().toLowerCase();
	}

	private static String getLog4jPropertiesFileName()
	{
		return getPropertiesFilePrefix() + "-log4j.properties";
	}

	private static String getLog4jXMLFileName()
	{
		return getPropertiesFilePrefix() + "-log4j.xml";
	}

	private static String getServletPropertiesFileName()
	{
		return getPropertiesFilePrefix() + ".properties";
	}

	private static Properties loadProperties(File file)
	{
		try
		{
			Properties properties = new Properties();
			FileInputStream stream = new FileInputStream(file);
			properties.load(stream);
			stream.close();
			return properties;
		}
		catch (Exception e)
		{
			logger.error("Failure reading " + getServletPropertiesFileName() + ". Unable to execute " + getServletName() + ".");
			return null;
		}
    }

	private static void initializeLog4j()
	{
		logger = Logger.getLogger(getServletName());

		// we have to initialize log4j first

		File file = new File(getLog4jXMLFileName());
		if (file.exists())
		{
			DOMConfigurator.configure(getLog4jXMLFileName()); // configure log4j
			logger.info("Using " + getLog4jXMLFileName() + " for log4j configuration.");
		}
		else
		{
			file = new File(getLog4jPropertiesFileName());
			if (file.exists())
			{
				PropertyConfigurator.configure(getLog4jPropertiesFileName()); // configure log4j
				logger.info("Using " + getLog4jPropertiesFileName() + " for log4j configuration.");
			}
			else
			{
				BasicConfigurator.configure(); // configure log4j
				logger.info("Using default log4j configuration.");
			}
		}
	}

	private static Boolean initializeServlet()
	{
		// checking the home path
		String homePath = getServletHomePath();
		if (homePath == "")
		{
			logger.error(getServletHomePathVariableName() + " is not set. Unable to execute " + getServletName() + ".");
			return false;
		}

		File homeDir = new File(homePath);
		if (!homeDir.exists())
		{
			logger.error(getServletHomePath() + " does not exist. Unable to execute " + getServletName() + ".");
			return false;
		}
		if(!homeDir.isDirectory())
		{
			logger.error(getServletHomePath() + " is not a directory. Unable to execute " + getServletName() + ".");
			return false;
		}

		// checking the properties file of the webapp and load the properties
		File propertiesFile = new File(getServletPropertiesFileName());
		if (propertiesFile.exists())
		{
			properties = loadProperties(propertiesFile);
			if (properties == null)
				return false;
		}
		else
		{
			logger.error(getServletPropertiesFileName() + " not found. Unable to execute " + getServletName() + ".");
			return false;
		}

		logger.info(getServletName() + " initialized.");

		return true;
	}

	public static Boolean initialize(ServletContext context)
	{
		if (_context != null)
			return false;

		_context = context;

		initializeLog4j();

		return initializeServlet();
	}
}