01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.filter;
17:
18: import java.lang.reflect.Method;
19:
20: import org.apache.commons.logging.LogFactory;
21: import org.apache.commons.logging.Log;
22: import org.directwebremoting.AjaxFilter;
23: import org.directwebremoting.AjaxFilterChain;
24:
25: /**
26: * An example filter that delays responding to a query by a customizable time
27: * to simulate internet latency. Half of the delay is inserted before and half
28: * after the invocation.
29: * <p>The default delay is 100 milliseconds.
30: * @author Joe Walker [joe at getahead dot ltd dot uk]
31: */
32: public class ExtraLatencyAjaxFilter implements AjaxFilter {
33: /* (non-Javadoc)
34: * @see org.directwebremoting.AjaxFilter#doFilter(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.directwebremoting.AjaxFilterChain)
35: */
36: public Object doFilter(Object obj, Method method, Object[] params,
37: AjaxFilterChain chain) throws Exception {
38: try {
39: Thread.sleep(delay / 2);
40: } catch (InterruptedException ex) {
41: log.warn("Pre-exec interuption", ex);
42: }
43:
44: Object reply = chain.doFilter(obj, method, params);
45:
46: try {
47: Thread.sleep(delay / 2);
48: } catch (InterruptedException ex) {
49: log.warn("Post-exec interuption", ex);
50: }
51:
52: return reply;
53: }
54:
55: /**
56: * @return Returns the delay in milliseconds.
57: */
58: public long getDelay() {
59: return delay;
60: }
61:
62: /**
63: * @param delay The delay to set.
64: */
65: public void setDelay(long delay) {
66: this .delay = delay;
67: }
68:
69: /**
70: * The delay time in milliseconds.
71: * We wait for half this value before and half after the call
72: */
73: private long delay = 100;
74:
75: /**
76: * The log stream
77: */
78: private static final Log log = LogFactory
79: .getLog(ExtraLatencyAjaxFilter.class);
80: }
|