public ZooKeeperCluster getZooKeeperClusterByCulsterId(int clusterId) throws DaoException { ZooKeeperCluster zookeeperCluster = null; // 从数据库中获取指定zookeeper集群中所有机器 ResultSet rs = null; DBConnectionResource myResultSet = null; try { String querySQL = StringUtil.replaceSequenced(SQL_QUERY_CLUSTER_BY_ID, clusterId + EMPTY_STRING); myResultSet = DbcpUtil.executeQuery(querySQL); if (null == myResultSet) throw new DaoException("没有返回结果"); rs = myResultSet.resultSet; if (rs.next()) { String clusterName = rs.getString("cluster_name"); String serverListStr = rs.getString("server_list"); String description = rs.getString("description"); List<String> serverList = null; if (!StringUtil.isBlank(serverListStr)) { String[] serverListArray = serverListStr.split(COMMA); serverList = ArrayUtil.toArrayList(serverListArray); } zookeeperCluster = new ZooKeeperCluster(); zookeeperCluster.setClusterId(clusterId); zookeeperCluster.setClusterName(clusterName); zookeeperCluster.setServerList(serverList); zookeeperCluster.setDescription(description); } return zookeeperCluster; } catch (Exception e) { throw new DaoException( "Error when query zookeeper cluster by cluster_id: " + clusterId + ", Error: " + e.getMessage(), e); } finally { if (null != myResultSet) { DbcpUtil.closeResultSetAndStatement(rs, myResultSet.statement); DbcpUtil.returnBackConnectionToPool(myResultSet.connection); } } }
@Override public boolean updateZooKeeperSettingsByClusterId(ZooKeeperCluster zooKeeperCluster) throws DaoException { if (null == zooKeeperCluster) return false; // 从数据库中获取指定zookeeper集群中所有机器 try { String serverListString = EMPTY_STRING; List<String> serverList = zooKeeperCluster.getServerList(); if (null != serverList && !serverList.isEmpty()) { for (String server : serverList) { serverListString += server + COMMA; } serverListString = StringUtil.replaceLast(serverListString, COMMA, EMPTY_STRING); } String updateSql = StringUtil.replaceSequenced( SQL_UPDATE_ZOOKEEPER_CLUSTER_SETTINGS_BY_ID, zooKeeperCluster.getClusterName(), serverListString, zooKeeperCluster.getDescription(), zooKeeperCluster.getClusterId() + EMPTY_STRING); int num = DbcpUtil.executeUpdate(updateSql); if (1 == num) { return true; } return false; } catch (Exception e) { throw new DaoException( "Error when update zooKeeperCluster by cluster_id: " + zooKeeperCluster + ", Error: " + e.getMessage(), e); } }
@Override public int addZooKeeper(ZooKeeperCluster zooKeeperCluster) throws DaoException { if (null == zooKeeperCluster) return -1; try { String serverListString = CollectionUtil.toString(zooKeeperCluster.getServerList()); String insertSql = StringUtil.replaceSequenced( SQL_ADD_ZOOKEEPER_CLUSTER, zooKeeperCluster.getClusterName(), serverListString, zooKeeperCluster.getDescription()); int key = DbcpUtil.executeInsertAndReturnGeneratedKeys(insertSql); return key; } catch (Exception e) { throw new DaoException( "Error when add zooKeeperCluster" + zooKeeperCluster + ", Error: " + e.getMessage(), e); } }
@Override public List<ZooKeeperCluster> getAllDetailZooKeeperCluster() throws DaoException { List<ZooKeeperCluster> zookeeperClusterList = new ArrayList<ZooKeeperCluster>(); // 从数据库中获取指定zookeeper集群中所有机器 ResultSet rs = null; DBConnectionResource myResultSet = null; try { myResultSet = DbcpUtil.executeQuery(SQL_QUERY_ALL_DETAIL_CLUSTER); if (null == myResultSet) throw new DaoException("没有返回结果"); rs = myResultSet.resultSet; while (rs.next()) { int clusterId = rs.getInt("cluster_id"); String clusterName = rs.getString("cluster_name"); String serverListStr = rs.getString("server_list"); String description = rs.getString("description"); List<String> serverList = null; if (!StringUtil.isBlank(serverListStr)) { String[] serverListArray = serverListStr.split(COMMA); serverList = ArrayUtil.toArrayList(serverListArray); } ZooKeeperCluster zookeeperCluster = new ZooKeeperCluster(); zookeeperCluster.setClusterId(clusterId); zookeeperCluster.setClusterName(clusterName); zookeeperCluster.setServerList(serverList); zookeeperCluster.setDescription(description); zookeeperClusterList.add(zookeeperCluster); } return zookeeperClusterList; } catch (Exception e) { throw new DaoException("Error when query all zookeeper cluster, Error: " + e.getMessage(), e); } finally { if (null != myResultSet) { DbcpUtil.closeResultSetAndStatement(rs, myResultSet.statement); DbcpUtil.returnBackConnectionToPool(myResultSet.connection); } } }
/** * 将字符串解析成NodePathCheckRule * * @param str 类似于的 [/:nileader,yinshi;/nileader:test]^{/:nileader,yinshi;/ nileader:test} * @return 可能会返回null * @throws Exception */ public static NodePathCheckRule parseNodePathCheckRuleFromString(String str) throws Exception { if (Strings.isNullOrEmpty(str)) { return null; } String[] temp = str.split("\\^"); if (0 == temp.length) { // 不合法的配置 return null; } try { // 可能是一个:/:nileader,yinshi;/nileader:test String strPathOnlyCanBeExist = StringUtil.replaceAll(temp[0], "", "\\|"); // 可能是一个:/:nileader,yinshi;/nileader:test String strPathCanNotBeExist = StringUtil.replaceAll(temp[1], "", "\\|"); /** 只能够出现这些path */ Map<String, List<String>> pathOnlyCanBeExist = new HashMap<String, List<String>>(); /** 不能出现这些path */ Map<String, List<String>> pathCanNotBeExist = new HashMap<String, List<String>>(); if (!StringUtil.isBlank(strPathOnlyCanBeExist)) { String[] ruleArray = strPathOnlyCanBeExist.split(";"); for (String rule : ruleArray) { if (StringUtil.isBlank(rule)) { continue; } String[] pathArray = StringUtil.trimToEmpty(rule).split(":"); String nodeName = StringUtil.trimToEmpty(pathArray[0]); List<String> pathList = Lists.newArrayList(pathArray[1]); pathOnlyCanBeExist.put(nodeName, pathList); } } if (!StringUtil.isBlank(strPathCanNotBeExist)) { String[] ruleArray = strPathCanNotBeExist.split(";"); for (String rule : ruleArray) { if (StringUtil.isBlank(rule)) { continue; } String[] pathArray = StringUtil.trimToEmpty(rule).split(":"); String nodeName = pathArray[0]; List<String> pathList = Lists.newArrayList(pathArray[1]); pathCanNotBeExist.put(nodeName, pathList); } } NodePathCheckRule nodePathCheckRule = new NodePathCheckRule(); nodePathCheckRule.setPathCanNotBeExist(pathCanNotBeExist); nodePathCheckRule.setPathOnlyCanBeExist(pathOnlyCanBeExist); return nodePathCheckRule; } catch (Exception e) { throw new Exception( "Error when parseNodePathCheckRuleFromString, String: " + str + ", Error: " + e.getMessage(), e); } }
/** * 获取当前操作系统的语言 * * @return 操作系统语言,例如zh(中文),en(英文) */ public static String getOSLanguage() { return StringUtil.trimToEmpty(System.getProperty(USER_LANGYAGE)); }
/** * 获取当前操作系统名称 * * @return 操作系统名称,例如windows xp,linux等。 */ public static String getOSName() { return StringUtil.trimToEmpty(System.getProperty(OS_NAME)).toLowerCase(); }