zallery/src/zall/Zallery.java

88 lines
3.3 KiB
Java
Executable file

package zall;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import zutil.ui.UserMessageManager;
@WebServlet(value = "/init", loadOnStartup = 1)
public class Zallery extends HttpServlet{
private static Logger logger = LogUtil.getLogger();
public static final String VERSION = "2.0.0";
public static String WEBSITE_NAME = "";
public static String WEBSITE_URL = "";
public static String ADMIN_EMAIL = "";
public static String ADMIN_EMAIL_NICE = "";
public static String SMTP_HOST = "";
public static String ROOT_PATH = "";
public static String DATA_PATH = "";
/**
* Config Options:
* <br>- WEBSITE_NAME
* <br>- WEBSITE_URL
* <br>- SMTP_HOST
* <br>- DATA_PATH
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
logger.info("Setting up Zallery environment.");
ROOT_PATH = config.getServletContext().getRealPath("/");
try {
Context context = new InitialContext();
// Check if Zallery has been properly configured
if (((String) context.lookup("java:comp/env/DATA_PATH")).isEmpty())
throw new ServletException("Zallery has not been properly configured, set proper configuration in Zallery.xml context file.");
WEBSITE_NAME = (String) context.lookup("java:comp/env/WEBSITE_NAME");
WEBSITE_URL = (String) context.lookup("java:comp/env/WEBSITE_URL");
if( WEBSITE_URL.charAt(WEBSITE_URL.length()-1) != '/')
WEBSITE_URL += "/";
ADMIN_EMAIL = (String) context.lookup("java:comp/env/ADMIN_EMAIL");
ADMIN_EMAIL_NICE = (String) context.lookup("java:comp/env/ADMIN_EMAIL_NICE");
SMTP_HOST = (String) context.lookup("java:comp/env/SMTP_HOST");
DATA_PATH = (String) context.lookup("java:comp/env/DATA_PATH");
LogUtil.setLevel("zall", Level.parse((String) context.lookup("java:comp/env/LOGGING_ZALL")));
LogUtil.setLevel("zutil", Level.parse((String) context.lookup("java:comp/env/LOGGING_ZUTIL")));
logger.info("Zallery ready to be used.");
} catch (NamingException e) {
throw new ServletException(e);
}
}
public void destroy(){
}
public static DBConnection getDB() throws ServletException{
try {
return new DBConnection("jdbc/mysql");
} catch (Exception e) {
throw new ServletException("Was unable to initialize DB connection", e);
}
}
public static UserMessageManager getUserMessage(HttpSession session) {
if (session.getAttribute(ZalleryConstants.SESSION_KEY_USER_MSG) == null)
session.setAttribute(ZalleryConstants.SESSION_KEY_USER_MSG, new UserMessageManager());
return (UserMessageManager) session.getAttribute(ZalleryConstants.SESSION_KEY_USER_MSG);
}
}