Skip to content

Commit

Permalink
Merge branch 'fix-cev' into '2.7.7-ctrip'
Browse files Browse the repository at this point in the history
Set specific serializer for native hessian and hessian rpc protocol (apache#8238)

See merge request framework/dubbo!51
  • Loading branch information
Yuyang Gong (龚昱阳) committed Sep 8, 2021
2 parents 4d4babe + c355cba commit f05b5d9
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 107 deletions.
4 changes: 2 additions & 2 deletions dubbo-dependencies-bom/pom.xml
Expand Up @@ -108,7 +108,7 @@
<xmemcached_version>1.3.6</xmemcached_version>
<cxf_version>3.1.15</cxf_version>
<thrift_version>0.12.0</thrift_version>
<hessian_version>4.0.38</hessian_version>
<hessian_version>4.0.51</hessian_version>
<protobuf-java_version>3.6.0</protobuf-java_version>
<javax_annotation-api_version>1.3.2</javax_annotation-api_version>
<servlet_version>3.1.0</servlet_version>
Expand Down Expand Up @@ -163,7 +163,7 @@
<mortbay_jetty_version>6.1.26</mortbay_jetty_version>
<portlet_version>2.0</portlet_version>
<maven_flatten_version>1.1.0</maven_flatten_version>
<revision>2.7.7-ctrip.9</revision>
<revision>2.7.7-ctrip.10</revision>
</properties>

<distributionManagement>
Expand Down
2 changes: 1 addition & 1 deletion dubbo-dependencies/dubbo-dependencies-zookeeper/pom.xml
Expand Up @@ -32,7 +32,7 @@
<packaging>pom</packaging>

<properties>
<revision>2.7.7-ctrip.9</revision>
<revision>2.7.7-ctrip.10</revision>
<maven_flatten_version>1.1.0</maven_flatten_version>
</properties>
<distributionManagement>
Expand Down
5 changes: 5 additions & 0 deletions dubbo-rpc/dubbo-rpc-hessian/pom.xml
Expand Up @@ -54,6 +54,11 @@
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-native-hession</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.dubbo.rpc.support.ProtocolUtils;
import org.apache.dubbo.serialize.hessian.dubbo.Hessian2FactoryInitializer;

import com.caucho.hessian.HessianException;
import com.caucho.hessian.client.HessianConnectionException;
Expand Down Expand Up @@ -131,6 +132,7 @@ protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
int timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
hessianProxyFactory.setConnectTimeout(timeout);
hessianProxyFactory.setReadTimeout(timeout);
hessianProxyFactory.setSerializerFactory(Hessian2FactoryInitializer.getInstance().getSerializerFactory());
return (T) hessianProxyFactory.create(serviceType, url.setProtocol("http").toJavaURL(), Thread.currentThread().getContextClassLoader());
}

Expand Down Expand Up @@ -190,7 +192,7 @@ public void handle(HttpServletRequest request, HttpServletResponse response)
}

try {
skeleton.invoke(request.getInputStream(), response.getOutputStream());
skeleton.invoke(request.getInputStream(), response.getOutputStream(), Hessian2FactoryInitializer.getInstance().getSerializerFactory());
} catch (Throwable e) {
throw new ServletException(e);
}
Expand Down
Expand Up @@ -16,8 +16,10 @@
*/
package org.apache.dubbo.serialize.hessian;

import com.caucho.hessian.io.Hessian2Input;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.serialize.hessian.dubbo.Hessian2FactoryInitializer;

import com.caucho.hessian.io.Hessian2Input;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -31,7 +33,7 @@ public class Hessian2ObjectInput implements ObjectInput {

public Hessian2ObjectInput(InputStream is) {
input = new Hessian2Input(is);
input.setSerializerFactory(Hessian2SerializerFactory.INSTANCE);
input.setSerializerFactory(Hessian2FactoryInitializer.getInstance().getSerializerFactory());
}

@Override
Expand Down
Expand Up @@ -16,8 +16,10 @@
*/
package org.apache.dubbo.serialize.hessian;

import com.caucho.hessian.io.Hessian2Output;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.serialize.hessian.dubbo.Hessian2FactoryInitializer;

import com.caucho.hessian.io.Hessian2Output;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -30,7 +32,7 @@ public class Hessian2ObjectOutput implements ObjectOutput {

public Hessian2ObjectOutput(OutputStream os) {
output = new Hessian2Output(os);
output.setSerializerFactory(Hessian2SerializerFactory.INSTANCE);
output.setSerializerFactory(Hessian2FactoryInitializer.getInstance().getSerializerFactory());
}

@Override
Expand Down
Expand Up @@ -16,27 +16,92 @@
*/
package org.apache.dubbo.serialize.hessian;

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 com.caucho.hessian.io.Deserializer;
import com.caucho.hessian.io.HessianProtocolException;
import com.caucho.hessian.io.Serializer;
import com.caucho.hessian.io.SerializerFactory;

import java.util.HashMap;

import static org.apache.dubbo.serialize.hessian.serializer.java8.Java8TimeSerializer.create;

public class Hessian2SerializerFactory extends SerializerFactory {
public static final SerializerFactory INSTANCE = new Hessian2SerializerFactory();
private HashMap _serializerMap = new HashMap();
private HashMap _deserializerMap = new HashMap();

private Hessian2SerializerFactory() {
public Hessian2SerializerFactory() {
super();
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
protected Serializer loadSerializer(Class<?> cl) throws HessianProtocolException {
Serializer serializer = Java8SerializerFactory.INSTANCE.getSerializer(cl);
return serializer != null ? serializer : super.loadSerializer(cl);
public Serializer getSerializer(Class cl) throws HessianProtocolException {
if (isZoneId(cl)) {
return ZoneIdSerializer.getInstance();
}
Object java8Serializer = this._serializerMap.get(cl);
if (java8Serializer != null) {
return (Serializer) java8Serializer;
}
return super.getSerializer(cl);
}

@Override
protected Deserializer loadDeserializer(Class cl) throws HessianProtocolException {
Deserializer deserializer = Java8SerializerFactory.INSTANCE.getDeserializer(cl);
return deserializer != null ? deserializer : super.loadDeserializer(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;
}

public void addSerializer(Class cl, Serializer serializer) {
this._serializerMap.put(cl, serializer);
}

public void addDeserializer(Class cl, Deserializer deserializer) {
this._deserializerMap.put(cl, deserializer);
}

}

This file was deleted.

@@ -0,0 +1,36 @@
/*
* 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.dubbo;

import com.caucho.hessian.io.SerializerFactory;

public abstract class AbstractHessian2FactoryInitializer implements Hessian2FactoryInitializer {
private static SerializerFactory SERIALIZER_FACTORY;

@Override
public SerializerFactory getSerializerFactory() {
if (SERIALIZER_FACTORY != null) {
return SERIALIZER_FACTORY;
}
synchronized (this) {
SERIALIZER_FACTORY = createSerializerFactory();
}
return SERIALIZER_FACTORY;
}

protected abstract SerializerFactory createSerializerFactory();
}
@@ -0,0 +1,28 @@
/*
* 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.dubbo;

import org.apache.dubbo.serialize.hessian.Hessian2SerializerFactory;

import com.caucho.hessian.io.SerializerFactory;

public class DefaultHessian2FactoryInitializer extends AbstractHessian2FactoryInitializer {
@Override
protected SerializerFactory createSerializerFactory() {
return new Hessian2SerializerFactory();
}
}

0 comments on commit f05b5d9

Please sign in to comment.