View Javadoc

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