View Javadoc

1   package org.apache.helix.webapp.resources;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import java.io.StringReader;
25  import java.io.StringWriter;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  import org.apache.helix.HelixDataAccessor;
32  import org.apache.helix.HelixProperty;
33  import org.apache.helix.PropertyKey;
34  import org.apache.helix.PropertyType;
35  import org.apache.helix.ZNRecord;
36  import org.apache.helix.PropertyKey.Builder;
37  import org.apache.helix.manager.zk.ZKHelixDataAccessor;
38  import org.apache.helix.manager.zk.ZNRecordSerializer;
39  import org.apache.helix.manager.zk.ZkBaseDataAccessor;
40  import org.apache.helix.manager.zk.ZkClient;
41  import org.apache.helix.model.LiveInstance.LiveInstanceProperty;
42  import org.apache.helix.util.HelixUtil;
43  import org.codehaus.jackson.JsonGenerationException;
44  import org.codehaus.jackson.JsonParseException;
45  import org.codehaus.jackson.map.JsonMappingException;
46  import org.codehaus.jackson.map.ObjectMapper;
47  import org.codehaus.jackson.map.SerializationConfig;
48  import org.codehaus.jackson.type.TypeReference;
49  import org.restlet.data.Form;
50  import org.restlet.data.MediaType;
51  
52  
53  public class ClusterRepresentationUtil
54  {
55    private static final ZNRecord EMPTY_ZNRECORD = new ZNRecord("EMPTY_ZNRECORD");
56  
57    public static String getClusterPropertyAsString(ZkClient zkClient,
58                                                    String clusterName,
59                                                    PropertyKey propertyKey,
60                                                    // String key,
61                                                    MediaType mediaType)
62  
63    throws JsonGenerationException,
64        JsonMappingException,
65        IOException
66    {
67      return getClusterPropertyAsString(zkClient, clusterName, mediaType, propertyKey);
68    }
69  
70    public static String getClusterPropertyAsString(ZkClient zkClient,
71                                                    String clusterName,
72                                                    MediaType mediaType,
73                                                    PropertyKey propertyKey) throws JsonGenerationException,
74        JsonMappingException,
75        IOException
76    {
77  
78      ZKHelixDataAccessor accessor =
79          new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
80  
81      HelixProperty property = accessor.getProperty(propertyKey);
82      ZNRecord record = property == null ? null : property.getRecord();
83      return ZNRecordToJson(record);
84    }
85  
86    public static String getInstancePropertyNameListAsString(ZkClient zkClient,
87                                                             String clusterName,
88                                                             String instanceName,
89                                                             PropertyType instanceProperty,
90                                                             String key,
91                                                             MediaType mediaType) throws JsonGenerationException,
92        JsonMappingException,
93        IOException
94    {
95      String path =
96          HelixUtil.getInstancePropertyPath(clusterName, instanceName, instanceProperty)
97              + "/" + key;
98      if (zkClient.exists(path))
99      {
100       List<String> recordNames = zkClient.getChildren(path);
101       return ObjectToJson(recordNames);
102     }
103 
104     return ObjectToJson(new ArrayList<String>());
105   }
106 
107   public static String getInstancePropertyAsString(ZkClient zkClient,
108                                                    String clusterName,
109                                                    PropertyKey propertyKey,
110                                                    MediaType mediaType) throws JsonGenerationException,
111       JsonMappingException,
112       IOException
113   {
114     ZKHelixDataAccessor accessor =
115         new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
116 
117     ZNRecord records = accessor.getProperty(propertyKey).getRecord();
118     return ZNRecordToJson(records);
119   }
120 
121   public static String getInstancePropertiesAsString(ZkClient zkClient,
122                                                      String clusterName,
123                                                      PropertyKey propertyKey,
124                                                      MediaType mediaType) throws JsonGenerationException,
125       JsonMappingException,
126       IOException
127   {
128     zkClient.setZkSerializer(new ZNRecordSerializer());
129     ZKHelixDataAccessor accessor =
130         new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
131 
132     List<ZNRecord> records =
133         HelixProperty.convertToList(accessor.getChildValues(propertyKey));
134     return ObjectToJson(records);
135   }
136 
137   public static String getPropertyAsString(ZkClient zkClient,
138                                            String clusterName,
139                                            PropertyKey propertyKey,
140                                            MediaType mediaType) throws JsonGenerationException,
141       JsonMappingException,
142       IOException
143   {
144     ZKHelixDataAccessor accessor =
145         new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
146 
147     ZNRecord record = EMPTY_ZNRECORD;
148     HelixProperty property = accessor.getProperty(propertyKey);
149     if (property != null)
150     {
151       record = property.getRecord();
152     }
153     return ObjectToJson(record);
154   }
155 
156   public static String ZNRecordToJson(ZNRecord record) throws JsonGenerationException,
157       JsonMappingException,
158       IOException
159   {
160     return ObjectToJson(record);
161   }
162 
163   public static String ObjectToJson(Object object) throws JsonGenerationException,
164       JsonMappingException,
165       IOException
166   {
167     ObjectMapper mapper = new ObjectMapper();
168     SerializationConfig serializationConfig = mapper.getSerializationConfig();
169     serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
170 
171     StringWriter sw = new StringWriter();
172     mapper.writeValue(sw, object);
173 
174     return sw.toString();
175   }
176 
177   public static HelixDataAccessor getClusterDataAccessor(ZkClient zkClient,
178                                                          String clusterName)
179   {
180     return new ZKHelixDataAccessor(clusterName,
181                                    new ZkBaseDataAccessor<ZNRecord>(zkClient));
182   }
183 
184   public static <T extends Object> T JsonToObject(Class<T> clazz, String jsonString) throws JsonParseException,
185       JsonMappingException,
186       IOException
187   {
188     StringReader sr = new StringReader(jsonString);
189     ObjectMapper mapper = new ObjectMapper();
190     return mapper.readValue(sr, clazz);
191 
192   }
193 
194   public static Map<String, String> JsonToMap(String jsonString) throws JsonParseException,
195       JsonMappingException,
196       IOException
197   {
198     StringReader sr = new StringReader(jsonString);
199     ObjectMapper mapper = new ObjectMapper();
200 
201     TypeReference<TreeMap<String, String>> typeRef =
202         new TypeReference<TreeMap<String, String>>()
203         {
204         };
205 
206     return mapper.readValue(sr, typeRef);
207   }
208 
209   public static Map<String, String> getFormJsonParameters(Form form) throws JsonParseException,
210       JsonMappingException,
211       IOException
212   {
213     String jsonPayload = form.getFirstValue(JsonParameters.JSON_PARAMETERS, true);
214     return ClusterRepresentationUtil.JsonToMap(jsonPayload);
215   }
216 
217   public static Map<String, String> getFormJsonParameters(Form form, String key) throws JsonParseException,
218       JsonMappingException,
219       IOException
220   {
221     String jsonPayload = form.getFirstValue(key, true);
222     return ClusterRepresentationUtil.JsonToMap(jsonPayload);
223   }
224 
225   public static String getFormJsonParameterString(Form form, String key) throws JsonParseException,
226       JsonMappingException,
227       IOException
228   {
229     return form.getFirstValue(key, true);
230   }
231 
232   public static <T extends Object> T getFormJsonParameters(Class<T> clazz,
233                                                            Form form,
234                                                            String key) throws JsonParseException,
235       JsonMappingException,
236       IOException
237   {
238     return JsonToObject(clazz, form.getFirstValue(key, true));
239   }
240 
241   public static String getErrorAsJsonStringFromException(Exception e)
242   {
243     StringWriter sw = new StringWriter();
244     PrintWriter pw = new PrintWriter(sw);
245     e.printStackTrace(pw);
246 
247     String error = e.getMessage() + '\n' + sw.toString();
248     Map<String, String> result = new TreeMap<String, String>();
249     result.put("ERROR", error);
250     try
251     {
252       return ObjectToJson(result);
253     }
254     catch (Exception e1)
255     {
256       StringWriter sw1 = new StringWriter();
257       PrintWriter pw1 = new PrintWriter(sw1);
258       e.printStackTrace(pw1);
259       return "{\"ERROR\": \"" + sw1.toString() + "\"}";
260     }
261   }
262 
263   public static String getInstanceSessionId(ZkClient zkClient,
264                                             String clusterName,
265                                             String instanceName)
266   {
267     ZKHelixDataAccessor accessor =
268         new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
269     Builder keyBuilder = accessor.keyBuilder();
270 
271     ZNRecord liveInstance =
272         accessor.getProperty(keyBuilder.liveInstance(instanceName)).getRecord();
273 
274     return liveInstance.getSimpleField(LiveInstanceProperty.SESSION_ID.toString());
275   }
276 
277   public static List<String> getInstancePropertyList(ZkClient zkClient,
278                                                      String clusterName,
279                                                      String instanceName,
280                                                      PropertyType property,
281                                                      String key)
282   {
283     String propertyPath =
284         HelixUtil.getInstancePropertyPath(clusterName, instanceName, property) + "/"
285             + key;
286 
287     return zkClient.getChildren(propertyPath);
288 
289   }
290 }