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