[1374] | 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 | * 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 |
---|
[1380] | 112 | public synchronized void handleHttpExchange(HttpExchange httpExchange) { |
---|
[1374] | 113 | try { |
---|
| 114 | if (writer == null) { |
---|
[1380] | 115 | writer = new HttpMonitorOutputWriter(logFileBaseDir); |
---|
| 116 | writer.init(); |
---|
[1374] | 117 | } |
---|
| 118 | |
---|
| 119 | writer.handleHttpExchange(httpExchange); |
---|
| 120 | } |
---|
| 121 | catch (Exception e) { |
---|
| 122 | Console.printerrln("could not handle message: " + e); |
---|
| 123 | Console.logException(e); |
---|
| 124 | |
---|
| 125 | // determine, if the writer exists but is not able to log something. In this case, |
---|
| 126 | // destroy the writer (part of the message may be logged twice through this). |
---|
| 127 | if (writer != null) { |
---|
| 128 | try { |
---|
| 129 | writer.handleHttpExchange(httpExchange); |
---|
| 130 | } |
---|
| 131 | catch (Exception e1) { |
---|
| 132 | synchronized (this) { |
---|
| 133 | writer.stop(); |
---|
| 134 | writer = null; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * <p> |
---|
| 143 | * internal timer task used for detecting session timeouts of clients |
---|
| 144 | * </p> |
---|
| 145 | * |
---|
| 146 | * @author Patrick Harms |
---|
| 147 | */ |
---|
| 148 | public class LogFileMonitorTimerTask extends TimerTask { |
---|
| 149 | |
---|
| 150 | /* (non-Javadoc) |
---|
| 151 | * @see java.lang.Runnable#run() |
---|
| 152 | */ |
---|
| 153 | @Override |
---|
| 154 | public void run() { |
---|
| 155 | synchronized (HttpMonitorLogManager.this) { |
---|
| 156 | if (System.currentTimeMillis() - writer.getLastUpdate() > SESSION_TIMEOUT) { |
---|
| 157 | writer.stop(); |
---|
| 158 | writer = null; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | } |
---|