-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from weibocom/feature/yar
Feature/yar
- Loading branch information
Showing
25 changed files
with
1,959 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
motan-extension/protocol-extension/motan-protocol-yar/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
56 changes: 56 additions & 0 deletions
56
.../motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/AttachmentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
...ension/motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/YarExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...n/motan-protocol-yar/src/main/java/com/weibo/api/motan/protocol/yar/YarMessageRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.