001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.model;
019
020import com.fasterxml.jackson.annotation.JsonProperty;
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.List;
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029import org.apache.hadoop.hbase.NamespaceDescriptor;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
032import org.apache.yetus.audience.InterfaceAudience;
033
034import org.apache.hadoop.hbase.shaded.rest.protobuf.generated.NamespacesMessage.Namespaces;
035
036/**
037 * A list of HBase namespaces.
038 * <ul>
039 * <li>Namespace: namespace name</li>
040 * </ul>
041 */
042@XmlRootElement(name = "Namespaces")
043@XmlAccessorType(XmlAccessType.FIELD)
044@InterfaceAudience.Private
045public class NamespacesModel implements Serializable, ProtobufMessageHandler {
046
047  private static final long serialVersionUID = 1L;
048
049  @JsonProperty("Namespace")
050  @XmlElement(name = "Namespace")
051  private List<String> namespaces = new ArrayList<>();
052
053  /**
054   * Default constructor. Do not use.
055   */
056  public NamespacesModel() {
057  }
058
059  /**
060   * Constructor
061   * @param admin the administrative API n
062   */
063  public NamespacesModel(Admin admin) throws IOException {
064    NamespaceDescriptor[] nds = admin.listNamespaceDescriptors();
065    namespaces = new ArrayList<>(nds.length);
066    for (NamespaceDescriptor nd : nds) {
067      namespaces.add(nd.getName());
068    }
069  }
070
071  /** Returns all namespaces */
072  public List<String> getNamespaces() {
073    return namespaces;
074  }
075
076  /**
077   * @param namespaces the namespace name array
078   */
079  public void setNamespaces(List<String> namespaces) {
080    this.namespaces = namespaces;
081  }
082
083  /*
084   * (non-Javadoc)
085   * @see java.lang.Object#toString()
086   */
087  @Override
088  public String toString() {
089    StringBuilder sb = new StringBuilder();
090    for (String namespace : namespaces) {
091      sb.append(namespace);
092      sb.append("\n");
093    }
094    return sb.toString();
095  }
096
097  @Override
098  public byte[] createProtobufOutput() {
099    Namespaces.Builder builder = Namespaces.newBuilder();
100    builder.addAllNamespace(namespaces);
101    return builder.build().toByteArray();
102  }
103
104  @Override
105  public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
106    Namespaces.Builder builder = Namespaces.newBuilder();
107    builder.mergeFrom(message);
108    namespaces = builder.getNamespaceList();
109    return this;
110  }
111}