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