001 /* 002 * Copyright 2009 Red Hat, Inc. 003 * Red Hat licenses this file to you under the Apache License, version 004 * 2.0 (the "License"); you may not use this file except in compliance 005 * with the License. You may obtain a copy of the License at 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * Unless required by applicable law or agreed to in writing, software 008 * distributed under the License is distributed on an "AS IS" BASIS, 009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 010 * implied. See the License for the specific language governing 011 * permissions and limitations under the License. 012 */ 013 014 package org.hornetq.api.core.management; 015 016 import java.util.Arrays; 017 018 import org.hornetq.core.logging.Logger; 019 import org.hornetq.utils.json.JSONArray; 020 import org.hornetq.utils.json.JSONException; 021 import org.hornetq.utils.json.JSONObject; 022 023 /** 024 * Helper class to create Java Objects from the 025 * JSON serialization returned by {@link QueueControl#listMessageCounterHistory()}. 026 * 027 * @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a> 028 */ 029 public class DayCounterInfo 030 { 031 // Constants ----------------------------------------------------- 032 033 private static final Logger log = Logger.getLogger(DayCounterInfo.class); 034 035 // Attributes ---------------------------------------------------- 036 037 private final String date; 038 039 private final int[] counters; 040 041 // Static -------------------------------------------------------- 042 043 public static String toJSON(final DayCounterInfo[] infos) throws JSONException 044 { 045 JSONObject json = new JSONObject(); 046 JSONArray counters = new JSONArray(); 047 for (DayCounterInfo info : infos) 048 { 049 JSONObject counter = new JSONObject(); 050 counter.put("date", info.getDate()); 051 counter.put("counters", Arrays.asList(info.getCounters())); 052 counters.put(counter); 053 } 054 json.put("dayCounters", counters); 055 return json.toString(); 056 } 057 058 /** 059 * Returns an array of RoleInfo corresponding to the JSON serialization returned 060 * by {@link QueueControl#listMessageCounterHistory()}. 061 */ 062 public static DayCounterInfo[] fromJSON(final String jsonString) throws JSONException 063 { 064 JSONObject json = new JSONObject(jsonString); 065 JSONArray dayCounters = json.getJSONArray("dayCounters"); 066 DayCounterInfo[] infos = new DayCounterInfo[dayCounters.length()]; 067 for (int i = 0; i < dayCounters.length(); i++) 068 { 069 070 JSONObject counter = (JSONObject)dayCounters.get(i); 071 JSONArray hour = (JSONArray)counter.getJSONArray("counters").get(0); 072 int[] hourCounters = new int[24]; 073 for (int j = 0; j < 24; j++) 074 { 075 hourCounters[j] = hour.getInt(j); 076 } 077 DayCounterInfo info = new DayCounterInfo(counter.getString("date"), hourCounters); 078 infos[i] = info; 079 } 080 return infos; 081 } 082 083 // Constructors -------------------------------------------------- 084 085 public DayCounterInfo(final String date, final int[] counters) 086 { 087 this.date = date; 088 this.counters = counters; 089 } 090 091 // Public -------------------------------------------------------- 092 093 /** 094 * Returns the date of the counter. 095 */ 096 public String getDate() 097 { 098 return date; 099 } 100 101 /** 102 * Returns a 24-length array corresponding to the number of messages added to the queue 103 * for the given hour of the day. 104 */ 105 public int[] getCounters() 106 { 107 return counters; 108 } 109 110 // Package protected --------------------------------------------- 111 112 // Protected ----------------------------------------------------- 113 114 // Private ------------------------------------------------------- 115 116 // Inner classes ------------------------------------------------- 117 }