/*
|
Transit - Remote procedure calls made simple
|
Copyright (c) 2012 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as published by
|
the Free Software Foundation, either version 3 of the License, or
|
(at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
*/
|
|
package de.uhilger.transit.web;
|
|
import java.io.File;
|
import java.util.logging.FileHandler;
|
import java.util.logging.Handler;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
|
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContextEvent;
|
import javax.servlet.ServletContextListener;
|
|
import de.uhilger.transit.TextLogFormatter;
|
|
/**
|
* Die Klasse WebApp kann von webanwendungen als Basisklasse verwendet
|
* werden. Sie etabliert Dinge, die von einer Webanwendung zentral
|
* benoetigt werden wie z.B. Protokollierung
|
*
|
* Die Klasse WebApp implementiert die Schnittstell ServletContextListener.
|
* Wird sie als Listener im Deployment Descriptor einer Webanwendung eingebunden,
|
* werden ihre Funktionen zum Start und zum Ende ihrer Laufzeit wirksam.
|
*/
|
public class WebApp implements ServletContextListener {
|
|
public static final String P_LOGGERNAME = "loggername";
|
public static final String P_LOGDIRNAME = "logdirname";
|
public static final String P_LOGFILENAME = "logfilename";
|
public static final String P_LOGFILESIZE = "logfilesize";
|
public static final String P_LOGFILECOUNT = "logfilecount";
|
public static final String P_LOGLEVEL = "loglevel";
|
|
public static final String P_DEBUG = "debug";
|
|
public static final String WEBAPP_LOGGER = "webapplogger";
|
|
private Level getLevel(String levelStr) {
|
Level level = Level.INFO;
|
if(levelStr.equalsIgnoreCase(Level.ALL.getName())) {
|
level = Level.ALL;
|
} else if(levelStr.equalsIgnoreCase(Level.CONFIG.getName())) {
|
level = Level.CONFIG;
|
} else if(levelStr.equalsIgnoreCase(Level.FINEST.getName())) {
|
level = Level.FINEST;
|
} else if(levelStr.equalsIgnoreCase(Level.FINER.getName())) {
|
level = Level.FINER;
|
} else if(levelStr.equalsIgnoreCase(Level.FINEST.getName())) {
|
level = Level.FINE;
|
} else if(levelStr.equalsIgnoreCase(Level.OFF.getName())) {
|
level = Level.OFF;
|
} else if(levelStr.equalsIgnoreCase(Level.SEVERE.getName())) {
|
level = Level.SEVERE;
|
} else if(levelStr.equalsIgnoreCase(Level.WARNING.getName())) {
|
level = Level.WARNING;
|
}
|
return level;
|
}
|
|
public void contextInitialized(ServletContextEvent event) {
|
ServletContext servletContext = event.getServletContext();
|
Object o = servletContext.getAttribute(WEBAPP_LOGGER);
|
if(o == null) {
|
String loggername = servletContext.getInitParameter(P_LOGGERNAME); // Name der root Package dieser Anwendung
|
String logdirname = servletContext.getInitParameter(P_LOGDIRNAME);
|
String logfilename = servletContext.getInitParameter(P_LOGFILENAME); // etwas wie z.B. "log%g.%u.txt"
|
String logfilesize = servletContext.getInitParameter(P_LOGFILESIZE); // Anzahl Bytes
|
String logfilecount = servletContext.getInitParameter(P_LOGFILECOUNT); // Anzahl Dateien fuer Rotation
|
String loglevel = servletContext.getInitParameter(P_LOGLEVEL);
|
FileHandler fh;
|
Logger logger = Logger.getLogger(loggername);
|
servletContext.setAttribute(WEBAPP_LOGGER, logger);
|
try {
|
File basis = new File(this.getClass().getResource("/").toURI());
|
File catalinaBase = basis.getParentFile().getParentFile().getParentFile().getParentFile();
|
File f = new File(catalinaBase, logdirname);
|
if (!f.exists()) {
|
f.mkdirs();
|
}
|
fh = new FileHandler(f.getAbsolutePath() + File.separator + logfilename,
|
Integer.parseInt(logfilesize), Integer.parseInt(logfilecount), true);
|
//fh.setLevel(getLevel(loglevel));
|
fh.setFormatter(new TextLogFormatter());
|
logger.setLevel(getLevel(loglevel));
|
logger.addHandler(fh);
|
logger.finest("logger " + loggername + " erzeugt");
|
} catch (Exception e) {
|
logger.log(Level.ALL, e.getMessage(), e);
|
}
|
} else {
|
// logger ist schon da
|
}
|
}
|
|
public void contextDestroyed(ServletContextEvent event) {
|
ServletContext servletContext = event.getServletContext();
|
Object o = servletContext.getAttribute(WEBAPP_LOGGER);
|
if(o != null) {
|
Logger logger = (Logger) o;
|
Handler[] handlers = logger.getHandlers();
|
for(int i = 0; i < handlers.length; i++) {
|
handlers[i].flush(); //
|
if(handlers[i] instanceof FileHandler) {
|
((FileHandler) handlers[i]).close();
|
}
|
logger.removeHandler(handlers[i]);
|
}
|
servletContext.removeAttribute(WEBAPP_LOGGER);
|
logger = null;
|
}
|
}
|
|
/**
|
* Den Logger aus dem ServletContext ermitteln.
|
*/
|
protected Logger getLogger(ServletContext ctx) {
|
Logger logger = null;
|
Object o = ctx.getAttribute(WEBAPP_LOGGER);
|
if(o != null) {
|
logger = (Logger) o;
|
}
|
return logger;
|
}
|
}
|