View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.io.IOException;
23  import java.io.Serializable;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlRootElement;
31  import javax.xml.bind.annotation.XmlTransient;
32  
33  import org.apache.hadoop.hbase.NamespaceDescriptor;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.client.Admin;
36  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
37  import org.apache.hadoop.hbase.rest.protobuf
38    .generated.NamespacePropertiesMessage.NamespaceProperties;
39  
40  /**
41   * List a HBase namespace's key/value properties.
42   * <ul>
43   * <li>NamespaceProperties: outer element</li>
44   * <li>properties: sequence property elements</li>
45   * <li>entry</li>
46   * <li>key: property key</li>
47   * <li>value: property value</li>
48   * </ul>
49   */
50  @XmlRootElement(name="NamespaceProperties")
51  @XmlAccessorType(XmlAccessType.FIELD)
52  @InterfaceAudience.Private
53  public class NamespacesInstanceModel implements Serializable, ProtobufMessageHandler {
54  
55    private static final long serialVersionUID = 1L;
56  
57    // JAX-RS automatically converts Map to XMLAnyElement.
58    private Map<String,String> properties = null;
59  
60    @XmlTransient
61    private String namespaceName;
62  
63    /**
64     * Default constructor. Do not use.
65     */
66    public NamespacesInstanceModel() {}
67  
68    /**
69     * Constructor to use if namespace does not exist in HBASE.
70     * @param namespaceName the namespace name.
71     * @throws IOException
72     */
73    public NamespacesInstanceModel(String namespaceName) throws IOException {
74      this(null, namespaceName);
75    }
76  
77    /**
78     * Constructor
79     * @param admin the administrative API
80     * @param namespaceName the namespace name.
81     * @throws IOException
82     */
83    public NamespacesInstanceModel(Admin admin, String namespaceName) throws IOException {
84      this.namespaceName = namespaceName;
85      if(admin == null) { return; }
86  
87      NamespaceDescriptor nd = admin.getNamespaceDescriptor(namespaceName);
88  
89      // For properly formed JSON, if no properties, field has to be null (not just no elements).
90      if(nd.getConfiguration().size() == 0){ return; }
91  
92      properties = new HashMap<String,String>();
93      properties.putAll(nd.getConfiguration());
94    }
95  
96    /**
97     * Add property to the namespace.
98     * @param key attribute name
99     * @param value attribute value
100    */
101   public void addProperty(String key, String value) {
102     if(properties == null){
103       properties = new HashMap<String,String>();
104     }
105     properties.put(key, value);
106   }
107 
108   /**
109    * @return The map of uncategorized namespace properties.
110    */
111   public Map<String,String> getProperties() {
112     if(properties == null){
113       properties = new HashMap<String,String>();
114     }
115     return properties;
116   }
117 
118   public String getNamespaceName(){
119     return namespaceName;
120   }
121 
122   /* (non-Javadoc)
123    * @see java.lang.Object#toString()
124    */
125   @Override
126   public String toString() {
127     StringBuilder sb = new StringBuilder();
128     sb.append("{NAME => \'");
129     sb.append(namespaceName);
130     sb.append("\'");
131     if(properties != null){
132       for (Map.Entry<String, String> entry : properties.entrySet()) {
133         sb.append(", ");
134         sb.append(entry.getKey());
135         sb.append(" => '");
136         sb.append(entry.getValue());
137         sb.append("\'");
138       }
139     }
140     sb.append("}");
141     return sb.toString();
142   }
143 
144   @Override
145   public byte[] createProtobufOutput() {
146     NamespaceProperties.Builder builder = NamespaceProperties.newBuilder();
147     if(properties != null){
148       for (Map.Entry<String, String> entry : properties.entrySet()) {
149         String key = entry.getKey();
150         NamespaceProperties.Property.Builder property = NamespaceProperties.Property.newBuilder();
151         property.setKey(key);
152         property.setValue(entry.getValue());
153         builder.addProps(property);
154       }
155     }
156     return builder.build().toByteArray();
157   }
158 
159   @Override
160   public ProtobufMessageHandler getObjectFromMessage(byte[] message) throws IOException {
161     NamespaceProperties.Builder builder = NamespaceProperties.newBuilder();
162     builder.mergeFrom(message);
163     List<NamespaceProperties.Property> properties = builder.getPropsList();
164     for(NamespaceProperties.Property property: properties){
165       addProperty(property.getKey(), property.getValue());
166     }
167     return this;
168   }
169 }