Search in sources :

Example 31 with MotanFrameworkException

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);
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) URL(com.weibo.api.motan.rpc.URL) ConfigHandler(com.weibo.api.motan.config.handler.ConfigHandler)

Example 32 with MotanFrameworkException

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);
    }
}
Also used : RegistryFactory(com.weibo.api.motan.registry.RegistryFactory) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanErrorMsg(com.weibo.api.motan.exception.MotanErrorMsg) Registry(com.weibo.api.motan.registry.Registry)

Example 33 with MotanFrameworkException

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);
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(java.net.URL) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 34 with MotanFrameworkException

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;
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Method(java.lang.reflect.Method) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 35 with MotanFrameworkException

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();
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Registry(com.weibo.api.motan.registry.Registry) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)41 URL (com.weibo.api.motan.rpc.URL)7 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 ArrayList (java.util.ArrayList)6 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)4 Registry (com.weibo.api.motan.registry.Registry)3 Response (com.weibo.api.motan.rpc.Response)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 ObjectInput (java.io.ObjectInput)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Serialization (com.weibo.api.motan.codec.Serialization)2 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)2 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 TransportException (com.weibo.api.motan.transport.TransportException)2 Map (java.util.Map)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2