/** * Copyright (C) 2010-2013 Alibaba Group Holding Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.rocketmq.tools.command; import java.util.*; import com.alibaba.rocketmq.client.exception.MQBrokerException; import com.alibaba.rocketmq.common.MixAll; import com.alibaba.rocketmq.common.protocol.body.ClusterInfo; import com.alibaba.rocketmq.common.protocol.route.BrokerData; import com.alibaba.rocketmq.remoting.exception.RemotingConnectException; import com.alibaba.rocketmq.remoting.exception.RemotingSendRequestException; import com.alibaba.rocketmq.remoting.exception.RemotingTimeoutException; import com.alibaba.rocketmq.tools.admin.MQAdminExt; /** * 各个子命令的接口 * * @author shijia.wxr<vintage.wang@gmail.com> * @since 2013-7-25 */ public class CommandUtil { public static Set<String> fetchMasterAddrByClusterName(final MQAdminExt adminExt, final String clusterName) throws InterruptedException, RemotingConnectException, RemotingTimeoutException, RemotingSendRequestException, MQBrokerException { Set<String> masterSet = new HashSet<String>(); ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(); Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName); if (brokerNameSet != null) { for (String brokerName : brokerNameSet) { BrokerData brokerData = clusterInfoSerializeWrapper.getBrokerAddrTable().get(brokerName); if (brokerData != null) { String addr = brokerData.getBrokerAddrs().get(MixAll.MASTER_ID); if (addr != null) { masterSet.add(addr); } } } } else { System.out .printf("[error] Make sure the specified clusterName exists or the nameserver which connected is correct."); } return masterSet; } public static Set<String> fetchBrokerNameByClusterName(final MQAdminExt adminExt, final String clusterName) throws Exception { ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(); Set<String> brokerNameSet = clusterInfoSerializeWrapper.getClusterAddrTable().get(clusterName); if (brokerNameSet.isEmpty()) { throw new Exception( "Make sure the specified clusterName exists or the nameserver which connected is correct."); } return brokerNameSet; } public static String fetchBrokerNameByAddr(final MQAdminExt adminExt, final String addr) throws Exception { ClusterInfo clusterInfoSerializeWrapper = adminExt.examineBrokerClusterInfo(); HashMap<String/* brokerName */, BrokerData> brokerAddrTable = clusterInfoSerializeWrapper.getBrokerAddrTable(); Iterator<Map.Entry<String, BrokerData>> it = brokerAddrTable.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, BrokerData> entry = it.next(); HashMap<Long, String> brokerAddrs = entry.getValue().getBrokerAddrs(); if (brokerAddrs.containsValue(addr)) return entry.getKey(); } throw new Exception( "Make sure the specified broker addr exists or the nameserver which connected is correct."); } }