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