PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/web/src/main/java/org/jboss/as/web/WebConnectorMetrics.java

#
Java | 133 lines | 97 code | 12 blank | 24 comment | 20 complexity | 75c6eb1d983e38c16750aed764585052 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, 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.web;
  23. import org.apache.catalina.connector.Connector;
  24. import org.apache.coyote.RequestGroupInfo;
  25. import org.jboss.as.controller.OperationContext;
  26. import org.jboss.as.controller.OperationFailedException;
  27. import org.jboss.as.controller.OperationStepHandler;
  28. import org.jboss.as.controller.PathAddress;
  29. import org.jboss.as.controller.SimpleAttributeDefinition;
  30. import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
  31. import org.jboss.dmr.ModelNode;
  32. import org.jboss.dmr.ModelType;
  33. import org.jboss.msc.service.ServiceController;
  34. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
  35. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  36. import static org.jboss.as.web.WebMessages.MESSAGES;
  37. /**
  38. * @author Emanuel Muckenhuber
  39. */
  40. class WebConnectorMetrics implements OperationStepHandler {
  41. static WebConnectorMetrics INSTANCE = new WebConnectorMetrics();
  42. protected static final SimpleAttributeDefinition BYTES_SENT =
  43. new SimpleAttributeDefinitionBuilder(Constants.BYTES_SENT, ModelType.INT, true)
  44. .setStorageRuntime()
  45. .build();
  46. protected static final SimpleAttributeDefinition BYTES_RECEIVED =
  47. new SimpleAttributeDefinitionBuilder(Constants.BYTES_RECEIVED, ModelType.INT, true)
  48. .setStorageRuntime()
  49. .build();
  50. protected static final SimpleAttributeDefinition PROCESSING_TIME =
  51. new SimpleAttributeDefinitionBuilder(Constants.PROCESSING_TIME, ModelType.INT, true)
  52. .setStorageRuntime()
  53. .build();
  54. protected static final SimpleAttributeDefinition ERROR_COUNT =
  55. new SimpleAttributeDefinitionBuilder(Constants.ERROR_COUNT, ModelType.INT, true)
  56. .setStorageRuntime()
  57. .build();
  58. protected static final SimpleAttributeDefinition MAX_TIME =
  59. new SimpleAttributeDefinitionBuilder(Constants.MAX_TIME, ModelType.INT, true)
  60. .setStorageRuntime()
  61. .build();
  62. protected static final SimpleAttributeDefinition REQUEST_COUNT =
  63. new SimpleAttributeDefinitionBuilder(Constants.REQUEST_COUNT, ModelType.INT, true)
  64. .setStorageRuntime()
  65. .build();
  66. @Deprecated
  67. static final String[] ATTRIBUTES_OLD = {Constants.BYTES_SENT, Constants.BYTES_RECEIVED, Constants.PROCESSING_TIME, Constants.ERROR_COUNT, Constants.MAX_TIME, Constants.REQUEST_COUNT};
  68. static final SimpleAttributeDefinition[] ATTRIBUTES = {
  69. BYTES_SENT,
  70. BYTES_RECEIVED,
  71. PROCESSING_TIME,
  72. ERROR_COUNT,
  73. MAX_TIME,
  74. REQUEST_COUNT
  75. };
  76. @Override
  77. public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
  78. if (context.isNormalServer()) {
  79. context.addStep(new OperationStepHandler() {
  80. @Override
  81. public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
  82. final PathAddress address = PathAddress.pathAddress(operation.require(OP_ADDR));
  83. final String name = address.getLastElement().getValue();
  84. final String attributeName = operation.require(NAME).asString();
  85. final ServiceController<?> controller = context.getServiceRegistry(false)
  86. .getService(WebSubsystemServices.JBOSS_WEB_CONNECTOR.append(name));
  87. if (controller != null) {
  88. try {
  89. final Connector connector = (Connector) controller.getValue();
  90. final ModelNode result = context.getResult();
  91. if (connector.getProtocolHandler() != null && connector.getProtocolHandler().getRequestGroupInfo() != null) {
  92. RequestGroupInfo info = connector.getProtocolHandler().getRequestGroupInfo();
  93. if (Constants.BYTES_SENT.equals(attributeName)) {
  94. result.set("" + info.getBytesSent());
  95. } else if (Constants.BYTES_RECEIVED.equals(attributeName)) {
  96. result.set("" + info.getBytesReceived());
  97. } else if (Constants.PROCESSING_TIME.equals(attributeName)) {
  98. result.set("" + info.getProcessingTime());
  99. } else if (Constants.ERROR_COUNT.equals(attributeName)) {
  100. result.set("" + info.getErrorCount());
  101. } else if (Constants.MAX_TIME.equals(attributeName)) {
  102. result.set("" + info.getMaxTime());
  103. } else if (Constants.REQUEST_COUNT.equals(attributeName)) {
  104. result.set("" + info.getRequestCount());
  105. }
  106. }
  107. } catch (Exception e) {
  108. throw new OperationFailedException(new ModelNode().set(MESSAGES.failedToGetMetrics(e.getMessage())));
  109. }
  110. } else {
  111. context.getResult().set(MESSAGES.noMetricsAvailable());
  112. }
  113. context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
  114. }
  115. }, OperationContext.Stage.RUNTIME);
  116. } else {
  117. context.getResult().set(MESSAGES.noMetricsAvailable());
  118. }
  119. context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
  120. }
  121. }