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.core.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 AddressControl#getRolesAsJSON()}.
022 * 
023 * @author <a href="jmesnil@redhat.com">Jeff Mesnil</a>
024 */
025public class RoleInfo
026{
027   final private String name;
028
029   final private boolean send;
030
031   final private boolean consume;
032
033   final private boolean createDurableQueue;
034
035   final private boolean deleteDurableQueue;
036
037   final private boolean createNonDurableQueue;
038
039   final private boolean deleteNonDurableQueue;
040
041   final private boolean manage;
042
043   /**
044    * Returns an array of RoleInfo corresponding to the JSON serialization returned
045    * by {@link AddressControl#getRolesAsJSON()}.
046    */
047   public static final RoleInfo[] from(final String jsonString) throws Exception
048   {
049      JSONArray array = new JSONArray(jsonString);
050      RoleInfo[] roles = new RoleInfo[array.length()];
051      for (int i = 0; i < array.length(); i++)
052      {
053         JSONObject r = array.getJSONObject(i);
054         RoleInfo role = new RoleInfo(r.getString("name"),
055                                      r.getBoolean("send"),
056                                      r.getBoolean("consume"),
057                                      r.getBoolean("createDurableQueue"),
058                                      r.getBoolean("deleteDurableQueue"),
059                                      r.getBoolean("createNonDurableQueue"),
060                                      r.getBoolean("deleteNonDurableQueue"),
061                                      r.getBoolean("manage"));
062         roles[i] = role;
063      }
064      return roles;
065   }
066
067   private RoleInfo(final String name,
068                    final boolean send,
069                    final boolean consume,
070                    final boolean createDurableQueue,
071                    final boolean deleteDurableQueue,
072                    final boolean createNonDurableQueue,
073                    final boolean deleteNonDurableQueue,
074                    final boolean manage)
075   {
076      this.name = name;
077      this.send = send;
078      this.consume = consume;
079      this.createDurableQueue = createDurableQueue;
080      this.deleteDurableQueue = deleteDurableQueue;
081      this.createNonDurableQueue = createNonDurableQueue;
082      this.deleteNonDurableQueue = deleteNonDurableQueue;
083      this.manage = manage;
084   }
085
086   /**
087    * Returns the name of the role.
088    */
089   public String getName()
090   {
091      return name;
092   }
093
094   /**
095    * Returns whether this role can send messages to the address.
096    */
097   public boolean isSend()
098   {
099      return send;
100   }
101
102   /**
103    * Returns whether this role can consume messages from queues bound to the address.
104    */
105   public boolean isConsume()
106   {
107      return consume;
108   }
109
110   /**
111    * Returns whether this role can create durable queues bound to the address.
112    */
113   public boolean isCreateDurableQueue()
114   {
115      return createDurableQueue;
116   }
117
118   /**
119    * Returns whether this role can delete durable queues bound to the address.
120    */
121   public boolean isDeleteDurableQueue()
122   {
123      return deleteDurableQueue;
124   }
125
126   /**
127    * Returns whether this role can create non-durable queues bound to the address.
128    */
129   public boolean isCreateNonDurableQueue()
130   {
131      return createNonDurableQueue;
132   }
133
134   /**
135    * Returns whether this role can delete non-durable queues bound to the address.
136    */
137   public boolean isDeleteNonDurableQueue()
138   {
139      return deleteNonDurableQueue;
140   }
141
142   /**
143    * Returns whether this role can send management messages to the address.
144    */
145   public boolean isManage()
146   {
147      return manage;
148   }
149}