View Javadoc

    1   // ========================================================================
    2   // Copyright 2007 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.cometd.demo;
    16
    17
    18  import java.util.Date;
    19  import java.util.Timer;
    20  import java.util.TimerTask;
    21
    22  import org.cometd.Client;
    23  import org.cometd.Extension;
    24  import org.cometd.Message;
    25  import org.mortbay.cometd.AbstractBayeux;
    26  import org.mortbay.cometd.ChannelImpl;
    27  import org.mortbay.cometd.MessageImpl;
    28  import org.mortbay.cometd.continuation.ContinuationCometdServlet;
    29  import org.mortbay.cometd.ext.StatisticsExtension;
    30  import org.mortbay.jetty.Server;
    31  import org.mortbay.jetty.bio.SocketConnector;
    32  import org.mortbay.jetty.handler.ContextHandlerCollection;
    33  import org.mortbay.jetty.handler.MovedContextHandler;
    34  import org.mortbay.jetty.nio.SelectChannelConnector;
    35  import org.mortbay.jetty.security.SslSelectChannelConnector;
    36  import org.mortbay.jetty.servlet.Context;
    37  import org.mortbay.jetty.servlet.DefaultServlet;
    38  import org.mortbay.jetty.servlet.ServletHolder;
    39  import org.mortbay.resource.Resource;
    40  import org.mortbay.resource.ResourceCollection;
    41  import org.mortbay.thread.QueuedThreadPool;
    42  import org.mortbay.util.ajax.JSON;
    43
    44
    45  /* ------------------------------------------------------------ */
    46  /** Main class for cometd demo.
    47   *
    48   * @author gregw
    49   *
    50   */
    51  public class CometdDemo
    52  {
    53      private static int _testHandshakeFailure;
    54
    55      /* ------------------------------------------------------------ */
    56      /**
    57       * @param args
    58       */
    59      public static void main(String[] args) throws Exception
    60      {
    61          int port = args.length==0?8080:Integer.parseInt(args[0]);
    62
    63          String base="../../..";
    64
    65          // Manually contruct context to avoid hassles with webapp classloaders for now.
    66          Server server = new Server();
    67          QueuedThreadPool qtp = new QueuedThreadPool();
    68          qtp.setMinThreads(5);
    69          qtp.setMaxThreads(200);
    70          server.setThreadPool(qtp);
    71
    72          SelectChannelConnector connector=new SelectChannelConnector();
    73          // SocketConnector connector=new SocketConnector();
    74          connector.setPort(port);
    75          server.addConnector(connector);
    76          SocketConnector bconnector=new SocketConnector();
    77          bconnector.setPort(port+1);
    78          server.addConnector(bconnector);
    79
    80
    81
    82          SslSelectChannelConnector ssl_connector=new SslSelectChannelConnector();
    83          ssl_connector.setPort(port-80+443);
    84          ssl_connector.setKeystore(base+"/etc/keystore");
    85          ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    86          ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    87          ssl_connector.setTruststore(base+"/etc/keystore");
    88          ssl_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    89          server.addConnector(ssl_connector);
    90
    91
    92          ContextHandlerCollection contexts = new ContextHandlerCollection();
    93          server.setHandler(contexts);
    94
    95          MovedContextHandler moved = new MovedContextHandler(contexts,"/","/cometd");
    96          moved.setDiscardPathInfo(true);
    97
    98          Context context = new Context(contexts,"/cometd",Context.NO_SECURITY|Context.SESSIONS);
    99
    100         String version = Server.getVersion();
    101         if (Server.UNKNOWN_VERSION.equals(version))
    102             version=Server.SNAPSHOT_VERSION;
    103         context.setBaseResource(new ResourceCollection(new Resource[]
    104         {
    105             Resource.newResource("./src/main/webapp/"),
    106             Resource.newResource("./target/cometd-demo-"+version+"/"),
    107         }));
    108
    109
    110         // Cometd servlet
    111
    112         ServletHolder dftServlet = context.addServlet(DefaultServlet.class, "/");
    113         dftServlet.setInitOrder(1);
    114
    115         ServletHolder comet = context.addServlet(ContinuationCometdServlet.class, "/cometd/*");
    116         comet.setInitParameter("filters","/WEB-INF/filters.json");
    117         comet.setInitParameter("timeout","30000");
    118         comet.setInitParameter("interval","0");
    119         comet.setInitParameter("maxInterval","15000");
    120         comet.setInitParameter("maxLazyLatency","2000");
    121         comet.setInitParameter("multiFrameInterval","2500");
    122         comet.setInitParameter("logLevel","0");
    123         comet.setInitOrder(2);
    124
    125
    126         ServletHolder demo=context.addServlet(CometdDemoServlet.class, "/demo");
    127         demo.setInitOrder(3);
    128
    129         server.start();
    130
    131         final AbstractBayeux bayeux = ((ContinuationCometdServlet)comet.getServlet()).getBayeux();
    132
    133         bayeux.setSecurityPolicy(new AbstractBayeux.DefaultPolicy()
    134         {
    135             public boolean canHandshake(Message message)
    136             {
    137                 if (_testHandshakeFailure<0)
    138                 {
    139                     _testHandshakeFailure++;
    140                     return false;
    141                 }
    142                 return true;
    143             }
    144         });
    145
    146         // Demo lazy messages
    147         if (Boolean.getBoolean("LAZY"))
    148         {
    149             bayeux.addExtension(new Extension()
    150             {
    151                 public Message rcv(Client from, Message message)
    152                 {
    153                     if (message.getChannel().startsWith("/chat/") && message.getData()!=null && message.getData().toString().indexOf("lazy")>=0)
    154                         ((MessageImpl)message).setLazy(true);
    155                     return message;
    156                 }
    157                 public Message rcvMeta(Client from, Message message)
    158                 {
    159                     return message;
    160                 }
    161                 public Message send(Client from, Message message)
    162                 {
    163                     return message;
    164                 }
    165                 public Message sendMeta(Client from, Message message)
    166                 {
    167                     return message;
    168                 }
    169             });
    170         }
    171
    172         // Demo lazy messages
    173         if (Boolean.getBoolean("LAZYCHAT"))
    174         {
    175             final ChannelImpl chat_demo = (ChannelImpl)bayeux.getChannel("/chat/demo",true);
    176             chat_demo.setLazy(true);
    177             chat_demo.setPersistent(true);
    178
    179             Timer timer = new Timer();
    180             timer.schedule(new TimerTask()
    181             {
    182                 public void run()
    183                 {
    184                     chat_demo.publish(null,new JSON.Literal("{\"user\":\"TICK\",\"chat\":\""+new Date()+"\"}"),null);
    185                 }
    186             },2000,2000);
    187         }
    188
    189         if (Boolean.getBoolean("STATS"))
    190         {
    191             StatisticsExtension ext = new StatisticsExtension();
    192             ext.setStatsRequestKeys(new String[]{"chat"});
    193             ext.setStatsRequestSentinel("//stats");
    194             ext.setStatsRequestChannel("/chat/demo");
    195             ext.setStatsResultSentinel("//stats-results");
    196             ext.setStatsConfirmSentinel("//stats-confirm");
    197             ext.setProbeChannel("/chat/demo");
    198             ext.setProbeSentinel("//stats-probe");
    199             ext.setProbeReplySentinel("//stats-reply");
    200             ext.setProbeReplyChannel("/chat/demo");
    201             ext.setProbeReplyKeys(new String[]{"chat"});
    202
    203             bayeux.addExtension(ext);
    204         }
    205     }
    206 }