native hessian merge (#3966)

* Merge serialization-native-hessian-for-apache-dubbo into incubator-dubbo

* test success

* test success

* Merge serialization-native-hessian-for-apache-dubbo into incubator-dubbo

* change maven install

* add apache license

* remove @Generated

* optimize import

* remove thrift

* merge master

* distribution pom add native hessian
This commit is contained in:
huaifeng 2019-05-06 10:26:31 +08:00 committed by Ian Luo
parent c98abb2f95
commit 77df9d7bd2
28 changed files with 1449 additions and 0 deletions

View File

@ -338,6 +338,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-native-hession</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-jdk</artifactId>
@ -583,6 +590,7 @@
<include>org.apache.dubbo:dubbo-metadata-report-zookeeper</include>
<include>org.apache.dubbo:dubbo-metadata-report-consul</include>
<include>org.apache.dubbo:dubbo-metadata-report-etcd</include>
<include>org.apache.dubbo:dubbo-serialization-native-hession</include>
</includes>
</artifactSet>
<transformers>

View File

@ -307,6 +307,11 @@
<artifactId>dubbo-serialization-hessian2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-native-hession</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-jdk</artifactId>

View File

@ -300,6 +300,11 @@
<artifactId>dubbo-serialization-hessian2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-native-hession</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-jdk</artifactId>

View File

@ -0,0 +1,42 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-serialization</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-serialization-native-hession</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The native-hession serialization module of dubbo project</description>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian;
import com.caucho.hessian.io.Hessian2Input;
import org.apache.dubbo.common.serialize.ObjectInput;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
/**
* Hessian2 Object input.
*/
public class Hessian2ObjectInput implements ObjectInput {
private final Hessian2Input input;
public Hessian2ObjectInput(InputStream is) {
input = new Hessian2Input(is);
input.setSerializerFactory(Hessian2SerializerFactory.INSTANCE);
}
@Override
public boolean readBool() throws IOException {
return input.readBoolean();
}
@Override
public byte readByte() throws IOException {
return (byte) input.readInt();
}
@Override
public short readShort() throws IOException {
return (short) input.readInt();
}
@Override
public int readInt() throws IOException {
return input.readInt();
}
@Override
public long readLong() throws IOException {
return input.readLong();
}
@Override
public float readFloat() throws IOException {
return (float) input.readDouble();
}
@Override
public double readDouble() throws IOException {
return input.readDouble();
}
@Override
public byte[] readBytes() throws IOException {
return input.readBytes();
}
@Override
public String readUTF() throws IOException {
return input.readString();
}
@Override
public Object readObject() throws IOException {
return input.readObject();
}
@Override
@SuppressWarnings("unchecked")
public <T> T readObject(Class<T> cls) throws IOException {
return (T) input.readObject(cls);
}
@Override
public <T> T readObject(Class<T> cls, Type type) throws IOException {
return readObject(cls);
}
}

View File

@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian;
import com.caucho.hessian.io.Hessian2Output;
import org.apache.dubbo.common.serialize.ObjectOutput;
import java.io.IOException;
import java.io.OutputStream;
/**
* Hessian2 Object output.
*/
public class Hessian2ObjectOutput implements ObjectOutput {
private final Hessian2Output output;
public Hessian2ObjectOutput(OutputStream os) {
output = new Hessian2Output(os);
output.setSerializerFactory(Hessian2SerializerFactory.INSTANCE);
}
@Override
public void writeBool(boolean v) throws IOException {
output.writeBoolean(v);
}
@Override
public void writeByte(byte v) throws IOException {
output.writeInt(v);
}
@Override
public void writeShort(short v) throws IOException {
output.writeInt(v);
}
@Override
public void writeInt(int v) throws IOException {
output.writeInt(v);
}
@Override
public void writeLong(long v) throws IOException {
output.writeLong(v);
}
@Override
public void writeFloat(float v) throws IOException {
output.writeDouble(v);
}
@Override
public void writeDouble(double v) throws IOException {
output.writeDouble(v);
}
@Override
public void writeBytes(byte[] b) throws IOException {
output.writeBytes(b);
}
@Override
public void writeBytes(byte[] b, int off, int len) throws IOException {
output.writeBytes(b, off, len);
}
@Override
public void writeUTF(String v) throws IOException {
output.writeString(v);
}
@Override
public void writeObject(Object obj) throws IOException {
output.writeObject(obj);
}
@Override
public void flushBuffer() throws IOException {
output.flushBuffer();
}
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.Serialization;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Hessian2Serialization implements Serialization {
@Override
public byte getContentTypeId() {
return 10;
}
@Override
public String getContentType() {
return "x-application/native-hessian";
}
@Override
public ObjectOutput serialize(URL url, OutputStream out) throws IOException {
return new Hessian2ObjectOutput(out);
}
@Override
public ObjectInput deserialize(URL url, InputStream is) throws IOException {
return new Hessian2ObjectInput(is);
}
}

View File

@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian;
import com.caucho.hessian.io.Deserializer;
import com.caucho.hessian.io.HessianProtocolException;
import com.caucho.hessian.io.Serializer;
import com.caucho.hessian.io.SerializerFactory;
public class Hessian2SerializerFactory extends SerializerFactory {
public static final SerializerFactory INSTANCE = new Hessian2SerializerFactory();
private Hessian2SerializerFactory() {
super();
}
@Override
protected Serializer loadSerializer(Class<?> cl) throws HessianProtocolException {
Serializer serializer = Java8SerializerFactory.INSTANCE.getSerializer(cl);
return serializer != null ? serializer : super.loadSerializer(cl);
}
@Override
protected Deserializer loadDeserializer(Class cl) throws HessianProtocolException {
Deserializer deserializer = Java8SerializerFactory.INSTANCE.getDeserializer(cl);
return deserializer != null ? deserializer : super.loadDeserializer(cl);
}
}

View File

@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian;
import com.caucho.hessian.io.AbstractSerializerFactory;
import com.caucho.hessian.io.ExtSerializerFactory;
import com.caucho.hessian.io.HessianProtocolException;
import com.caucho.hessian.io.Serializer;
import org.apache.dubbo.serialize.hessian.serializer.java8.DurationHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.InstantHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.LocalDateHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.LocalDateTimeHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.LocalTimeHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.MonthDayHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.OffsetDateTimeHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.OffsetTimeHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.PeriodHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.YearHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.YearMonthHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.ZoneIdSerializer;
import org.apache.dubbo.serialize.hessian.serializer.java8.ZoneOffsetHandle;
import org.apache.dubbo.serialize.hessian.serializer.java8.ZonedDateTimeHandle;
import static org.apache.dubbo.serialize.hessian.serializer.java8.Java8TimeSerializer.create;
public class Java8SerializerFactory extends ExtSerializerFactory {
public static final AbstractSerializerFactory INSTANCE = new Java8SerializerFactory();
private Java8SerializerFactory() {
if (isJava8()) {
try {
this.addSerializer(Class.forName("java.time.LocalTime"), create(LocalTimeHandle.class));
this.addSerializer(Class.forName("java.time.LocalDate"), create(LocalDateHandle.class));
this.addSerializer(Class.forName("java.time.LocalDateTime"), create(LocalDateTimeHandle.class));
this.addSerializer(Class.forName("java.time.Instant"), create(InstantHandle.class));
this.addSerializer(Class.forName("java.time.Duration"), create(DurationHandle.class));
this.addSerializer(Class.forName("java.time.Period"), create(PeriodHandle.class));
this.addSerializer(Class.forName("java.time.Year"), create(YearHandle.class));
this.addSerializer(Class.forName("java.time.YearMonth"), create(YearMonthHandle.class));
this.addSerializer(Class.forName("java.time.MonthDay"), create(MonthDayHandle.class));
this.addSerializer(Class.forName("java.time.OffsetDateTime"), create(OffsetDateTimeHandle.class));
this.addSerializer(Class.forName("java.time.ZoneOffset"), create(ZoneOffsetHandle.class));
this.addSerializer(Class.forName("java.time.OffsetTime"), create(OffsetTimeHandle.class));
this.addSerializer(Class.forName("java.time.ZonedDateTime"), create(ZonedDateTimeHandle.class));
} catch (ClassNotFoundException e) {
// ignore
}
}
}
@Override
public Serializer getSerializer(Class cl) throws HessianProtocolException {
return isZoneId(cl) ? ZoneIdSerializer.getInstance() : super.getSerializer(cl);
}
private static boolean isZoneId(Class cl) {
try {
return isJava8() && Class.forName("java.time.ZoneId").isAssignableFrom(cl);
} catch (ClassNotFoundException e) {
// ignore
}
return false;
}
private static boolean isJava8() {
String javaVersion = System.getProperty("java.specification.version");
return Double.valueOf(javaVersion) >= 1.8;
}
}

View File

@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.Duration;
public class DurationHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -4367309317780077156L;
private long seconds;
private int nanos;
public DurationHandle() {
}
public DurationHandle(Object o) {
try {
Duration duration = (Duration) o;
this.seconds = duration.getSeconds();
this.nanos = duration.getNano();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return Duration.ofSeconds(seconds, nanos);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.Instant;
public class InstantHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -4367309317780077156L;
private long seconds;
private int nanos;
public InstantHandle() {
}
public InstantHandle(Object o) {
try {
Instant instant = (Instant) o;
this.seconds = instant.getEpochSecond();
this.nanos = instant.getNano();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return Instant.ofEpochSecond(seconds, nanos);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.AbstractHessianOutput;
import com.caucho.hessian.io.AbstractSerializer;
import java.io.IOException;
import java.lang.reflect.Constructor;
public class Java8TimeSerializer<T> extends AbstractSerializer {
// Type of handle
private Class<T> handleType;
private Java8TimeSerializer(Class<T> handleType) {
this.handleType = handleType;
}
public static <T> Java8TimeSerializer<T> create(Class<T> handleType) {
return new Java8TimeSerializer<T>(handleType);
}
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (obj == null) {
out.writeNull();
return;
}
T handle = null;
try {
Constructor<T> constructor = handleType.getConstructor(Object.class);
handle = constructor.newInstance(obj);
} catch (Exception e) {
throw new RuntimeException("the class :" + handleType.getName() + " construct failed:" + e.getMessage(), e);
}
out.writeObject(handle);
}
}

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.LocalDate;
public class LocalDateHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = 166018689500019951L;
private int year;
private int month;
private int day;
public LocalDateHandle() {
}
public LocalDateHandle(Object o) {
try {
LocalDate localDate = (LocalDate) o;
this.year = localDate.getYear();
this.month = localDate.getMonthValue();
this.day = localDate.getDayOfMonth();
} catch (Throwable t) {
// ignore
}
}
public Object readResolve() {
try {
return LocalDate.of(year, month, day);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalDateTimeHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = 7563825215275989361L;
private LocalDate date;
private LocalTime time;
public LocalDateTimeHandle() {
}
public LocalDateTimeHandle(Object o) {
try {
LocalDateTime localDateTime = (LocalDateTime) o;
date = localDateTime.toLocalDate();
time = localDateTime.toLocalTime();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return LocalDateTime.of(date, time);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.LocalTime;
public class LocalTimeHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -5892919085390462315L;
private int hour;
private int minute;
private int second;
private int nano;
public LocalTimeHandle() {
}
public LocalTimeHandle(Object o) {
try {
LocalTime localTime = (LocalTime) o;
this.hour = localTime.getHour();
this.minute = localTime.getMinute();
this.second = localTime.getSecond();
this.nano = localTime.getNano();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return LocalTime.of(hour, minute, second, nano);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.MonthDay;
public class MonthDayHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = 5288238558666577745L;
private int month;
private int day;
public MonthDayHandle() {
}
public MonthDayHandle(Object o) {
try {
MonthDay monthDay = (MonthDay) o;
this.month = monthDay.getMonthValue();
this.day = monthDay.getDayOfMonth();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return MonthDay.of(month, day);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class OffsetDateTimeHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -7823900532640515312L;
private LocalDateTime dateTime;
private ZoneOffset offset;
public OffsetDateTimeHandle() {
}
public OffsetDateTimeHandle(Object o) {
try {
OffsetDateTime offsetDateTime = (OffsetDateTime) o;
this.dateTime = offsetDateTime.toLocalDateTime();
this.offset = offsetDateTime.getOffset();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return OffsetDateTime.of(dateTime, offset);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class OffsetTimeHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -3269846941421652860L;
private LocalTime localTime;
private ZoneOffset zoneOffset;
public OffsetTimeHandle() {
}
public OffsetTimeHandle(Object o) {
try {
OffsetTime offsetTime = (OffsetTime) o;
this.zoneOffset = offsetTime.getOffset();
this.localTime = offsetTime.toLocalTime();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return OffsetTime.of(localTime, zoneOffset);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.Period;
public class PeriodHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = 4399720381283781186L;
private int years;
private int months;
private int days;
public PeriodHandle() {
}
public PeriodHandle(Object o) {
try {
Period period = (Period) o;
this.years = period.getYears();
this.months = period.getMonths();
this.days = period.getDays();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return Period.of(years, months, days);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.Year;
public class YearHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -6299552890287487926L;
private int year;
public YearHandle() {
}
public YearHandle(Object o) {
try {
Year y = (Year) o;
this.year = y.getValue();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return Year.of(year);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.YearMonth;
public class YearMonthHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -4150786187896925314L;
private int year;
private int month;
public YearMonthHandle() {
}
public YearMonthHandle(Object o) {
try {
YearMonth yearMonth = (YearMonth) o;
this.year = yearMonth.getYear();
this.month = yearMonth.getMonthValue();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return YearMonth.of(year, month);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.ZoneId;
public class ZoneIdHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = 8789182864066905552L;
private String zoneId;
public ZoneIdHandle() {
}
public ZoneIdHandle(Object o) {
try {
ZoneId zoneId = (ZoneId) o;
this.zoneId = zoneId.getId();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return ZoneId.of(this.zoneId);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.AbstractHessianOutput;
import com.caucho.hessian.io.AbstractSerializer;
import java.io.IOException;
public class ZoneIdSerializer extends AbstractSerializer {
private static final ZoneIdSerializer SERIALIZER = new ZoneIdSerializer();
public static ZoneIdSerializer getInstance() {
return SERIALIZER;
}
@Override
public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
if (obj == null) {
out.writeNull();
} else {
out.writeObject(new ZoneIdHandle(obj));
}
}
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.ZoneOffset;
public class ZoneOffsetHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = 8841589723587858789L;
private int seconds;
public ZoneOffsetHandle() {
}
public ZoneOffsetHandle(Object o) {
try {
ZoneOffset zoneOffset = (ZoneOffset) o;
this.seconds = zoneOffset.getTotalSeconds();
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return ZoneOffset.ofTotalSeconds(seconds);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian.serializer.java8;
import com.caucho.hessian.io.HessianHandle;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ZonedDateTimeHandle implements HessianHandle, Serializable {
private static final long serialVersionUID = -6933460123278647569L;
private LocalDateTime dateTime;
private ZoneOffset offset;
private String zoneId;
public ZonedDateTimeHandle() {
}
public ZonedDateTimeHandle(Object o) {
try {
ZonedDateTime zonedDateTime = (ZonedDateTime) o;
this.dateTime = zonedDateTime.toLocalDateTime();
this.offset = zonedDateTime.getOffset();
ZoneId zone = zonedDateTime.getZone();
if (zone != null) {
this.zoneId = zone.getId();
}
} catch (Throwable t) {
// ignore
}
}
private Object readResolve() {
try {
return ZonedDateTime.ofLocal(dateTime, ZoneId.of(zoneId), offset);
} catch (Throwable t) {
// ignore
}
return null;
}
}

View File

@ -0,0 +1 @@
native-hessian=org.apache.dubbo.serialize.hessian.Hessian2Serialization

View File

@ -0,0 +1,150 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.dubbo.serialize.hessian;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Calendar;
/**
* Test Java8TimeSerializer class
*/
public class Java8TimeSerializerTest {
private static SerializerFactory factory = Hessian2SerializerFactory.INSTANCE;
private static ByteArrayOutputStream os = new ByteArrayOutputStream();
@Test
public void testNull() throws IOException {
testJava8Time(null);
}
@Test
public void testInstant() throws Exception {
Instant.now();
testJava8Time(Instant.now());
}
@Test
public void testDuration() throws Exception {
testJava8Time(Duration.ofDays(2));
}
@Test
public void testLocalDate() throws Exception {
testJava8Time(LocalDate.now());
}
@Test
public void testLocalDateTime() throws Exception {
testJava8Time(LocalDateTime.now());
}
@Test
public void testLocalTime() throws Exception {
testJava8Time(LocalTime.now());
}
@Test
public void testYear() throws Exception {
testJava8Time(Year.now());
}
@Test
public void testYearMonth() throws Exception {
testJava8Time(YearMonth.now());
}
@Test
public void testMonthDay() throws Exception {
testJava8Time(MonthDay.now());
}
@Test
public void testPeriod() throws Exception {
testJava8Time(Period.ofDays(3));
}
@Test
public void testOffsetTime() throws Exception {
testJava8Time(OffsetTime.now());
}
@Test
public void testZoneOffset() throws Exception {
testJava8Time(ZoneOffset.ofHours(8));
}
@Test
public void testOffsetDateTime() throws Throwable {
testJava8Time(OffsetDateTime.now());
}
@Test
public void testZonedDateTime() throws Exception {
testJava8Time(ZonedDateTime.now());
}
@Test
public void testZoneId() throws Exception {
testJava8Time(ZoneId.of("America/New_York"));
}
@Test
public void testCalendar() throws IOException {
testJava8Time(Calendar.getInstance());
}
private void testJava8Time(Object expected) throws IOException {
os.reset();
Hessian2Output output = new Hessian2Output(os);
output.setSerializerFactory(factory);
output.writeObject(expected);
output.flush();
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
Hessian2Input input = new Hessian2Input(is);
input.setSerializerFactory(factory);
Object actual = input.readObject();
Assertions.assertEquals(expected, actual);
}
}

View File

@ -39,5 +39,6 @@
<module>dubbo-serialization-avro</module>
<module>dubbo-serialization-test</module>
<module>dubbo-serialization-gson</module>
<module>dubbo-serialization-native-hession</module>
</modules>
</project>