Merge remote-tracking branch 'origin/jetty-9.4.x'
This commit is contained in:
commit
7dc283cc0c
|
@ -297,6 +297,7 @@ public class GCloudSessionDataStore extends AbstractSessionDataStore
|
||||||
{
|
{
|
||||||
if (LOG.isDebugEnabled()) LOG.debug("Writing session {} to DataStore", data.getId());
|
if (LOG.isDebugEnabled()) LOG.debug("Writing session {} to DataStore", data.getId());
|
||||||
|
|
||||||
|
//TODO implement backoff strategy
|
||||||
Entity entity = entityFromSession(data, makeKey(id, _context));
|
Entity entity = entityFromSession(data, makeKey(id, _context));
|
||||||
_datastore.put(entity);
|
_datastore.put(entity);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,103 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<parent>
|
||||||
|
<groupId>org.eclipse.jetty.memcached</groupId>
|
||||||
|
<artifactId>memcached-parent</artifactId>
|
||||||
|
<version>9.4.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>jetty-memcached-sessions</artifactId>
|
||||||
|
<name>Jetty :: Memcached :: Sessions</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-server</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.xmemcached</groupId>
|
||||||
|
<artifactId>xmemcached</artifactId>
|
||||||
|
<version>2.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>1.7.9</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-webapp</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-client</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||||
|
<artifactId>jetty-test-helper</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<bundle-symbolic-name>${project.groupId}.session</bundle-symbolic-name>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>manifest</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Export-Package>org.eclipse.jetty.memcached.session.*;version="${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}";</Export-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>memcached</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>memcached.enabled</name>
|
||||||
|
<value>true</value>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>false</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,129 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// All rights reserved. This program and the accompanying materials
|
||||||
|
// are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
// and Apache License v2.0 which accompanies this distribution.
|
||||||
|
//
|
||||||
|
// The Eclipse Public License is available at
|
||||||
|
// http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
//
|
||||||
|
// The Apache License v2.0 is available at
|
||||||
|
// http://www.opensource.org/licenses/apache2.0.php
|
||||||
|
//
|
||||||
|
// You may elect to redistribute this code under either of these licenses.
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
package org.eclipse.jetty.memcached.session;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.server.session.SessionContext;
|
||||||
|
import org.eclipse.jetty.server.session.SessionData;
|
||||||
|
import org.eclipse.jetty.server.session.SessionDataMap;
|
||||||
|
|
||||||
|
import net.rubyeye.xmemcached.MemcachedClient;
|
||||||
|
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MemcachedSessionDataMap
|
||||||
|
*
|
||||||
|
* Uses memcached as a cache for SessionData.
|
||||||
|
*/
|
||||||
|
public class MemcachedSessionDataMap implements SessionDataMap
|
||||||
|
{
|
||||||
|
public static final String DEFAULT_HOST = "localhost";
|
||||||
|
public static final String DEFAULT_PORT = "11211";
|
||||||
|
protected MemcachedClient _client;
|
||||||
|
protected int _expirySec = 0;
|
||||||
|
protected XMemcachedClientBuilder _builder;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param host
|
||||||
|
* @param port
|
||||||
|
*/
|
||||||
|
public MemcachedSessionDataMap(String host, String port)
|
||||||
|
{
|
||||||
|
if (host == null || port == null)
|
||||||
|
throw new IllegalArgumentException("Host: "+host+" port: "+port);
|
||||||
|
_builder = new XMemcachedClientBuilder(host+":"+port);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the builder
|
||||||
|
*/
|
||||||
|
public XMemcachedClientBuilder getBuilder()
|
||||||
|
{
|
||||||
|
return _builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param sec
|
||||||
|
*/
|
||||||
|
public void setExpirySec (int sec)
|
||||||
|
{
|
||||||
|
_expirySec = sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.SessionDataMap#initialize(org.eclipse.jetty.server.session.SessionContext)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void initialize(SessionContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_client = _builder.build();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.SessionDataMap#load(java.lang.String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SessionData load(String id) throws Exception
|
||||||
|
{
|
||||||
|
SessionData data = _client.get(id);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.SessionDataMap#store(java.lang.String, org.eclipse.jetty.server.session.SessionData)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void store(String id, SessionData data) throws Exception
|
||||||
|
{
|
||||||
|
_client.set(id, _expirySec, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.SessionDataMap#delete(java.lang.String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean delete(String id) throws Exception
|
||||||
|
{
|
||||||
|
_client.delete(id);
|
||||||
|
return true; //delete returns false if the value didn't exist
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// All rights reserved. This program and the accompanying materials
|
||||||
|
// are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
// and Apache License v2.0 which accompanies this distribution.
|
||||||
|
//
|
||||||
|
// The Eclipse Public License is available at
|
||||||
|
// http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
//
|
||||||
|
// The Apache License v2.0 is available at
|
||||||
|
// http://www.opensource.org/licenses/apache2.0.php
|
||||||
|
//
|
||||||
|
// You may elect to redistribute this code under either of these licenses.
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
package org.eclipse.jetty.memcached.session;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.server.session.SessionDataMap;
|
||||||
|
import org.eclipse.jetty.server.session.SessionDataMapFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MemcachedSessionDataMapFactory
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MemcachedSessionDataMapFactory implements SessionDataMapFactory
|
||||||
|
{
|
||||||
|
protected String _host = "localhost";
|
||||||
|
protected String _port = "11211";
|
||||||
|
protected int _expiry;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getHost()
|
||||||
|
{
|
||||||
|
return _host;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setHost(String host)
|
||||||
|
{
|
||||||
|
_host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getPort()
|
||||||
|
{
|
||||||
|
return _port;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setPort(String port)
|
||||||
|
{
|
||||||
|
_port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int getExpiry()
|
||||||
|
{
|
||||||
|
return _expiry;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setExpiry(int expiry)
|
||||||
|
{
|
||||||
|
_expiry = expiry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.SessionDataMapFactory#getSessionDataMap()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public SessionDataMap getSessionDataMap()
|
||||||
|
{
|
||||||
|
MemcachedSessionDataMap m = new MemcachedSessionDataMap(_host, _port);
|
||||||
|
m.setExpirySec(_expiry);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,225 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// All rights reserved. This program and the accompanying materials
|
||||||
|
// are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
// and Apache License v2.0 which accompanies this distribution.
|
||||||
|
//
|
||||||
|
// The Eclipse Public License is available at
|
||||||
|
// http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
//
|
||||||
|
// The Apache License v2.0 is available at
|
||||||
|
// http://www.opensource.org/licenses/apache2.0.php
|
||||||
|
//
|
||||||
|
// You may elect to redistribute this code under either of these licenses.
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
package org.eclipse.jetty.memcached.session;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.client.api.ContentResponse;
|
||||||
|
import org.eclipse.jetty.client.api.Request;
|
||||||
|
import org.eclipse.jetty.server.NetworkConnector;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.session.AbstractSessionCache;
|
||||||
|
import org.eclipse.jetty.server.session.CachingSessionDataStore;
|
||||||
|
import org.eclipse.jetty.server.session.NullSessionDataStore;
|
||||||
|
import org.eclipse.jetty.server.session.Session;
|
||||||
|
import org.eclipse.jetty.server.session.SessionData;
|
||||||
|
import org.eclipse.jetty.server.session.SessionHandler;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.servlet.ServletHolder;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TestMemcachedSessions
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestMemcachedSessions
|
||||||
|
{
|
||||||
|
public static class TestServlet extends HttpServlet
|
||||||
|
{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
|
||||||
|
{
|
||||||
|
String arg = req.getParameter("action");
|
||||||
|
if (arg == null)
|
||||||
|
return;
|
||||||
|
HttpSession s = null;
|
||||||
|
if ("set".equals(arg))
|
||||||
|
{
|
||||||
|
s = req.getSession(true);
|
||||||
|
assertNotNull(s);
|
||||||
|
s.setAttribute("val", req.getParameter("value"));
|
||||||
|
}
|
||||||
|
else if ("get".equals(arg))
|
||||||
|
{
|
||||||
|
s = req.getSession(false);
|
||||||
|
System.err.println("GET: s="+s);
|
||||||
|
}
|
||||||
|
else if ("del".equals(arg))
|
||||||
|
{
|
||||||
|
s = req.getSession();
|
||||||
|
assertNotNull(s);
|
||||||
|
s.invalidate();
|
||||||
|
s = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.setContentType("text/html");
|
||||||
|
PrintWriter w = resp.getWriter();
|
||||||
|
if (s == null)
|
||||||
|
w.write("No session");
|
||||||
|
else
|
||||||
|
w.write((String)s.getAttribute("val"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class NullSessionCache extends AbstractSessionCache
|
||||||
|
{
|
||||||
|
|
||||||
|
public NullSessionCache(SessionHandler handler)
|
||||||
|
{
|
||||||
|
super(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Session newSession(SessionData data)
|
||||||
|
{
|
||||||
|
return new Session(_handler, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Session newSession(HttpServletRequest request, SessionData data)
|
||||||
|
{
|
||||||
|
return new Session(_handler, request, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Session doGet(String id)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Session doPutIfAbsent(String id, Session session)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doReplace(String id, Session oldValue, Session newValue)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Session doDelete(String id)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMemcached () throws Exception
|
||||||
|
{
|
||||||
|
String contextPath = "/";
|
||||||
|
Server server = new Server(0);
|
||||||
|
|
||||||
|
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||||
|
context.setContextPath("/");
|
||||||
|
context.setResourceBase(System.getProperty("java.io.tmpdir"));
|
||||||
|
server.setHandler(context);
|
||||||
|
NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
|
||||||
|
dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
|
||||||
|
context.getSessionHandler().setSessionCache(dsc);
|
||||||
|
|
||||||
|
// Add a test servlet
|
||||||
|
ServletHolder h = new ServletHolder();
|
||||||
|
h.setServlet(new TestServlet());
|
||||||
|
context.addServlet(h, "/");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
server.start();
|
||||||
|
int port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort();
|
||||||
|
HttpClient client = new HttpClient();
|
||||||
|
client.start();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
int value = 42;
|
||||||
|
ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
|
||||||
|
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||||
|
String sessionCookie = response.getHeaders().get("Set-Cookie");
|
||||||
|
assertTrue(sessionCookie != null);
|
||||||
|
// Mangle the cookie, replacing Path with $Path, etc.
|
||||||
|
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
|
||||||
|
|
||||||
|
String resp = response.getContentAsString();
|
||||||
|
assertEquals(resp.trim(),String.valueOf(value));
|
||||||
|
|
||||||
|
// Be sure the session value is still there
|
||||||
|
Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
|
||||||
|
request.header("Cookie", sessionCookie);
|
||||||
|
response = request.send();
|
||||||
|
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||||
|
|
||||||
|
resp = response.getContentAsString();
|
||||||
|
assertEquals(String.valueOf(value),resp.trim());
|
||||||
|
|
||||||
|
|
||||||
|
//Delete the session
|
||||||
|
request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
|
||||||
|
request.header("Cookie", sessionCookie);
|
||||||
|
response = request.send();
|
||||||
|
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||||
|
|
||||||
|
|
||||||
|
//Check that the session is gone
|
||||||
|
request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
|
||||||
|
request.header("Cookie", sessionCookie);
|
||||||
|
response = request.send();
|
||||||
|
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||||
|
resp = response.getContentAsString();
|
||||||
|
assertEquals("No session", resp.trim());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
client.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>jetty-project</artifactId>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<version>9.4.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.eclipse.jetty.memcached</groupId>
|
||||||
|
<artifactId>memcached-parent</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>Jetty :: Memcached</name>
|
||||||
|
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>jetty-memcached-sessions</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
|
@ -666,6 +666,7 @@ public abstract class AbstractSessionCache extends ContainerLifeCycle implements
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
LOG.warn("Passivation of idle session {} failed", session.getId(), e);
|
LOG.warn("Passivation of idle session {} failed", session.getId(), e);
|
||||||
|
//TODO reset the timer so it can be retried later and leave it in the cache
|
||||||
doDelete(session.getId()); //detach it
|
doDelete(session.getId()); //detach it
|
||||||
session.setResident(false);
|
session.setResident(false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ public abstract class AbstractSessionDataStore extends AbstractLifeCycle impleme
|
||||||
/**
|
/**
|
||||||
* @see org.eclipse.jetty.server.session.SessionDataStore#initialize(org.eclipse.jetty.server.session.SessionContext)
|
* @see org.eclipse.jetty.server.session.SessionDataStore#initialize(org.eclipse.jetty.server.session.SessionContext)
|
||||||
*/
|
*/
|
||||||
public void initialize (SessionContext context)
|
public void initialize (SessionContext context) throws Exception
|
||||||
{
|
{
|
||||||
if (isStarted())
|
if (isStarted())
|
||||||
throw new IllegalStateException("Context set after SessionDataStore started");
|
throw new IllegalStateException("Context set after SessionDataStore started");
|
||||||
|
|
|
@ -24,7 +24,7 @@ import java.util.Set;
|
||||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CachingSessionStore
|
* CachingSessionDataStore
|
||||||
*
|
*
|
||||||
* A SessionDataStore is a mechanism for (persistently) storing data associated with sessions.
|
* A SessionDataStore is a mechanism for (persistently) storing data associated with sessions.
|
||||||
* This implementation delegates to a pluggable SessionDataStore for actually storing the
|
* This implementation delegates to a pluggable SessionDataStore for actually storing the
|
||||||
|
@ -42,7 +42,7 @@ import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||||
* possible that failures can result in cache inconsistency.
|
* possible that failures can result in cache inconsistency.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CachingSessionStore extends AbstractLifeCycle implements SessionDataStore
|
public class CachingSessionDataStore extends AbstractLifeCycle implements SessionDataStore
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,7 +61,7 @@ public class CachingSessionStore extends AbstractLifeCycle implements SessionDat
|
||||||
* @param cache
|
* @param cache
|
||||||
* @param store
|
* @param store
|
||||||
*/
|
*/
|
||||||
public CachingSessionStore (SessionDataMap cache, SessionDataStore store)
|
public CachingSessionDataStore (SessionDataMap cache, SessionDataStore store)
|
||||||
{
|
{
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_store = store;
|
_store = store;
|
||||||
|
@ -143,7 +143,6 @@ public class CachingSessionStore extends AbstractLifeCycle implements SessionDat
|
||||||
{
|
{
|
||||||
//write to the SessionDataStore first
|
//write to the SessionDataStore first
|
||||||
_store.store(id, data);
|
_store.store(id, data);
|
||||||
|
|
||||||
//then update the cache with written data
|
//then update the cache with written data
|
||||||
_cache.store(id,data);
|
_cache.store(id,data);
|
||||||
}
|
}
|
||||||
|
@ -191,7 +190,7 @@ public class CachingSessionStore extends AbstractLifeCycle implements SessionDat
|
||||||
* @see org.eclipse.jetty.server.session.SessionDataStore#initialize(org.eclipse.jetty.server.session.SessionContext)
|
* @see org.eclipse.jetty.server.session.SessionDataStore#initialize(org.eclipse.jetty.server.session.SessionContext)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void initialize(SessionContext context)
|
public void initialize(SessionContext context) throws Exception
|
||||||
{
|
{
|
||||||
//pass through
|
//pass through
|
||||||
_store.initialize(context);
|
_store.initialize(context);
|
||||||
|
@ -206,6 +205,4 @@ public class CachingSessionStore extends AbstractLifeCycle implements SessionDat
|
||||||
{
|
{
|
||||||
return _store.newSessionData(id, created, accessed, lastAccessed, maxInactiveMs);
|
return _store.newSessionData(id, created, accessed, lastAccessed, maxInactiveMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -20,11 +20,11 @@
|
||||||
package org.eclipse.jetty.server.session;
|
package org.eclipse.jetty.server.session;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CachingSessionStoreFactory
|
* CachingSessionDataStoreFactory
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CachingSessionStoreFactory extends AbstractSessionDataStoreFactory
|
public class CachingSessionDataStoreFactory extends AbstractSessionDataStoreFactory
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,12 +32,24 @@ public class CachingSessionStoreFactory extends AbstractSessionDataStoreFactory
|
||||||
*/
|
*/
|
||||||
protected SessionDataStoreFactory _backingSessionStoreFactory;
|
protected SessionDataStoreFactory _backingSessionStoreFactory;
|
||||||
|
|
||||||
|
protected SessionDataMapFactory _mapFactory;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public SessionDataMapFactory getMapFactory()
|
||||||
|
{
|
||||||
|
return _mapFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMapFactory(SessionDataMapFactory map)
|
||||||
|
{
|
||||||
|
_mapFactory = map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param factory The factory for the actual SessionDataStore that the
|
* @param factory The factory for the actual SessionDataStore that the
|
||||||
* CachingSessionStore will delegate to
|
* CachingSessionDataStore will delegate to
|
||||||
*/
|
*/
|
||||||
public void setBackingSessionStoreFactory (SessionDataStoreFactory factory)
|
public void setBackingSessionStoreFactory (SessionDataStoreFactory factory)
|
||||||
{
|
{
|
||||||
|
@ -51,7 +63,7 @@ public class CachingSessionStoreFactory extends AbstractSessionDataStoreFactory
|
||||||
public SessionDataStore getSessionDataStore(SessionHandler handler) throws Exception
|
public SessionDataStore getSessionDataStore(SessionHandler handler) throws Exception
|
||||||
{
|
{
|
||||||
// TODO configure and create a cache!
|
// TODO configure and create a cache!
|
||||||
return new CachingSessionStore(null, _backingSessionStoreFactory.getSessionDataStore(handler));
|
return new CachingSessionDataStore(_mapFactory.getSessionDataMap(), _backingSessionStoreFactory.getSessionDataStore(handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -33,6 +33,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
|
||||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
import org.omg.CORBA.Environment;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -324,8 +325,17 @@ public class DefaultSessionIdManager extends AbstractLifeCycle implements Sessio
|
||||||
protected void doStart() throws Exception
|
protected void doStart() throws Exception
|
||||||
{
|
{
|
||||||
if (_server == null)
|
if (_server == null)
|
||||||
throw new IllegalStateException("No Server for SessionIdManager");
|
throw new IllegalStateException ("No Server for SessionIdManager");
|
||||||
|
|
||||||
initRandom();
|
initRandom();
|
||||||
|
|
||||||
|
if (_workerName == null)
|
||||||
|
{
|
||||||
|
String inst = System.getenv("JETTY_WORKER_INSTANCE");
|
||||||
|
_workerName = "node"+ (inst==null?"0":inst);
|
||||||
|
LOG.warn("No workerName configured for DefaultSessionIdManager, using {}",_workerName);
|
||||||
|
}
|
||||||
|
|
||||||
_workerAttr=(_workerName!=null && _workerName.startsWith("$"))?_workerName.substring(1):null;
|
_workerAttr=(_workerName!=null && _workerName.startsWith("$"))?_workerName.substring(1):null;
|
||||||
|
|
||||||
if (_houseKeeper == null)
|
if (_houseKeeper == null)
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// All rights reserved. This program and the accompanying materials
|
||||||
|
// are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
// and Apache License v2.0 which accompanies this distribution.
|
||||||
|
//
|
||||||
|
// The Eclipse Public License is available at
|
||||||
|
// http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
//
|
||||||
|
// The Apache License v2.0 is available at
|
||||||
|
// http://www.opensource.org/licenses/apache2.0.php
|
||||||
|
//
|
||||||
|
// You may elect to redistribute this code under either of these licenses.
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
package org.eclipse.jetty.server.session;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NullSessionCache
|
||||||
|
*
|
||||||
|
* Does not actually cache any Session objects. Useful for testing.
|
||||||
|
* Also useful if you do not want to share Session objects with the same id between
|
||||||
|
* simultaneous requests: note that this means that context forwarding can't share
|
||||||
|
* the same id either.
|
||||||
|
*/
|
||||||
|
public class NullSessionCache extends AbstractSessionCache
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param handler
|
||||||
|
*/
|
||||||
|
public NullSessionCache(SessionHandler handler)
|
||||||
|
{
|
||||||
|
super(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.SessionCache#shutdown()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void shutdown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.AbstractSessionCache#newSession(org.eclipse.jetty.server.session.SessionData)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Session newSession(SessionData data)
|
||||||
|
{
|
||||||
|
return new Session(getSessionHandler(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.AbstractSessionCache#newSession(javax.servlet.http.HttpServletRequest, org.eclipse.jetty.server.session.SessionData)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Session newSession(HttpServletRequest request, SessionData data)
|
||||||
|
{
|
||||||
|
return new Session(getSessionHandler(), request, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.AbstractSessionCache#doGet(java.lang.String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Session doGet(String id)
|
||||||
|
{
|
||||||
|
//do not cache anything
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.AbstractSessionCache#doPutIfAbsent(java.lang.String, org.eclipse.jetty.server.session.Session)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Session doPutIfAbsent(String id, Session session)
|
||||||
|
{
|
||||||
|
//nothing was stored previously
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.AbstractSessionCache#doReplace(java.lang.String, org.eclipse.jetty.server.session.Session, org.eclipse.jetty.server.session.Session)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean doReplace(String id, Session oldValue, Session newValue)
|
||||||
|
{
|
||||||
|
//always accept new value
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.eclipse.jetty.server.session.AbstractSessionCache#doDelete(java.lang.String)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Session doDelete(String id)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,7 +24,7 @@ import java.util.Set;
|
||||||
/**
|
/**
|
||||||
* NullSessionDataStore
|
* NullSessionDataStore
|
||||||
*
|
*
|
||||||
*
|
* Does not actually store anything, useful for testing.
|
||||||
*/
|
*/
|
||||||
public class NullSessionDataStore extends AbstractSessionDataStore
|
public class NullSessionDataStore extends AbstractSessionDataStore
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,17 +26,17 @@ package org.eclipse.jetty.server.session;
|
||||||
public interface SessionDataMap
|
public interface SessionDataMap
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Initialize this session data store for the
|
* Initialize this data map for the
|
||||||
* given context. A SessionDataStore can only
|
* given context. A SessionDataMap can only
|
||||||
* be used by one context(/session manager).
|
* be used by one context(/session manager).
|
||||||
*
|
*
|
||||||
* @param context context associated
|
* @param context context associated
|
||||||
*/
|
*/
|
||||||
void initialize(SessionContext context);
|
void initialize(SessionContext context) throws Exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read in session data from storage
|
* Read in session data.
|
||||||
* @param id identity of session to load
|
* @param id identity of session to load
|
||||||
* @return the SessionData matching the id
|
* @return the SessionData matching the id
|
||||||
* @throws Exception if unable to load session data
|
* @throws Exception if unable to load session data
|
||||||
|
@ -46,7 +46,7 @@ public interface SessionDataMap
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write out session data to storage
|
* Store the session data.
|
||||||
* @param id identity of session to store
|
* @param id identity of session to store
|
||||||
* @param data info of session to store
|
* @param data info of session to store
|
||||||
* @throws Exception if unable to write session data
|
* @throws Exception if unable to write session data
|
||||||
|
@ -56,9 +56,9 @@ public interface SessionDataMap
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete session data from storage
|
* Delete session data
|
||||||
* @param id identity of session to delete
|
* @param id identity of session to delete
|
||||||
* @return true if the session was deleted from the permanent store
|
* @return true if the session was deleted
|
||||||
* @throws Exception if unable to delete session data
|
* @throws Exception if unable to delete session data
|
||||||
*/
|
*/
|
||||||
public boolean delete (String id) throws Exception;
|
public boolean delete (String id) throws Exception;
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
// All rights reserved. This program and the accompanying materials
|
||||||
|
// are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
// and Apache License v2.0 which accompanies this distribution.
|
||||||
|
//
|
||||||
|
// The Eclipse Public License is available at
|
||||||
|
// http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
//
|
||||||
|
// The Apache License v2.0 is available at
|
||||||
|
// http://www.opensource.org/licenses/apache2.0.php
|
||||||
|
//
|
||||||
|
// You may elect to redistribute this code under either of these licenses.
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
package org.eclipse.jetty.server.session;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SessionDataMapFactory
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface SessionDataMapFactory
|
||||||
|
{
|
||||||
|
SessionDataMap getSessionDataMap();
|
||||||
|
}
|
1
pom.xml
1
pom.xml
|
@ -549,6 +549,7 @@
|
||||||
<module>jetty-nosql</module>
|
<module>jetty-nosql</module>
|
||||||
<module>jetty-infinispan</module>
|
<module>jetty-infinispan</module>
|
||||||
<module>jetty-gcloud</module>
|
<module>jetty-gcloud</module>
|
||||||
|
<module>jetty-memcached</module>
|
||||||
<module>jetty-unixsocket</module>
|
<module>jetty-unixsocket</module>
|
||||||
<module>tests</module>
|
<module>tests</module>
|
||||||
<module>examples</module>
|
<module>examples</module>
|
||||||
|
|
Loading…
Reference in New Issue