/
MemcachedSessionDataMap.java
225 lines (200 loc) · 6.26 KB
/
MemcachedSessionDataMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.memcached.session;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.transcoders.SerializingTranscoder;
import org.eclipse.jetty.server.session.SessionContext;
import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataMap;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
/**
* MemcachedSessionDataMap
*
* Uses memcached as a cache for SessionData.
*/
@ManagedObject
public class MemcachedSessionDataMap extends AbstractLifeCycle implements SessionDataMap
{
public static final String DEFAULT_HOST = "localhost";
public static final String DEFAULT_PORT = "11211";
protected MemcachedClient _client;
protected int _expirySec = 0;
protected boolean _heartbeats = true;
protected XMemcachedClientBuilder _builder;
protected SessionContext _context;
/**
* SessionDataTranscoder
*
* We override memcached deserialization to use our classloader-aware
* ObjectInputStream.
*/
public static class SessionDataTranscoder extends SerializingTranscoder
{
@Override
protected Object deserialize(byte[] in)
{
Object rv = null;
if (in != null)
{
try (ByteArrayInputStream bis = new ByteArrayInputStream(in);
ClassLoadingObjectInputStream is = new ClassLoadingObjectInputStream(bis))
{
rv = is.readObject();
}
catch (IOException e)
{
log.error("Caught IOException decoding " + in.length + " bytes of data", e);
}
catch (ClassNotFoundException e)
{
log.error("Caught CNFE decoding " + in.length + " bytes of data", e);
}
}
return rv;
}
}
/**
* @param host address of memcache server
* @param port address of memcache server
*/
public MemcachedSessionDataMap(String host, String port)
{
if (host == null || port == null)
throw new IllegalArgumentException("Host: " + host + " port: " + port);
_builder = new XMemcachedClientBuilder(host + ":" + port);
}
public MemcachedSessionDataMap(List<InetSocketAddress> addresses)
{
_builder = new XMemcachedClientBuilder(addresses);
}
public MemcachedSessionDataMap(List<InetSocketAddress> addresses, int[] weights)
{
_builder = new XMemcachedClientBuilder(addresses, weights);
}
/**
* @return the builder
*/
public XMemcachedClientBuilder getBuilder()
{
return _builder;
}
/**
* @param sec the expiry to use in seconds
*/
public void setExpirySec(int sec)
{
_expirySec = sec;
}
/**
* Expiry time for memached entries.
*
* @return memcached expiry time in sec
*/
@ManagedAttribute(value = "memcached expiry time in sec", readonly = true)
public int getExpirySec()
{
return _expirySec;
}
@ManagedAttribute(value = "enable memcached heartbeats", readonly = true)
public boolean isHeartbeats()
{
return _heartbeats;
}
public void setHeartbeats(boolean heartbeats)
{
_heartbeats = heartbeats;
}
@Override
public void initialize(SessionContext context)
{
if (isStarted())
throw new IllegalStateException("Context set after MemcachedSessionDataMap started");
try
{
_context = context;
_builder.setTranscoder(new SessionDataTranscoder());
_client = _builder.build();
_client.setEnableHeartBeat(isHeartbeats());
}
catch (IOException e)
{
throw new IllegalStateException(e);
}
}
@Override
public SessionData load(String id) throws Exception
{
if (!isStarted())
throw new IllegalStateException("Not started");
final FuturePromise<SessionData> result = new FuturePromise<>();
Runnable r = () ->
{
try
{
result.succeeded(_client.get(id));
}
catch (Exception e)
{
result.failed(e);
}
};
_context.run(r);
return result.getOrThrow();
}
@Override
public void store(String id, SessionData data) throws Exception
{
if (!isStarted())
throw new IllegalStateException("Not started");
final FuturePromise<Void> result = new FuturePromise<>();
Runnable r = () ->
{
try
{
_client.set(id, _expirySec, data);
result.succeeded(null);
}
catch (Exception e)
{
result.failed(e);
}
};
_context.run(r);
result.getOrThrow();
}
@Override
public boolean delete(String id) throws Exception
{
_client.delete(id);
return true; //delete returns false if the value didn't exist
}
@Override
protected void doStop() throws Exception
{
super.doStop();
if (_client != null)
{
_client.shutdown();
_client = null;
}
}
}