View Javadoc

1   // ========================================================================
2   // Copyright 2004-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.session;
15  
16  import org.eclipse.jetty.server.Server;
17  import org.eclipse.jetty.server.SessionIdManager;
18  import org.eclipse.jetty.server.SessionManager;
19  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
20  import org.eclipse.jetty.servlet.ServletContextHandler;
21  import org.eclipse.jetty.webapp.WebAppContext;
22  
23  
24  /**
25   * AbstractTestServer
26   *
27   *
28   */
29  public abstract class AbstractTestServer
30  {
31      protected final Server _server;
32      protected final int _maxInactivePeriod;
33      protected final int _scavengePeriod;
34      protected final ContextHandlerCollection _contexts;
35      protected SessionIdManager _sessionIdManager;
36  
37      public AbstractTestServer(int port)
38      {
39          this(port, 30, 10);
40      }
41  
42      public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod)
43      {
44          _server = new Server(port);
45          _maxInactivePeriod = maxInactivePeriod;
46          _scavengePeriod = scavengePeriod;
47          _contexts = new ContextHandlerCollection();
48          _sessionIdManager = newSessionIdManager();
49      }
50  
51  
52      public abstract SessionIdManager newSessionIdManager();
53      public abstract AbstractSessionManager newSessionManager();
54      public abstract SessionHandler newSessionHandler(SessionManager sessionManager);
55  
56  
57      public void start() throws Exception
58      {
59          // server -> contexts collection -> context handler -> session handler -> servlet handler
60          _server.setHandler(_contexts);
61          _server.start();
62      }
63  
64      public ServletContextHandler addContext(String contextPath)
65      {
66          ServletContextHandler context = new ServletContextHandler(_contexts, contextPath);
67  
68          AbstractSessionManager sessionManager = newSessionManager();
69          sessionManager.setIdManager(_sessionIdManager);
70          sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
71  
72          SessionHandler sessionHandler = newSessionHandler(sessionManager);
73          sessionManager.setSessionHandler(sessionHandler);
74          context.setSessionHandler(sessionHandler);
75  
76          return context;
77      }
78  
79      public void stop() throws Exception
80      {
81          _server.stop();
82      }
83  
84      public WebAppContext addWebAppContext(String warPath, String contextPath)
85      {
86          WebAppContext context = new WebAppContext(_contexts, warPath, contextPath);
87  
88          AbstractSessionManager sessionManager = newSessionManager();
89          sessionManager.setIdManager(_sessionIdManager);
90          sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
91  
92          SessionHandler sessionHandler = newSessionHandler(sessionManager);
93          sessionManager.setSessionHandler(sessionHandler);
94          context.setSessionHandler(sessionHandler);
95  
96          return context;
97      }
98  }