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 javax.management.ObjectName;
017    
018    import org.hornetq.api.core.SimpleString;
019    import org.hornetq.api.jms.management.ConnectionFactoryControl;
020    import org.hornetq.api.jms.management.JMSQueueControl;
021    import org.hornetq.api.jms.management.JMSServerControl;
022    import org.hornetq.api.jms.management.TopicControl;
023    import org.hornetq.core.config.impl.ConfigurationImpl;
024    
025    /**
026     * Helper class to build ObjectNames for HornetQ resources.
027     *
028     * @author <a href="jmesnil@redhat.com">Jeff Mesnil</a>
029     *
030     */
031    public class ObjectNameBuilder
032    {
033    
034       // Constants -----------------------------------------------------
035    
036       /**
037        * Default JMX domain for HornetQ resources.
038        */
039       public static ObjectNameBuilder DEFAULT = new ObjectNameBuilder(ConfigurationImpl.DEFAULT_JMX_DOMAIN);
040    
041       public static final String JMS_MODULE = "JMS";
042    
043       public static final String CORE_MODULE = "Core";
044    
045       // Attributes ----------------------------------------------------
046    
047       private final String domain;
048    
049       // Static --------------------------------------------------------
050    
051       public static ObjectNameBuilder create(final String domain)
052       {
053          if (domain == null)
054          {
055             return new ObjectNameBuilder(ConfigurationImpl.DEFAULT_JMX_DOMAIN);
056          }
057          else
058          {
059             return new ObjectNameBuilder(domain);
060          }
061       }
062    
063       // Constructors --------------------------------------------------
064    
065       private ObjectNameBuilder(final String domain)
066       {
067          this.domain = domain;
068       }
069    
070       // Public --------------------------------------------------------
071    
072       /**
073        * Returns the ObjectName used by the single HornetQServerControl.
074        */
075       public ObjectName getHornetQServerObjectName() throws Exception
076       {
077          return ObjectName.getInstance(domain + ":module=Core,type=Server");
078       }
079    
080       /**
081        * Returns the ObjectName used by AddressControl.
082        * 
083        * @see AddressControl
084        */
085       public ObjectName getAddressObjectName(final SimpleString address) throws Exception
086       {
087          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Address", address.toString());
088       }
089    
090       /**
091        * Returns the ObjectName used by QueueControl.
092        * 
093        * @see QueueControl
094        */
095       public ObjectName getQueueObjectName(final SimpleString address, final SimpleString name) throws Exception
096       {
097          return ObjectName.getInstance(String.format("%s:module=%s,type=%s,address=%s,name=%s",
098                                                      domain,
099                                                      ObjectNameBuilder.CORE_MODULE,
100                                                      "Queue",
101                                                      ObjectName.quote(address.toString()),
102                                                      ObjectName.quote(name.toString())));
103       }
104    
105       /**
106        * Returns the ObjectName used by DivertControl.
107        * 
108        * @see DivertControl
109        */
110       public ObjectName getDivertObjectName(final String name) throws Exception
111       {
112          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Divert", name.toString());
113       }
114    
115       /**
116        * Returns the ObjectName used by AcceptorControl.
117        * 
118        * @see AcceptorControl
119        */
120       public ObjectName getAcceptorObjectName(final String name) throws Exception
121       {
122          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Acceptor", name);
123       }
124    
125       /**
126        * Returns the ObjectName used by BroadcastGroupControl.
127        * 
128        * @see BroadcastGroupControl
129        */
130       public ObjectName getBroadcastGroupObjectName(final String name) throws Exception
131       {
132          return createObjectName(ObjectNameBuilder.CORE_MODULE, "BroadcastGroup", name);
133       }
134    
135       /**
136        * Returns the ObjectName used by BridgeControl.
137        * 
138        * @see BridgeControl
139        */
140       public ObjectName getBridgeObjectName(final String name) throws Exception
141       {
142          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Bridge", name);
143       }
144    
145       /**
146        * Returns the ObjectName used by ClusterConnectionControl.
147        * 
148        * @see ClusterConnectionControl
149        */
150       public ObjectName getClusterConnectionObjectName(final String name) throws Exception
151       {
152          return createObjectName(ObjectNameBuilder.CORE_MODULE, "ClusterConnection", name);
153       }
154    
155       /**
156        * Returns the ObjectName used by DiscoveryGroupControl.
157        * 
158        * @see DiscoveryGroupControl
159        */
160       public ObjectName getDiscoveryGroupObjectName(final String name) throws Exception
161       {
162          return createObjectName(ObjectNameBuilder.CORE_MODULE, "DiscoveryGroup", name);
163       }
164    
165       /**
166        * Returns the ObjectName used by JMSServerControl.
167        * 
168        * @see JMSServerControl
169        */
170       public ObjectName getJMSServerObjectName() throws Exception
171       {
172          return ObjectName.getInstance(domain + ":module=JMS,type=Server");
173       }
174    
175       /**
176        * Returns the ObjectName used by JMSQueueControl.
177        * 
178        * @see JMSQueueControl
179        */
180       public ObjectName getJMSQueueObjectName(final String name) throws Exception
181       {
182          return createObjectName(ObjectNameBuilder.JMS_MODULE, "Queue", name);
183       }
184    
185       /**
186        * Returns the ObjectName used by TopicControl.
187        * 
188        * @see TopicControl
189        */
190       public ObjectName getJMSTopicObjectName(final String name) throws Exception
191       {
192          return createObjectName(ObjectNameBuilder.JMS_MODULE, "Topic", name);
193       }
194    
195       /**
196        * Returns the ObjectName used by ConnectionFactoryControl.
197        * 
198        * @see ConnectionFactoryControl
199        */
200       public ObjectName getConnectionFactoryObjectName(final String name) throws Exception
201       {
202          return createObjectName(ObjectNameBuilder.JMS_MODULE, "ConnectionFactory", name);
203       }
204    
205       // Package protected ---------------------------------------------
206    
207       // Protected -----------------------------------------------------
208    
209       // Private -------------------------------------------------------
210    
211       private ObjectName createObjectName(final String module, final String type, final String name) throws Exception
212       {
213          return ObjectName.getInstance(String.format("%s:module=%s,type=%s,name=%s",
214                                                      domain,
215                                                      module,
216                                                      type,
217                                                      ObjectName.quote(name)));
218       }
219    
220       // Inner classes -------------------------------------------------
221    
222    }