telosys
Blame | Last modification | View Log | RSS feed
package org.objectweb.telosys.common;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.objectweb.telosys.dal.tools.ConnectionPoolThread;
import org.objectweb.telosys.screen.env.ScreenApplication;
import org.objectweb.telosys.screen.env.ScreenApplicationManager;
import org.objectweb.telosys.util.StrUtil;
/**
*
* @author Laurent GUERIN
*
*/
public abstract class TelosysContextListener implements ServletContextListener {
private static final String SEPARATOR1 = "===========================================================================";
private static final String SEPARATOR2 = "---------------------------------------------------------------------------";
private static final TelosysClassLogger log = new TelosysClassLogger(TelosysContextListener.class);
private ConnectionPoolThread _connectionPoolThread = null ;
/**
* Constructs a ServletContextListener dedicated to a Telosys web application
*/
public TelosysContextListener()
{
super();
log.info("Context listener instance created.");
}
/**
* First initialization step : Telosys initialization
* @param servletContext
* @param screenApplication
*/
public abstract void initTelosys(ServletContext servletContext, ScreenApplication screenApplication);
/**
* Second initialization step : Application initialization
* @param servletContext
* @param screenApplication
*/
public abstract void initApplication(ServletContext servletContext, ScreenApplication screenApplication);
/* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
*/
public void contextInitialized(ServletContextEvent event)
{
log.trace("context initialized");
ServletContext servletContext = event.getServletContext();
info(SEPARATOR1);
info("Application start-up ...");
info(SEPARATOR1);
//--- STEP 1 : TELOSYS STARTUP
infoStep(1);
Telosys.startup(servletContext);
printContextInfo(servletContext);
info(SEPARATOR2);
printTelosysInfo();
info(SEPARATOR2);
//--- STEP 2 : CHECK SCREEN APPLICATION OBJECT
infoStep(2);
// ScreenApplication screenApplication = ScreenApplicationManager.openScreenApplication ( servletContext );
// if ( screenApplication != null )
// {
// info("ScreenApplication instance created.");
// }
// else
// {
// error("Cannot open ScreenApplication !" );
// }
ScreenApplication screenApplication = ScreenApplicationManager.getScreenApplication();
if ( screenApplication != null )
{
info("ScreenApplication ready.");
}
else
{
error("Cannot get ScreenApplication !" );
}
//--- STEP 3 : DELEGATE TELOSYS INITIALIZATION
infoStep(3);
info("Telosys initialization ...");
try {
initTelosys(servletContext, screenApplication);
} catch (Exception e)
{
//error("Exception in initialization ! ");
error("Exception = " + e.toString() + " - Message = " + e.getMessage());
e.printStackTrace();
} catch (Throwable t)
{
//error("Throwable exception in initialization ! ");
error("Throwable exception = " + t.toString() + " - Message = " + t.getMessage());
t.printStackTrace();
}
info(SEPARATOR2);
printTelosysConfig();
info(SEPARATOR2);
//--- STEP 4 : DELEGATE APPLICATION INITIALIZATION
infoStep(4);
info("Application initialization ...");
try {
initApplication(servletContext, screenApplication);
} catch (Exception e)
{
//error("Exception in initialization ! ");
error("Exception = " + e.toString() + " - Message = " + e.getMessage());
e.printStackTrace();
} catch (Throwable t)
{
//error("Throwable exception in initialization ! ");
error("Throwable exception = " + t.toString() + " - Message = " + t.getMessage());
t.printStackTrace();
}
//--- STEP 5 : CONNECTION POOL THREAD MANAGEMENT
infoStep(5);
startConnectionPoolThread();
info(SEPARATOR1);
info("End of application start-up.");
info(SEPARATOR1);
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
*/
public void contextDestroyed(ServletContextEvent event)
{
info(SEPARATOR1);
stopConnectionPoolThread() ;
info(SEPARATOR1);
// TODO : Stop ConnectionPoolThread
info("Context destroyed.");
info(SEPARATOR1);
}
//-----------------------------------------------------------------------------
/**
* Print the step message
* @param step
*/
public void infoStep(int step)
{
info("========== STEP " + step + " ...");
}
//-----------------------------------------------------------------------------
/**
* Print a standard info message, using the active loggers
* @param s
*/
public void info(String s)
{
Telosys.info(this.getClass().getName(), s);
}
//-----------------------------------------------------------------------------
/**
* Print a standard error message, using the active loggers
* @param sMsg
*/
public void error(String sMsg)
{
Telosys.error(this.getClass().getName(), sMsg);
}
//-----------------------------------------------------------------------------
// private void printSeparator()
// {
// info(SEPARATOR);
// }
//-----------------------------------------------------------------------------
private void printContextInfo(ServletContext context)
{
if (context != null)
{
info("ServletContext : " );
info(" . Server Info : " + context.getServerInfo());
info(" . Version : " + context.getMajorVersion() + "." + context.getMinorVersion());
info(" . Context Name : " + context.getServletContextName());
info(" . Real Path : " + context.getRealPath(""));
}
else
{
//info("Context not found ! ");
error("ServletContext is null !");
}
}
//-----------------------------------------------------------------------------
private void printTelosysInfo()
{
info("TELOSYS Version : " + Telosys.getVersion() );
}
//-----------------------------------------------------------------------------
private void printTelosysConfig()
{
info("Telosys configuration");
info(" . Web App Context Name : " + Telosys.getWebAppName() );
info(" . Web App Root Directory : " + Telosys.getWebAppRootDir() );
//info(" . Configuration directory = " + Telosys.getConfigDirectory());
//info(" . transform('./myfile.txt') = " + Telosys.transformFileName("./myfile.txt"));
//info(" . transform('$ROOT/myfile.txt') = " + Telosys.transformFileName("$ROOT/myfile.txt"));
//info(" . Properties file name = " + Telosys.getPropertiesFile());
info(" . SAX parser class : " + Telosys.getSAXParserClassName() );
info(" . Authentication required : " + Telosys.isAuthenticationRequired() ) ;
info(" . Factory : " + ( Telosys.getFactory() != null ? "ready" : "not set !" ) ) ;
}
//-----------------------------------------------------------------------------
/**
* Starts the Connection Pool Thread if necessary <br>
* ( if there's a property set in telosys.properties )
*
* @since v 1.0.2
*/
private void startConnectionPoolThread()
{
info("Start ConnectionPoolThread management ...");
int iSleepDuration = -1 ;
Properties prop = Telosys.getProperties();
if ( prop != null )
{
String sSleepDuration = prop.getProperty("ConnectionPoolThread.sleepDuration");
if ( sSleepDuration != null )
{
iSleepDuration = StrUtil.getInt(sSleepDuration, -1);
}
if ( iSleepDuration > 0 )
{
if ( iSleepDuration < 600 ) // Less than 10 minutes is not acceptable
{
info("Duration property increased to 600 seconds ( original value = " + iSleepDuration + " )");
iSleepDuration = 600 ;
}
try
{
ConnectionPoolThread thread = new ConnectionPoolThread(iSleepDuration);
thread.start();
info("Thread " + thread.toString() + " started : sleep duration = " + iSleepDuration );
_connectionPoolThread = thread ;
} catch (Throwable t)
{
error("Exception : " + t.toString() + " - Message = " + t.getMessage());
t.printStackTrace();
}
}
else
{
info("No property for ConnectionPoolThread => thread not started.");
}
}
else
{
info("Telosys properties not loaded => thread not started.");
}
}
private void stopConnectionPoolThread()
{
if ( _connectionPoolThread != null )
{
info("Stop ConnectionPoolThread management ...");
_connectionPoolThread.interrupt();
}
else
{
info("No ConnectionPoolThread to stop.");
}
}
}