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
014package org.hornetq.api.jms.management;
015
016import org.hornetq.utils.json.JSONArray;
017import org.hornetq.utils.json.JSONObject;
018
019/**
020 * Helper class to create Java Objects from the
021 * JSON serialization returned by {@link TopicControl#listAllSubscriptionsAsJSON()} and related methods.
022 * 
023 * @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a>
024 */
025public class SubscriptionInfo
026{
027   // Constants -----------------------------------------------------
028
029   // Attributes ----------------------------------------------------
030
031   private final String queueName;
032
033   private final String clientID;
034
035   private final String name;
036
037   private final boolean durable;
038
039   private final String selector;
040
041   private final int messageCount;
042
043   private final int deliveringCount;
044   
045   // Static --------------------------------------------------------
046
047   /**
048    * Returns an array of SubscriptionInfo corresponding to the JSON serialization returned
049    * by {@link TopicControl#listAllSubscriptionsAsJSON()} and related methods.
050    */
051   public static SubscriptionInfo[] from(final String jsonString) throws Exception
052   {
053      JSONArray array = new JSONArray(jsonString);
054      SubscriptionInfo[] infos = new SubscriptionInfo[array.length()];
055      for (int i = 0; i < array.length(); i++)
056      {
057         JSONObject sub = array.getJSONObject(i);
058         SubscriptionInfo info = new SubscriptionInfo(sub.getString("queueName"),
059                                                      sub.optString("clientID", null),
060                                                      sub.optString("name", null),
061                                                      sub.getBoolean("durable"),
062                                                      sub.optString("selector", null),
063                                                      sub.getInt("messageCount"),
064                                                      sub.getInt("deliveringCount"));
065         infos[i] = info;
066      }
067
068      return infos;
069   }
070
071   // Constructors --------------------------------------------------
072
073   private SubscriptionInfo(final String queueName,
074                            final String clientID,
075                            final String name,
076                            final boolean durable,
077                            final String selector,
078                            final int messageCount,
079                            final int deliveringCount)
080   {
081      this.queueName = queueName;
082      this.clientID = clientID;
083      this.name = name;
084      this.durable = durable;
085      this.selector = selector;
086      this.messageCount = messageCount;
087      this.deliveringCount = deliveringCount;
088   }
089
090   // Public --------------------------------------------------------
091
092   /**
093    * Returns the name of the HornetQ core queue corresponding to this subscription.
094    */
095   public String getQueueName()
096   {
097      return queueName;
098   }
099
100   /**
101    * Returns the client ID of this subscription or {@code null}.
102    */
103   public String getClientID()
104   {
105      return clientID;
106   }
107
108   /**
109    * Returns the name of this subscription.
110    */
111   public String getName()
112   {
113      return name;
114   }
115
116   /**
117    * Returns whether this subscription is durable.
118    */
119   public boolean isDurable()
120   {
121      return durable;
122   }
123
124   /**
125    * Returns the JMS message selector associated to this subscription.
126    */
127   public String getSelector()
128   {
129      return selector;
130   }
131
132   /**
133    * Returns the number of messages currently held by this subscription.
134    */
135   public int getMessageCount()
136   {
137      return messageCount;
138   }
139   
140   /**
141    * Returns the number of messages currently delivered to this subscription.
142    */
143   public int getDeliveringCount()
144   {
145      return deliveringCount;
146   }
147
148   // Package protected ---------------------------------------------
149
150   // Protected -----------------------------------------------------
151
152   // Private -------------------------------------------------------
153
154   // Inner classes -------------------------------------------------
155}