-
Notifications
You must be signed in to change notification settings - Fork 37
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
Added support for all serialization methods in Hazelcast #68
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
import org.apache.juli.logging.LogFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Field; | ||
import java.security.Principal; | ||
import java.util.ArrayList; | ||
|
@@ -128,16 +127,19 @@ private void serializeMap(Map map, ObjectDataOutput objectDataOutput) throws IOE | |
Map.Entry entry = (Map.Entry) entryObject; | ||
Object key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
if (key != null && value != null && key instanceof Serializable && value instanceof Serializable) { | ||
if (key != null && value != null) { | ||
serializableEntries.put(key, value); | ||
} | ||
} | ||
|
||
objectDataOutput.writeInt(serializableEntries.size()); | ||
for (Map.Entry<Object, Object> entryObject : serializableEntries.entrySet()) { | ||
Map.Entry entry = (Map.Entry) entryObject; | ||
objectDataOutput.writeObject(entry.getKey()); | ||
objectDataOutput.writeObject(entry.getValue()); | ||
try { | ||
objectDataOutput.writeObject(entryObject.getKey()); | ||
objectDataOutput.writeObject(entryObject.getValue()); | ||
} catch (Exception e) { | ||
LOG.warn("Unable to serialize object in session", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a warning or more severe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was also a warning when deserialising, I kept it the same for the serialisation exceptions. We can create an issue, if you think we should increase the severity level. |
||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
*/ | ||
|
||
package com.hazelcast.session; | ||
|
||
public class CustomAttribute { | ||
private String value; | ||
|
||
//Needed for Kryo | ||
public CustomAttribute() { | ||
} | ||
|
||
public CustomAttribute(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CustomAttribute{" + "value='" + value + '\'' + '}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.hazelcast.session; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.io.InputChunked; | ||
import com.esotericsoftware.kryo.io.OutputChunked; | ||
import com.hazelcast.nio.ObjectDataInput; | ||
import com.hazelcast.nio.ObjectDataOutput; | ||
import com.hazelcast.nio.serialization.StreamSerializer; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public class HazelcastGlobalKryoSerializer | ||
implements StreamSerializer { | ||
|
||
private final ThreadLocal<Kryo> kryoThreadLocal = new ThreadLocal<Kryo>() { | ||
@Override | ||
protected Kryo initialValue() { | ||
Kryo kryo = new Kryo(); | ||
kryo.register(CustomAttribute.class); | ||
return kryo; | ||
} | ||
}; | ||
|
||
@Override | ||
public int getTypeId() { | ||
return 123; | ||
} | ||
|
||
@Override | ||
public void write(ObjectDataOutput objectDataOutput, Object object) | ||
throws IOException { | ||
OutputChunked output = new OutputChunked((OutputStream) objectDataOutput, 4096); | ||
kryoThreadLocal.get().writeClassAndObject(output, object); | ||
output.endChunk(); | ||
output.flush(); | ||
} | ||
|
||
@Override | ||
public Object read(ObjectDataInput objectDataInput) | ||
throws IOException { | ||
InputStream in = (InputStream) objectDataInput; | ||
InputChunked input = new InputChunked(in, 4096); | ||
return kryoThreadLocal.get().readClassAndObject(input); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
|
||
<!--suppress XmlDefaultAttributeValue --> | ||
<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config | ||
http://www.hazelcast.com/schema/client-config/hazelcast-client-config-3.11.xsd"> | ||
|
||
<serialization> | ||
<serializers> | ||
<global-serializer>com.hazelcast.session.HazelcastGlobalKryoSerializer</global-serializer> | ||
</serializers> | ||
</serialization> | ||
|
||
</hazelcast-client> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a minor comment. Can the link provided here be broken over time? cc: @Serdaro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it is mostly ok to use, since it redirects to the latest version.