| 1 | package net.spy.memcached; |
| 2 | |
| 3 | import java.net.InetSocketAddress; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 8 | * Convenience utilities for simplifying common address parsing. |
| 9 | */ |
| 10 | public class AddrUtil { |
| 11 | |
| 12 | /** |
| 13 | * Split a string in the form of "host:port host2:port" into a List of |
| 14 | * InetSocketAddress instances suitable for instantiating a MemcachedClient. |
| 15 | * |
| 16 | * Note that colon-delimited IPv6 is also supported. |
| 17 | * For example: ::1:11211 |
| 18 | */ |
| 19 | public static List<InetSocketAddress> getAddresses(String s) { |
| 20 | if(s == null) { |
| 21 | throw new NullPointerException("Null host list"); |
| 22 | } |
| 23 | if(s.trim().equals("")) { |
| 24 | throw new IllegalArgumentException("No hosts in list: ``" |
| 25 | + s + "''"); |
| 26 | } |
| 27 | ArrayList<InetSocketAddress> addrs= |
| 28 | new ArrayList<InetSocketAddress>(); |
| 29 | |
| 30 | for(String hoststuff : s.split(" ")) { |
| 31 | int finalColon=hoststuff.lastIndexOf(':'); |
| 32 | if(finalColon < 1) { |
| 33 | throw new IllegalArgumentException("Invalid server ``" |
| 34 | + hoststuff + "'' in list: " + s); |
| 35 | |
| 36 | } |
| 37 | String hostPart=hoststuff.substring(0, finalColon); |
| 38 | String portNum=hoststuff.substring(finalColon+1); |
| 39 | |
| 40 | addrs.add(new InetSocketAddress(hostPart, |
| 41 | Integer.parseInt(portNum))); |
| 42 | } |
| 43 | assert !addrs.isEmpty() : "No addrs found"; |
| 44 | return addrs; |
| 45 | } |
| 46 | } |