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.NetworkConnector;
22  import org.eclipse.jetty.server.Server;
23  import org.eclipse.jetty.server.SessionIdManager;
24  import org.eclipse.jetty.server.SessionManager;
25  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
26  import org.eclipse.jetty.servlet.ServletContextHandler;
27  import org.eclipse.jetty.webapp.WebAppContext;
28  
29  
30  /**
31   * AbstractTestServer
32   *
33   *
34   */
35  public abstract class AbstractTestServer
36  {
37      protected final Server _server;
38      protected final int _maxInactivePeriod;
39      protected final int _scavengePeriod;
40      protected final ContextHandlerCollection _contexts;
41      protected SessionIdManager _sessionIdManager;
42  
43      
44      
45      public AbstractTestServer(int port)
46      {
47          this(port, 30, 10);
48      }
49  
50      public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod)
51      {
52          this (port, maxInactivePeriod, scavengePeriod, null);
53      }
54      
55      public AbstractTestServer(int port, int maxInactivePeriod, int scavengePeriod, String sessionIdMgrConfig)
56      {
57          _server = new Server(port);
58          _maxInactivePeriod = maxInactivePeriod;
59          _scavengePeriod = scavengePeriod;
60          _contexts = new ContextHandlerCollection();
61          _sessionIdManager = newSessionIdManager(sessionIdMgrConfig);
62          _server.setSessionIdManager(_sessionIdManager);
63      }
64      
65      
66  
67      public abstract SessionIdManager newSessionIdManager(String config);
68      public abstract SessionManager newSessionManager();
69      public abstract SessionHandler newSessionHandler(SessionManager sessionManager);
70  
71  
72      public void start() throws Exception
73      {
74          // server -> contexts collection -> context handler -> session handler -> servlet handler
75          _server.setHandler(_contexts);
76          _server.start();
77      }
78      
79      public int getPort()
80      {
81          return ((NetworkConnector)getServer().getConnectors()[0]).getLocalPort();
82      }
83  
84      public ServletContextHandler addContext(String contextPath)
85      {
86          ServletContextHandler context = new ServletContextHandler(_contexts, contextPath);
87  
88          SessionManager sessionManager = newSessionManager();
89          sessionManager.setSessionIdManager(_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  
99      public void stop() throws Exception
100     {
101         _server.stop();
102     }
103 
104     public void join() throws Exception
105     {
106         _server.join();
107     }
108 
109     public WebAppContext addWebAppContext(String warPath, String contextPath)
110     {
111         WebAppContext context = new WebAppContext(_contexts, warPath, contextPath);
112 
113         SessionManager sessionManager = newSessionManager();
114         sessionManager.setSessionIdManager(_sessionIdManager);
115         sessionManager.setMaxInactiveInterval(_maxInactivePeriod);
116 
117         SessionHandler sessionHandler = newSessionHandler(sessionManager);
118         sessionManager.setSessionHandler(sessionHandler);
119         context.setSessionHandler(sessionHandler);
120 
121         return context;
122     }
123     
124     public Server getServer()
125     {
126         return _server;
127     }
128 }