diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml index cceac62cc8..816dfa7507 100644 --- a/dubbo-all/pom.xml +++ b/dubbo-all/pom.xml @@ -338,6 +338,13 @@ compile true + + org.apache.dubbo + dubbo-serialization-native-hession + ${project.version} + compile + true + org.apache.dubbo dubbo-serialization-jdk @@ -583,6 +590,7 @@ org.apache.dubbo:dubbo-metadata-report-zookeeper org.apache.dubbo:dubbo-metadata-report-consul org.apache.dubbo:dubbo-metadata-report-etcd + org.apache.dubbo:dubbo-serialization-native-hession diff --git a/dubbo-bom/pom.xml b/dubbo-bom/pom.xml index 2a6ef723e0..fb28b65bc4 100644 --- a/dubbo-bom/pom.xml +++ b/dubbo-bom/pom.xml @@ -307,6 +307,11 @@ dubbo-serialization-hessian2 ${project.version} + + org.apache.dubbo + dubbo-serialization-native-hession + ${project.version} + org.apache.dubbo dubbo-serialization-jdk diff --git a/dubbo-distribution/pom.xml b/dubbo-distribution/pom.xml index f830aa4379..ef24feb356 100644 --- a/dubbo-distribution/pom.xml +++ b/dubbo-distribution/pom.xml @@ -300,6 +300,11 @@ dubbo-serialization-hessian2 ${project.version} + + org.apache.dubbo + dubbo-serialization-native-hession + ${project.version} + org.apache.dubbo dubbo-serialization-jdk diff --git a/dubbo-serialization/dubbo-serialization-native-hession/pom.xml b/dubbo-serialization/dubbo-serialization-native-hession/pom.xml new file mode 100644 index 0000000000..8d641db13c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/pom.xml @@ -0,0 +1,42 @@ + + + + + dubbo-serialization + org.apache.dubbo + ${revision} + + 4.0.0 + + dubbo-serialization-native-hession + jar + ${project.artifactId} + The native-hession serialization module of dubbo project + + + + org.apache.dubbo + dubbo-serialization-api + ${project.parent.version} + + + com.caucho + hessian + + + \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2ObjectInput.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2ObjectInput.java new file mode 100644 index 0000000000..4bbb13fc97 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2ObjectInput.java @@ -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 readObject(Class cls) throws IOException { + return (T) input.readObject(cls); + } + + @Override + public T readObject(Class cls, Type type) throws IOException { + return readObject(cls); + } + +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2ObjectOutput.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2ObjectOutput.java new file mode 100644 index 0000000000..e3f5fa2661 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2ObjectOutput.java @@ -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(); + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2Serialization.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2Serialization.java new file mode 100644 index 0000000000..aeda121d01 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2Serialization.java @@ -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); + } + +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2SerializerFactory.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2SerializerFactory.java new file mode 100644 index 0000000000..2e87375c5f --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Hessian2SerializerFactory.java @@ -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); + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Java8SerializerFactory.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Java8SerializerFactory.java new file mode 100644 index 0000000000..6280023315 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/Java8SerializerFactory.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/DurationHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/DurationHandle.java new file mode 100755 index 0000000000..67615f3d86 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/DurationHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/InstantHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/InstantHandle.java new file mode 100755 index 0000000000..6b9a1a8d9b --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/InstantHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/Java8TimeSerializer.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/Java8TimeSerializer.java new file mode 100755 index 0000000000..4b4494db52 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/Java8TimeSerializer.java @@ -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 extends AbstractSerializer { + + // Type of handle + private Class handleType; + + private Java8TimeSerializer(Class handleType) { + this.handleType = handleType; + } + + public static Java8TimeSerializer create(Class handleType) { + return new Java8TimeSerializer(handleType); + } + + @Override + public void writeObject(Object obj, AbstractHessianOutput out) throws IOException { + if (obj == null) { + out.writeNull(); + return; + } + + T handle = null; + try { + Constructor 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); + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalDateHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalDateHandle.java new file mode 100755 index 0000000000..f994bc4c2d --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalDateHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalDateTimeHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalDateTimeHandle.java new file mode 100755 index 0000000000..094ced53c4 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalDateTimeHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalTimeHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalTimeHandle.java new file mode 100755 index 0000000000..b0d9619901 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/LocalTimeHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/MonthDayHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/MonthDayHandle.java new file mode 100755 index 0000000000..8c19273c10 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/MonthDayHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/OffsetDateTimeHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/OffsetDateTimeHandle.java new file mode 100755 index 0000000000..b785266ef2 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/OffsetDateTimeHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/OffsetTimeHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/OffsetTimeHandle.java new file mode 100755 index 0000000000..5a5730054f --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/OffsetTimeHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/PeriodHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/PeriodHandle.java new file mode 100755 index 0000000000..d29b39f771 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/PeriodHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/YearHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/YearHandle.java new file mode 100755 index 0000000000..6560c0d839 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/YearHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/YearMonthHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/YearMonthHandle.java new file mode 100755 index 0000000000..5f67c2ef75 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/YearMonthHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneIdHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneIdHandle.java new file mode 100755 index 0000000000..f1254690d7 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneIdHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneIdSerializer.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneIdSerializer.java new file mode 100755 index 0000000000..7104670296 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneIdSerializer.java @@ -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)); + } + } + +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneOffsetHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneOffsetHandle.java new file mode 100755 index 0000000000..f7e622a73c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZoneOffsetHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZonedDateTimeHandle.java b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZonedDateTimeHandle.java new file mode 100755 index 0000000000..44b3ff536c --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/java/org/apache/dubbo/serialize/hessian/serializer/java8/ZonedDateTimeHandle.java @@ -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; + } +} diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization b/dubbo-serialization/dubbo-serialization-native-hession/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization new file mode 100644 index 0000000000..ad6601604e --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.common.serialize.Serialization @@ -0,0 +1 @@ +native-hessian=org.apache.dubbo.serialize.hessian.Hessian2Serialization \ No newline at end of file diff --git a/dubbo-serialization/dubbo-serialization-native-hession/src/test/java/org/apache/dubbo/serialize/hessian/Java8TimeSerializerTest.java b/dubbo-serialization/dubbo-serialization-native-hession/src/test/java/org/apache/dubbo/serialize/hessian/Java8TimeSerializerTest.java new file mode 100644 index 0000000000..a481dc0cca --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-native-hession/src/test/java/org/apache/dubbo/serialize/hessian/Java8TimeSerializerTest.java @@ -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); + } +} diff --git a/dubbo-serialization/pom.xml b/dubbo-serialization/pom.xml index 97b986d1b8..b6b4b5c5ce 100644 --- a/dubbo-serialization/pom.xml +++ b/dubbo-serialization/pom.xml @@ -39,5 +39,6 @@ dubbo-serialization-avro dubbo-serialization-test dubbo-serialization-gson + dubbo-serialization-native-hession