use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ServiceConfig method doExport.
@SuppressWarnings("unchecked")
private void doExport(ProtocolConfig protocolConfig, int port, List<URL> registryURLs) {
String protocolName = protocolConfig.getName();
if (protocolName == null || protocolName.length() == 0) {
protocolName = URLParamType.protocol.getValue();
}
String hostAddress = host;
if (StringUtils.isBlank(hostAddress) && basicServiceConfig != null) {
hostAddress = basicServiceConfig.getHost();
}
if (NetUtils.isInvalidLocalHost(hostAddress)) {
hostAddress = getLocalHostAddress(registryURLs);
}
Map<String, String> map = new HashMap<String, String>();
map.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_SERVICE);
map.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis()));
collectConfigParams(map, protocolConfig, basicServiceConfig, extConfig, this);
collectMethodConfigParams(map, this.getMethods());
URL serviceUrl = new URL(protocolName, hostAddress, port, interfaceClass.getName(), map);
if (serviceExists(serviceUrl)) {
LoggerUtil.warn(String.format("%s configService is malformed, for same service (%s) already exists ", interfaceClass.getName(), serviceUrl.getIdentity()));
throw new MotanFrameworkException(String.format("%s configService is malformed, for same service (%s) already exists ", interfaceClass.getName(), serviceUrl.getIdentity()), MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
List<URL> urls = new ArrayList<URL>();
// injvm 协议只支持注册到本地,其他协议可以注册到local、remote
if (MotanConstants.PROTOCOL_INJVM.equals(protocolConfig.getId())) {
URL localRegistryUrl = null;
for (URL ru : registryURLs) {
if (MotanConstants.REGISTRY_PROTOCOL_LOCAL.equals(ru.getProtocol())) {
localRegistryUrl = ru.createCopy();
break;
}
}
if (localRegistryUrl == null) {
localRegistryUrl = new URL(MotanConstants.REGISTRY_PROTOCOL_LOCAL, hostAddress, MotanConstants.DEFAULT_INT_VALUE, RegistryService.class.getName());
}
urls.add(localRegistryUrl);
} else {
for (URL ru : registryURLs) {
urls.add(ru.createCopy());
}
}
for (URL u : urls) {
u.addParameter(URLParamType.embed.getName(), StringTools.urlEncode(serviceUrl.toFullStr()));
registereUrls.add(u.createCopy());
}
ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
exporters.add(configHandler.export(interfaceClass, ref, urls));
initLocalAppInfo(serviceUrl);
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class SimpleConfigHandler method register.
private void register(List<URL> registryUrls, URL serviceUrl) {
for (URL url : registryUrls) {
// 根据check参数的设置,register失败可能会抛异常,上层应该知晓
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getExtension(url.getProtocol());
if (registryFactory == null) {
throw new MotanFrameworkException(new MotanErrorMsg(500, MotanErrorMsgConstant.FRAMEWORK_REGISTER_ERROR_CODE, "register error! Could not find extension for registry protocol:" + url.getProtocol() + ", make sure registry module for " + url.getProtocol() + " is in classpath!"));
}
Registry registry = registryFactory.getRegistry(url);
registry.register(serviceUrl);
}
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ExtensionLoader method loadExtensionClasses.
private ConcurrentMap<String, Class<T>> loadExtensionClasses(String prefix) {
String fullName = prefix + type.getName();
List<String> classNames = new ArrayList<String>();
try {
Enumeration<URL> urls;
if (classLoader == null) {
urls = ClassLoader.getSystemResources(fullName);
} else {
urls = classLoader.getResources(fullName);
}
if (urls == null || !urls.hasMoreElements()) {
return new ConcurrentHashMap<String, Class<T>>();
}
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
parseUrl(type, url, classNames);
}
} catch (Exception e) {
throw new MotanFrameworkException("ExtensionLoader loadExtensionClasses error, prefix: " + prefix + " type: " + type.getClass(), e);
}
return loadClass(classNames);
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class ServiceMockFilter method filter.
@Override
public Response filter(Caller<?> caller, Request request) {
// Do nothing when mock is empty.
String mockServiceName = caller.getUrl().getParameter(URLParamType.mock.getName());
if (StringUtils.isEmpty(mockServiceName) || "false".equals(mockServiceName)) {
return caller.call(request);
}
MockInfo info = getServiceStat(caller.getUrl());
DefaultResponse response = new DefaultResponse();
if (mockServiceName.startsWith(RETURN_PREFIX)) {
String value = mockServiceName.substring(RETURN_PREFIX.length());
try {
info.callNum.addAndGet(1);
long sleepTime = caclSleepTime(info);
Thread.sleep(sleepTime);
response.setValue(parseMockValue(value));
} catch (RuntimeException e) {
if (e.getCause() != null) {
response.setException(new MotanBizException("mock service call process error", e.getCause()));
} else {
response.setException(new MotanBizException("mock service call process error", e));
}
} catch (Exception e) {
throw new IllegalStateException("Illegal mock json value in <motan:service ... mock=\"" + mockServiceName + "\" />");
}
} else {
try {
Class<?> mockClass = isDefault(mockServiceName) ? ReflectUtil.forName(caller.getInterface().getName() + "Mock") : ReflectUtil.forName(mockServiceName);
if (!caller.getInterface().isAssignableFrom(mockClass)) {
throw new MotanFrameworkException("The mock implemention class " + mockClass.getName() + " not implement interface " + caller.getInterface().getName());
}
try {
mockClass.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No such empty constructor \"public " + mockClass.getSimpleName() + "()\" in mock implemention class " + mockClass.getName());
}
String methodDesc = ReflectUtil.getMethodDesc(request.getMethodName(), request.getParamtersDesc());
Method[] methods = mockClass.getMethods();
boolean invoke = false;
for (Method method : methods) {
if (methodDesc.equals(ReflectUtil.getMethodDesc(method))) {
Object value = invoke(mockClass.newInstance(), method, request.getArguments(), info);
response.setValue(value);
invoke = true;
break;
}
}
if (!invoke) {
throw new MotanFrameworkException("Mock method is not found." + methodDesc);
}
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("Mock service is not found." + (isDefault(mockServiceName) ? caller.getInterface().getName() + "Mock" : mockServiceName));
} catch (Exception e) {
if (e.getCause() != null) {
response.setException(new MotanBizException("mock service call process error", e.getCause()));
} else {
response.setException(new MotanBizException("mock service call process error", e));
}
}
}
return response;
}
use of com.weibo.api.motan.exception.MotanFrameworkException in project motan by weibocom.
the class AbstractRegistryFactory method getRegistry.
@Override
public Registry getRegistry(URL url) {
String registryUri = getRegistryUri(url);
try {
lock.lock();
Registry registry = registries.get(registryUri);
if (registry != null) {
return registry;
}
registry = createRegistry(url);
if (registry == null) {
throw new MotanFrameworkException("Create registry false for url:" + url, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
registries.put(registryUri, registry);
return registry;
} catch (Exception e) {
throw new MotanFrameworkException("Create registry false for url:" + url, e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
} finally {
lock.unlock();
}
}
Aggregations