commit | author | age
|
ca8e1e
|
1 |
/* |
U |
2 |
Transit - Remote procedure calls made simple |
|
3 |
Copyright (c) 2012 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as published by |
|
7 |
the Free Software Foundation, either version 3 of the License, or |
|
8 |
(at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.transit.web; |
|
20 |
|
|
21 |
import java.io.File; |
|
22 |
import java.util.logging.FileHandler; |
|
23 |
import java.util.logging.Handler; |
|
24 |
import java.util.logging.Level; |
|
25 |
import java.util.logging.Logger; |
|
26 |
|
|
27 |
import javax.servlet.ServletContext; |
|
28 |
import javax.servlet.ServletContextEvent; |
|
29 |
import javax.servlet.ServletContextListener; |
|
30 |
|
|
31 |
import de.uhilger.transit.TextLogFormatter; |
|
32 |
|
|
33 |
/** |
|
34 |
* Die Klasse WebApp kann von webanwendungen als Basisklasse verwendet |
|
35 |
* werden. Sie etabliert Dinge, die von einer Webanwendung zentral |
|
36 |
* benoetigt werden wie z.B. Protokollierung |
|
37 |
* |
|
38 |
* Die Klasse WebApp implementiert die Schnittstell ServletContextListener. |
|
39 |
* Wird sie als Listener im Deployment Descriptor einer Webanwendung eingebunden, |
|
40 |
* werden ihre Funktionen zum Start und zum Ende ihrer Laufzeit wirksam. |
|
41 |
*/ |
|
42 |
public class WebApp implements ServletContextListener { |
|
43 |
|
6b1009
|
44 |
/*public static final String P_LOGGERNAME = "loggername"; |
ca8e1e
|
45 |
public static final String P_LOGDIRNAME = "logdirname"; |
U |
46 |
public static final String P_LOGFILENAME = "logfilename"; |
|
47 |
public static final String P_LOGFILESIZE = "logfilesize"; |
|
48 |
public static final String P_LOGFILECOUNT = "logfilecount"; |
6b1009
|
49 |
public static final String P_LOGLEVEL = "loglevel";*/ |
ca8e1e
|
50 |
|
U |
51 |
public static final String P_DEBUG = "debug"; |
|
52 |
|
|
53 |
public static final String WEBAPP_LOGGER = "webapplogger"; |
|
54 |
|
6b1009
|
55 |
/* |
ca8e1e
|
56 |
private Level getLevel(String levelStr) { |
U |
57 |
Level level = Level.INFO; |
|
58 |
if(levelStr.equalsIgnoreCase(Level.ALL.getName())) { |
|
59 |
level = Level.ALL; |
|
60 |
} else if(levelStr.equalsIgnoreCase(Level.CONFIG.getName())) { |
|
61 |
level = Level.CONFIG; |
|
62 |
} else if(levelStr.equalsIgnoreCase(Level.FINEST.getName())) { |
|
63 |
level = Level.FINEST; |
|
64 |
} else if(levelStr.equalsIgnoreCase(Level.FINER.getName())) { |
|
65 |
level = Level.FINER; |
|
66 |
} else if(levelStr.equalsIgnoreCase(Level.FINEST.getName())) { |
|
67 |
level = Level.FINE; |
|
68 |
} else if(levelStr.equalsIgnoreCase(Level.OFF.getName())) { |
|
69 |
level = Level.OFF; |
|
70 |
} else if(levelStr.equalsIgnoreCase(Level.SEVERE.getName())) { |
|
71 |
level = Level.SEVERE; |
|
72 |
} else if(levelStr.equalsIgnoreCase(Level.WARNING.getName())) { |
|
73 |
level = Level.WARNING; |
|
74 |
} |
|
75 |
return level; |
|
76 |
} |
6b1009
|
77 |
*/ |
ca8e1e
|
78 |
public void contextInitialized(ServletContextEvent event) { |
U |
79 |
ServletContext servletContext = event.getServletContext(); |
6b1009
|
80 |
/* |
ca8e1e
|
81 |
Object o = servletContext.getAttribute(WEBAPP_LOGGER); |
6b1009
|
82 |
|
ca8e1e
|
83 |
if(o == null) { |
U |
84 |
String loggername = servletContext.getInitParameter(P_LOGGERNAME); // Name der root Package dieser Anwendung |
|
85 |
String logdirname = servletContext.getInitParameter(P_LOGDIRNAME); |
|
86 |
String logfilename = servletContext.getInitParameter(P_LOGFILENAME); // etwas wie z.B. "log%g.%u.txt" |
|
87 |
String logfilesize = servletContext.getInitParameter(P_LOGFILESIZE); // Anzahl Bytes |
|
88 |
String logfilecount = servletContext.getInitParameter(P_LOGFILECOUNT); // Anzahl Dateien fuer Rotation |
|
89 |
String loglevel = servletContext.getInitParameter(P_LOGLEVEL); |
|
90 |
FileHandler fh; |
|
91 |
Logger logger = Logger.getLogger(loggername); |
|
92 |
servletContext.setAttribute(WEBAPP_LOGGER, logger); |
|
93 |
try { |
|
94 |
File basis = new File(this.getClass().getResource("/").toURI()); |
|
95 |
File catalinaBase = basis.getParentFile().getParentFile().getParentFile().getParentFile(); |
|
96 |
File f = new File(catalinaBase, logdirname); |
|
97 |
if (!f.exists()) { |
|
98 |
f.mkdirs(); |
|
99 |
} |
|
100 |
fh = new FileHandler(f.getAbsolutePath() + File.separator + logfilename, |
|
101 |
Integer.parseInt(logfilesize), Integer.parseInt(logfilecount), true); |
|
102 |
//fh.setLevel(getLevel(loglevel)); |
|
103 |
fh.setFormatter(new TextLogFormatter()); |
|
104 |
logger.setLevel(getLevel(loglevel)); |
|
105 |
logger.addHandler(fh); |
|
106 |
logger.finest("logger " + loggername + " erzeugt"); |
|
107 |
} catch (Exception e) { |
|
108 |
logger.log(Level.ALL, e.getMessage(), e); |
|
109 |
} |
|
110 |
} else { |
|
111 |
// logger ist schon da |
|
112 |
} |
6b1009
|
113 |
*/ |
ca8e1e
|
114 |
} |
U |
115 |
|
|
116 |
public void contextDestroyed(ServletContextEvent event) { |
|
117 |
ServletContext servletContext = event.getServletContext(); |
6b1009
|
118 |
/* |
ca8e1e
|
119 |
Object o = servletContext.getAttribute(WEBAPP_LOGGER); |
U |
120 |
if(o != null) { |
|
121 |
Logger logger = (Logger) o; |
|
122 |
Handler[] handlers = logger.getHandlers(); |
|
123 |
for(int i = 0; i < handlers.length; i++) { |
|
124 |
handlers[i].flush(); // |
|
125 |
if(handlers[i] instanceof FileHandler) { |
|
126 |
((FileHandler) handlers[i]).close(); |
|
127 |
} |
|
128 |
logger.removeHandler(handlers[i]); |
|
129 |
} |
|
130 |
servletContext.removeAttribute(WEBAPP_LOGGER); |
|
131 |
logger = null; |
|
132 |
} |
6b1009
|
133 |
*/ |
ca8e1e
|
134 |
} |
U |
135 |
|
|
136 |
/** |
|
137 |
* Den Logger aus dem ServletContext ermitteln. |
|
138 |
*/ |
|
139 |
protected Logger getLogger(ServletContext ctx) { |
|
140 |
Logger logger = null; |
|
141 |
Object o = ctx.getAttribute(WEBAPP_LOGGER); |
|
142 |
if(o != null) { |
|
143 |
logger = (Logger) o; |
|
144 |
} |
|
145 |
return logger; |
|
146 |
} |
|
147 |
} |
|
148 |
|