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      
38      
39      public AbstractTestServer(int port)
40      {
41          this(port, 30, 10);
42      }
43  
44      public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod)
45      {
46          this (port, maxInactivePeriod, scavengePeriod, null);
47      }
48      
49      public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod, String sessionIdMgrConfig)
50      {
51          _server = new Server(port);
52          _maxInactivePeriod = maxInactivePeriod;
53          _scavengePeriod = scavengePeriod;
54          _contexts = new ContextHandlerCollection();
55          _sessionIdManager = newSessionIdManager(sessionIdMgrConfig);
56          _server.setSessionIdManager(_sessionIdManager);
57      }
58      
59      
60  
61      public abstract SessionIdManager newSessionIdManager(String config);
62      public abstract SessionManager newSessionManager();
63      public abstract SessionHandler newSessionHandler(SessionManager sessionManager);
64  
65  
66      public void start() throws Exception
67      {
68          // server -> contexts collection -> context handler -> session handler -> servlet handler
69          _server.setHandler(_contexts);
70          _server.start();
71      }
72      
73      public int getPort()
74      {
75          return _server.getConnectors()[0].getLocalPort();
76      }
77  
78      public ServletContextHandler addContext(String contextPath)
79      {
80          ServletContextHandler context = new ServletContextHandler(_contexts, contextPath);
81  
82          SessionManager sessionManager = newSessionManager();
83          sessionManager.setSessionIdManager(_sessionIdManager);
84          sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
85  
86          SessionHandler sessionHandler = newSessionHandler(sessionManager);
87          sessionManager.setSessionHandler(sessionHandler);
88          context.setSessionHandler(sessionHandler);
89  
90          return context;
91      }
92  
93      public void stop() throws Exception
94      {
95          _server.stop();
96      }
97  
98      public void join() throws Exception
99      {
100         _server.join();
101     }
102 
103     public WebAppContext addWebAppContext(String warPath, String contextPath)
104     {
105         WebAppContext context = new WebAppContext(_contexts, warPath, contextPath);
106 
107         SessionManager sessionManager = newSessionManager();
108         sessionManager.setSessionIdManager(_sessionIdManager);
109         sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
110 
111         SessionHandler sessionHandler = newSessionHandler(sessionManager);
112         sessionManager.setSessionHandler(sessionHandler);
113         context.setSessionHandler(sessionHandler);
114 
115         return context;
116     }
117     
118     public Server getServer()
119     {
120         return _server;
121     }
122 }