| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.session.infinispan; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
|
| 23 |
import org.infinispan.protostream.FileDescriptorSource; |
| 24 |
import org.infinispan.protostream.MessageMarshaller; |
| 25 |
import org.infinispan.protostream.ProtobufUtil; |
| 26 |
import org.infinispan.protostream.SerializationContext; |
| 27 |
|
| 28 |
/** |
| 29 |
* SessionDataMarshaller |
| 30 |
* |
| 31 |
* A marshaller for converting a SessionData object into protobuf format which |
| 32 |
* gives greater control over serialization/deserialization. We use that extra |
| 33 |
* control to ensure that session attributes can be deserialized using either |
| 34 |
* the container class loader or the webapp classloader, as appropriate. |
| 35 |
*/ |
| 36 |
public class SessionDataMarshaller implements MessageMarshaller<InfinispanSessionData> |
| 37 |
{ |
| 38 |
/** |
| 39 |
* The version of the serializer. |
| 40 |
*/ |
| 41 |
private static final int VERSION = 0; |
| 42 |
|
| 43 |
private static SerializationContext serializationContext; |
| 44 |
|
| 45 |
private static synchronized void initSerializationContext() throws IOException |
| 46 |
{ |
| 47 |
if (serializationContext != null) |
| 48 |
{ |
| 49 |
return; |
| 50 |
} |
| 51 |
FileDescriptorSource fds = new FileDescriptorSource(); |
| 52 |
fds.addProtoFiles("/session.proto"); |
| 53 |
SerializationContext sCtx = ProtobufUtil.newSerializationContext(); |
| 54 |
sCtx.registerProtoFiles(fds); |
| 55 |
sCtx.registerMarshaller(new SessionDataMarshaller()); |
| 56 |
serializationContext = sCtx; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Class<? extends InfinispanSessionData> getJavaClass() |
| 61 |
{ |
| 62 |
return InfinispanSessionData.class; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public String getTypeName() |
| 67 |
{ |
| 68 |
return "org_eclipse_jetty_session_infinispan.InfinispanSessionData"; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public InfinispanSessionData readFrom(ProtoStreamReader in) throws IOException |
| 73 |
{ |
| 74 |
int version = in.readInt("version"); // version of serialized session |
| 75 |
String id = in.readString("id"); // session id |
| 76 |
String cpath = in.readString("contextPath"); // context path |
| 77 |
String vhost = in.readString("vhost"); // first vhost |
| 78 |
|
| 79 |
long accessed = in.readLong("accessed"); // accessTime |
| 80 |
long lastAccessed = in.readLong("lastAccessed"); // lastAccessTime |
| 81 |
long created = in.readLong("created"); // time created |
| 82 |
long cookieSet = in.readLong("cookieSet"); // time cookie was set |
| 83 |
String lastNode = in.readString("lastNode"); // name of last node |
| 84 |
// managing |
| 85 |
|
| 86 |
long expiry = in.readLong("expiry"); |
| 87 |
long maxInactiveMs = in.readLong("maxInactiveMs"); |
| 88 |
|
| 89 |
InfinispanSessionData sd = new InfinispanSessionData(id, cpath, vhost, created, accessed, lastAccessed, |
| 90 |
maxInactiveMs); |
| 91 |
sd.setCookieSet(cookieSet); |
| 92 |
sd.setLastNode(lastNode); |
| 93 |
sd.setExpiry(expiry); |
| 94 |
|
| 95 |
if (version == 0) |
| 96 |
{ |
| 97 |
byte[] attributeArray = in.readBytes("attributes"); |
| 98 |
//only save the serialized bytes here, do NOT deserialize because |
| 99 |
//infinispan has called this method with their own thread without |
| 100 |
//the appropriate classloader being set |
| 101 |
sd.setSerializedAttributes(attributeArray); |
| 102 |
return sd; |
| 103 |
} |
| 104 |
else |
| 105 |
throw new IOException("Unrecognized infinispan session version " + version); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void writeTo(ProtoStreamWriter out, InfinispanSessionData sdata) throws IOException |
| 110 |
{ |
| 111 |
out.writeInt("version", VERSION); |
| 112 |
out.writeString("id", sdata.getId()); // session id |
| 113 |
out.writeString("contextPath", sdata.getContextPath()); // context path |
| 114 |
out.writeString("vhost", sdata.getVhost()); // first vhost |
| 115 |
|
| 116 |
out.writeLong("accessed", sdata.getAccessed()); // accessTime |
| 117 |
out.writeLong("lastAccessed", sdata.getLastAccessed()); // lastAccessTime |
| 118 |
out.writeLong("created", sdata.getCreated()); // time created |
| 119 |
out.writeLong("cookieSet", sdata.getCookieSet()); // time cookie was set |
| 120 |
out.writeString("lastNode", sdata.getLastNode()); // name of last node |
| 121 |
// managing |
| 122 |
|
| 123 |
out.writeLong("expiry", sdata.getExpiry()); |
| 124 |
out.writeLong("maxInactiveMs", sdata.getMaxInactiveMs()); |
| 125 |
|
| 126 |
//the session data attributes MUST be previously serialized |
| 127 |
//because this method is called from an infinispan thread that cannot |
| 128 |
//have the appropriate classloader set on it |
| 129 |
out.writeBytes("attributes", sdata.getSerializedAttributes()); |
| 130 |
} |
| 131 |
|
| 132 |
} |