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

Last change on this file since 1381 was 1381, checked in by pharms, 10 years ago
  • removed find bugs warning
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    private static final long serialVersionUID = 1L;
52
53    /**
54     * the timeout after which a writer of an inactive client is closed
55     */
56    private static final int SESSION_TIMEOUT = 10 * 60 * 1000;
57   
58    /**
59     *
60     */
61    private HttpMonitorExchangeHandler exchangeHandler;
62   
63    /**
64     * the mapping of client ids to the respective writers.
65     */
66    private Map<HttpServletRequest, ExchangeListener> listeners;
67
68    /**
69     * a timer used to detect exchange timeouts
70     */
71    private Timer listenerTimer;
72
73    /**
74     *
75     *
76     */
77    ExchangeListenerManager(HttpMonitorExchangeHandler exchangeHandler) {
78        this.exchangeHandler = exchangeHandler;
79    }
80
81    /* (non-Javadoc)
82     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init()
83     */
84    @Override
85    public synchronized void init() throws IllegalStateException, HttpMonitorException {
86        Console.traceln(Level.FINER, "initializing exchange listener manager");
87       
88        if (listeners != null) {
89            throw new IllegalStateException("already initialized");
90        }
91       
92        listeners = new HashMap<HttpServletRequest, ExchangeListener>();
93        listenerTimer = new Timer();
94    }
95
96    /* (non-Javadoc)
97     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start()
98     */
99    @Override
100    public synchronized void start() throws IllegalStateException, HttpMonitorException {
101        Console.traceln(Level.FINER, "starting exchange listener manager");
102
103        if (listeners == null) {
104            throw new IllegalStateException("not initialized");
105        }
106       
107        listenerTimer.schedule
108            (new ListenerMonitorTimerTask(), SESSION_TIMEOUT / 2, SESSION_TIMEOUT / 2);
109    }
110
111    /* (non-Javadoc)
112     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop()
113     */
114    @Override
115    public synchronized void stop() {
116        Console.traceln(Level.FINER, "stopping exchange listener manager");
117
118        if (listenerTimer != null) {
119            listenerTimer.cancel();
120        }
121       
122        if (listeners != null) {
123            for (ExchangeListener listener : listeners.values()) {
124                listener.onFinish(Status.TIMEOUT);
125            }
126        }
127       
128        listeners = null;
129    }
130
131    /**
132     *
133     */
134    public void onRequest(HttpServletRequest request) throws IllegalStateException {
135        ensureListener(request).onRequest(request);
136    }
137
138    /**
139     *
140     */
141    public void onRequestContent(HttpServletRequest request, ByteBuffer data)
142        throws IllegalStateException
143    {
144        ensureListener(request).onRequestContent(data);
145    }
146
147    /**
148     *
149     */
150    public void onResponse(HttpServletRequest request, HttpServletResponse response)
151        throws IllegalStateException
152    {
153        ensureListener(request).onResponse(response);
154    }
155
156    /**
157     *
158     */
159    public void onResponseContent(HttpServletRequest request, ByteBuffer data)
160        throws IllegalStateException
161    {
162        ensureListener(request).onResponseContent(data);
163    }
164
165    /**
166     *
167     */
168    public void onFinish(HttpServletRequest request, Status status) throws IllegalStateException {
169        ensureListener(request).onFinish(status);
170        Console.traceln(Level.FINEST, "removing exchange listener for " + request);
171        listeners.remove(request);
172    }
173
174    /**
175     *
176     */
177    private ExchangeListener ensureListener(HttpServletRequest request) {
178        ExchangeListener listener = listeners.get(request);
179       
180        if (listener == null) {
181            synchronized (this) {
182                listener = listeners.get(request);
183                if (listener == null) {
184                    Console.traceln(Level.FINEST, "creating exchange listener for " + request);
185                    listener = new ExchangeListener(exchangeHandler);
186                    listeners.put(request, listener);
187                }
188            }
189        }
190
191        return listener;
192    }
193
194    /**
195     * <p>
196     * internal timer task used for detecting session timeouts of clients
197     * </p>
198     *
199     * @author Patrick Harms
200     */
201    public class ListenerMonitorTimerTask extends TimerTask {
202
203        /* (non-Javadoc)
204         * @see java.lang.Runnable#run()
205         */
206        @Override
207        public void run() {
208            synchronized (ExchangeListenerManager.this) {
209                List<HttpServletRequest> timeoutRequests = new ArrayList<HttpServletRequest>();
210                for (Map.Entry<HttpServletRequest, ExchangeListener> entry : listeners.entrySet()) {
211                    ExchangeListener listener = entry.getValue();
212                   
213                    if (System.currentTimeMillis() - listener.getLastUpdate() > SESSION_TIMEOUT) {
214                        timeoutRequests.add(entry.getKey());
215                    }
216                }
217               
218                for (HttpServletRequest request : timeoutRequests) {
219                    ExchangeListener listener = listeners.remove(request);
220                    listener.onFinish(Status.TIMEOUT);
221                }
222            }
223        }
224
225    }
226
227}
Note: See TracBrowser for help on using the repository browser.