Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #6556 Ensure context classloader set when operating on memcache. #6557

Merged
merged 5 commits into from Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,8 +142,12 @@ public void setHeartbeats(boolean 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());
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);
gregw marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}