/*
* 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.rpc;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import com.weibo.api.motan.util.ReflectUtil;
/**
* @author maijunsheng
* @version 创建时间:2013-5-23
*
*/
public abstract class AbstractProvider<T> implements Provider<T> {
protected Class<T> clz;
protected URL url;
protected boolean alive = false;
protected boolean close = false;
protected Map<String, Method> methodMap = new HashMap<String, Method>();
public AbstractProvider(URL url, Class<T> clz) {
this.url = url;
this.clz = clz;
initMethodMap(clz);
}
@Override
public Response call(Request request) {
Response response = invoke(request);
return response;
}
protected abstract Response invoke(Request request);
@Override
public void init() {
alive = true;
}
@Override
public void destroy() {
alive = false;
close = true;
}
@Override
public boolean isAvailable() {
return alive;
}
@Override
public String desc() {
if (url != null) {
return url.toString();
}
return null;
}
@Override
public URL getUrl() {
return url;
}
@Override
public Class<T> getInterface() {
return clz;
}
protected Method lookup(Request request) {
String methodDesc = ReflectUtil.getMethodDesc(request.getMethodName(), request.getParamtersDesc());
return methodMap.get(methodDesc);
}
private void initMethodMap(Class<T> clz) {
Method[] methods = clz.getMethods();
for (Method method : methods) {
String methodDesc = ReflectUtil.getMethodDesc(method);
methodMap.put(methodDesc, method);
}
}
}