source: trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/HttpMonitorLogManager.java @ 1374

Last change on this file since 1374 was 1374, checked in by pharms, 10 years ago

Initial import.

File size: 5.1 KB
Line 
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
15package de.ugoe.cs.autoquest.httpmonitor;
16
17import java.util.Timer;
18import java.util.TimerTask;
19
20import de.ugoe.cs.autoquest.httpmonitor.exchange.HttpExchange;
21import 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 */
34public class HttpMonitorLogManager implements HttpMonitorComponent, HttpMonitorExchangeHandler {
35   
36    /**
37     * the timeout after which the writer is closed
38     */
39    private static final int SESSION_TIMEOUT = 10 * 60 * 1000;
40   
41    /**
42     * the directory into which the log files shall be written
43     */
44    private String logFileBaseDir;
45
46    /**
47     * the writer for the log file
48     */
49    private HttpMonitorOutputWriter writer;
50
51    /**
52     * a timer used to detect timeouts
53     */
54    private Timer logFileMonitorTimer;
55
56    /**
57     * <p>
58     * initializes the log manager with the directory into which the log files shall be stored
59     * </p>
60     *
61     * @param logFileBaseDir the directory into which the log files shall be stored
62     */
63    public HttpMonitorLogManager(String logFileBaseDir) {
64        this.logFileBaseDir = logFileBaseDir;
65    }
66
67    /* (non-Javadoc)
68     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#init()
69     */
70    @Override
71    public synchronized void init() throws IllegalStateException, HttpMonitorException {
72        if (logFileMonitorTimer != null) {
73            throw new IllegalStateException("already initialized");
74        }
75       
76        logFileMonitorTimer = new Timer();
77    }
78
79    /* (non-Javadoc)
80     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#start()
81     */
82    @Override
83    public synchronized void start() throws IllegalStateException, HttpMonitorException {
84        if (logFileMonitorTimer == null) {
85            throw new IllegalStateException("not initialized");
86        }
87       
88        logFileMonitorTimer.schedule
89            (new LogFileMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2);
90    }
91
92    /* (non-Javadoc)
93     * @see de.ugoe.cs.autoquest.htmlmonitor.HttpMonitorComponent#stop()
94     */
95    @Override
96    public synchronized void stop() {
97        if (logFileMonitorTimer != null) {
98            logFileMonitorTimer.cancel();
99        }
100           
101        if (writer != null) {
102            writer.stop();
103        }
104       
105        writer = null;
106    }
107
108    /* (non-Javadoc)
109     * @see HttpMonitoringListener#handleHttpMessage()
110     */
111    @Override
112    public void handleHttpExchange(HttpExchange httpExchange) {
113        try {
114            if (writer == null) {
115                synchronized (this) {
116                    if (writer == null) {
117                        writer = new HttpMonitorOutputWriter(logFileBaseDir);
118                        writer.init();
119                    }
120                }
121            }
122
123            writer.handleHttpExchange(httpExchange);
124        }
125        catch (Exception e) {
126            Console.printerrln("could not handle message: " + e);
127            e.printStackTrace();
128            Console.logException(e);
129           
130            // determine, if the writer exists but is not able to log something. In this case,
131            // destroy the writer (part of the message may be logged twice through this).
132            if (writer != null) {
133                try {
134                    writer.handleHttpExchange(httpExchange);
135                }
136                catch (Exception e1) {
137                    synchronized (this) {
138                        writer.stop();
139                        writer = null;
140                    }
141                }
142            }
143        }
144    }
145
146    /**
147     * <p>
148     * internal timer task used for detecting session timeouts of clients
149     * </p>
150     *
151     * @author Patrick Harms
152     */
153    public class LogFileMonitorTimerTask extends TimerTask {
154
155        /* (non-Javadoc)
156         * @see java.lang.Runnable#run()
157         */
158        @Override
159        public void run() {
160            synchronized (HttpMonitorLogManager.this) {
161                if (System.currentTimeMillis() - writer.getLastUpdate() > SESSION_TIMEOUT) {
162                    writer.stop();
163                    writer = null;
164                }
165            }
166        }
167
168    }
169
170}
Note: See TracBrowser for help on using the repository browser.