1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.httpmonitor; |
---|
16 | |
---|
17 | import java.util.Timer; |
---|
18 | import java.util.TimerTask; |
---|
19 | |
---|
20 | import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange; |
---|
21 | import de.ugoe.cs.util.console.Console; |
---|
22 | |
---|
23 | /** |
---|
24 | * <p> |
---|
25 | * The log manager handles an {@link HttpMonitorOutputWriter} to perform the logging |
---|
26 | * of recorded exchanges. It initializes the writer if the first exchange is received and it |
---|
27 | * closes the writer if for a specific period of time no further exchange was received. The writer |
---|
28 | * themselves considers log rotation. For handling exchanges, the {@link HttpMonitorExchangeHandler} |
---|
29 | * mechanism is used. |
---|
30 | * </p> |
---|
31 | * |
---|
32 | * @author Patrick Harms |
---|
33 | */ |
---|
34 | public class HttpMonitorLogManager implements HttpMonitorComponent, HttpMonitorExchangeHandler { |
---|
35 | |
---|
36 | /** */ |
---|
37 | private static final long serialVersionUID = 1L; |
---|
38 | |
---|
39 | /** |
---|
40 | * the timeout after which the writer is closed |
---|
41 | */ |
---|
42 | private static final int SESSION_TIMEOUT = 10 * 60 * 1000; |
---|
43 | |
---|
44 | /** |
---|
45 | * the directory into which the log files shall be written |
---|
46 | */ |
---|
47 | private String logFileBaseDir; |
---|
48 | |
---|
49 | /** |
---|
50 | * the writer for the log file |
---|
51 | */ |
---|
52 | private HttpMonitorOutputWriter writer; |
---|
53 | |
---|
54 | /** |
---|
55 | * a timer used to detect timeouts |
---|
56 | */ |
---|
57 | private Timer logFileMonitorTimer; |
---|
58 | |
---|
59 | /** |
---|
60 | * <p> |
---|
61 | * initializes the log manager with the directory into which the log files shall be stored |
---|
62 | * </p> |
---|
63 | * |
---|
64 | * @param logFileBaseDir the directory into which the log files shall be stored |
---|
65 | */ |
---|
66 | public HttpMonitorLogManager(String logFileBaseDir) { |
---|
67 | this.logFileBaseDir = logFileBaseDir; |
---|
68 | } |
---|
69 | |
---|
70 | /* (non-Javadoc) |
---|
71 | * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init() |
---|
72 | */ |
---|
73 | @Override |
---|
74 | public synchronized void init() throws IllegalStateException, HttpMonitorException { |
---|
75 | if (logFileMonitorTimer != null) { |
---|
76 | throw new IllegalStateException("already initialized"); |
---|
77 | } |
---|
78 | |
---|
79 | logFileMonitorTimer = new Timer(); |
---|
80 | } |
---|
81 | |
---|
82 | /* (non-Javadoc) |
---|
83 | * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start() |
---|
84 | */ |
---|
85 | @Override |
---|
86 | public synchronized void start() throws IllegalStateException, HttpMonitorException { |
---|
87 | if (logFileMonitorTimer == null) { |
---|
88 | throw new IllegalStateException("not initialized"); |
---|
89 | } |
---|
90 | |
---|
91 | logFileMonitorTimer.schedule |
---|
92 | (new LogFileMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2); |
---|
93 | } |
---|
94 | |
---|
95 | /* (non-Javadoc) |
---|
96 | * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop() |
---|
97 | */ |
---|
98 | @Override |
---|
99 | public synchronized void stop() { |
---|
100 | if (logFileMonitorTimer != null) { |
---|
101 | logFileMonitorTimer.cancel(); |
---|
102 | } |
---|
103 | |
---|
104 | if (writer != null) { |
---|
105 | writer.stop(); |
---|
106 | } |
---|
107 | |
---|
108 | writer = null; |
---|
109 | } |
---|
110 | |
---|
111 | /* (non-Javadoc) |
---|
112 | * @see HttpMonitoringListener#handleHttpMessage() |
---|
113 | */ |
---|
114 | @Override |
---|
115 | public synchronized void handleHttpExchange(HttpExchange httpExchange) { |
---|
116 | try { |
---|
117 | if (writer == null) { |
---|
118 | writer = new HttpMonitorOutputWriter(logFileBaseDir); |
---|
119 | writer.init(); |
---|
120 | } |
---|
121 | |
---|
122 | writer.handleHttpExchange(httpExchange); |
---|
123 | } |
---|
124 | catch (Exception e) { |
---|
125 | Console.printerrln("could not handle message: " + e); |
---|
126 | Console.logException(e); |
---|
127 | |
---|
128 | // determine, if the writer exists but is not able to log something. In this case, |
---|
129 | // destroy the writer (part of the message may be logged twice through this). |
---|
130 | if (writer != null) { |
---|
131 | try { |
---|
132 | writer.handleHttpExchange(httpExchange); |
---|
133 | } |
---|
134 | catch (Exception e1) { |
---|
135 | synchronized (this) { |
---|
136 | writer.stop(); |
---|
137 | writer = null; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * <p> |
---|
146 | * internal timer task used for detecting session timeouts of clients |
---|
147 | * </p> |
---|
148 | * |
---|
149 | * @author Patrick Harms |
---|
150 | */ |
---|
151 | public class LogFileMonitorTimerTask extends TimerTask { |
---|
152 | |
---|
153 | /* (non-Javadoc) |
---|
154 | * @see java.lang.Runnable#run() |
---|
155 | */ |
---|
156 | @Override |
---|
157 | public void run() { |
---|
158 | synchronized (HttpMonitorLogManager.this) { |
---|
159 | if (System.currentTimeMillis() - writer.getLastUpdate() > SESSION_TIMEOUT) { |
---|
160 | writer.stop(); |
---|
161 | writer = null; |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|
167 | |
---|
168 | } |
---|