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  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 }