Search in sources :

Example 1 with Client

use of j.service.Client in project JFramework by gugumall.

the class JRouterImpl method unregister.

/*
	 *  (non-Javadoc)
	 * @see j.service.router.RouterInterface#unregister(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
public String unregister(String clientUuid, String clusterCode, String uuid, String md54Routing) throws RemoteException {
    if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
    // is public, nothing to do
    } else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
        Client client = routerConfig.getClient(clientUuid);
        if (client == null) {
            log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_DEBUG);
            return Constants.AUTH_FAILED;
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += clusterCode;
        md5 += uuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            return Constants.AUTH_FAILED;
        }
    } else {
        // 未实现的隐私策略
        return Constants.AUTH_FAILED;
    }
    ConcurrentList clusterNodes = (ConcurrentList) serviceNodesOfClusters.get(clusterCode);
    ConcurrentMap servantsOfCluster = (ConcurrentMap) servantsOfClusters.get(clusterCode);
    ConcurrentMap httpsOfCluster = (ConcurrentMap) httpsOfClusters.get(clusterCode);
    if (clusterNodes != null) {
        for (int i = 0; i < clusterNodes.size(); i++) {
            Service node = (Service) clusterNodes.get(i);
            if (node.uuid.equals(uuid)) {
                if (nodeInfoList.contains(node.toString()))
                    nodeInfoList.remove(node.toString());
                clusterNodes.remove(i);
                if (servantsOfCluster != null)
                    servantsOfCluster.remove(node.uuid);
                if (httpsOfCluster != null)
                    httpsOfCluster.remove(node.uuid);
                update = SysUtil.getNow();
                break;
            }
        }
    }
    if (clusterNodes == null || clusterNodes.isEmpty()) {
        serviceNodesOfClusters.remove(clusterCode);
        servantsOfClusters.remove(clusterCode);
        httpsOfClusters.remove(clusterCode);
        clusterCodes.remove(clusterCode);
    }
    return Constants.INVOKING_DONE;
}
Also used : ConcurrentList(j.util.ConcurrentList) ConcurrentMap(j.util.ConcurrentMap) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient)

Example 2 with Client

use of j.service.Client in project JFramework by gugumall.

the class JRouterImpl method register.

/*
	 *  (non-Javadoc)
	 * @see j.service.router.RouterInterface#register(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
public String register(String clientUuid, String clusterCode, String uuid, String rmi, String http, String interfaceClassName, String md54Routing) throws RemoteException {
    if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
    // is public, nothing to do
    } else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
        Client client = routerConfig.getClient(clientUuid);
        if (client == null) {
            log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_DEBUG);
            return Constants.AUTH_FAILED;
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += clusterCode;
        md5 += uuid;
        md5 += rmi;
        md5 += http;
        md5 += interfaceClassName;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            return Constants.AUTH_FAILED;
        }
    } else {
        // 未实现的隐私策略
        return Constants.AUTH_FAILED;
    }
    ConcurrentList clusterNodes = (ConcurrentList) serviceNodesOfClusters.get(clusterCode);
    if (clusterNodes == null) {
        clusterNodes = new ConcurrentList();
    }
    Service node = new Service(uuid, rmi, http, interfaceClassName, clusterCode);
    if (!nodeInfoList.contains(node.toString())) {
        log.log("register new service - " + node.toString(), -1);
        nodeInfoList.add(node.toString());
        clusterNodes.add(node);
        update = SysUtil.getNow();
    }
    serviceNodesOfClusters.put(clusterCode, clusterNodes);
    if (!clusterCodes.contains(clusterCode))
        clusterCodes.add(clusterCode);
    return Constants.INVOKING_DONE;
}
Also used : ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient)

Example 3 with Client

use of j.service.Client in project JFramework by gugumall.

the class RouterManager method load.

/**
 */
