View Javadoc

    1   // ========================================================================
    2   // Copyright 2006 Mort Bay Consulting Pty. Ltd.
    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   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.cometd.continuation;
    16
    17  import java.util.Timer;
    18  import java.util.TimerTask;
    19
    20  import javax.servlet.ServletContext;
    21
    22  import org.mortbay.cometd.AbstractBayeux;
    23  import org.mortbay.cometd.ClientImpl;
    24  import org.mortbay.thread.Timeout;
    25  import org.mortbay.thread.Timeout.Task;
    26
    27  /* ------------------------------------------------------------ */
    28  /**
    29   * Extension of Bayeux that uses {@link ContinuationClient}s.
    30   *
    31   * @author gregw
    32   *
    33   */
    34  public class ContinuationBayeux extends AbstractBayeux
    35  {
    36      private static int __id;
    37      private transient Timer _tick;
    38      private transient Timeout _timeout;
    39      private long _now;
    40
    41      /* ------------------------------------------------------------ */
    42      /*
    43       * (non-Javadoc)
    44       *
    45       * @see org.mortbay.cometd.AbstractBayeux#newClient(java.lang.String,
    46       * dojox.io.cometd.Destination)
    47       */
    48      @Override
    49      public ClientImpl newRemoteClient()
    50      {
    51          ClientImpl client=new ContinuationClient(this);
    52          addClient(client,null);
    53          return client;
    54      }
    55
    56      /* ------------------------------------------------------------ */
    57      /*
    58       * (non-Javadoc)
    59       *
    60       * @see
    61       * org.mortbay.cometd.AbstractBayeux#initialize(javax.servlet.ServletContext
    62       * )
    63       */
    64      @Override
    65      protected void initialize(ServletContext context)
    66      {
    67          super.initialize(context);
    68
    69          _tick=new Timer("ContinuationBayeux-" + __id++,true);
    70          _timeout=new Timeout();
    71
    72          _tick.schedule(new TimerTask()
    73          {
    74              @Override
    75              public void run()
    76              {
    77                  _now=System.currentTimeMillis();
    78                  _timeout.tick(_now);
    79              }
    80          },100L,100L);
    81      }
    82
    83      /* ------------------------------------------------------------ */
    84      long getNow()
    85      {
    86          return _now;
    87      }
    88
    89      /* ------------------------------------------------------------ */
    90      /*
    91       * (non-Javadoc)
    92       *
    93       * @see
    94       * org.mortbay.cometd.AbstractBayeux#initialize(javax.servlet.ServletContext
    95       * )
    96       */
    97      public void destroy()
    98      {
    99          _tick.cancel();
    100     }
    101
    102     /* ------------------------------------------------------------ */
    103     void startTimeout(Timeout.Task timeout, long interval)
    104     {
    105         synchronized(_timeout)
    106         {
    107             _timeout.schedule(timeout,interval);
    108         }
    109     }
    110
    111     /* ------------------------------------------------------------ */
    112     void cancelTimeout(Timeout.Task timeout)
    113     {
    114         synchronized(_timeout)
    115         {
    116             if (timeout != null)
    117                 timeout.cancel();
    118         }
    119     }
    120
    121 }