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.hadoop.hbase.rest.protobuf.generated.NamespacesMessage.Namespaces;
033import org.apache.yetus.audience.InterfaceAudience;
034
035/**
036 * A list of HBase namespaces.
037 * <ul>
038 * <li>Namespace: namespace name</li>
039 * </ul>
040 */
041@XmlRootElement(name = "Namespaces")
042@XmlAccessorType(XmlAccessType.FIELD)
043@InterfaceAudience.Private
044public class NamespacesModel implements Serializable, ProtobufMessageHandler {
045
046  private static final long serialVersionUID = 1L;
047
048  @JsonProperty("Namespace")
049  @XmlElement(name = "Namespace")
050  private List<String> namespaces = new ArrayList<>();
051
052  /**
053   * Default constructor. Do not use.
054   */
055  public NamespacesModel() {
056  }
057
058  /**
059   * Constructor
060   * @param admin the administrative API
061   */
062  public NamespacesModel(Admin admin) throws IOException {
063    NamespaceDescriptor[] nds = admin.listNamespaceDescriptors();
064    namespaces = new ArrayList<>(nds.length);
065    for (NamespaceDescriptor nd : nds) {
066      namespaces.add(nd.getName());
067    }
068  }
069
070  /** Returns all namespaces */
071  public List<String> getNamespaces() {
072    return namespaces;
073  }
074
075  /**
076   * @param namespaces the namespace name array
077   */
078  public void setNamespaces(List<String> namespaces) {
079    this.namespaces = namespaces;
080  }
081
082  /*
083   * (non-Javadoc)
084   * @see java.lang.Object#toString()
085   */
086  @Override
087  public String toString() {
088    StringBuilder sb = new StringBuilder();
089    for (String namespace : namespaces) {
090      sb.append(namespace);
091      sb.append("\n");
092    }
093    return sb.toString();
094  }
095
096  @Override
097  public byte[] createProtobufOutput() {
098    Namespaces.Builder builder = Namespaces.newBuilder();
099    builder.addAllNamespace(namespaces);
100    return builder.build().toByteArray();
101  }
102
103  @Override
104  public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
105    Namespaces.Builder builder = Namespaces.newBuilder();
106    builder.mergeFrom(message);
107    namespaces = builder.getNamespaceList();
108    return this;
109  }
110}