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.text.DateFormat;
017    import java.util.Date;
018    
019    import org.hornetq.core.logging.Logger;
020    import org.hornetq.core.messagecounter.MessageCounter;
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#listMessageCounter()}.
026     *
027     *  @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a>
028     */
029    public class MessageCounterInfo
030    {
031       // Constants -----------------------------------------------------
032    
033       private static final Logger log = Logger.getLogger(MessageCounterInfo.class);
034    
035       // Attributes ----------------------------------------------------
036    
037       private final String name;
038    
039       private final String subscription;
040    
041       private final boolean durable;
042    
043       private final long count;
044    
045       private final long countDelta;
046    
047       private final int depth;
048    
049       private final int depthDelta;
050    
051       private final String lastAddTimestamp;
052    
053       private final String udpateTimestamp;
054    
055       // Static --------------------------------------------------------
056    
057       public static String toJSon(final MessageCounter counter) throws Exception
058       {
059          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);
060    
061          JSONObject json = new JSONObject(counter);
062          String lastAddTimestamp = dateFormat.format(new Date(counter.getLastAddedMessageTime()));
063          json.put("lastAddTimestamp", lastAddTimestamp);
064          String updateTimestamp = dateFormat.format(new Date(counter.getLastUpdate()));
065          json.put("updateTimestamp", updateTimestamp);
066    
067          return json.toString();
068       }
069    
070       /**
071        * Returns an array of RoleInfo corresponding to the JSON serialization returned
072        * by {@link QueueControl#listMessageCounter()}.
073        */
074       public static MessageCounterInfo fromJSON(final String jsonString) throws Exception
075       {
076          JSONObject data = new JSONObject(jsonString);
077          String name = data.getString("destinationName");
078          String subscription = data.getString("destinationSubscription");
079          boolean durable = data.getBoolean("destinationDurable");
080          long count = data.getLong("count");
081          long countDelta = data.getLong("countDelta");
082          int depth = data.getInt("messageCount");
083          int depthDelta = data.getInt("messageCountDelta");
084          String lastAddTimestamp = data.getString("lastAddTimestamp");
085          String updateTimestamp = data.getString("updateTimestamp");
086    
087          return new MessageCounterInfo(name,
088                                        subscription,
089                                        durable,
090                                        count,
091                                        countDelta,
092                                        depth,
093                                        depthDelta,
094                                        lastAddTimestamp,
095                                        updateTimestamp);
096       }
097    
098       // Constructors --------------------------------------------------
099    
100       public MessageCounterInfo(final String name,
101                                 final String subscription,
102                                 final boolean durable,
103                                 final long count,
104                                 final long countDelta,
105                                 final int depth,
106                                 final int depthDelta,
107                                 final String lastAddTimestamp,
108                                 final String udpateTimestamp)
109       {
110          this.name = name;
111          this.subscription = subscription;
112          this.durable = durable;
113          this.count = count;
114          this.countDelta = countDelta;
115          this.depth = depth;
116          this.depthDelta = depthDelta;
117          this.lastAddTimestamp = lastAddTimestamp;
118          this.udpateTimestamp = udpateTimestamp;
119       }
120    
121       // Public --------------------------------------------------------
122    
123       /**
124        * Returns the name of the queue.
125        */
126       public String getName()
127       {
128          return name;
129       }
130    
131       /**
132        * Returns the name of the subscription.
133        */
134       public String getSubscription()
135       {
136          return subscription;
137       }
138    
139       /**
140        * Returns whether the queue is durable.
141        */
142       public boolean isDurable()
143       {
144          return durable;
145       }
146    
147       /**
148        * Returns the number of messages added to the queue since it was created.
149        */
150       public long getCount()
151       {
152          return count;
153       }
154    
155       /**
156        * Returns the number of messages added to the queue since the last counter sample.
157        */
158       public long getCountDelta()
159       {
160          return countDelta;
161       }
162    
163       /**
164        * Returns the number of messages currently in the queue.
165        */
166       public int getDepth()
167       {
168          return depth;
169       }
170    
171       /**
172        * Returns the number of messages in the queue since last counter sample.
173        */
174       public int getDepthDelta()
175       {
176          return depthDelta;
177       }
178    
179       /**
180        * Returns the timestamp of the last time a message was added to the queue.
181        */
182       public String getLastAddTimestamp()
183       {
184          return lastAddTimestamp;
185       }
186    
187       /**
188        * Returns the timestamp of the last time the queue was updated.
189        */
190       public String getUdpateTimestamp()
191       {
192          return udpateTimestamp;
193       }
194    
195       // Package protected ---------------------------------------------
196    
197       // Protected -----------------------------------------------------
198    
199       // Private -------------------------------------------------------
200    
201       // Inner classes -------------------------------------------------
202    }