1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45
46
47
48
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
58 private Map<String,String> properties = null;
59
60 @XmlTransient
61 private String namespaceName;
62
63
64
65
66 public NamespacesInstanceModel() {}
67
68
69
70
71
72
73 public NamespacesInstanceModel(String namespaceName) throws IOException {
74 this(null, namespaceName);
75 }
76
77
78
79
80
81
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
90 if(nd.getConfiguration().size() == 0){ return; }
91
92 properties = new HashMap<String,String>();
93 properties.putAll(nd.getConfiguration());
94 }
95
96
97
98
99
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
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
123
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 }