1   //========================================================================
2   //$Id: WaitingContinuation.java,v 1.1 2005/11/14 17:45:56 gregwilkins Exp $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
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  //========================================================================
15  
16  package org.mortbay.util.ajax;
17  
18  import org.mortbay.log.Log;
19  
20  public class WaitingContinuation implements org.mortbay.util.ajax.Continuation
21  {
22      Object _mutex;
23      Object _object;
24      boolean _new=true;
25      boolean _resumed=false;
26      boolean _pending=false;
27      boolean _expired=false;
28  
29      public WaitingContinuation()
30      {
31          _mutex=this;
32      }
33      
34      public WaitingContinuation(Object mutex)
35      {
36          _mutex=mutex==null?this:mutex;
37      }
38      
39      public void resume()
40      {
41          synchronized (_mutex)
42          {
43              _resumed=true;
44              _mutex.notify();
45          }
46      }
47      
48      public void reset()
49      {
50          synchronized (_mutex)
51          {
52              _resumed=false;
53              _pending=false;
54              _expired=false;
55              _mutex.notify();
56          }
57      }
58  
59      public boolean isNew()
60      {
61          return _new;
62      }
63  
64      public boolean suspend(long timeout)
65      {
66          synchronized (_mutex)
67          {
68              _new=false;
69              _pending=true;
70              boolean result;
71              try
72              {
73                  if (!_resumed && timeout>=0)
74                  {
75                      if (timeout==0)
76                          _mutex.wait();
77                      else if (timeout>0)
78                          _mutex.wait(timeout);
79                          
80                  }
81              }
82              catch (InterruptedException e)
83              {
84                  _expired=true;
85                  Log.ignore(e);
86              }
87              finally
88              {
89                  result=_resumed;
90                  _resumed=false;
91                  _pending=false;
92              }
93              
94              return result;
95          }
96      }
97      
98      public boolean isPending()
99      {
100         synchronized (_mutex)
101         {
102             return _pending;
103         }
104     }
105     
106     public boolean isResumed()
107     {
108         synchronized (_mutex)
109         {
110             return _resumed;
111         }
112     }
113     
114     public boolean isExpired()
115     {
116         synchronized (_mutex)
117         {
118             return _expired;
119         }
120     }
121 
122     public Object getObject()
123     {
124         return _object;
125     }
126 
127     public void setObject(Object object)
128     {
129         _object = object;
130     }
131 
132     public Object getMutex()
133     {
134         return _mutex;
135     }
136 
137     public void setMutex(Object mutex)
138     {
139         if (!_new && _mutex!=this && _pending)
140             throw new IllegalStateException();
141         _mutex = mutex==null ? this : mutex; 
142     }
143 
144     public String toString()
145     {
146         synchronized (this)
147         {
148             return "WaitingContinuation@"+hashCode()+
149             (_new?",new":"")+
150             (_pending?",pending":"")+
151             (_resumed?",resumed":"")+
152             (_expired?",expired":"");
153         }
154     }
155 }