Skip to content

Commit

Permalink
Code clean up. (sofastack#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjboy authored Sep 28, 2018
1 parent a74e9d0 commit 68f2519
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 20 deletions.
6 changes: 0 additions & 6 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
Expand Down Expand Up @@ -317,11 +316,6 @@
<artifactId>consul-api</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import javax.annotation.concurrent.ThreadSafe;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.rpc.codec;

import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoader;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
Expand Down Expand Up @@ -76,7 +77,11 @@ public static Compressor getCompressor(String alias) {
* @return Compressor
*/
public static Compressor getCompressor(byte code) {
return TYPE_COMPRESSOR_MAP.get(code);
Compressor compressor = TYPE_COMPRESSOR_MAP.get(code);
if (compressor == null) {
throw new SofaRpcRuntimeException("Compressor Not Found :\"" + code + "\"!");
}
return compressor;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.rpc.codec;

import com.alipay.sofa.rpc.common.struct.TwoWayMap;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoader;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
Expand Down Expand Up @@ -78,7 +79,11 @@ public static Serializer getSerializer(String alias) {
* @return 序列化器
*/
public static Serializer getSerializer(byte type) {
return TYPE_SERIALIZER_MAP.get(type);
Serializer serializer = TYPE_SERIALIZER_MAP.get(type);
if (serializer == null) {
throw new SofaRpcRuntimeException("Serializer Not Found :\"" + type + "\"!");
}
return serializer;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadPoolExecutor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ protected void readLine(URL url, String line) {
LOGGER.warn("Extension {} of extensible {} is disabled, cause by: {}",
className, interfaceName, ExceptionUtils.toShortString(e, 2));
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Extension " + className + " of extensible " + interfaceName + " is disabled.", e);
}
return;
}
if (!interfaceClass.isAssignableFrom(tmp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
*/
package com.alipay.sofa.rpc.codec;

import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import org.junit.Assert;
import org.junit.Test;

/**
*
*
* @author <a href="mailto:[email protected]">GengZhang</a>
*/
public class CompressorFactoryTest {
Expand All @@ -44,4 +43,12 @@ public void getCodeByAlias() throws Exception {
Assert.assertEquals(CompressorFactory.getCodeByAlias("test"), (byte) 113);
}

@Test
public void getCompressorNotExist() throws Exception {
try {
CompressorFactory.getCompressor((byte) 999);
Assert.fail();
} catch (SofaRpcRuntimeException e) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.rpc.codec;

import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -47,4 +48,13 @@ public void getAliasByCode() {
Assert.assertEquals("test", SerializerFactory.getAliasByCode((byte) 117));
}

}
@Test
public void getSerializerNotExist() {
try {
SerializerFactory.getSerializer((byte) 999);
Assert.fail();
} catch (SofaRpcRuntimeException e) {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alipay.sofa.rpc.registry.utils;

import com.alipay.sofa.rpc.client.ProviderHelper;
import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.client.ProviderInfoAttrs;
import com.alipay.sofa.rpc.client.ProviderStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public void run() {
HttpResponseStatus status = null;
ProviderConfig providerConfig = null;
String serviceName = request.getTargetServiceUniqueName();
Serializer serializer = SerializerFactory.getSerializer(request.getSerializeType());
Serializer serializer = null;
if (request.getSerializeType() > 0) {
serializer = SerializerFactory.getSerializer(request.getSerializeType());
}

try { // 这个try-catch 保证一定有Response
invoke:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ protected void sendRpcError(HttpResponseStatus status, ByteBuf data) {

private void sendHttp2Response0(HttpResponseStatus status, boolean error, ByteBuf data) {
Http2Headers headers = new DefaultHttp2Headers().status(status.codeAsText());

if (request.getSerializeType() > 0) {
String serialization = SerializerFactory.getAliasByCode(request.getSerializeType());
headers.set(RemotingConstants.HEAD_SERIALIZE_TYPE, serialization);
} else {
headers.set(CONTENT_TYPE, "text/plain; charset=" + RpcConstants.DEFAULT_CHARSET.displayName());
}
if (error) {
headers.set(RemotingConstants.HEAD_RESPONSE_ERROR, "true");
headers.set(CONTENT_TYPE, "text/plain; charset=" + RpcConstants.DEFAULT_CHARSET.displayName());
} else {
headers.set(RemotingConstants.HEAD_SERIALIZE_TYPE,
SerializerFactory.getAliasByCode(request.getSerializeType()));
}
if (data != null) {
encoder.writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
Expand Down

0 comments on commit 68f2519

Please sign in to comment.