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   * @author gregw
31   *
32   */
33  public class ContinuationBayeux extends AbstractBayeux
34  {
35      private static int __id;
36      private transient Timer _tick;
37      private transient Timeout _timeout;
38      private long _now;
39      
40      
41      /* ------------------------------------------------------------ */
42      /* (non-Javadoc)
43       * @see org.mortbay.cometd.AbstractBayeux#newClient(java.lang.String, dojox.io.cometd.Destination)
44       */
45      @Override
46  	public ClientImpl newRemoteClient()
47      {
48          return new ContinuationClient(this);
49      }
50  
51      /* ------------------------------------------------------------ */
52      /* (non-Javadoc)
53       * @see org.mortbay.cometd.AbstractBayeux#initialize(javax.servlet.ServletContext)
54       */
55      @Override
56  	protected void initialize(ServletContext context)
57      {
58          super.initialize(context);
59          
60          _tick=new Timer("ContinuationBayeux-"+__id++, true);
61          _timeout=new Timeout();
62          _timeout.setDuration(getMaxInterval());
63      
64          _tick.schedule(new TimerTask()
65          {
66              @Override
67              public void run()
68              {
69                  _now=System.currentTimeMillis();
70                  _timeout.tick(_now);
71              }
72          },500L,500L);
73      }
74  
75      /* ------------------------------------------------------------ */
76      long getNow()
77      {
78          return _now;
79      }
80      
81      /* ------------------------------------------------------------ */
82      @Override
83  	public void setMaxInterval(long ms)
84      {
85          _timeout.setDuration(ms);
86          super.setMaxInterval(ms);
87      }
88  
89      /* ------------------------------------------------------------ */
90      /* (non-Javadoc)
91       * @see org.mortbay.cometd.AbstractBayeux#initialize(javax.servlet.ServletContext)
92       */
93      public void destroy()
94      {
95          _tick.cancel();
96      }
97  
98      /* ------------------------------------------------------------ */
99      void startTimeout(Task timeout,long delay)
100     {
101         synchronized(_timeout)
102         {
103             if (delay==0)
104                 _timeout.schedule(timeout);
105             else
106                 _timeout.schedule(timeout,delay);
107         }
108     }
109 
110     /* ------------------------------------------------------------ */
111     public void cancelTimeout(Task timeout)
112     {
113         synchronized(_timeout)
114         {
115             if (timeout!=null)
116                 timeout.cancel();
117         }
118     }
119     
120 }