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