PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 111 lines | 62 code | 20 blank | 29 comment | 23 complexity | 8bed30506938b9430decdc94a56ef264 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 org.jboss.as.controller.OperationContext;
  25. import org.jboss.as.controller.OperationFailedException;
  26. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  27. import org.jboss.as.controller.registry.AttributeAccess;
  28. import org.jboss.as.controller.registry.ManagementResourceRegistration;
  29. import org.jboss.dmr.ModelNode;
  30. /**
  31. * Handles read-attribute and write-attribute for the resource representing {@link java.lang.management.OperatingSystemMXBean}.
  32. *
  33. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  34. */
  35. public class OperatingSystemMXBeanAttributeHandler extends AbstractPlatformMBeanAttributeHandler {
  36. public static OperatingSystemMXBeanAttributeHandler INSTANCE = new OperatingSystemMXBeanAttributeHandler();
  37. private OperatingSystemMXBeanAttributeHandler() {
  38. }
  39. @Override
  40. protected void executeReadAttribute(OperationContext context, ModelNode operation) throws OperationFailedException {
  41. final String name = operation.require(ModelDescriptionConstants.NAME).asString();
  42. try {
  43. if ((PlatformMBeanUtil.JVM_MAJOR_VERSION > 6 && PlatformMBeanConstants.OBJECT_NAME.equals(name))
  44. || PlatformMBeanConstants.OPERATING_SYSTEM_READ_ATTRIBUTES.contains(name)
  45. || PlatformMBeanConstants.OPERATING_SYSTEM_METRICS.contains(name)) {
  46. storeResult(name, context.getResult());
  47. } else {
  48. // Shouldn't happen; the global handler should reject
  49. throw unknownAttribute(operation);
  50. }
  51. } catch (SecurityException e) {
  52. throw new OperationFailedException(new ModelNode().set(e.toString()));
  53. }
  54. }
  55. @Override
  56. protected void executeWriteAttribute(OperationContext context, ModelNode operation) throws OperationFailedException {
  57. // Shouldn't happen; the global handler should reject
  58. throw unknownAttribute(operation);
  59. }
  60. @Override
  61. protected void register(ManagementResourceRegistration registration) {
  62. if (PlatformMBeanUtil.JVM_MAJOR_VERSION > 6) {
  63. registration.registerReadOnlyAttribute(PlatformMBeanConstants.OBJECT_NAME, this, AttributeAccess.Storage.RUNTIME);
  64. }
  65. for (String attribute : PlatformMBeanConstants.OPERATING_SYSTEM_READ_ATTRIBUTES) {
  66. registration.registerReadOnlyAttribute(attribute, this, AttributeAccess.Storage.RUNTIME);
  67. }
  68. for (String attribute : PlatformMBeanConstants.OPERATING_SYSTEM_METRICS) {
  69. registration.registerMetric(attribute, this);
  70. }
  71. }
  72. static void storeResult(final String name, final ModelNode store) {
  73. if (PlatformMBeanUtil.JVM_MAJOR_VERSION > 6 && PlatformMBeanConstants.OBJECT_NAME.equals(name)) {
  74. store.set(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
  75. } else if (ModelDescriptionConstants.NAME.equals(name)) {
  76. store.set(ManagementFactory.getOperatingSystemMXBean().getName());
  77. } else if (PlatformMBeanConstants.ARCH.equals(name)) {
  78. store.set(ManagementFactory.getOperatingSystemMXBean().getArch());
  79. } else if (PlatformMBeanConstants.VERSION.equals(name)) {
  80. store.set(ManagementFactory.getOperatingSystemMXBean().getVersion());
  81. } else if (PlatformMBeanConstants.AVAILABLE_PROCESSORS.equals(name)) {
  82. store.set(ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors());
  83. } else if (PlatformMBeanConstants.SYSTEM_LOAD_AVERAGE.equals(name)) {
  84. store.set(ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage());
  85. } else if (PlatformMBeanConstants.OPERATING_SYSTEM_READ_ATTRIBUTES.contains(name)
  86. || PlatformMBeanConstants.OPERATING_SYSTEM_METRICS.contains(name)) {
  87. // Bug
  88. throw PlatformMBeanMessages.MESSAGES.badReadAttributeImpl8(name);
  89. }
  90. }
  91. }