Skip to content

Commit

Permalink
Issue #6556 Ensure context classloader set when operating on memcache.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Jul 28, 2021
1 parent a405357 commit a80a379
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 42 deletions.
Expand Up @@ -21,6 +21,7 @@
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.transcoders.SerializingTranscoder;
import org.eclipse.jetty.server.session.RunnableResult;
import org.eclipse.jetty.server.session.SessionContext;
import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataMap;
Expand All @@ -43,6 +44,7 @@ public class MemcachedSessionDataMap extends AbstractLifeCycle implements Sessio
protected int _expirySec = 0;
protected boolean _heartbeats = true;
protected XMemcachedClientBuilder _builder;
protected SessionContext _context;

/**
* SessionDataTranscoder
Expand Down Expand Up @@ -140,9 +142,13 @@ public void setHeartbeats(boolean heartbeats)
@Override
public void initialize(SessionContext context)
{
if (isStarted())
throw new IllegalStateException("Context set after MemcachedSessionDataMap started");

try
{
_builder.setTranscoder(new SessionDataTranscoder());
_context = context;
_builder.setTranscoder(new SessionDataTranscoder(_context.getContext().getClassLoader()));
_client = _builder.build();
_client.setEnableHeartBeat(isHeartbeats());
}
Expand All @@ -155,14 +161,47 @@ public void initialize(SessionContext context)
@Override
public SessionData load(String id) throws Exception
{
SessionData data = _client.get(id);
return data;
if (!isStarted())
throw new IllegalStateException("Not started");

final RunnableResult<SessionData> result = new RunnableResult<>();

Runnable r = () ->
{
try
{
result.setResult(_client.get(id));
}
catch (Exception e)
{
result.setException(e);
}
};

_context.run(r);
return result.getOrThrow();
}

@Override
public void store(String id, SessionData data) throws Exception
{
_client.set(id, _expirySec, data);
if (!isStarted())
throw new IllegalStateException("Not started");

final RunnableResult<Object> result = new RunnableResult<>();
Runnable r = () ->
{
try
{
_client.set(id, _expirySec, data);
}
catch (Exception e)
{
result.setException(e);
}
};
_context.run(r);
result.throwIfException();
}

@Override
Expand Down
Expand Up @@ -40,41 +40,6 @@ public abstract class AbstractSessionDataStore extends ContainerLifeCycle implem
protected long _lastOrphanSweepTime = 0; //last time in ms that we deleted orphaned sessions
protected int _savePeriodSec = DEFAULT_SAVE_PERIOD_SEC; //time in sec between saves

/**
* Small utility class to allow us to
* return a result and an Exception
* from invocation of Runnables.
*
* @param <V> the type of the result.
*/
private class Result<V>
{
private V _result;
private Exception _exception;

public void setResult(V result)
{
_result = result;
}

public void setException(Exception exception)
{
_exception = exception;
}

private void throwIfException() throws Exception
{
if (_exception != null)
throw _exception;
}

public V getOrThrow() throws Exception
{
throwIfException();
return _result;
}
}

/**
* Check if a session for the given id exists.
*
Expand Down Expand Up @@ -171,7 +136,7 @@ public SessionData load(String id) throws Exception
if (!isStarted())
throw new IllegalStateException("Not started");

final Result<SessionData> result = new Result<>();
final RunnableResult<SessionData> result = new RunnableResult<>();

Runnable r = () ->
{
Expand Down Expand Up @@ -214,7 +179,7 @@ public void store(String id, SessionData data) throws Exception
//set the last saved time to now
data.setLastSaved(System.currentTimeMillis());

final Result<Object> result = new Result<>();
final RunnableResult<Object> result = new RunnableResult<>();
Runnable r = () ->
{
try
Expand All @@ -238,7 +203,7 @@ public void store(String id, SessionData data) throws Exception
@Override
public boolean exists(String id) throws Exception
{
Result<Boolean> result = new Result<>();
RunnableResult<Boolean> result = new RunnableResult<>();
Runnable r = () ->
{
try
Expand Down
@@ -0,0 +1,49 @@
//
// ========================================================================
// 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.server.session;

/**
* Small utility class to allow us to
* return a result and an Exception
* from invocation of Runnables.
*
* @param <V> the type of the result.
*/
public class RunnableResult<V>
{
private V _result;
private Exception _exception;

public void setResult(V result)
{
_result = result;
}

public void setException(Exception exception)
{
_exception = exception;
}

public void throwIfException() throws Exception
{
if (_exception != null)
throw _exception;
}

public V getOrThrow() throws Exception
{
throwIfException();
return _result;
}
}

0 comments on commit a80a379

Please sign in to comment.