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