View Javadoc

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