Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/jetty-9.4.x' into jetty-10.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel committed Mar 23, 2020
2 parents d3567fc + d445d7e commit ea1418a
Show file tree
Hide file tree
Showing 8 changed files with 545 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// 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.session.infinispan;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;

/**
* BoundDelegatingInputStream
*
* An InputStream that delegates methods to an ObjectInput. The ObjectInput must start
* with an integer containing the length of the data.
*/
public class BoundDelegatingInputStream extends InputStream
{

protected final ObjectInput objectInput;
private final int length;
private int position = 0;

public BoundDelegatingInputStream(ObjectInput objectInput) throws IOException
{
this.objectInput = objectInput;
this.length = objectInput.readInt();
}

@Override
public int read() throws IOException
{
if (position < length)
{
position++;
return objectInput.read();
}
return -1;
}

@Override
public int read(byte[] b) throws IOException
{
int available = length - position;
int read = -1;
if (position == length)
{
return read;
}
if (b.length > available)
{
read = objectInput.read(b, 0, available);
}
else
{
read = objectInput.read(b);
}
if (read != -1)
{
position += read;
}
return read;
}

@Override
public int read(byte[] b, int off, int len) throws IOException
{
int read = -1;
if (position == length)
{
return read;
}
if (position + len > length)
{
read = objectInput.read(b, off, length - position);
}
else
{
read = objectInput.read(b, off, len);
}
if (read != -1)
{
position += read;
}
return read;
}

@Override
public long skip(long n) throws IOException
{
long skip = 0;
if (position + n < length)
{
skip = objectInput.skip(length - position);
}
else
{
skip = objectInput.skip(n);
}
if (skip > 0)
{
position += skip;
}
return skip;
}

@Override
public int available() throws IOException
{
if (position < length)
{
int available = objectInput.available();
if (position + available > length)
{
return length - position;
}
else
{
return available;
}
}
return 0;
}

@Override
public void close() throws IOException
{
objectInput.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import org.infinispan.commons.marshall.SerializeWith;

/**
* InfinispanSessionData
Expand All @@ -37,6 +38,7 @@
* pool and thus these threads have no knowledge of the correct classloader to
* use.
*/
@SerializeWith(SessionDataMarshaller.class)
public class InfinispanSessionData extends SessionData
{
protected byte[] _serializedAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
package org.eclipse.jetty.session.infinispan;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.infinispan.commons.marshall.Externalizer;
import org.infinispan.protostream.FileDescriptorSource;
import org.infinispan.protostream.MessageMarshaller;
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.protostream.SerializationContext;

/**
* SessionDataMarshaller
Expand All @@ -30,13 +36,30 @@
* control to ensure that session attributes can be deserialized using either
* the container class loader or the webapp classloader, as appropriate.
*/
public class SessionDataMarshaller implements MessageMarshaller<InfinispanSessionData>
public class SessionDataMarshaller
implements MessageMarshaller<InfinispanSessionData>, Externalizer<InfinispanSessionData>
{
/**
* The version of the serializer.
*/
private static final int VERSION = 0;

private static SerializationContext serializationContext;

private static synchronized void initSerializationContext() throws IOException
{
if (serializationContext != null)
{
return;
}
FileDescriptorSource fds = new FileDescriptorSource();
fds.addProtoFiles("/session.proto");
SerializationContext sCtx = ProtobufUtil.newSerializationContext();
sCtx.registerProtoFiles(fds);
sCtx.registerMarshaller(new SessionDataMarshaller());
serializationContext = sCtx;
}

@Override
public Class<? extends InfinispanSessionData> getJavaClass()
{
Expand All @@ -49,6 +72,39 @@ public String getTypeName()
return "org_eclipse_jetty_session_infinispan.InfinispanSessionData";
}

@Override
public InfinispanSessionData readObject(ObjectInput input) throws IOException, ClassNotFoundException
{
if (serializationContext == null)
{
initSerializationContext();
}

// invokes readFrom(ProtoStreamReader)
InfinispanSessionData data = ProtobufUtil.readFrom(serializationContext, new BoundDelegatingInputStream(input),
InfinispanSessionData.class);
if (data != null)
{
data.deserializeAttributes();
}
return data;
}

@Override
public void writeObject(ObjectOutput output, InfinispanSessionData object) throws IOException
{
if (serializationContext == null)
{
initSerializationContext();
}

// invokes writeTo(ProtoStreamWriter, InfinispanSessionData)
byte[] data = ProtobufUtil.toByteArray(serializationContext, object);
int length = data.length;
output.writeInt(length);
output.write(data);
}

@Override
public InfinispanSessionData readFrom(ProtoStreamReader in) throws IOException
{
Expand All @@ -67,7 +123,8 @@ public InfinispanSessionData readFrom(ProtoStreamReader in) throws IOException
final long expiry = in.readLong("expiry");
final long maxInactiveMs = in.readLong("maxInactiveMs");

InfinispanSessionData sd = new InfinispanSessionData(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs);
InfinispanSessionData sd = new InfinispanSessionData(id, cpath, vhost, created, accessed, lastAccessed,
maxInactiveMs);
sd.setCookieSet(cookieSet);
sd.setLastNode(lastNode);
sd.setExpiry(expiry);
Expand Down Expand Up @@ -103,4 +160,5 @@ public void writeTo(ProtoStreamWriter out, InfinispanSessionData sdata) throws I
sdata.serializeAttributes();
out.writeBytes("attributes", sdata.getSerializedAttributes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// 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 org.eclipse.jetty.session.infinispan.InfinispanSessionDataStoreFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* ClusteredSerializedSessionScavengingTest
*/
public class ClusteredSerializedSessionScavengingTest extends AbstractClusteredSessionScavengingTest
{
public static InfinispanTestSupport __testSupport;

@BeforeAll
public static void setup() throws Exception
{
__testSupport = new InfinispanTestSupport();
__testSupport.setUseFileStore(true);
__testSupport.setSerializeSessionData(true);
__testSupport.setup();
}

@AfterAll
public static void teardown() throws Exception
{
if (__testSupport != null)
__testSupport.teardown();
}

@Override
@Test
public void testClusteredScavenge()
throws Exception
{
super.testClusteredScavenge();
}

/**
* @see org.eclipse.jetty.server.session.AbstractTestBase#createSessionDataStoreFactory()
*/
@Override
public SessionDataStoreFactory createSessionDataStoreFactory()
{
InfinispanSessionDataStoreFactory factory = new InfinispanSessionDataStoreFactory();
factory.setCache(__testSupport.getCache());
return factory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// 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 org.junit.jupiter.api.BeforeEach;

/**
* HotInitInfinispanSessionDataStoreTest
*/
public class InfinispanFileSessionDataStoreTest extends InfinispanSessionDataStoreTest
{

@BeforeEach
public void setup() throws Exception
{
_testSupport = new InfinispanTestSupport();
_testSupport.setUseFileStore(true);
_testSupport.setup();
}

}
Loading

0 comments on commit ea1418a

Please sign in to comment.