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

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

Initial import.

File size: 6.9 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.proxy;
16
17import java.nio.ByteBuffer;
18import java.util.ArrayList;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.Timer;
23import java.util.TimerTask;
24import java.util.logging.Level;
25
26import javax.servlet.http.HttpServletRequest;
27import javax.servlet.http.HttpServletResponse;
28
29import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorComponent;
30import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorException;
31import de.ugoe.cs.autoquest.httpmonitor.HttpMonitorExchangeHandler;
32import de.ugoe.cs.autoquest.httpmonitor.exchange.Status;
33import de.ugoe.cs.util.console.Console;
34
35/**
36 * <p>
37 * TODO update comment
38 * The log manager handles different {@link HttpMonitorOutputWriter}s to perform the logging
39 * of recorded messages. It initializes a new writer if the first message for a specific
40 * client is received and it closes a writer if for a specific period of time no further message
41 * of the same client was received. The writers themselves consider log rotation. For handling
42 * messages, the {@link HttpMonitorExchangeHandler} mechanism provided by the
43 * {@link HttpMonitorServer} is used.
44 * </p>
45 *
46 * @author Patrick Harms
47 */
48class ExchangeListenerManager implements HttpMonitorComponent {
49   
50    /**
51     * the timeout after which a writer of an inactive client is closed
52     */
53    private static final int SESSION_TIMEOUT = 10 * 60 * 1000;
54   
55    /**
56     *
57     */
58    private HttpMonitorExchangeHandler exchangeHandler;
59   
60    /**
61     * the mapping of client ids to the respective writers.
62     */
63    private Map<HttpServletRequest, ExchangeListener> listeners;
64
65    /**
66     * a timer used to detect exchange timeouts
67     */
68    private Timer listenerTimer;
69
70    /**
71     *
72     *
73     */
74    ExchangeListenerManager(HttpMonitorExchangeHandler exchangeHandler) {
75        this.exchangeHandler = exchangeHandler;
76    }
77
78    /* (non-Javadoc)
79     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init()
80     */
81    @Override
82    public synchronized void init() throws IllegalStateException, HttpMonitorException {
83        Console.traceln(Level.FINER, "initializing exchange listener manager");
84       
85        if (listeners != null) {
86            throw new IllegalStateException("already initialized");
87        }
88       
89        listeners = new HashMap<HttpServletRequest, ExchangeListener>();
90        listenerTimer = new Timer();
91    }
92
93    /* (non-Javadoc)
94     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start()
95     */
96    @Override
97    public synchronized void start() throws IllegalStateException, HttpMonitorException {
98        Console.traceln(Level.FINER, "starting exchange listener manager");
99
100        if (listeners == null) {
101            throw new IllegalStateException("not initialized");
102        }
103       
104        listenerTimer.schedule
105            (new ListenerMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2);
106    }
107
108    /* (non-Javadoc)
109     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop()
110     */
111    @Override
112    public synchronized void stop() {
113        Console.traceln(Level.FINER, "stopping exchange listener manager");
114
115        if (listenerTimer != null) {
116            listenerTimer.cancel();
117        }
118       
119        if (listeners != null) {
120            for (ExchangeListener listener : listeners.values()) {
121                listener.onFinish(Status.TIMEOUT);
122            }
123        }
124       
125        listeners = null;
126    }
127
128    /**
129     *
130     */
131    public void onRequest(HttpServletRequest request) throws IllegalStateException {
132        ensureListener(request).onRequest(request);
133    }
134
135    /**
136     *
137     */
138    public void onRequestContent(HttpServletRequest request, ByteBuffer data)
139        throws IllegalStateException
140    {
141        ensureListener(request).onRequestContent(data);
142    }
143
144    /**
145     *
146     */
147    public void onResponse(HttpServletRequest request, HttpServletResponse response)
148        throws IllegalStateException
149    {
150        ensureListener(request).onResponse(response);
151    }
152
153    /**
154     *
155     */
156    public void onResponseContent(HttpServletRequest request, ByteBuffer data)
157        throws IllegalStateException
158    {
159        ensureListener(request).onResponseContent(data);
160    }
161
162    /**
163     *
164     */
165    public void onFinish(HttpServletRequest request, Status status) throws IllegalStateException {
166        ensureListener(request).onFinish(status);
167        Console.traceln(Level.FINEST, "removing exchange listener for " + request);
168        listeners.remove(request);
169    }
170
171    /**
172     *
173     */
174    private ExchangeListener ensureListener(HttpServletRequest request) {
175        ExchangeListener listener = listeners.get(request);
176       
177        if (listener == null) {
178            synchronized (this) {
179                listener = listeners.get(request);
180                if (listener == null) {
181                    Console.traceln(Level.FINEST, "creating exchange listener for " + request);
182                    listener = new ExchangeListener(exchangeHandler);
183                    listeners.put(request, listener);
184                }
185            }
186        }
187
188        return listener;
189    }
190
191    /**
192     * <p>
193     * internal timer task used for detecting session timeouts of clients
194     * </p>
195     *
196     * @author Patrick Harms
197     */
198    public class ListenerMonitorTimerTask extends TimerTask {
199
200        /* (non-Javadoc)
201         * @see java.lang.Runnable#run()
202         */
203        @Override
204        public void run() {
205            synchronized (ExchangeListenerManager.this) {
206                List<HttpServletRequest> timeoutRequests = new ArrayList<HttpServletRequest>();
207                for (Map.Entry<HttpServletRequest, ExchangeListener> entry : listeners.entrySet()) {
208                    ExchangeListener listener = entry.getValue();
209                   
210                    if (System.currentTimeMillis() - listener.getLastUpdate() > SESSION_TIMEOUT) {
211                        timeoutRequests.add(entry.getKey());
212                    }
213                }
214               
215                for (HttpServletRequest request : timeoutRequests) {
216                    ExchangeListener listener = listeners.remove(request);
217                    listener.onFinish(Status.TIMEOUT);
218                }
219            }
220        }
221
222    }
223
224}
Note: See TracBrowser for help on using the repository browser.