PageRenderTime 165ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/messaging/src/main/java/org/jboss/as/messaging/jms/JMSQueueReadAttributeHandler.java

#
Java | 139 lines | 95 code | 16 blank | 28 comment | 24 complexity | b875dfc24bca7053ca944d4378167519 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.messaging.jms;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import org.hornetq.api.core.management.ResourceNames;
  26. import org.hornetq.api.jms.management.JMSQueueControl;
  27. import org.hornetq.core.server.HornetQServer;
  28. import org.jboss.as.controller.AbstractRuntimeOnlyHandler;
  29. import org.jboss.as.controller.OperationContext;
  30. import org.jboss.as.controller.OperationFailedException;
  31. import org.jboss.as.controller.PathAddress;
  32. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  33. import org.jboss.as.controller.operations.validation.ParametersValidator;
  34. import org.jboss.as.controller.operations.validation.StringLengthValidator;
  35. import org.jboss.as.controller.registry.AttributeAccess;
  36. import org.jboss.as.controller.registry.ManagementResourceRegistration;
  37. import org.jboss.as.messaging.MessagingServices;
  38. import org.jboss.dmr.ModelNode;
  39. import org.jboss.msc.service.ServiceController;
  40. import org.jboss.msc.service.ServiceName;
  41. import static org.jboss.as.messaging.CommonAttributes.CONSUMER_COUNT;
  42. import static org.jboss.as.messaging.CommonAttributes.DEAD_LETTER_ADDRESS;
  43. import static org.jboss.as.messaging.CommonAttributes.DELIVERING_COUNT;
  44. import static org.jboss.as.messaging.CommonAttributes.EXPIRY_ADDRESS;
  45. import static org.jboss.as.messaging.CommonAttributes.MESSAGES_ADDED;
  46. import static org.jboss.as.messaging.CommonAttributes.MESSAGE_COUNT;
  47. import static org.jboss.as.messaging.CommonAttributes.NAME;
  48. import static org.jboss.as.messaging.CommonAttributes.PAUSED;
  49. import static org.jboss.as.messaging.CommonAttributes.QUEUE_ADDRESS;
  50. import static org.jboss.as.messaging.CommonAttributes.SCHEDULED_COUNT;
  51. import static org.jboss.as.messaging.CommonAttributes.TEMPORARY;
  52. import static org.jboss.as.messaging.MessagingMessages.MESSAGES;
  53. /**
  54. * Implements the {@code read-attribute} operation for runtime attributes exposed by a HornetQ
  55. * {@link org.hornetq.api.jms.management.JMSQueueControl}.
  56. *
  57. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  58. */
  59. public class JMSQueueReadAttributeHandler extends AbstractRuntimeOnlyHandler {
  60. public static final JMSQueueReadAttributeHandler INSTANCE = new JMSQueueReadAttributeHandler();
  61. public static final List<String> METRICS = Arrays.asList( MESSAGE_COUNT, SCHEDULED_COUNT, CONSUMER_COUNT, DELIVERING_COUNT, MESSAGES_ADDED );
  62. public static final List<String> READ_ATTRIBUTES = Arrays.asList( QUEUE_ADDRESS.getName(),
  63. EXPIRY_ADDRESS.getName(), DEAD_LETTER_ADDRESS.getName(), PAUSED, TEMPORARY );
  64. private ParametersValidator validator = new ParametersValidator();
  65. private JMSQueueReadAttributeHandler() {
  66. validator.registerValidator(NAME, new StringLengthValidator(1));
  67. }
  68. @Override
  69. public void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
  70. validator.validate(operation);
  71. final String attributeName = operation.require(ModelDescriptionConstants.NAME).asString();
  72. String queueName = PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR)).getLastElement().getValue();
  73. final ServiceName hqServiceName = MessagingServices.getHornetQServiceName(PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR)));
  74. ServiceController<?> hqService = context.getServiceRegistry(false).getService(hqServiceName);
  75. HornetQServer hqServer = HornetQServer.class.cast(hqService.getValue());
  76. JMSQueueControl control = JMSQueueControl.class.cast(hqServer.getManagementService().getResource(ResourceNames.JMS_QUEUE + queueName));
  77. if (MESSAGE_COUNT.equals(attributeName)) {
  78. try {
  79. context.getResult().set(control.getMessageCount());
  80. } catch (RuntimeException e) {
  81. throw e;
  82. } catch (Exception e) {
  83. throw new RuntimeException(e);
  84. }
  85. } else if (SCHEDULED_COUNT.equals(attributeName)) {
  86. context.getResult().set(control.getScheduledCount());
  87. } else if (CONSUMER_COUNT.equals(attributeName)) {
  88. context.getResult().set(control.getConsumerCount());
  89. } else if (DELIVERING_COUNT.equals(attributeName)) {
  90. context.getResult().set(control.getDeliveringCount());
  91. } else if (MESSAGES_ADDED.equals(attributeName)) {
  92. context.getResult().set(control.getMessagesAdded());
  93. } else if (QUEUE_ADDRESS.getName().equals(attributeName)) {
  94. context.getResult().set(control.getAddress());
  95. } else if (EXPIRY_ADDRESS.getName().equals(attributeName)) {
  96. context.getResult().set(control.getExpiryAddress());
  97. } else if (DEAD_LETTER_ADDRESS.getName().equals(attributeName)) {
  98. context.getResult().set(control.getDeadLetterAddress());
  99. } else if (PAUSED.equals(attributeName)) {
  100. try {
  101. context.getResult().set(control.isPaused());
  102. } catch (RuntimeException e) {
  103. throw e;
  104. } catch (Exception e) {
  105. throw new RuntimeException(e);
  106. }
  107. } else if (TEMPORARY.equals(attributeName)) {
  108. context.getResult().set(control.isTemporary());
  109. } else if (METRICS.contains(attributeName) || READ_ATTRIBUTES.contains(attributeName)) {
  110. // Bug
  111. throw MESSAGES.unsupportedAttribute(attributeName);
  112. }
  113. context.completeStep();
  114. }
  115. public void registerAttributes(final ManagementResourceRegistration registration) {
  116. for (String attr : READ_ATTRIBUTES) {
  117. registration.registerReadOnlyAttribute(attr, this, AttributeAccess.Storage.RUNTIME);
  118. }
  119. for (String metric : METRICS) {
  120. registration.registerMetric(metric, this);
  121. }
  122. }
  123. }