1 /* 2 * Util.java 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 22 package org.apache.hadoop.metrics.spi; 23 24 import java.net.InetSocketAddress; 25 import java.net.SocketAddress; 26 import java.util.ArrayList; 27 import java.util.List; 28 29 import org.apache.hadoop.classification.InterfaceAudience; 30 import org.apache.hadoop.classification.InterfaceStability; 31 32 /** 33 * Static utility methods 34 */ 35 @InterfaceAudience.Public 36 @InterfaceStability.Evolving 37 public class Util { 38 39 /** 40 * This class is not intended to be instantiated 41 */ Util()42 private Util() {} 43 44 /** 45 * Parses a space and/or comma separated sequence of server specifications 46 * of the form <i>hostname</i> or <i>hostname:port</i>. If 47 * the specs string is null, defaults to localhost:defaultPort. 48 * 49 * @return a list of InetSocketAddress objects. 50 */ parse(String specs, int defaultPort)51 public static List<InetSocketAddress> parse(String specs, int defaultPort) { 52 List<InetSocketAddress> result = new ArrayList<InetSocketAddress>(1); 53 if (specs == null) { 54 result.add(new InetSocketAddress("localhost", defaultPort)); 55 } 56 else { 57 String[] specStrings = specs.split("[ ,]+"); 58 for (String specString : specStrings) { 59 int colon = specString.indexOf(':'); 60 if (colon < 0 || colon == specString.length() - 1) { 61 result.add(new InetSocketAddress(specString, defaultPort)); 62 } else { 63 String hostname = specString.substring(0, colon); 64 int port = Integer.parseInt(specString.substring(colon+1)); 65 result.add(new InetSocketAddress(hostname, port)); 66 } 67 } 68 } 69 return result; 70 } 71 72 } 73