source: trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorLogManager.java @ 872

Last change on this file since 872 was 872, checked in by pharms, 12 years ago
  • removed find bug warnings
File size: 5.8 KB
Line 
1package de.ugoe.cs.autoquest.htmlmonitor;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Timer;
8import java.util.TimerTask;
9
10import de.ugoe.cs.util.console.Console;
11
12/**
13 * <p>
14 * The log manager handles different {@link HtmlMonitorOutputWriter}s to perform the logging
15 * of recorded messages. It initializes a new writer if the first message for a specific
16 * client is received and it closes a writer if for a specific period of time no further message
17 * of the same client was received. The writers themselves consider log rotation. For handling
18 * messages, the {@link HtmlMonitorMessageListener} mechanism provided by the
19 * {@link HtmlMonitorServer} is used.
20 * </p>
21 *
22 * @author Patrick Harms
23 */
24class HtmlMonitorLogManager implements HtmlMonitorComponent, HtmlMonitorMessageListener {
25   
26    /**  */
27    private static final long serialVersionUID = 1L;
28
29    /**
30     * the timeout after which a writer of an inactive client is closed
31     */
32    private static final int SESSION_TIMEOUT = 100000;
33   
34    /**
35     * the directory into which the log files shall be written
36     */
37    private String logFileBaseDir;
38
39    /**
40     * the mapping of client ids to the respective writers.
41     */
42    private Map<String, HtmlMonitorOutputWriter> writers;
43
44    /**
45     * a timer used to detect client timouts
46     */
47    private Timer logFileMonitorTimer;
48
49    /**
50     * <p>
51     * initializes the log manager with the directory into which the log files shall be stored
52     * </p>
53     *
54     * @param logFileBaseDir the directory into which the log files shall be stored
55     */
56    HtmlMonitorLogManager(String logFileBaseDir) {
57        this.logFileBaseDir = logFileBaseDir;
58    }
59
60    /* (non-Javadoc)
61     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init()
62     */
63    @Override
64    public synchronized void init() throws IllegalStateException, HtmlMonitorException {
65        if (writers != null) {
66            throw new IllegalStateException("already initialized");
67        }
68       
69        writers = new HashMap<String, HtmlMonitorOutputWriter>();
70        logFileMonitorTimer = new Timer();
71    }
72
73    /* (non-Javadoc)
74     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start()
75     */
76    @Override
77    public synchronized void start() throws IllegalStateException, HtmlMonitorException {
78        if (writers == null) {
79            throw new IllegalStateException("not initialized");
80        }
81       
82        logFileMonitorTimer.schedule
83            (new LogFileMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2);
84    }
85
86    /* (non-Javadoc)
87     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop()
88     */
89    @Override
90    public synchronized void stop() {
91        if (writers != null) {
92            logFileMonitorTimer.cancel();
93           
94            for (HtmlMonitorOutputWriter writer : writers.values()) {
95                writer.stop();
96            }
97        }
98       
99        writers = null;
100    }
101
102    /* (non-Javadoc)
103     * @see HtmlMonitoringListener#handleEvents(HtmlClientInfos, HtmlEvent[])
104     */
105    @Override
106    public void handleMessage(HtmlClientInfos clientInfos, HtmlEvent[] events) {
107        HtmlMonitorOutputWriter writer = writers.get(clientInfos.getClientId());
108       
109        try {
110            if (writer == null) {
111                synchronized (this) {
112                    writer = writers.get(clientInfos.getClientId());
113                    if (writer == null) {
114                        writer =
115                            new HtmlMonitorOutputWriter(logFileBaseDir, clientInfos.getClientId());
116                        writer.init();
117                        writer.start();
118                        writers.put(clientInfos.getClientId(), writer);
119                    }
120                }
121            }
122
123            writer.handleMessage(clientInfos, events);
124        }
125        catch (Exception e) {
126            Console.printerrln("could not handle message of client " + clientInfos.getClientId() +
127                               ": " + e);
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            writer = writers.get(clientInfos.getClientId());
133            if (writer != null) {
134                try {
135                    writer.handleMessage(clientInfos, events);
136                }
137                catch (Exception e1) {
138                    synchronized (this) {
139                        writers.remove(clientInfos.getClientId());
140                        writer.stop();
141                    }
142                }
143            }
144        }
145    }
146
147    /**
148     * <p>
149     * internal timer task used for detecting session timeouts of clients
150     * </p>
151     *
152     * @author Patrick Harms
153     */
154    public class LogFileMonitorTimerTask extends TimerTask {
155
156        /* (non-Javadoc)
157         * @see java.lang.Runnable#run()
158         */
159        @Override
160        public void run() {
161            synchronized (HtmlMonitorLogManager.this) {
162                List<String> timeoutSessions = new ArrayList<String>();
163                for (Map.Entry<String, HtmlMonitorOutputWriter> entry : writers.entrySet()) {
164                    HtmlMonitorOutputWriter writer = entry.getValue();
165                   
166                    if (System.currentTimeMillis() - writer.getLastUpdate() > SESSION_TIMEOUT) {
167                        timeoutSessions.add(entry.getKey());
168                    }
169                }
170               
171                for (String clientId : timeoutSessions) {
172                    HtmlMonitorOutputWriter writer = writers.remove(clientId);
173                    writer.stop();
174                }
175            }
176        }
177
178    }
179
180}
Note: See TracBrowser for help on using the repository browser.