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.ext;
16  
17  import java.util.TimeZone;
18  
19  import org.cometd.Client;
20  import org.cometd.Extension;
21  import org.cometd.Message;
22  import org.mortbay.cometd.AbstractBayeux;
23  import org.mortbay.util.DateCache;
24  
25  
26  public class TimestampExtension implements Extension
27  {
28      private final DateCache _dateCache;
29      
30      public TimestampExtension()
31      {
32          _dateCache=new DateCache();
33          _dateCache.setTimeZone(TimeZone.getTimeZone("UTC"));
34      }
35      
36      public TimestampExtension(String format)
37      {
38          _dateCache=new DateCache(format);
39          _dateCache.setTimeZone(TimeZone.getTimeZone("UTC"));
40      }
41      
42      public TimestampExtension(String format,TimeZone tz)
43      {
44          _dateCache=new DateCache(format);
45          _dateCache.setTimeZone(tz);
46      }
47      
48      public Message rcv(Client from, Message message)
49      {
50          return message;
51      }
52  
53      public Message rcvMeta(Client from, Message message)
54      {
55          return message;
56      }
57  
58      public Message send(Client from, Message message)
59      {
60          message.put(AbstractBayeux.TIMESTAMP_FIELD, _dateCache.format(System.currentTimeMillis()));
61          return message;
62      }
63  
64      public Message sendMeta(Client from, Message message)
65      {
66          message.put(AbstractBayeux.TIMESTAMP_FIELD, _dateCache.format(System.currentTimeMillis()));
67          return message;
68      }
69      
70  }