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.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import javax.xml.bind.annotation.XmlAccessType;
28  import javax.xml.bind.annotation.XmlAccessorType;
29  import javax.xml.bind.annotation.XmlElement;
30  import javax.xml.bind.annotation.XmlRootElement;
31  
32  import org.apache.hadoop.hbase.NamespaceDescriptor;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.client.Admin;
35  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
36  import org.apache.hadoop.hbase.rest.protobuf.generated.NamespacesMessage.Namespaces;
37  import org.codehaus.jackson.annotate.JsonProperty;
38  
39  
40  /**
41   * A list of HBase namespaces.
42   * <ul>
43   * <li>Namespace: namespace name</li>
44   * </ul>
45   */
46  @XmlRootElement(name="Namespaces")
47  @XmlAccessorType(XmlAccessType.FIELD)
48  @InterfaceAudience.Private
49  public class NamespacesModel implements Serializable, ProtobufMessageHandler {
50  
51    private static final long serialVersionUID = 1L;
52  
53    @JsonProperty("Namespace")
54    @XmlElement(name="Namespace")
55    private List<String> namespaces = new ArrayList<String>();
56  
57    /**
58     * Default constructor. Do not use.
59     */
60    public NamespacesModel() {}
61  
62    /**
63     * Constructor
64     * @param admin the administrative API
65     * @throws IOException
66     */
67    public NamespacesModel(Admin admin) throws IOException {
68      NamespaceDescriptor[] nds = admin.listNamespaceDescriptors();
69      namespaces = new ArrayList<String>();
70      for (NamespaceDescriptor nd : nds) {
71        namespaces.add(nd.getName());
72      }
73    }
74  
75    /**
76     * @return all namespaces
77     */
78    public List<String> getNamespaces() {
79      return namespaces;
80    }
81  
82    /**
83     * @param namespaces the namespace name array
84     */
85    public void setNamespaces(List<String> namespaces) {
86      this.namespaces = namespaces;
87    }
88  
89    /* (non-Javadoc)
90     * @see java.lang.Object#toString()
91     */
92    @Override
93    public String toString() {
94      StringBuilder sb = new StringBuilder();
95      for (String namespace : namespaces) {
96        sb.append(namespace);
97        sb.append("\n");
98      }
99      return sb.toString();
100   }
101 
102   @Override
103   public byte[] createProtobufOutput() {
104     Namespaces.Builder builder = Namespaces.newBuilder();
105     builder.addAllNamespace(namespaces);
106     return builder.build().toByteArray();
107   }
108 
109   @Override
110   public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
111     Namespaces.Builder builder = Namespaces.newBuilder();
112     builder.mergeFrom(message);
113     namespaces = builder.getNamespaceList();
114     return this;
115   }
116 }