public static void load() {
    try {
        loading = true;
        List currentRouters = new LinkedList();
        // 文件是否存在
        File file = new File(Properties.getConfigPath() + "service.router.xml");
        if (!file.exists()) {
            throw new Exception("找不到配置文件:" + file.getAbsolutePath());
        }
        Document document = JUtilDom4j.parse(Properties.getConfigPath() + "service.router.xml", "UTF-8");
        Element root = document.getRootElement();
        List routerEles = root.elements("router");
        for (int i = 0; i < routerEles.size(); i++) {
            Element routerEle = (Element) routerEles.get(i);
            RouterConfig config = new RouterConfig();
            config.setServerUuid(routerEle.attributeValue("server-uuid"));
            config.setUuid(routerEle.elementText("uuid"));
            config.setName(routerEle.elementText("name"));
            config.setPrivacy(routerEle.elementText("privacy"));
            List routerProps = routerEle.elements("property");
            for (int j = 0; j < routerProps.size(); j++) {
                Element pEle = (Element) routerProps.get(j);
                config.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
            }
            Element rmiEle = routerEle.element("rmi");
            if (rmiEle != null && "true".equalsIgnoreCase(rmiEle.attributeValue("available"))) {
                Rmi rmi = new Rmi(Properties.getProperties("rmi"));
                List props = rmiEle.elements("property");
                for (int j = 0; j < props.size(); j++) {
                    Element pEle = (Element) props.get(j);
                    rmi.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                }
                config.setRmi(rmi);
            }
            Element httpEle = routerEle.element("http");
            if (httpEle != null && "true".equalsIgnoreCase(httpEle.attributeValue("available"))) {
                Http http = new Http();
                List props = httpEle.elements("property");
                for (int j = 0; j < props.size(); j++) {
                    Element pEle = (Element) props.get(j);
                    http.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                }
                config.setHttp(http);
            }
            List clients = root.elements("client");
            for (int j = 0; j < clients.size(); j++) {
                Element clientEle = (Element) clients.get(j);
                Client client = new Client();
                client.setUuid(clientEle.attributeValue("uuid"));
                client.setName(clientEle.attributeValue("name"));
                client.setKey(clientEle.attributeValue("key"));
                config.addClient(client);
            }
            currentRouters.add(config);
        }
        root = null;
        document = null;
        // 停止已启动服务
        List olds = routerConfigs.listValues();
        for (int i = 0; i < olds.size(); i++) {
            RouterConfig config = (RouterConfig) olds.get(i);
            RouterContainer containter = (RouterContainer) routerContainers.get(config.getUuid());
            if (containter != null && containter.getStarted()) {
                containter.shutdown();
            }
            RouterAgent agent = (RouterAgent) agents.get(config.getUuid());
            agent.shutdown();
        }
        JUtilList.clear_AllNull(olds);
        routerConfigs.clear();
        routerContainers.clear();
        agents.clear();
        uuidOfRouters.clear();
        uuidOfRoutersAvailableHttp.clear();
        // 处理最新服务
        for (int i = 0; i < currentRouters.size(); i++) {
            RouterConfig routerConfig = (RouterConfig) currentRouters.get(i);
            routerConfigs.put(routerConfig.getUuid(), routerConfig);
            uuidOfRouters.add(routerConfig.getUuid());
            if (routerConfig.getServerUuid().equals(Manager.getRouterNodeUuid())) {
                // 如果是路由节点,启动路由服务
                // 托管至nvwa
                NvwaObject nvwaObject = Nvwa.entrust(routerConfig.getRelatedHttpHandlerPath(), routerConfig.getClassName(), true);
                nvwaObject.setFiled("routerConfig", "ref", true);
                JRouter router = (JRouter) Nvwa.entrustCreate(routerConfig.getRelatedHttpHandlerPath(), routerConfig.getClassName(), true);
                router.setRouterConfig(routerConfig);
                RouterContainer container = new RouterContainer(routerConfig, router);
                routerContainers.put(routerConfig.getUuid(), container);
                container.startup();
            }
            // 启动路由代理
            RouterAgent agent = new RouterAgent(routerConfig);
            agents.put(routerConfig.getUuid(), agent);
            agent.startup();
        }
        currentRouters.clear();
        currentRouters = null;
        // 处理最新服务 END
        // 配置文件最近修改时间
        File configFile = new File(Properties.getConfigPath() + "service.router.xml");
        configLastModified = configFile.lastModified();
        configFile = null;
        loading = false;
    } catch (Exception e) {
        loading = false;
        log.log(e, Logger.LEVEL_FATAL);
    }
}
Also used : Element(org.dom4j.Element) NvwaObject(j.nvwa.NvwaObject) JHttp(j.http.JHttp) Http(j.service.Http) Document(org.dom4j.Document) LinkedList(java.util.LinkedList) RemoteException(java.rmi.RemoteException) Rmi(j.service.Rmi) List(java.util.List) ConcurrentList(j.util.ConcurrentList) JUtilList(j.util.JUtilList) LinkedList(java.util.LinkedList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) File(java.io.File)

Example 4 with Client

