    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 javax.jms.JMSException;
    017    import javax.jms.Message;
    018
    019    import org.hornetq.api.core.management.ManagementHelper;
    020    import org.hornetq.api.core.management.ResourceNames;
    021    import org.hornetq.jms.client.HornetQMessage;
    022
    023    /**
    024     * Helper class to use JMS messages to manage HornetQ server resources.
    025     *
    026     * @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a>
    027     * @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
    028     *
    029     * @version <tt>$Revision$</tt>
    030     */
    031    public class JMSManagementHelper
    032    {
    033       // Constants -----------------------------------------------------
    034
    035       // Attributes ----------------------------------------------------
    036
    037       // Static --------------------------------------------------------
    038
    039       private static org.hornetq.api.core.Message getCoreMessage(final Message jmsMessage)
    040       {
    041          if (jmsMessage instanceof HornetQMessage == false)
    042          {
    043             throw new IllegalArgumentException("Cannot send a non HornetQ message as a management message " + jmsMessage.getClass()
    044                                                                                                                       .getName());
    045          }
    046
    047          return ((HornetQMessage)jmsMessage).getCoreMessage();
    048       }
    049
    050       /**
    051        * Stores a resource attribute in a JMS message to retrieve the value from the server resource.
    052        *
    053        * @param message JMS message
    054        * @param resourceName the name of the resource
    055        * @param attribute the name of the attribute
    056        * @throws JMSException if an exception occurs while putting the information in the message
    057        *
    058        * @see ResourceNames
    059        */
    060       public static void putAttribute(final Message message, final String resourceName, final String attribute) throws JMSException
    061       {
    062          ManagementHelper.putAttribute(JMSManagementHelper.getCoreMessage(message), resourceName, attribute);
    063       }
    064
    065       /**
    066        * Stores a operation invocation in a JMS message to invoke the corresponding operation the value from the server resource.
    067        *
    068        * @param message JMS message
    069        * @param resourceName the name of the resource
    070        * @param operationName the name of the operation to invoke on the resource
    071        * @throws JMSException if an exception occurs while putting the information in the message
    072        *
    073        * @see ResourceNames
    074        */
    075       public static void putOperationInvocation(final Message message,
    076                                                 final String resourceName,
    077                                                 final String operationName) throws JMSException
    078       {
    079          try
    080          {
    081             ManagementHelper.putOperationInvocation(JMSManagementHelper.getCoreMessage(message),
    082                                                     resourceName,
    083                                                     operationName);
    084          }
    085          catch (Exception e)
    086          {
    087             throw JMSManagementHelper.convertFromException(e);
    088          }
    089       }
    090
    091       private static JMSException convertFromException(final Exception e)
    092       {
    093          JMSException jmse = new JMSException(e.getMessage());
    094
    095          jmse.initCause(e);
    096
    097          return jmse;
    098       }
    099
    100       /**
    101        * Stores a operation invocation in a JMS message to invoke the corresponding operation the value from the server resource.
    102        *
    103        * @param message JMS message
    104        * @param resourceName the name of the server resource
    105        * @param operationName the name of the operation to invoke on the server resource
    106        * @param parameters the parameters to use to invoke the server resource
    107        * @throws JMSException if an exception occurs while putting the information in the message
    108        *
    109        * @see ResourceNames
    110        */
    111       public static void putOperationInvocation(final Message message,
    112                                                 final String resourceName,
    113                                                 final String operationName,
    114                                                 final Object... parameters) throws JMSException
    115       {
    116          try
    117          {
    118             ManagementHelper.putOperationInvocation(JMSManagementHelper.getCoreMessage(message),
    119                                                     resourceName,
    120                                                     operationName,
    121                                                     parameters);
    122          }
    123          catch (Exception e)
    124          {
    125             throw JMSManagementHelper.convertFromException(e);
    126          }
    127       }
    128
    129       /**
    130        * Returns whether the JMS message corresponds to the result of a management operation invocation.
    131        */
    132       public static boolean isOperationResult(final Message message) throws JMSException
    133       {
    134          return ManagementHelper.isOperationResult(JMSManagementHelper.getCoreMessage(message));
    135       }
    136
    137       /**
    138        * Returns whether the JMS message corresponds to the result of a management attribute value.
    139        */
    140       public static boolean isAttributesResult(final Message message) throws JMSException
    141       {
    142          return ManagementHelper.isAttributesResult(JMSManagementHelper.getCoreMessage(message));
    143       }
    144
    145       /**
    146        * Returns whether the invocation of the management operation on the server resource succeeded.
    147        */
    148       public static boolean hasOperationSucceeded(final Message message) throws JMSException
    149       {
    150          return ManagementHelper.hasOperationSucceeded(JMSManagementHelper.getCoreMessage(message));
    151       }
    152
    153       /**
    154        * Returns the result of an operation invocation or an attribute value.
    155        * <br>
    156        * If an error occurred on the server, {@link #hasOperationSucceeded(Message)} will return {@code false}.
    157        * and the result will be a String corresponding to the server exception.
    158        */
    159       public static Object[] getResults(final Message message) throws Exception
    160       {
    161          return ManagementHelper.getResults(JMSManagementHelper.getCoreMessage(message));
    162       }
    163
    164       /**
    165        * Returns the result of an operation invocation or an attribute value.
    166        * <br>
    167        * If an error occurred on the server, {@link #hasOperationSucceeded(Message)} will return {@code false}.
    168        * and the result will be a String corresponding to the server exception.
    169        */
    170       public static Object getResult(final Message message) throws Exception
    171       {
    172          return ManagementHelper.getResult(JMSManagementHelper.getCoreMessage(message));
    173       }
    174
    175       // Constructors --------------------------------------------------
    176
    177       private JMSManagementHelper()
    178       {
    179       }
    180
    181       // Public --------------------------------------------------------
    182
    183       // Package protected ---------------------------------------------
    184
    185       // Protected -----------------------------------------------------
    186
    187       // Private -------------------------------------------------------
    188
    189       // Inner classes -------------------------------------------------
    190    }