1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.util.ajax;
17
18 public class WaitingContinuation implements org.mortbay.util.ajax.Continuation
19 {
20 Object _mutex;
21 Object _object;
22 boolean _new=true;
23 boolean _resumed=false;
24 boolean _pending=false;
25
26 public WaitingContinuation()
27 {
28 _mutex=this;
29 }
30
31 public WaitingContinuation(Object mutex)
32 {
33 _mutex=mutex==null?this:mutex;
34 }
35
36 public void resume()
37 {
38 synchronized (_mutex)
39 {
40 _resumed=true;
41 _mutex.notify();
42 }
43 }
44
45 public void reset()
46 {
47 synchronized (_mutex)
48 {
49 _resumed=false;
50 _pending=false;
51 _mutex.notify();
52 }
53 }
54
55 public boolean isNew()
56 {
57 return _new;
58 }
59
60 public boolean suspend(long timeout)
61 {
62 synchronized (_mutex)
63 {
64 _new=false;
65 _pending=true;
66 boolean result;
67 try
68 {
69 if (!_resumed && timeout>=0)
70 {
71 if (timeout==0)
72 _mutex.wait();
73 else if (timeout>0)
74 _mutex.wait(timeout);
75
76 }
77 }
78 catch (InterruptedException e)
79 {
80 e.printStackTrace();
81 }
82 finally
83 {
84 result=_resumed;
85 _resumed=false;
86 _pending=false;
87 }
88
89 return result;
90 }
91 }
92
93 public boolean isPending()
94 {
95 synchronized (_mutex)
96 {
97 return _pending;
98 }
99 }
100
101 public boolean isResumed()
102 {
103 synchronized (_mutex)
104 {
105 return _resumed;
106 }
107 }
108
109 public Object getObject()
110 {
111 return _object;
112 }
113
114 public void setObject(Object object)
115 {
116 _object = object;
117 }
118
119 public Object getMutex()
120 {
121 return _mutex;
122 }
123
124 public void setMutex(Object mutex)
125 {
126 if (_pending && mutex!=_mutex)
127 throw new IllegalStateException();
128 _mutex = mutex==null ? this : mutex;
129 }
130
131 public String toString()
132 {
133 synchronized (this)
134 {
135 return "WaitingContinuation@"+hashCode()+
136 (_new?",new":"")+
137 (_pending?",pending":"")+
138 (_resumed?",resumed":"");
139 }
140 }
141 }