PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/platform-mbean/src/main/java/org/jboss/as/platform/mbean/ThreadMXBeanThreadInfoHandler.java

#
Java | 83 lines | 46 code | 11 blank | 26 comment | 4 complexity | 1f95b027e4ac442f6864330b86b5048e 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.platform.mbean;
  23. import java.lang.management.ManagementFactory;
  24. import java.lang.management.ThreadInfo;
  25. import java.lang.management.ThreadMXBean;
  26. import java.util.Locale;
  27. import org.jboss.as.controller.OperationContext;
  28. import org.jboss.as.controller.OperationFailedException;
  29. import org.jboss.as.controller.OperationStepHandler;
  30. import org.jboss.as.controller.descriptions.DescriptionProvider;
  31. import org.jboss.as.controller.operations.validation.IntRangeValidator;
  32. import org.jboss.as.controller.operations.validation.LongRangeValidator;
  33. import org.jboss.as.controller.operations.validation.ParametersValidator;
  34. import org.jboss.dmr.ModelNode;
  35. /**
  36. * Executes the {@link java.lang.management.ThreadMXBean} {@code getThreadInfo} methods that return a single thread id.
  37. *
  38. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  39. */
  40. public class ThreadMXBeanThreadInfoHandler implements OperationStepHandler, DescriptionProvider {
  41. public static final ThreadMXBeanThreadInfoHandler INSTANCE = new ThreadMXBeanThreadInfoHandler();
  42. private final ParametersValidator validator = new ParametersValidator();
  43. private ThreadMXBeanThreadInfoHandler() {
  44. validator.registerValidator(PlatformMBeanConstants.ID, new LongRangeValidator(1));
  45. validator.registerValidator(PlatformMBeanConstants.MAX_DEPTH, new IntRangeValidator(1, Integer.MAX_VALUE, true, false));
  46. }
  47. @Override
  48. public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
  49. validator.validate(operation);
  50. ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  51. try {
  52. long id = operation.require(PlatformMBeanConstants.ID).asLong();
  53. ThreadInfo info;
  54. if (operation.hasDefined(PlatformMBeanConstants.MAX_DEPTH)) {
  55. info = mbean.getThreadInfo(id, operation.require(PlatformMBeanConstants.MAX_DEPTH).asInt());
  56. } else {
  57. info = mbean.getThreadInfo(id);
  58. }
  59. final ModelNode result = context.getResult();
  60. if (info != null) {
  61. result.set(PlatformMBeanUtil.getDetypedThreadInfo(info, mbean.isThreadCpuTimeSupported()));
  62. }
  63. } catch (SecurityException e) {
  64. throw new OperationFailedException(new ModelNode().set(e.toString()));
  65. }
  66. context.completeStep();
  67. }
  68. @Override
  69. public ModelNode getModelDescription(Locale locale) {
  70. return PlatformMBeanDescriptions.getGetThreadInfoDescription(locale);
  71. }
  72. }