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