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          return new ObjectNameBuilder(domain);
054       }
055    
056       // Constructors --------------------------------------------------
057    
058       private ObjectNameBuilder(final String domain)
059       {
060          this.domain = domain;
061       }
062    
063       // Public --------------------------------------------------------
064    
065       /**
066        * Returns the ObjectName used by the single HornetQServerControl.
067        */
068       public ObjectName getHornetQServerObjectName() throws Exception
069       {
070          return ObjectName.getInstance(domain + ":module=Core,type=Server");
071       }
072    
073       /**
074        * Returns the ObjectName used by AddressControl.
075        * 
076        * @see AddressControl
077        */
078       public ObjectName getAddressObjectName(final SimpleString address) throws Exception
079       {
080          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Address", address.toString());
081       }
082    
083       /**
084        * Returns the ObjectName used by QueueControl.
085        * 
086        * @see QueueControl
087        */
088       public ObjectName getQueueObjectName(final SimpleString address, final SimpleString name) throws Exception
089       {
090          return ObjectName.getInstance(String.format("%s:module=%s,type=%s,address=%s,name=%s",
091                                                      domain,
092                                                      ObjectNameBuilder.CORE_MODULE,
093                                                      "Queue",
094                                                      ObjectName.quote(address.toString()),
095                                                      ObjectName.quote(name.toString())));
096       }
097    
098       /**
099        * Returns the ObjectName used by DivertControl.
100        * 
101        * @see DivertControl
102        */
103       public ObjectName getDivertObjectName(final String name) throws Exception
104       {
105          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Divert", name.toString());
106       }
107    
108       /**
109        * Returns the ObjectName used by AcceptorControl.
110        * 
111        * @see AcceptorControl
112        */
113       public ObjectName getAcceptorObjectName(final String name) throws Exception
114       {
115          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Acceptor", name);
116       }
117    
118       /**
119        * Returns the ObjectName used by BroadcastGroupControl.
120        * 
121        * @see BroadcastGroupControl
122        */
123       public ObjectName getBroadcastGroupObjectName(final String name) throws Exception
124       {
125          return createObjectName(ObjectNameBuilder.CORE_MODULE, "BroadcastGroup", name);
126       }
127    
128       /**
129        * Returns the ObjectName used by BridgeControl.
130        * 
131        * @see BridgeControl
132        */
133       public ObjectName getBridgeObjectName(final String name) throws Exception
134       {
135          return createObjectName(ObjectNameBuilder.CORE_MODULE, "Bridge", name);
136       }
137    
138       /**
139        * Returns the ObjectName used by ClusterConnectionControl.
140        * 
141        * @see ClusterConnectionControl
142        */
143       public ObjectName getClusterConnectionObjectName(final String name) throws Exception
144       {
145          return createObjectName(ObjectNameBuilder.CORE_MODULE, "ClusterConnection", name);
146       }
147    
148       /**
149        * Returns the ObjectName used by DiscoveryGroupControl.
150        * 
151        * @see DiscoveryGroupControl
152        */
153       public ObjectName getDiscoveryGroupObjectName(final String name) throws Exception
154       {
155          return createObjectName(ObjectNameBuilder.CORE_MODULE, "DiscoveryGroup", name);
156       }
157    
158       /**
159        * Returns the ObjectName used by JMSServerControl.
160        * 
161        * @see JMSServerControl
162        */
163       public ObjectName getJMSServerObjectName() throws Exception
164       {
165          return ObjectName.getInstance(domain + ":module=JMS,type=Server");
166       }
167    
168       /**
169        * Returns the ObjectName used by JMSQueueControl.
170        * 
171        * @see JMSQueueControl
172        */
173       public ObjectName getJMSQueueObjectName(final String name) throws Exception
174       {
175          return createObjectName(ObjectNameBuilder.JMS_MODULE, "Queue", name);
176       }
177    
178       /**
179        * Returns the ObjectName used by TopicControl.
180        * 
181        * @see TopicControl
182        */
183       public ObjectName getJMSTopicObjectName(final String name) throws Exception
184       {
185          return createObjectName(ObjectNameBuilder.JMS_MODULE, "Topic", name);
186       }
187    
188       /**
189        * Returns the ObjectName used by ConnectionFactoryControl.
190        * 
191        * @see ConnectionFactoryControl
192        */
193       public ObjectName getConnectionFactoryObjectName(final String name) throws Exception
194       {
195          return createObjectName(ObjectNameBuilder.JMS_MODULE, "ConnectionFactory", name);
196       }
197    
198       // Package protected ---------------------------------------------
199    
200       // Protected -----------------------------------------------------
201    
202       // Private -------------------------------------------------------
203    
204       private ObjectName createObjectName(final String module, final String type, final String name) throws Exception
205       {
206          return ObjectName.getInstance(String.format("%s:module=%s,type=%s,name=%s",
207                                                      domain,
208                                                      module,
209                                                      type,
210                                                      ObjectName.quote(name)));
211       }
212    
213       // Inner classes -------------------------------------------------
214    
215    }