use of j.service.Client in project JFramework by gugumall.

the class JRouterImpl method service.

/*
	 *  (non-Javadoc)
	 * @see j.service.router.RouterInterface#service(j.app.webserver.JSession, javax.servlet.http.HttpSession, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
public void service(JSession jsession, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws RemoteException {
    waitWhileMonitoring();
    String clientUuid = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_CLIENT_UUID);
    String clusterCode = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_SERVICE_CODE);
    String md54Routing = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER);
    if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
    // is public, nothing to do
    } else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
        Client client = routerConfig.getClient(clientUuid);
        if (client == null) {
            log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_ERROR);
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
    } else {
        // 未实现的隐私策略
        jsession.resultString = Constants.AUTH_FAILED;
        return;
    }
    String codeOfUuid = null;
    for (int i = 0; i < nodeInfoList.size(); i++) {
        // 按uuid获得
        String[] node = ((String) nodeInfoList.get(i)).split(",");
        if (node[0].equals(clusterCode)) {
            codeOfUuid = node[4];
            break;
        }
    }
    ConcurrentMap httpsOfCluster = (ConcurrentMap) httpsOfClusters.get(codeOfUuid == null ? clusterCode : codeOfUuid);
    if (httpsOfCluster == null) {
        log.log("the service(http channel) " + clusterCode + " is not found.", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_FOUND;
        return;
    }
    List nodes = httpsOfCluster.listValues();
    if (nodes.size() == 0) {
        log.log("no valid node(http channel) for the service " + clusterCode + ".", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_AVAIL;
        return;
    }
    if (httpsOfCluster.containsKey(clusterCode)) {
        // 按uuid获得
        nodes.clear();
        nodes = null;
        jsession.resultString = ((Service) httpsOfCluster.get(clusterCode)).http;
        return;
    } else {
        String entrance = ((Service) nodes.get(JUtilRandom.nextInt(nodes.size()))).http;
        nodes.clear();
        nodes = null;
        jsession.resultString = entrance;
    }
}
Also used : ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient)

Example 5 with Client

use of j.service.Client in project JFramework by gugumall.

the class JRouterImpl method getAllServiceNodeAvailable.

/*
	 * (non-Javadoc)
	 * @see j.service.router.JRouter#getAllServiceNode(j.app.webserver.JSession, javax.servlet.http.HttpSession, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
public void getAllServiceNodeAvailable(JSession jsession, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws RemoteException {
    waitWhileMonitoring();
    String clientUuid = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_CLIENT_UUID);
    String clusterCode = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_SERVICE_CODE);
    String md54Routing = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER);
    if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
    // is public, nothing to do
    } else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
        Client client = routerConfig.getClient(clientUuid);
        if (client == null) {
            log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_ERROR);
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
    } else {
        // 未实现的隐私策略
        jsession.resultString = Constants.AUTH_FAILED;
        return;
    }
    ConcurrentMap httpsOfCluster = (ConcurrentMap) httpsOfClusters.get(clusterCode);
    if (httpsOfCluster == null) {
        log.log("the service(http channel) " + clusterCode + " is not found.", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_FOUND;
        return;
    }
    List nodes = httpsOfCluster.listValues();
    if (nodes.size() == 0) {
        log.log("no valid node(http channel) for the service " + clusterCode + ".", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_AVAIL;
        return;
    }
    String[] entrances = new String[nodes.size()];
    for (int i = 0; i < nodes.size(); i++) {
        Service service = (Service) nodes.get(i);
        entrances[i] = service.http;
    }
    nodes.clear();
    nodes = null;
    try {
        jsession.resultString = JObject.serializable2String(entrances, false);
    } catch (Exception e) {
        log.log(e, Logger.LEVEL_ERROR);
        jsession.resultString = "ERR";
    }
}
Also used : ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) RemoteException(java.rmi.RemoteException)

Aggregations

Client (j.service.Client)8 ConcurrentList (j.util.ConcurrentList)8 HttpClient (org.apache.http.client.HttpClient)7 List (java.util.List)6 ConcurrentMap (j.util.ConcurrentMap)5 RemoteException (java.rmi.RemoteException)4 Http (j.service.Http)2 Rmi (j.service.Rmi)2 ServiceBase (j.service.server.ServiceBase)2 JUtilList (j.util.JUtilList)2 File (java.io.File)2 LinkedList (java.util.LinkedList)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 JHttp (j.http.JHttp)1 NvwaObject (j.nvwa.NvwaObject)1