/**
  * 開啟world服務
  *
  * @param batchOpearVo 操作vo
  * @param worldServer world服務
  * @param session HttpSession
  */
 public boolean startWorld(
     BatchOpearVo batchOpearVo,
     WorldServer worldServer,
     HttpSession session,
     ServerService serverService)
     throws Exception {
   boolean startResult = false;
   batchOpearVo.setRemark(worldServer.getName() + "正在开启");
   WorldServerService worldServerService = Application.getBean(WorldServerService.class);
   Server server = serverService.get(worldServer.getServerId());
   // 判断world是否已开启
   boolean result =
       SshxcuteUtils.progressIsExistNew(server, worldServer.getPath(), Global.WORLD_PID_FILE);
   if (result) {
     batchOpearVo.isFail(worldServer.getName() + "服务已启动,请关闭重启");
     return startResult;
   }
   Object[] runResult = SshxcuteUtils.runExec(server, worldServer.getPath());
   // 启动服务出错,关闭服务
   if (runResult != null
       && runResult.length > 0
       && (Integer) runResult[0] != CommanConstant.RESULT_TRUE_STATE) {
     batchOpearVo.isFail(worldServer.getName() + "world启动出错");
     SshxcuteUtils.killExec(server, worldServer.getPath());
     return startResult;
   }
   int i = 0;
   while (true) {
     if (i == 0) {
       Thread.sleep(4000L);
     } else {
       Thread.sleep(500L);
     }
     if (i >= 120) {
       batchOpearVo.isFail(worldServer.getName() + "world启动超时");
       break;
     }
     Boolean bResult =
         SshxcuteUtils.getBatchStdout(
             session, worldServer, worldServerService, Global.WORLD_SUCCESS_RESULT);
     if (bResult != null) {
       if (bResult) {
         batchOpearVo.isSUCCESS(worldServer.getName() + "world启动成功");
         startResult = true;
       } else {
         batchOpearVo.isFail(worldServer.getName() + "world启动失败");
         SshxcuteUtils.killExec(server, worldServer.getPath());
       }
       break;
     }
     System.out.println("============check start world " + worldServer.getName() + " => " + i);
     i++;
   }
   return startResult;
 }
 /** 批量关闭World */
 public void batchCloseWorldNew(
     String worldIds, HttpSession session, Map<Integer, List<Integer>> openDispatchIdMap)
     throws Exception {
   System.out.println("worldIds=" + worldIds);
   if (!StringUtils.isEmpty(worldIds)) {
     WorldServerService worldServerService = Application.getBean(WorldServerService.class);
     String[] worldIdArray = worldIds.split(",");
     for (int i = 0; i < worldIdArray.length; i++) {
       System.out.println("worldIdArray[i]=" + worldIdArray[i]);
       Application.getManager()
           .getSimpleThreadPool()
           .execute(
               this.createCloseTaskNew(
                   Integer.valueOf(worldIdArray[i]),
                   session,
                   worldServerService,
                   openDispatchIdMap));
     }
   }
 }
 /**
  * 關閉world
  *
  * @param batchOpearVo 操作vo
  * @param dispatchServerIds 已開啟的dispatch服務Id
  * @param worldId worldID
  */
 public boolean closeWorld(
     BatchOpearVo batchOpearVo,
     WorldServer worldServer,
     List<Integer> dispatchServerIds,
     ServerService serverService)
     throws Exception {
   boolean closeResult = false;
   batchOpearVo.setRemark(worldServer.getName() + "正在关闭");
   WorldServerService worldServerService = Application.getBean(WorldServerService.class);
   Server server = serverService.get(worldServer.getServerId());
   // worldServer已关闭的情况
   if (worldServer.getState() == CommanConstant.STATE_STOP) {
     boolean result =
         SshxcuteUtils.progressIsExistNew(server, worldServer.getPath(), Global.WORLD_PID_FILE);
     if (!result) {
       return true;
     }
   }
   if (dispatchServerIds != null && dispatchServerIds.size() > 0) {
     // System.out.println("=====dispatchServerIds=====" + dispatchServerIds.toArray().toString());
     boolean dispatchCloseIsSuccess =
         worldServerService.dispatchCloseIsSuccess(
             dispatchServerIds, worldServer, server, CommanConstant.STATE_STOP);
     // 检测Dispatch服务是否关闭完成,若未完成则不关闭world服务
     if (!dispatchCloseIsSuccess) {
       batchOpearVo.isFail("分发服关闭未完成,请关闭再试");
       return closeResult;
     }
   }
   Object[] killResult = SshxcuteUtils.killExec(server, worldServer.getPath());
   if (killResult != null
       && killResult.length > 0
       && (Integer) killResult[0] != CommanConstant.RESULT_TRUE_STATE) {
     batchOpearVo.isFail(worldServer.getName() + "world关闭出错");
     return closeResult;
   }
   worldServer.setState(CommanConstant.STATE_STOP);
   worldServerService.update(worldServer);
   CacheService.getWorldServerById(worldServer.getId()).setState(CommanConstant.STATE_STOP);
   batchOpearVo.isSUCCESS(worldServer.getName() + "world关闭成功");
   closeResult = true;
   return closeResult;
 }
 public void synVersion(HttpSession session, String ids, String fileName) {
   String[] beanIds = ids.split(",");
   session.removeAttribute(Global.BATCH_UPDATE_REMOTE_CFG);
   session.removeAttribute(Global.UPDATE_CFG_BEAN_LIST);
   List<BeanInterface> beanInterfaceList = new ArrayList<BeanInterface>();
   List<BatchOpearVo> batchOpearVoList = new ArrayList<BatchOpearVo>();
   BatchOpearVo batchOpearVo = null;
   for (int i = 0; i < beanIds.length; i++) {
     batchOpearVo = new BatchOpearVo();
     batchOpearVo.setId(Integer.valueOf(beanIds[i]));
     batchOpearVo.setRemark("开始更新");
     System.out.println("更新" + beanIds[i]);
     // 更新文件操作
     Application.getManager()
         .getSimpleThreadPool()
         .execute(
             this.createSynVersionTask(
                 session, this.get(Integer.valueOf(beanIds[i])), batchOpearVo, fileName));
     beanInterfaceList.add(this.get(Integer.valueOf(beanIds[i])));
     batchOpearVoList.add(batchOpearVo);
   }
   session.setAttribute(Global.BATCH_UPDATE_REMOTE_CFG, batchOpearVoList);
   session.setAttribute(Global.UPDATE_CFG_BEAN_LIST, beanInterfaceList);
 }
 /**
  * 获取运行结果信息
  *
  * @author:LKJ
  */
 public String getStdout(Server server, WorldServer worldServer, HttpSession session)
     throws Exception {
   boolean successResult = false;
   boolean exceptionResult = false;
   String result = "";
   Object[] stdResult = SshxcuteUtils.showStdout(server, worldServer.getPath());
   String successStr = Global.WORLD_SUCCESS_RESULT;
   String exceptionStr = "Exception";
   if (stdResult != null && stdResult.length > 0 && stdResult[1] != null) {
     successResult = CommonUtil.Pattern(successStr, stdResult[1].toString());
     exceptionResult = CommonUtil.Pattern(exceptionStr, stdResult[1].toString());
   }
   if (exceptionResult) {
     result = "false";
     SystemLogService.worldServerLog(
         session, ":" + worldServer.getId() + "," + stdResult[1].toString());
   } else if (successResult) {
     result = "true";
     CacheService.getWorldServerById(worldServer.getId()).setState(CommanConstant.STATE_START);
     worldServer.setState(CommanConstant.STATE_START);
     Application.getBean(WorldServerService.class).update(worldServer);
   }
   return result;
 }
 /** 生成worldServer对象 */
 public String createWorldServer(WorldServer worldServers, String mergeIds) {
   String createResult = "";
   try {
     String cnType = Application.getConfig("system", "cnType"); // 分区前缀类型
     int serverId = worldServers.getServerId(); // 服务器Id(哪台机子)
     Server server = CacheService.getServerById(serverId);
     int machinecode = worldServers.getMachinecode(); // 分区号
     String publicip = server.getServerName().trim(); // 公网ip
     String battleip = worldServers.getBattleip(); // 对战服ip
     int battleport = worldServers.getBattleport(); // 对战服端口
     String areaId = cnType + "_" + machinecode; // 加前缀的分编号,如“CN_1”
     int serverType = worldServers.getServerType();
     String path = getWorldPath(worldServers, cnType);
     String name = getWorldName(worldServers, cnType);
     List<WorldServer> worldServerListCheck =
         worldServerDao.getWorldServerByServerTypeAndAreaId(serverType, machinecode);
     if (worldServerListCheck != null && worldServerListCheck.size() > 0) {
       createResult = "存在相同類型和分區號的記錄";
       return createResult;
     }
     WorldServer worldServer = null;
     List<WorldServer> worldServerList =
         worldServerDao.getWorldServerListByServerId(worldServers.getServerId());
     if (worldServerList != null && worldServerList.size() > 0) {
       worldServer = new WorldServer();
       WorldServer worldServerTemp = worldServerList.get(0);
       BeanUtils.copyProperties(worldServer, worldServerTemp);
       if (worldServerDao.get(
               new String[] {"port", "serverId"},
               new Object[] {worldServerTemp.getPort() + 1, serverId})
           != null) {
         createResult = "存在相同的port,生成失败";
         return createResult;
       }
       worldServer.setId(null);
       worldServer.setPort(worldServerTemp.getPort() + 1);
       worldServer.setAdminport(worldServerTemp.getAdminport() + 1);
       worldServer.setHttpport(worldServerTemp.getHttpport() + 1);
       worldServer.setAuthport(worldServerTemp.getAuthport());
       worldServer.setUpdateTime(null);
       worldServer.setState(0);
       worldServer.setIsDeploy(1);
       worldServer.setMergeIds("");
     } else {
       worldServer = this.getDefaultWorld();
     }
     worldServer.setServerId(serverId);
     worldServer.setAccountId(CacheService.getAccountServerByServerId(serverId).getId());
     worldServer.setName(name);
     worldServer.setPublicip(publicip);
     worldServer.setAreaid(areaId);
     worldServer.setMachinecode(machinecode);
     worldServer.setBattleport(battleport);
     worldServer.setBattleip(battleip);
     worldServer.setPath(path);
     worldServer.setServerType(worldServers.getServerType());
     worldServer.setOrders(this.getMaxOrderByType(serverType, worldServers.getOrders()) + 1);
     if (worldServers.getIsDeploy() != null) {
       worldServer.setIsDeploy(worldServers.getIsDeploy());
     }
     // 合服區
     if (!StringUtils.isEmpty(mergeIds)) {
       // String mergeIds = "";
       StringBuilder sb = new StringBuilder();
       String[] mergeIdArray = mergeIds.split(",");
       // if (mergeIdArray.length == 2) {
       for (String id : mergeIdArray) {
         if (!StringUtils.isNumeric(id)) {
           createResult = "合服id串格式錯誤";
           return createResult;
         }
         String mergeId = cnType + "_" + id;
         sb.append(mergeId).append(",");
       }
       mergeIds = sb.substring(0, sb.lastIndexOf(","));
       // } else if (mergeIdArray.length == 1) {
       // mergeIds = cnType + "_" + mergeIdArray[0];
       // } else {
       // createResult = "名稱格式錯誤";
       // return createResult;
       // }
       worldServer.setMergeIds(mergeIds);
     }
     this.saveOrUpdate(worldServer);
     CacheService.initWorldServer();
     createResult = "true";
   } catch (Exception e) {
     e.printStackTrace();
     createResult = "遇到未知错误";
   }
   return createResult;
 }