KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > filter > ExtraLatencyAjaxFilter


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19
20 import org.directwebremoting.AjaxFilter;
21 import org.directwebremoting.AjaxFilterChain;
22
23 /**
24  * An example filter that delays responding to a query by a customizable time
25  * to simulate internet latency. Half of the delay is inserted before and half
26  * after the invocation.
27  * <p>The default delay is 100 milliseconds.
28  * @author Joe Walker [joe at getahead dot ltd dot uk]
29  */

30 public class ExtraLatencyAjaxFilter implements AjaxFilter
31 {
32     /* (non-Javadoc)
33      * @see org.directwebremoting.AjaxFilter#doFilter(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.directwebremoting.AjaxFilterChain)
34      */

35     public Object JavaDoc doFilter(Object JavaDoc obj, Method JavaDoc method, Object JavaDoc[] params, AjaxFilterChain chain) throws Exception JavaDoc
36     {
37         synchronized (this)
38         {
39             try
40             {
41                 wait(delay/2);
42             }
43             catch (InterruptedException JavaDoc ex)
44             {
45                 ex.printStackTrace();
46             }
47         }
48
49         Object JavaDoc reply = chain.doFilter(obj, method, params);
50
51         synchronized (this)
52         {
53             try
54             {
55                 wait(delay/2);
56             }
57             catch (InterruptedException JavaDoc ex)
58             {
59                 ex.printStackTrace();
60             }
61         }
62
63         return reply;
64     }
65
66     /**
67      * @return Returns the delay in milliseconds.
68      */

69     public long getDelay()
70     {
71         return delay;
72     }
73
74     /**
75      * @param delay The delay to set.
76      */

77     public void setDelay(long delay)
78     {
79         this.delay = delay;
80     }
81
82     private long delay = 100;
83 }
84
Popular Tags