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 java.io.IOException; 021import java.io.Serializable; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025import javax.xml.bind.annotation.XmlAccessType; 026import javax.xml.bind.annotation.XmlAccessorType; 027import javax.xml.bind.annotation.XmlRootElement; 028import javax.xml.bind.annotation.XmlTransient; 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.NamespacePropertiesMessage.NamespaceProperties; 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 * List a HBase namespace's key/value properties. 041 * <ul> 042 * <li>NamespaceProperties: outer element</li> 043 * <li>properties: sequence property elements</li> 044 * <li>entry</li> 045 * <li>key: property key</li> 046 * <li>value: property value</li> 047 * </ul> 048 */ 049@XmlRootElement(name = "NamespaceProperties") 050@XmlAccessorType(XmlAccessType.FIELD) 051@InterfaceAudience.Private 052public class NamespacesInstanceModel implements Serializable, ProtobufMessageHandler { 053 054 private static final long serialVersionUID = 1L; 055 056 // JAX-RS automatically converts Map to XMLAnyElement. 057 private Map<String, String> properties = null; 058 059 @XmlTransient 060 private String namespaceName; 061 062 /** 063 * Default constructor. Do not use. 064 */ 065 public NamespacesInstanceModel() { 066 } 067 068 /** 069 * Constructor to use if namespace does not exist in HBASE. 070 * @param namespaceName the namespace name. 071 */ 072 public NamespacesInstanceModel(String namespaceName) throws IOException { 073 this(null, namespaceName); 074 } 075 076 /** 077 * Constructor 078 * @param admin the administrative API 079 * @param namespaceName the namespace name. 080 */ 081 public NamespacesInstanceModel(Admin admin, String namespaceName) throws IOException { 082 this.namespaceName = namespaceName; 083 if (admin == null) { 084 return; 085 } 086 087 NamespaceDescriptor nd = admin.getNamespaceDescriptor(namespaceName); 088 089 // For properly formed JSON, if no properties, field has to be null (not just no elements). 090 if (nd.getConfiguration().isEmpty()) { 091 return; 092 } 093 094 properties = new HashMap<>(); 095 properties.putAll(nd.getConfiguration()); 096 } 097 098 /** 099 * Add property to the namespace. 100 * @param key attribute name 101 * @param value attribute value 102 */ 103 public void addProperty(String key, String value) { 104 if (properties == null) { 105 properties = new HashMap<>(); 106 } 107 properties.put(key, value); 108 } 109 110 /** Returns The map of uncategorized namespace properties. */ 111 public Map<String, String> getProperties() { 112 if (properties == null) { 113 properties = new HashMap<>(); 114 } 115 return properties; 116 } 117 118 public String getNamespaceName() { 119 return namespaceName; 120 } 121 122 /* 123 * (non-Javadoc) 124 * @see java.lang.Object#toString() 125 */ 126 @Override 127 public String toString() { 128 StringBuilder sb = new StringBuilder(); 129 sb.append("{NAME => \'"); 130 sb.append(namespaceName); 131 sb.append("\'"); 132 if (properties != null) { 133 for (Map.Entry<String, String> entry : properties.entrySet()) { 134 sb.append(", "); 135 sb.append(entry.getKey()); 136 sb.append(" => '"); 137 sb.append(entry.getValue()); 138 sb.append("\'"); 139 } 140 } 141 sb.append("}"); 142 return sb.toString(); 143 } 144 145 @Override 146 public Message messageFromObject() { 147 NamespaceProperties.Builder builder = NamespaceProperties.newBuilder(); 148 if (properties != null) { 149 for (Map.Entry<String, String> entry : properties.entrySet()) { 150 String key = entry.getKey(); 151 NamespaceProperties.Property.Builder property = NamespaceProperties.Property.newBuilder(); 152 property.setKey(key); 153 property.setValue(entry.getValue()); 154 builder.addProps(property); 155 } 156 } 157 return builder.build(); 158 } 159 160 @Override 161 public ProtobufMessageHandler getObjectFromMessage(CodedInputStream cis) throws IOException { 162 NamespaceProperties.Builder builder = NamespaceProperties.newBuilder(); 163 RestUtil.mergeFrom(builder, cis); 164 List<NamespaceProperties.Property> properties = builder.getPropsList(); 165 for (NamespaceProperties.Property property : properties) { 166 addProperty(property.getKey(), property.getValue()); 167 } 168 return this; 169 } 170 171}