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.jms.management;
015    
016    import org.hornetq.utils.json.JSONArray;
017    import 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     */
025    public 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       // Static --------------------------------------------------------
044    
045       /**
046        * Returns an array of SubscriptionInfo corresponding to the JSON serialization returned
047        * by {@link TopicControl#listAllSubscriptionsAsJSON()} and related methods.
048        */
049       public static SubscriptionInfo[] from(final String jsonString) throws Exception
050       {
051          JSONArray array = new JSONArray(jsonString);
052          SubscriptionInfo[] infos = new SubscriptionInfo[array.length()];
053          for (int i = 0; i < array.length(); i++)
054          {
055             JSONObject sub = array.getJSONObject(i);
056             SubscriptionInfo info = new SubscriptionInfo(sub.getString("queueName"),
057                                                          sub.optString("clientID", null),
058                                                          sub.optString("name", null),
059                                                          sub.getBoolean("durable"),
060                                                          sub.optString("selector", null),
061                                                          sub.getInt("messageCount"));
062             infos[i] = info;
063          }
064    
065          return infos;
066       }
067    
068       // Constructors --------------------------------------------------
069    
070       private SubscriptionInfo(final String queueName,
071                                final String clientID,
072                                final String name,
073                                final boolean durable,
074                                final String selector,
075                                final int messageCount)
076       {
077          this.queueName = queueName;
078          this.clientID = clientID;
079          this.name = name;
080          this.durable = durable;
081          this.selector = selector;
082          this.messageCount = messageCount;
083       }
084    
085       // Public --------------------------------------------------------
086    
087       /**
088        * Returns the name of the HornetQ core queue corresponding to this subscription.
089        */
090       public String getQueueName()
091       {
092          return queueName;
093       }
094    
095       /**
096        * Returns the client ID of this subscription or {@code null}.
097        */
098       public String getClientID()
099       {
100          return clientID;
101       }
102    
103       /**
104        * Returns the name of this subscription.
105        */
106       public String getName()
107       {
108          return name;
109       }
110    
111       /**
112        * Returns whether this subscription is durable.
113        */
114       public boolean isDurable()
115       {
116          return durable;
117       }
118    
119       /**
120        * Returns the JMS message selector associated to this subscription.
121        */
122       public String getSelector()
123       {
124          return selector;
125       }
126    
127       /**
128        * Returns the number of messages currently held by this subscription.
129        */
130       public int getMessageCount()
131       {
132          return messageCount;
133       }
134    
135       // Package protected ---------------------------------------------
136    
137       // Protected -----------------------------------------------------
138    
139       // Private -------------------------------------------------------
140    
141       // Inner classes -------------------------------------------------
142    }