Skip to content

Commit

Permalink
Merge pull request #160 from weibocom/feature/yar
Browse files Browse the repository at this point in the history
Feature/yar
  • Loading branch information
rayzhang0603 authored Aug 4, 2016
2 parents 4a77411 + 3bb0db3 commit 3ec0585
Show file tree
Hide file tree
Showing 25 changed files with 1,959 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected Referer<T> doSelect(Request request) {

List<Referer<T>> localReferers = searchLocalReferer(referers, NetUtils.getLocalAddress().getHostAddress());

if (localReferers.isEmpty()) {
if (!localReferers.isEmpty()) {
referers = localReferers;
}

Expand Down
13 changes: 6 additions & 7 deletions motan-extension/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright 2009-2016 Weibo, Inc. ~ ~ Licensed 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
Expand All @@ -7,10 +7,8 @@
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
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.weibo</groupId>
Expand All @@ -19,12 +17,13 @@
</parent>
<artifactId>motan-extension</artifactId>
<name>motan-extension</name>
<url>http://maven.apache.org</url>
<url>https://github.com/weibocom/motan</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<packaging>pom</packaging>
<modules>
<module>serialization-extension</module>
<module>protocol-extension</module>
</modules>
</project>
</project>
44 changes: 44 additions & 0 deletions motan-extension/protocol-extension/motan-protocol-yar/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0"?>
<!--
~ Copyright 2009-2016 Weibo, Inc.
~
~ Licensed 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
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.weibo</groupId>
<artifactId>protocol-extension</artifactId>
<version>0.1.3-SNAPSHOT</version>
</parent>
<artifactId>motan-protocol-yar</artifactId>
<name>motan-protocol-yar</name>
<url>https://github.com/weibocom/motan</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>yar-java</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* Licensed 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 com.weibo.api.motan.protocol.yar;

import java.util.Map;

import com.weibo.yar.YarRequest;

/**
*
* @Description YarRequest with attachments. rpc attachments such as auth,application can pass with
* this class.
* @author zhanglei
* @date 2016-7-26
*
*/
public class AttachmentRequest extends YarRequest {
private Map<String, String> attachments;

public AttachmentRequest(String packagerName, String methodName, Object[] parameters) {
super(packagerName, methodName, parameters);
}

public AttachmentRequest(long id, String packagerName, String methodName, Object[] parameters) {
super(id, packagerName, methodName, parameters);
}

public AttachmentRequest() {
super();
}

public AttachmentRequest(YarRequest yarRequest, Map<String, String> attachments) {
this(yarRequest.getId(), yarRequest.getPackagerName(), yarRequest.getMethodName(), yarRequest.getParameters());
this.attachments = attachments;
}

public Map<String, String> getAttachments() {
return attachments;
}

public void setAttachments(Map<String, String> attachments) {
this.attachments = attachments;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* Licensed 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 com.weibo.api.motan.protocol.yar;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.weibo.api.motan.common.URLParamType;
import com.weibo.api.motan.core.extension.ExtensionLoader;
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.rpc.AbstractExporter;
import com.weibo.api.motan.rpc.Provider;
import com.weibo.api.motan.rpc.URL;
import com.weibo.api.motan.transport.EndpointFactory;
import com.weibo.api.motan.transport.Server;

/**
*
* @Description YarExporter
* @author zhanglei
* @date 2016-5-31
*
*/
public class YarExporter<T> extends AbstractExporter<T> {
protected Server server;
private YarRpcProtocol yarProtocol;

public YarExporter(URL url, Provider<T> provider, YarRpcProtocol yarProtocol) {
super(provider, url);
EndpointFactory endpointFactory =
ExtensionLoader.getExtensionLoader(EndpointFactory.class).getExtension(
url.getParameter(URLParamType.endpointFactory.getName(), "netty4yar"));
// set noheartbeat factory
String heartbeatFactory = url.getParameter(URLParamType.heartbeatFactory.getName());
if (heartbeatFactory == null) {
url.addParameter(URLParamType.heartbeatFactory.getName(), "noHeartbeat");
}
// FIXME to avoid parameters ambiguous in weak type language,parameters size of method with
// same name must be different.
validateInterface(provider.getInterface());
server = endpointFactory.createServer(url, yarProtocol.initRequestRouter(url, provider));

}


@Override
public void destroy() {
server.close();
}

@Override
public boolean isAvailable() {
return server.isAvailable();
}

@Override
public void unexport() {
yarProtocol.unexport(url, provider);
}

@Override
protected boolean doInit() {
return server.open();
}

protected void validateInterface(Class<?> interfaceClazz) {
HashMap<String, List<Integer>> tempMap = new HashMap<String, List<Integer>>();
for (Method m : interfaceClazz.getDeclaredMethods()) {
if (!tempMap.containsKey(m.getName())) {
List<Integer> templist = new ArrayList<Integer>();
templist.add(m.getParameterTypes().length);
tempMap.put(m.getName(), templist);
} else {
List<Integer> templist = tempMap.get(m.getName());
if (templist.contains(m.getParameterTypes().length)) {
throw new MotanFrameworkException("in yar protocol, methods with same name must have different params size !");
} else {
templist.add(m.getParameterTypes().length);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2009-2016 Weibo, Inc.
*
* Licensed 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 com.weibo.api.motan.protocol.yar;

import java.util.concurrent.ConcurrentHashMap;

import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.exception.MotanServiceException;
import com.weibo.api.motan.rpc.Provider;
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.rpc.Response;
import com.weibo.api.motan.transport.Channel;
import com.weibo.api.motan.transport.ProviderProtectedMessageRouter;
import com.weibo.yar.YarRequest;
import com.weibo.yar.YarResponse;
/**
*
* @Description yar message router
* @author zhanglei
* @date 2016-6-8
*
*/
public class YarMessageRouter extends ProviderProtectedMessageRouter {
protected ConcurrentHashMap<String, Provider<?>> providerMap = new ConcurrentHashMap<String, Provider<?>>();

@Override
public Object handle(Channel channel, Object message) {
YarRequest yarRequest = (YarRequest) message;

String packagerName = yarRequest.getPackagerName();
Provider<?> provider = providerMap.get(yarRequest.getRequestPath());
if (provider == null) {
throw new MotanServiceException("can not find service provider. request path:" + yarRequest.getRequestPath());
}
Class<?> clazz = provider.getInterface();
Request request = YarProtocolUtil.convert(yarRequest, clazz);
Response response = call(request, provider);
YarResponse yarResponse = YarProtocolUtil.convert(response, packagerName);
return yarResponse;
}

@Override
public void addProvider(Provider<?> provider) {
String path = YarProtocolUtil.getYarPath(provider.getInterface(), provider.getUrl());
Provider<?> old = providerMap.putIfAbsent(path, provider);
if (old != null) {
throw new MotanFrameworkException("duplicate yar provider");
}
}

@Override
public void removeProvider(Provider<?> provider) {
String path = YarProtocolUtil.getYarPath(provider.getInterface(), provider.getUrl());
providerMap.remove(path);
}

}
Loading

0 comments on commit 3ec0585

Please # to comment.