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