package com.icentris;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.Timer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.icentris.sql.ConnectionPool;
import com.icentris.util.PropertiesUtil;
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(InitServlet.class);
private static String docRoot;
private Timer timer;
public InitServlet() {
// System.out.println("DEBUG: InitServlet() executing...");
}
public void init(ServletConfig config)
throws ServletException
{
// System.out.println("DEBUG: InitServlet.init() executing...");
ServletContext application = config.getServletContext();
InputStream stream = null;
try {
setDocRoot( application.getRealPath("/") );
// System.out.println("DEBUG [InitServlet]: setting docRoot to [" + docRoot + "]");
// load db settings for this company
// this is where dbDriver, dbUrl, dbUser, and dbPass are specified
// initialize the "default" connection pool
Properties dbProps = new Properties();
URL propsURL = application.getResource("/WEB-INF/classes/db.properties");
if ( propsURL == null ) {
throw new IllegalStateException("cannot find WEB-INF/classes/db.properties!");
}
stream = propsURL.openStream();
dbProps.load(stream);
PropertiesUtil.interpolateProperties(dbProps);
ConnectionPool pool = new ConnectionPool( dbProps.getProperty("dbDriver"), dbProps.getProperty("dbUrl"),
dbProps.getProperty("dbUser"), dbProps.getProperty("dbPass") );
pool.setMaxConnections ( Integer.parseInt(dbProps.getProperty("maxConnections")) );
pool.setMinConnections ( Integer.parseInt(dbProps.getProperty("minConnections")) );
pool.setTimeoutInMinutes( Integer.parseInt(dbProps.getProperty("timeoutInMinutes")) );
pool.setStrictTimeout ( "true".equals(dbProps.getProperty("strictTimeout")) );
pool.setCloseOnError ( "true".equals(dbProps.getProperty("closeOnError")) );
pool.setShowDebugging ( "true".equals(dbProps.getProperty("showDebugging")) );
pool.setCallersToIgnore ( PropertiesUtil.getArray("callersToIgnore", ',', dbProps) );
double pruneRunInMins = Double.parseDouble( dbProps.getProperty("pruneRunInMinutes") );
if ( pruneRunInMins > -1 ) {
pool.startPruner( pruneRunInMins );
}
ConnectionPool.initializePool("default", pool);
} catch ( Exception e ) {
logger.error("init(ServletConfig)", e); //$NON-NLS-1$
} finally {
try { stream.close(); } catch (Exception e) {}
}
}
public void destroy () {
//System.out.println("DEBUG [InitServlet]: running destroy()...");
timer.cancel();
}
public static String getDocRoot () {
return docRoot;
}
public static void setDocRoot( String docRootPath ) {
docRoot = docRootPath;
}
}