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 int getPort()
65      {
66          return _server.getConnectors()[0].getLocalPort();
67      }
68  
69      public ServletContextHandler addContext(String contextPath)
70      {
71          ServletContextHandler context = new ServletContextHandler(_contexts, contextPath);
72  
73          AbstractSessionManager sessionManager = newSessionManager();
74          sessionManager.setIdManager(_sessionIdManager);
75          sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
76  
77          SessionHandler sessionHandler = newSessionHandler(sessionManager);
78          sessionManager.setSessionHandler(sessionHandler);
79          context.setSessionHandler(sessionHandler);
80  
81          return context;
82      }
83  
84      public void stop() throws Exception
85      {
86          _server.stop();
87      }
88  
89      public WebAppContext addWebAppContext(String warPath, String contextPath)
90      {
91          WebAppContext context = new WebAppContext(_contexts, warPath, contextPath);
92  
93          AbstractSessionManager sessionManager = newSessionManager();
94          sessionManager.setIdManager(_sessionIdManager);
95          sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
96  
97          SessionHandler sessionHandler = newSessionHandler(sessionManager);
98          sessionManager.setSessionHandler(sessionHandler);
99          context.setSessionHandler(sessionHandler);
100 
101         return context;
102     }
103 }