Skip to content

Commit c043dd3

Browse files
committed
[H2] Use custom QName serializer to fix Kryo ser/deser on JDK17
1 parent ba8a246 commit c043dd3

File tree

10 files changed

+72
-2
lines changed

10 files changed

+72
-2
lines changed

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/CommandDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.sensorhub.api.command.CommandData;
1920
import org.sensorhub.api.common.BigId;
2021
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
2122
import org.sensorhub.impl.datastore.h2.kryo.PersistentClassResolver;
2223
import org.sensorhub.impl.serialization.kryo.BigIdSerializers;
24+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2325
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2426
import org.sensorhub.impl.serialization.kryo.compat.v1.CommandDataSerializerV1;
2527
import org.sensorhub.impl.serialization.kryo.compat.v2.CommandDataSerializerV2;
@@ -39,6 +41,7 @@ class CommandDataType extends KryoDataType
3941
{
4042
this.classResolver = () -> new PersistentClassResolver(kryoClassMap);
4143
this.configurator = kryo -> {
44+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
4245

4346
// register custom serializers w/ backward compatibility
4447
kryo.addDefaultSerializer(CommandData.class,

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/CommandStatusDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.sensorhub.api.command.CommandStatus;
1920
import org.sensorhub.api.common.BigId;
2021
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
2122
import org.sensorhub.impl.datastore.h2.kryo.PersistentClassResolver;
2223
import org.sensorhub.impl.serialization.kryo.BigIdSerializers;
24+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2325
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2426
import org.sensorhub.impl.serialization.kryo.compat.v1.CommandStatusSerializerV1;
2527
import org.sensorhub.impl.serialization.kryo.compat.v2.CommandStatusSerializerV2;
@@ -39,6 +41,7 @@ class CommandStatusDataType extends KryoDataType
3941
{
4042
this.classResolver = () -> new PersistentClassResolver(kryoClassMap);
4143
this.configurator = kryo -> {
44+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
4245

4346
// register custom serializers w/ backward compatibility
4447
kryo.addDefaultSerializer(CommandStatus.class,

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/CommandStreamInfoDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.sensorhub.api.command.CommandStreamInfo;
1920
import org.sensorhub.api.common.BigId;
2021
import org.sensorhub.api.feature.FeatureId;
2122
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
2223
import org.sensorhub.impl.datastore.h2.kryo.PersistentClassResolver;
2324
import org.sensorhub.impl.serialization.kryo.BigIdSerializers;
25+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2426
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2527
import org.sensorhub.impl.serialization.kryo.compat.v1.FeatureIdSerializerV1;
2628
import org.sensorhub.impl.serialization.kryo.compat.v2.CommandStreamInfoSerializerV2;
@@ -34,6 +36,7 @@ class CommandStreamInfoDataType extends KryoDataType
3436
{
3537
this.classResolver = () -> new PersistentClassResolver(kryoClassMap);
3638
this.configurator = kryo -> {
39+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
3740

3841
// avoid using collection serializer on OgcPropertyList because
3942
// the add method doesn't behave as expected

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/DataStreamInfoDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.sensorhub.api.common.BigId;
1920
import org.sensorhub.api.feature.FeatureId;
2021
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
2122
import org.sensorhub.impl.datastore.h2.kryo.PersistentClassResolver;
2223
import org.sensorhub.impl.serialization.kryo.BigIdSerializers;
24+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2325
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2426
import org.sensorhub.impl.serialization.kryo.compat.v1.FeatureIdSerializerV1;
2527
import com.esotericsoftware.kryo.serializers.FieldSerializer;
@@ -32,6 +34,7 @@ class DataStreamInfoDataType extends KryoDataType
3234
{
3335
this.classResolver = () -> new PersistentClassResolver(kryoClassMap);
3436
this.configurator = kryo -> {
37+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
3538

3639
// avoid using collection serializer on OgcPropertyList because
3740
// the add method doesn't behave as expected

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/FeatureDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.locationtech.jts.geom.PrecisionModel;
1920
import org.sensorhub.impl.datastore.h2.kryo.FeatureClassResolver;
2021
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
22+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2123
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2224
import org.sensorhub.impl.serialization.kryo.compat.v1.FeatureSerializerV1;
2325
import org.sensorhub.impl.serialization.kryo.compat.v1.PrecisionModelSerializerV1;
@@ -42,6 +44,7 @@ class FeatureDataType extends KryoDataType
4244
{
4345
this.classResolver = () -> new FeatureClassResolver(kryoClassMap);
4446
this.configurator = kryo -> {
47+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
4548

4649
// register custom serializer w/ backward compatibility
4750
kryo.addDefaultSerializer(IFeature.class,

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/ObsDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.sensorhub.api.common.BigId;
1920
import org.sensorhub.api.data.IObsData;
2021
import org.sensorhub.api.data.ObsData;
2122
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
2223
import org.sensorhub.impl.datastore.h2.kryo.PersistentClassResolver;
2324
import org.sensorhub.impl.serialization.kryo.BigIdSerializers;
25+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2426
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2527
import org.sensorhub.impl.serialization.kryo.compat.v1.ObsDataSerializerV1;
2628

@@ -39,6 +41,7 @@ class ObsDataType extends KryoDataType
3941
{
4042
this.classResolver = () -> new PersistentClassResolver(kryoClassMap);
4143
this.configurator = kryo -> {
44+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
4245

4346
// register custom serializers w/ backward compatibility
4447
kryo.addDefaultSerializer(ObsData.class,

sensorhub-datastore-h2/src/main/java/org/sensorhub/impl/datastore/h2/SensorMLDataType.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
package org.sensorhub.impl.datastore.h2;
1616

17+
import javax.xml.namespace.QName;
1718
import org.h2.mvstore.MVMap;
1819
import org.sensorhub.impl.datastore.h2.kryo.KryoDataType;
1920
import org.sensorhub.impl.datastore.h2.kryo.PersistentClassResolver;
21+
import org.sensorhub.impl.serialization.kryo.QNameSerializer;
2022
import org.sensorhub.impl.serialization.kryo.VersionedSerializer;
2123
import org.sensorhub.impl.serialization.kryo.compat.v3.CapabilityListSerializerV3;
2224
import org.sensorhub.impl.serialization.kryo.compat.v3.CharacteristicListSerializerV3;
@@ -40,6 +42,7 @@ public class SensorMLDataType extends KryoDataType
4042
{
4143
this.classResolver = () -> new PersistentClassResolver(kryoClassMap);
4244
this.configurator = kryo -> {
45+
kryo.addDefaultSerializer(QName.class, QNameSerializer.class);
4346

4447
// avoid using collection serializer on OgcPropertyList because
4548
// the add method doesn't behave as expected

sensorhub-utils-kryo/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ext.details = 'Utility classes for serialization/deserialization/cloning of data
33

44
dependencies {
55
implementation project(':sensorhub-core')
6-
embeddedApi 'com.esotericsoftware:kryo:5.2.0'
6+
embeddedApi 'com.esotericsoftware:kryo:5.5.0'
77
testImplementation project(path: ':sensorhub-core', configuration: 'testArtifacts')
88
}
99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/***************************** BEGIN LICENSE BLOCK ***************************
2+
3+
The contents of this file are subject to the Mozilla Public License, v. 2.0.
4+
If a copy of the MPL was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
7+
Software distributed under the License is distributed on an "AS IS" basis,
8+
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
9+
for the specific language governing rights and limitations under the License.
10+
11+
Copyright (C) 2023 Sensia Software LLC. All Rights Reserved.
12+
13+
******************************* END LICENSE BLOCK ***************************/
14+
15+
package org.sensorhub.impl.serialization.kryo;
16+
17+
import javax.xml.namespace.QName;
18+
import com.esotericsoftware.kryo.Kryo;
19+
import com.esotericsoftware.kryo.Serializer;
20+
import com.esotericsoftware.kryo.io.Input;
21+
import com.esotericsoftware.kryo.io.Output;
22+
23+
24+
public class QNameSerializer extends Serializer<QName> {
25+
26+
public QNameSerializer()
27+
{
28+
setImmutable(true);
29+
}
30+
31+
@Override
32+
public void write(final Kryo kryo, final Output output, final QName qname)
33+
{
34+
var serializer = kryo.getSerializer(String.class);
35+
kryo.writeObjectOrNull(output, qname.getNamespaceURI(), serializer);
36+
kryo.writeObjectOrNull(output, qname.getLocalPart(), serializer);
37+
kryo.writeObjectOrNull(output, qname.getPrefix(), serializer);
38+
}
39+
40+
@Override
41+
public QName read(final Kryo kryo, final Input input, final Class<? extends QName> clazz)
42+
{
43+
var serializer = kryo.getSerializer(String.class);
44+
var namespaceURI = kryo.readObjectOrNull(input, String.class, serializer);
45+
var localPart = kryo.readObjectOrNull(input, String.class, serializer);
46+
var prefix = kryo.readObjectOrNull(input, String.class, serializer);
47+
return new QName(namespaceURI, localPart, prefix);
48+
}
49+
}

0 commit comments

Comments
 (0)