View Javadoc

1   // ========================================================================
2   // Copyright 2004-2005 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.terracotta.servlet;
16  
17  import java.util.concurrent.TimeUnit;
18  
19  import org.mortbay.jetty.Server;
20  import org.mortbay.jetty.SessionIdManager;
21  import org.mortbay.jetty.handler.ContextHandlerCollection;
22  import org.mortbay.jetty.servlet.Context;
23  import org.mortbay.jetty.servlet.SessionHandler;
24  
25  /**
26   * @version $Revision: 1216 $ $Date: 2008-09-19 09:24:33 -0500 (Fri, 19 Sep 2008) $
27   */
28  public class TerracottaJettyServer
29  {
30      private final Server server;
31      private final int maxInactivePeriod;
32      private final int scavengePeriod;
33      private final ContextHandlerCollection contexts;
34      private SessionIdManager sessionIdManager;
35  
36      public TerracottaJettyServer(int port)
37      {
38          this(port, 30, 10);
39      }
40  
41      public TerracottaJettyServer(int port, int maxInactivePeriod, int scavengePeriod)
42      {
43          this.server = new Server(port);
44          this.maxInactivePeriod = maxInactivePeriod;
45          this.scavengePeriod = scavengePeriod;
46          this.contexts = new ContextHandlerCollection();
47          this.sessionIdManager = new TerracottaSessionIdManager(server);
48      }
49  
50      public void start() throws Exception
51      {
52          // server -> contexts collection -> context handler -> session handler -> servlet handler
53          server.setHandler(contexts);
54          server.start();
55      }
56  
57      public Context addContext(String contextPath)
58      {
59          Context context = new Context(contexts, contextPath);
60  
61          TerracottaSessionManager sessionManager = new TerracottaSessionManager();
62          sessionManager.setIdManager(sessionIdManager);
63          sessionManager.setMaxInactiveInterval(maxInactivePeriod);
64          sessionManager.setScavengePeriodMs(TimeUnit.SECONDS.toMillis(scavengePeriod));
65  
66          SessionHandler sessionHandler = new SessionHandler(sessionManager);
67          sessionManager.setSessionHandler(sessionHandler);
68          context.setSessionHandler(sessionHandler);
69  
70          return context;
71      }
72  
73      public void stop() throws Exception
74      {
75          server.stop();
76      }
77  }