Improved log configuration init.

This commit is contained in:
Christian P. MOMON 2016-12-06 14:35:18 +01:00
parent cc4bec97c2
commit ad97832f48

View file

@ -31,14 +31,16 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.kiss4web.catchers.BlankCatcher; import fr.devinsy.kiss4web.hooks.BlankHook;
import fr.devinsy.kiss4web.catchers.FolderCatcher; import fr.devinsy.kiss4web.hooks.FolderHook;
import fr.devinsy.kiss4web.catchers.LongURLCatcher; import fr.devinsy.kiss4web.hooks.Hook;
import fr.devinsy.kiss4web.catchers.RootCatcher; import fr.devinsy.kiss4web.hooks.HookRegister;
import fr.devinsy.kiss4web.catchers.ShortURLCatcher; import fr.devinsy.kiss4web.hooks.LongURLHook;
import fr.devinsy.kiss4web.catchers.WebContentCatcher; import fr.devinsy.kiss4web.hooks.RootHook;
import fr.devinsy.kiss4web.catchers.WebInfCatcher; import fr.devinsy.kiss4web.hooks.ShortURLHook;
import fr.devinsy.kiss4web.catchers.XHTMLCatcher; import fr.devinsy.kiss4web.hooks.WebContentHook;
import fr.devinsy.kiss4web.hooks.WebInfHook;
import fr.devinsy.kiss4web.hooks.XHTMLHook;
/** /**
* *
@ -52,14 +54,14 @@ public class KissDispatcher extends HttpServlet
private static Logger logger = LoggerFactory.getLogger(KissDispatcher.class); private static Logger logger = LoggerFactory.getLogger(KissDispatcher.class);
public static final int DEFAULT_CACHE_AGE = 2 * 60 * 60; public static final int DEFAULT_CACHE_AGE = 2 * 60 * 60;
private String webclassesRootPackage; private KissClassCache cache;
/** /**
* *
*/ */
public KissDispatcher() public KissDispatcher()
{ {
this.webclassesRootPackage = null; this.cache = new KissClassCache();
} }
/** /**
@ -75,7 +77,7 @@ public class KissDispatcher extends HttpServlet
* </pre> * </pre>
* *
*/ */
public void dispatch(final HttpServletRequest request, final HttpServletResponse response, final String webClassesRootPath) throws IOException, ServletException public void dispatch(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{ {
long startTime = new Date().getTime(); long startTime = new Date().getTime();
@ -141,43 +143,30 @@ public class KissDispatcher extends HttpServlet
*/ */
// String urlPath = request.getPathInfo(); // String urlPath = request.getPathInfo();
if (BlankCatcher.matches(request)) HookRegister hookRegister = KissDispatcherFactory.instance().catchers();
if (hookRegister.hooks().isEmpty())
{ {
BlankCatcher.doCatch(response); hookRegister.register(new BlankHook());
hookRegister.register(new RootHook());
hookRegister.register(new LongURLHook());
hookRegister.register(new FolderHook());
hookRegister.register(new ShortURLHook());
hookRegister.register(new XHTMLHook());
hookRegister.register(new WebContentHook());
hookRegister.register(new WebInfHook());
} }
else if (RootCatcher.matches(request))
{ // Search a matching catcher.
RootCatcher.doCatch(this.getServletConfig(), request, response, webClassesRootPath); Hook hook = hookRegister.getMatching(this.getServletContext(), request);
} if (hook == null)
else if (LongURLCatcher.matches(request))
{
LongURLCatcher.doCatch(this.getServletConfig(), request, response, webClassesRootPath);
}
else if (FolderCatcher.matches(request))
{
FolderCatcher.doCatch(this.getServletConfig(), request, response, webClassesRootPath);
}
else if (ShortURLCatcher.matches(request))
{
ShortURLCatcher.doCatch(this.getServletConfig(), request, response, webClassesRootPath);
}
else if (XHTMLCatcher.matches(request))
{
XHTMLCatcher.doCatch(this.getServletConfig(), request, response, webClassesRootPath);
}
else if (WebContentCatcher.matches(this.getServletContext(), request))
{
WebContentCatcher.doCatch(this.getServletContext(), request, response);
}
else if (WebInfCatcher.matches(this.getServletContext(), request, webClassesRootPath))
{
WebInfCatcher.doCatch(this.getServletContext(), request, response, webClassesRootPath);
}
else
{ {
logger.info("Request not satisfied [" + request.getPathInfo() + "]"); logger.info("Request not satisfied [" + request.getPathInfo() + "]");
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);
} }
else
{
hook.process(this.getServletConfig(), this.getServletContext(), request, response);
}
// //
long endTime = new Date().getTime(); long endTime = new Date().getTime();
@ -190,7 +179,7 @@ public class KissDispatcher extends HttpServlet
@Override @Override
public void doDelete(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException public void doDelete(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{ {
dispatch(request, response, this.webclassesRootPackage); dispatch(request, response);
} }
/** /**
@ -199,7 +188,7 @@ public class KissDispatcher extends HttpServlet
@Override @Override
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{ {
dispatch(request, response, this.webclassesRootPackage); dispatch(request, response);
} }
/** /**
@ -208,7 +197,7 @@ public class KissDispatcher extends HttpServlet
@Override @Override
public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{ {
dispatch(request, response, this.webclassesRootPackage); dispatch(request, response);
} }
/** /**
@ -217,7 +206,7 @@ public class KissDispatcher extends HttpServlet
@Override @Override
public void doPut(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException public void doPut(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{ {
dispatch(request, response, this.webclassesRootPackage); dispatch(request, response);
} }
/** /**
@ -253,6 +242,13 @@ public class KissDispatcher extends HttpServlet
try try
{ {
System.out.println("Log configuration defined (" + logFilePathname + "), will use it."); System.out.println("Log configuration defined (" + logFilePathname + "), will use it.");
if (!logFilePathname.startsWith("/"))
{
logFilePathname = getServletContext().getRealPath("/") + logFilePathname;
System.out.println("Log configuration redefined (" + logFilePathname + "), will use it.");
}
org.apache.log4j.PropertyConfigurator.configure(getServletContext().getRealPath("/") + logFilePathname); org.apache.log4j.PropertyConfigurator.configure(getServletContext().getRealPath("/") + logFilePathname);
} }
catch (Exception exception) catch (Exception exception)
@ -264,9 +260,5 @@ public class KissDispatcher extends HttpServlet
logger = LoggerFactory.getLogger(this.getClass()); logger = LoggerFactory.getLogger(this.getClass());
logger.info("Log initialization done."); logger.info("Log initialization done.");
} }
//
this.webclassesRootPackage = getInitParameter("webClassesRootPackage");
logger.info("webClassesRootPackage loaded=[{}]", this.webclassesRootPackage);
} }
} }