1 /* 2 * Copyright 2010 The Apache Software Foundation 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 package org.apache.hadoop.hbase.rest.client; 22 23 import java.util.ArrayList; 24 import java.util.Collections; 25 import java.util.List; 26 27 /** 28 * A list of 'host:port' addresses of HTTP servers operating as a single 29 * entity, for example multiple redundant web service gateways. 30 */ 31 public class Cluster { 32 protected List<String> nodes = 33 Collections.synchronizedList(new ArrayList<String>()); 34 protected String lastHost; 35 36 /** 37 * Constructor 38 */ 39 public Cluster() {} 40 41 /** 42 * Constructor 43 * @param nodes a list of service locations, in 'host:port' format 44 */ 45 public Cluster(List<String> nodes) { 46 nodes.addAll(nodes); 47 } 48 49 /** 50 * @return true if no locations have been added, false otherwise 51 */ 52 public boolean isEmpty() { 53 return nodes.isEmpty(); 54 } 55 56 /** 57 * Add a node to the cluster 58 * @param node the service location in 'host:port' format 59 */ 60 public Cluster add(String node) { 61 nodes.add(node); 62 return this; 63 } 64 65 /** 66 * Add a node to the cluster 67 * @param name host name 68 * @param port service port 69 */ 70 public Cluster add(String name, int port) { 71 StringBuilder sb = new StringBuilder(); 72 sb.append(name); 73 sb.append(':'); 74 sb.append(port); 75 return add(sb.toString()); 76 } 77 78 /** 79 * Remove a node from the cluster 80 * @param node the service location in 'host:port' format 81 */ 82 public Cluster remove(String node) { 83 nodes.remove(node); 84 return this; 85 } 86 87 /** 88 * Remove a node from the cluster 89 * @param name host name 90 * @param port service port 91 */ 92 public Cluster remove(String name, int port) { 93 StringBuilder sb = new StringBuilder(); 94 sb.append(name); 95 sb.append(':'); 96 sb.append(port); 97 return remove(sb.toString()); 98 } 99 }