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.Set;
    19
    20  import org.mortbay.cometd.AbstractBayeux;
    21  import org.mortbay.cometd.ClientImpl;
    22  import org.mortbay.cometd.continuation.ContinuationCometdServlet;
    23  import org.mortbay.cometd.ext.TimesyncExtension;
    24  import org.mortbay.jetty.Server;
    25  import org.mortbay.jetty.bio.SocketConnector;
    26  import org.mortbay.jetty.handler.ContextHandlerCollection;
    27  import org.mortbay.jetty.handler.MovedContextHandler;
    28  import org.mortbay.jetty.nio.SelectChannelConnector;
    29  import org.mortbay.jetty.security.SslSelectChannelConnector;
    30  import org.mortbay.jetty.security.SslSocketConnector;
    31  import org.mortbay.jetty.servlet.Context;
    32  import org.mortbay.jetty.servlet.ServletHolder;
    33  import org.mortbay.resource.Resource;
    34  import org.mortbay.resource.ResourceCollection;
    35  import org.mortbay.thread.QueuedThreadPool;
    36
    37  import org.cometd.Message;
    38
    39
    40  /* ------------------------------------------------------------ */
    41  /** Main class for cometd demo.
    42   *
    43   * This is of use when running demo in a terracotta cluster
    44   *
    45   * @author gregw
    46   *
    47   */
    48  public class CometdDemo
    49  {
    50      private static int _testHandshakeFailure;
    51
    52      /* ------------------------------------------------------------ */
    53      /**
    54       * @param args
    55       */
    56      public static void main(String[] args) throws Exception
    57      {
    58          int port = args.length==0?8080:Integer.parseInt(args[0]);
    59
    60          String base="../../..";
    61
    62          // Manually contruct context to avoid hassles with webapp classloaders for now.
    63          Server server = new Server();
    64          QueuedThreadPool qtp = new QueuedThreadPool();
    65          qtp.setMinThreads(5);
    66          qtp.setMaxThreads(200);
    67          server.setThreadPool(qtp);
    68
    69          SelectChannelConnector connector=new SelectChannelConnector();
    70          // SocketConnector connector=new SocketConnector();
    71          connector.setPort(port);
    72          server.addConnector(connector);
    73          SocketConnector bconnector=new SocketConnector();
    74          bconnector.setPort(port+1);
    75          server.addConnector(bconnector);
    76
    77          SslSelectChannelConnector connector2=new SslSelectChannelConnector();
    78          // SslSocketConnector connector2=new SslSocketConnector();
    79          connector2.setPort(port-80+443);
    80          connector2.setKeystore(base+"/etc/keystore");
    81          connector2.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    82          connector2.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    83          connector2.setTruststore(base+"/etc/keystore");
    84          connector2.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    85          server.addConnector(connector2);
    86
    87          ContextHandlerCollection contexts = new ContextHandlerCollection();
    88          server.setHandler(contexts);
    89
    90          MovedContextHandler moved = new MovedContextHandler(contexts,"/","/cometd");
    91          moved.setDiscardPathInfo(true);
    92
    93          Context context = new Context(contexts,"/cometd",Context.NO_SECURITY|Context.SESSIONS);
    94
    95          context.setBaseResource(new ResourceCollection(new Resource[]
    96          {
    97              Resource.newResource("./src/main/webapp/"),
    98              Resource.newResource("./target/cometd-demo-6.1.14/"),
    99          }));
    100
    101         // Demo bayeux session manager
    102         // context.getSessionHandler().setSessionManager(new BayeuxSessionManager());
    103         // context.addServlet(com.acme.SessionDump.class,"/session");
    104         // context.addServlet(com.acme.Dump.class,"/dump");
    105
    106         // Cometd servlet
    107         ContinuationCometdServlet cometd_servlet=new ContinuationCometdServlet();
    108         ServletHolder cometd_holder = new ServletHolder(cometd_servlet);
    109         cometd_holder.setInitParameter("filters","/WEB-INF/filters.json");
    110         cometd_holder.setInitParameter("timeout","180000");
    111         cometd_holder.setInitParameter("interval","100");
    112         cometd_holder.setInitParameter("maxInterval","60000");
    113         cometd_holder.setInitParameter("multiFrameInterval","1500");
    114         cometd_holder.setInitParameter("directDeliver","true");
    115         cometd_holder.setInitParameter("logLevel","1");
    116
    117         context.addServlet(cometd_holder, "/cometd/*");
    118         context.addServlet("org.mortbay.jetty.servlet.DefaultServlet", "/");
    119         context.addEventListener(new BayeuxServicesListener());
    120
    121         server.start();
    122
    123         AbstractBayeux bayeux = cometd_servlet.getBayeux();
    124         bayeux.addExtension(new TimesyncExtension());
    125
    126         bayeux.setSecurityPolicy(new AbstractBayeux.DefaultPolicy(){
    127             public boolean canHandshake(Message message)
    128             {
    129                 if (_testHandshakeFailure<0)
    130                 {
    131                     _testHandshakeFailure++;
    132                     return false;
    133                 }
    134                 return true;
    135             }
    136
    137         });
    138
    139         while (true)
    140         {
    141             Thread.sleep(2000);
    142             Set<String> ids=bayeux.getClientIDs();
    143             ClientImpl[] clients=new ClientImpl[ids.size()];
    144             int i=0;
    145             for (String id : ids)
    146             {
    147                 clients[i]=(ClientImpl)bayeux.getClient(id);
    148                 i++;
    149             }
    150             i=0;
    151         }
    152     }
    153 }