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

Last change on this file since 857 was 857, checked in by pharms, 12 years ago
  • initial version of the HTML monitor
File size: 5.0 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 * TODO comment
15 * </p>
16 *
17 * @author Patrick Harms
18 */
19public class HtmlMonitorLogManager implements HtmlMonitorComponent, HtmlMonitorMessageListener {
20   
21    /**
22     *
23     */
24    private static final int SESSION_TIMEOUT = 100000;
25   
26    /**
27     *
28     */
29    private String logFileBaseDir;
30
31    /**
32     *
33     */
34    private Map<String, HtmlMonitorOutputWriter> writers;
35
36    /**
37     *
38     */
39    private Timer logFileMonitorTimer;
40
41    /**
42     * <p>
43     * TODO: comment
44     * </p>
45     *
46     * @param logFileBaseDir
47     */
48    HtmlMonitorLogManager(String logFileBaseDir) {
49        this.logFileBaseDir = logFileBaseDir;
50    }
51
52    /* (non-Javadoc)
53     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init()
54     */
55    @Override
56    public synchronized void init() throws IllegalStateException, HtmlMonitorException {
57        if (writers != null) {
58            throw new IllegalStateException("already initialized");
59        }
60       
61        writers = new HashMap<String, HtmlMonitorOutputWriter>();
62        logFileMonitorTimer = new Timer();
63    }
64
65    /* (non-Javadoc)
66     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start()
67     */
68    @Override
69    public synchronized void start() throws IllegalStateException, HtmlMonitorException {
70        if (writers == null) {
71            throw new IllegalStateException("not initialized");
72        }
73       
74        logFileMonitorTimer.schedule
75            (new LogFileMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2);
76    }
77
78    /* (non-Javadoc)
79     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop()
80     */
81    @Override
82    public synchronized void stop() {
83        if (writers != null) {
84            logFileMonitorTimer.cancel();
85           
86            for (HtmlMonitorOutputWriter writer : writers.values()) {
87                writer.stop();
88            }
89        }
90       
91        writers = null;
92    }
93
94    /* (non-Javadoc)
95     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitoringListener#handleEvents(de.ugoe.cs.quest.htmlmonitor.HtmlClientInfos, de.ugoe.cs.quest.htmlmonitor.HtmlEvent[])
96     */
97    @Override
98    public void handleMessage(HtmlClientInfos clientInfos, HtmlEvent[] events) {
99        HtmlMonitorOutputWriter writer = writers.get(clientInfos.getClientId());
100       
101        try {
102            if (writer == null) {
103                synchronized (this) {
104                    writer = writers.get(clientInfos.getClientId());
105                    if (writer == null) {
106                        writer =
107                            new HtmlMonitorOutputWriter(logFileBaseDir, clientInfos.getClientId());
108                        writer.init();
109                        writer.start();
110                        writers.put(clientInfos.getClientId(), writer);
111                    }
112                }
113            }
114
115            writer.handleMessage(clientInfos, events);
116        }
117        catch (Exception e) {
118            Console.printerrln("could not handle message of client " + clientInfos.getClientId() +
119                               ": " + e);
120            Console.logException(e);
121           
122            // determine, if the writer exists but is not able to log something. In this case,
123            // destroy the writer (part of the message may be logged twice through this).
124            writer = writers.get(clientInfos.getClientId());
125            if (writer != null) {
126                try {
127                    writer.handleMessage(clientInfos, events);
128                }
129                catch (Exception e1) {
130                    synchronized (this) {
131                        writers.remove(clientInfos.getClientId());
132                        writer.stop();
133                    }
134                }
135            }
136        }
137    }
138
139    /**
140     * <p>
141     * TODO comment
142     * </p>
143     *
144     * @author Patrick Harms
145     */
146    public class LogFileMonitorTimerTask extends TimerTask {
147
148        /* (non-Javadoc)
149         * @see java.lang.Runnable#run()
150         */
151        @Override
152        public void run() {
153            synchronized (HtmlMonitorLogManager.this) {
154                List<String> timeoutSessions = new ArrayList<String>();
155                for (Map.Entry<String, HtmlMonitorOutputWriter> entry : writers.entrySet()) {
156                    HtmlMonitorOutputWriter writer = entry.getValue();
157                   
158                    if (System.currentTimeMillis() - writer.getLastUpdate() > SESSION_TIMEOUT) {
159                        timeoutSessions.add(entry.getKey());
160                    }
161                }
162               
163                for (String clientId : timeoutSessions) {
164                    HtmlMonitorOutputWriter writer = writers.remove(clientId);
165                    writer.stop();
166                }
167            }
168        }
169
170    }
171
172}
Note: See TracBrowser for help on using the repository browser.