| 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.ByteArrayInputStream; |
| 22 |
import java.io.ByteArrayOutputStream; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.ObjectOutputStream; |
| 25 |
import java.util.Map; |
| 26 |
|
| 27 |
import org.eclipse.jetty.server.session.SessionData; |
| 28 |
import org.eclipse.jetty.util.ClassLoadingObjectInputStream; |
| 29 |
import org.infinispan.commons.marshall.SerializeWith; |
| 30 |
|
| 31 |
/** |
| 32 |
* InfinispanSessionData |
| 33 |
* |
| 34 |
* Specialization of SessionData to hold the attributes as a serialized byte |
| 35 |
* array. This is necessary because to deserialize the attributes correctly, we |
| 36 |
* need to know which classloader to use, which is normally provided as the |
| 37 |
* thread context classloader. However, infinispan marshalling uses a thread |
| 38 |
* pool and thus these threads have no knowledge of the correct classloader to |
| 39 |
* use. |
| 40 |
*/ |
| 41 |
public class InfinispanSessionData extends SessionData |
| 42 |
{ |
| 43 |
protected byte[] _serializedAttributes; |
| 44 |
|
| 45 |
public InfinispanSessionData(String id, String cpath, String vhost, long created, long accessed, long lastAccessed, long maxInactiveMs) |
| 46 |
{ |
| 47 |
super(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs); |
| 48 |
} |
| 49 |
|
| 50 |
public InfinispanSessionData(String id, String cpath, String vhost, long created, long accessed, long lastAccessed, long maxInactiveMs, |
| 51 |
Map<String, Object> attributes) |
| 52 |
{ |
| 53 |
super(id, cpath, vhost, created, accessed, lastAccessed, maxInactiveMs, attributes); |
| 54 |
} |
| 55 |
|
| 56 |
public byte[] getSerializedAttributes() |
| 57 |
{ |
| 58 |
return _serializedAttributes; |
| 59 |
} |
| 60 |
|
| 61 |
public void setSerializedAttributes(byte[] serializedAttributes) |
| 62 |
{ |
| 63 |
_serializedAttributes = serializedAttributes; |
| 64 |
} |
| 65 |
|
| 66 |
public void deserializeAttributes() throws ClassNotFoundException, IOException |
| 67 |
{ |
| 68 |
if (_serializedAttributes == null) |
| 69 |
return; |
| 70 |
|
| 71 |
try (ByteArrayInputStream bais = new ByteArrayInputStream(_serializedAttributes); |
| 72 |
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(bais)) |
| 73 |
{ |
| 74 |
SessionData.deserializeAttributes(this, ois); |
| 75 |
_serializedAttributes = null; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public void serializeAttributes() throws IOException |
| 80 |
{ |
| 81 |
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 82 |
ObjectOutputStream oos = new ObjectOutputStream(baos)) |
| 83 |
{ |
| 84 |
SessionData.serializeAttributes(this, oos); |
| 85 |
_serializedAttributes = baos.toByteArray(); |
| 86 |
} |
| 87 |
} |
| 88 |
} |