package org.saf.application;
import org.apache.log4j.Logger;
import org.saf.shutdownhook.ShutdownHook;
import org.saf.shutdownhook.ShutdownHookListener;
public abstract class AbstractApplicationController implements
ShutdownHookListener, ApplicationCheckerListener {
protected static final Logger logger =
Logger.getLogger(AbstractApplicationController.class);
private final static String SHUTDOWN_HOOK_NAME = "shutdownHook";
protected ShutdownHook shutdownHook = null;
private Thread shutdownHookThread = null;
private ApplicationChecker applicationChecker = null;
private Thread applicationCheckerThread = null;
public Priority getPriority() {
return Priority.NORMAL;
}
public void shutdownHookReceived() {
logger.info("application controller shutdown process..");
customStop();
}
public void start() {
Runtime.getRuntime().addShutdownHook(this.shutdownHookThread);
this.applicationCheckerThread.start();
customStart();
}
protected abstract void customStart();
protected abstract void customStop();
protected AbstractApplicationController() {
init();
}
protected void forceStop() {
System.exit(0);
}
private void init() {
this.applicationChecker = new ApplicationChecker(this);
this.applicationCheckerThread =
new Thread(this.applicationChecker, "memory checker");
this.shutdownHook = new ShutdownHook();
this.shutdownHook.addShutdownHookListener(this);
this.shutdownHook.addShutdownHookListener(this.applicationChecker);
this.shutdownHookThread =
new Thread(shutdownHook, SHUTDOWN_HOOK_NAME);
}
}