View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.client;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  
29  /**
30   * A list of 'host:port' addresses of HTTP servers operating as a single
31   * entity, for example multiple redundant web service gateways.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Stable
35  public class Cluster {
36    protected List<String> nodes = 
37      Collections.synchronizedList(new ArrayList<String>());
38    protected String lastHost;
39  
40    /**
41     * Constructor
42     */
43    public Cluster() {}
44  
45    /**
46     * Constructor
47     * @param nodes a list of service locations, in 'host:port' format
48     */
49    public Cluster(List<String> nodes) {
50      nodes.addAll(nodes);
51    }
52  
53    /**
54     * @return true if no locations have been added, false otherwise
55     */
56    public boolean isEmpty() {
57      return nodes.isEmpty();
58    }
59  
60    /**
61     * Add a node to the cluster
62     * @param node the service location in 'host:port' format
63     */
64    public Cluster add(String node) {
65      nodes.add(node);
66      return this;
67    }
68  
69    /**
70     * Add a node to the cluster
71     * @param name host name
72     * @param port service port
73     */
74    public Cluster add(String name, int port) {
75      StringBuilder sb = new StringBuilder();
76      sb.append(name);
77      sb.append(':');
78      sb.append(port);
79      return add(sb.toString());
80    }
81  
82    /**
83     * Remove a node from the cluster
84     * @param node the service location in 'host:port' format
85     */
86    public Cluster remove(String node) {
87      nodes.remove(node);
88      return this;
89    }
90  
91    /**
92     * Remove a node from the cluster
93     * @param name host name
94     * @param port service port
95     */
96    public Cluster remove(String name, int port) {
97      StringBuilder sb = new StringBuilder();
98      sb.append(name);
99      sb.append(':');
100     sb.append(port);
101     return remove(sb.toString());
102   }
103 
104   @Override public String toString() {
105     return "Cluster{" +
106         "nodes=" + nodes +
107         ", lastHost='" + lastHost + '\'' +
108         '}';
109   }
110 }