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. |
ae1838
|
41 |
* |
U |
42 |
* @deprecated class is obsolete, do not use anymore |
ca8e1e
|
43 |
*/ |
ae1838
|
44 |
@Deprecated |
ca8e1e
|
45 |
public class WebApp implements ServletContextListener { |
U |
46 |
|
6b1009
|
47 |
/*public static final String P_LOGGERNAME = "loggername"; |
ca8e1e
|
48 |
public static final String P_LOGDIRNAME = "logdirname"; |
U |
49 |
public static final String P_LOGFILENAME = "logfilename"; |
|
50 |
public static final String P_LOGFILESIZE = "logfilesize"; |
|
51 |
public static final String P_LOGFILECOUNT = "logfilecount"; |
6b1009
|
52 |
public static final String P_LOGLEVEL = "loglevel";*/ |
ca8e1e
|
53 |
|
U |
54 |
public static final String P_DEBUG = "debug"; |
|
55 |
|
|
56 |
public static final String WEBAPP_LOGGER = "webapplogger"; |
|
57 |
|
6b1009
|
58 |
/* |
ca8e1e
|
59 |
private Level getLevel(String levelStr) { |
U |
60 |
Level level = Level.INFO; |
|
61 |
if(levelStr.equalsIgnoreCase(Level.ALL.getName())) { |
|
62 |
level = Level.ALL; |
|
63 |
} else if(levelStr.equalsIgnoreCase(Level.CONFIG.getName())) { |
|
64 |
level = Level.CONFIG; |
|
65 |
} else if(levelStr.equalsIgnoreCase(Level.FINEST.getName())) { |
|
66 |
level = Level.FINEST; |
|
67 |
} else if(levelStr.equalsIgnoreCase(Level.FINER.getName())) { |
|
68 |
level = Level.FINER; |
|
69 |
} else if(levelStr.equalsIgnoreCase(Level.FINEST.getName())) { |
|
70 |
level = Level.FINE; |
|
71 |
} else if(levelStr.equalsIgnoreCase(Level.OFF.getName())) { |
|
72 |
level = Level.OFF; |
|
73 |
} else if(levelStr.equalsIgnoreCase(Level.SEVERE.getName())) { |
|
74 |
level = Level.SEVERE; |
|
75 |
} else if(levelStr.equalsIgnoreCase(Level.WARNING.getName())) { |
|
76 |
level = Level.WARNING; |
|
77 |
} |
|
78 |
return level; |
|
79 |
} |
6b1009
|
80 |
*/ |
ca8e1e
|
81 |
public void contextInitialized(ServletContextEvent event) { |
U |
82 |
ServletContext servletContext = event.getServletContext(); |
6b1009
|
83 |
/* |
ca8e1e
|
84 |
Object o = servletContext.getAttribute(WEBAPP_LOGGER); |
6b1009
|
85 |
|
ca8e1e
|
86 |
if(o == null) { |
U |
87 |
String loggername = servletContext.getInitParameter(P_LOGGERNAME); // Name der root Package dieser Anwendung |
|
88 |
String logdirname = servletContext.getInitParameter(P_LOGDIRNAME); |
|
89 |
String logfilename = servletContext.getInitParameter(P_LOGFILENAME); // etwas wie z.B. "log%g.%u.txt" |
|
90 |
String logfilesize = servletContext.getInitParameter(P_LOGFILESIZE); // Anzahl Bytes |
|
91 |
String logfilecount = servletContext.getInitParameter(P_LOGFILECOUNT); // Anzahl Dateien fuer Rotation |
|
92 |
String loglevel = servletContext.getInitParameter(P_LOGLEVEL); |
|
93 |
FileHandler fh; |
|
94 |
Logger logger = Logger.getLogger(loggername); |
|
95 |
servletContext.setAttribute(WEBAPP_LOGGER, logger); |
|
96 |
try { |
|
97 |
File basis = new File(this.getClass().getResource("/").toURI()); |
|
98 |
File catalinaBase = basis.getParentFile().getParentFile().getParentFile().getParentFile(); |
|
99 |
File f = new File(catalinaBase, logdirname); |
|
100 |
if (!f.exists()) { |
|
101 |
f.mkdirs(); |
|
102 |
} |
|
103 |
fh = new FileHandler(f.getAbsolutePath() + File.separator + logfilename, |
|
104 |
Integer.parseInt(logfilesize), Integer.parseInt(logfilecount), true); |
|
105 |
//fh.setLevel(getLevel(loglevel)); |
|
106 |
fh.setFormatter(new TextLogFormatter()); |
|
107 |
logger.setLevel(getLevel(loglevel)); |
|
108 |
logger.addHandler(fh); |
|
109 |
logger.finest("logger " + loggername + " erzeugt"); |
|
110 |
} catch (Exception e) { |
|
111 |
logger.log(Level.ALL, e.getMessage(), e); |
|
112 |
} |
|
113 |
} else { |
|
114 |
// logger ist schon da |
|
115 |
} |
6b1009
|
116 |
*/ |
ca8e1e
|
117 |
} |
U |
118 |
|
|
119 |
public void contextDestroyed(ServletContextEvent event) { |
|
120 |
ServletContext servletContext = event.getServletContext(); |
6b1009
|
121 |
/* |
ca8e1e
|
122 |
Object o = servletContext.getAttribute(WEBAPP_LOGGER); |
U |
123 |
if(o != null) { |
|
124 |
Logger logger = (Logger) o; |
|
125 |
Handler[] handlers = logger.getHandlers(); |
|
126 |
for(int i = 0; i < handlers.length; i++) { |
|
127 |
handlers[i].flush(); // |
|
128 |
if(handlers[i] instanceof FileHandler) { |
|
129 |
((FileHandler) handlers[i]).close(); |
|
130 |
} |
|
131 |
logger.removeHandler(handlers[i]); |
|
132 |
} |
|
133 |
servletContext.removeAttribute(WEBAPP_LOGGER); |
|
134 |
logger = null; |
|
135 |
} |
6b1009
|
136 |
*/ |
ca8e1e
|
137 |
} |
U |
138 |
|
|
139 |
/** |
|
140 |
* Den Logger aus dem ServletContext ermitteln. |
|
141 |
*/ |
|
142 |
protected Logger getLogger(ServletContext ctx) { |
|
143 |
Logger logger = null; |
|
144 |
Object o = ctx.getAttribute(WEBAPP_LOGGER); |
|
145 |
if(o != null) { |
|
146 |
logger = (Logger) o; |
|
147 |
} |
|
148 |
return logger; |
|
149 |
} |
|
150 |
} |
|
151 |
|