package hirondelle.web4j.config;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import hirondelle.web4j.StartupTasks;
import hirondelle.web4j.util.Util;
import hirondelle.web4j.database.DAOException;
import hirondelle.fish.main.codes.CodeTableUtil;
import hirondelle.web4j.Controller;
public final class Startup implements StartupTasks {
public void startApplication(ServletConfig aConfig, String aDbName) throws DAOException {
if (! Util.textHasContent(aDbName)){
fContext = aConfig.getServletContext();
checkForVersionMismatch();
}
else if (ConnectionSrc.DEFAULT.equals(aDbName)){
lookUpCodeTablesAndPlaceIntoAppScope();
}
else if (ConnectionSrc.TRANSLATION.equals(aDbName)){
fLogger.fine("Reading Translations.");
TranslatorImpl.read();
TranslatorImpl.startRecordingUnknowns();
}
}
public static void lookUpCodeTablesAndPlaceIntoAppScope() throws DAOException {
CodeTableUtil.init(fContext);
}
private static ServletContext fContext;
private static final Logger fLogger = Util.getLogger(Startup.class);
private void checkForVersionMismatch(){
String appVersion = getAppVersionWithoutBugFix();
String web4jVersion = getWeb4JVersion();
AppInfo appInfo = new AppInfo();
if ( appVersion.equals(web4jVersion)) {
fLogger.config("Version of Fish & Chips Club (" + appInfo.getVersion() + ") compatible with that of web4j.jar (" + web4jVersion + ")");
}
else {
fLogger.severe("Version of Fish & Chips Club (" + appInfo.getVersion() + ") NOT COMPATIBLE WITH THAT OF web4j.jar (" + web4jVersion + "). These versions should match exactly to ensure correct operation. Please update versions to ensure correct operation.");
}
}
private String getAppVersionWithoutBugFix(){
AppInfo appInfo = new AppInfo();
int end = appInfo.getVersion().lastIndexOf(".");
return appInfo.getVersion().substring(0,end);
}
private String getWeb4JVersion(){
int start = Controller.WEB4J_VERSION.indexOf("/");
return Controller.WEB4J_VERSION.substring(start + 1);
}
}