Splitt out configuration to external context file

This commit is contained in:
Ziver Koc 2016-12-20 16:53:03 +01:00
parent 300ce27c07
commit 7390cb8b7a
4 changed files with 100 additions and 115 deletions

41
src/zall/Zallery.java Normal file → Executable file
View file

@ -6,6 +6,9 @@ import java.util.List;
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.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@ -31,7 +34,6 @@ public class Zallery extends HttpServlet{
public static String WEBSITE_NAME = "Example.com";
public static String WEBSITE_URL = "http://example.com";
public static String THEME = "";
public static String ROOT_PATH = "";
public static String DATA_PATH = "";
@ -41,30 +43,25 @@ public class Zallery extends HttpServlet{
* <br>- WEBSITE_URL
* <br>- SMTP_HOST
* <br>- DATA_PATH
* <br>- THEME
* <br>- FB_APPID
* <br>- FB_APPSEC
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
// java:comp/env
ROOT_PATH = config.getServletContext().getRealPath("/");
if( config.getInitParameter("WEBSITE_NAME") != null )
WEBSITE_NAME = config.getInitParameter("WEBSITE_NAME");
if( config.getInitParameter("WEBSITE_URL") != null ){
WEBSITE_URL = config.getInitParameter("WEBSITE_URL");
if( WEBSITE_URL.charAt(WEBSITE_URL.length()-1) != '/')
WEBSITE_URL += "/";
}
if( config.getInitParameter("SMTP_HOST") != null )
Email.setServer( config.getInitParameter("SMTP_HOST") );
if( config.getInitParameter("DATA_PATH") != null )
DATA_PATH = config.getInitParameter("DATA_PATH");
else
throw new ServletException("Missing DATA_PATH parameter!");
if( config.getInitParameter("THEME") != null )
THEME = config.getInitParameter("THEME");
LogUtil.setLevel("zall", Level.FINEST);
//LogUtil.setLevel("zutil", Level.FINEST);
try {
Context context = new InitialContext();
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 += "/";
Email.setServer( (String)context.lookup("java:comp/env/SMTP_HOST") );
DATA_PATH = (String)context.lookup("java:comp/env/DATA_PATH");
LogUtil.setLevel("zall", Level.FINEST);
//LogUtil.setLevel("zutil", Level.FINEST);
} catch (NamingException e) {
throw new ServletException(e);
}
}
public void destroy(){
@ -272,7 +269,7 @@ public class Zallery extends HttpServlet{
}
protected void include(String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(THEME+"/"+url);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/"+url);
if (dispatcher != null)
dispatcher.include(request, response);
}