Path: blob/master/src/java.management/share/classes/sun/management/spi/PlatformMBeanProvider.java
41154 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management.spi;2627import java.util.Collections;28import java.util.List;29import java.util.Map;30import java.util.ServiceLoader;31import java.util.Set;32import java.util.stream.Collectors;3334/**35* The PlatformMBeanProvider class defines the abstract service interface36* that the {@link java.lang.management.ManagementFactory} will invoke to find,37* load, and register Platform MBeans.38*39* ManagementFactory loads the {@linkplain ServiceLoader#loadInstalled(java.lang.Class)40* installed providers} of this service interface and each provides the41* {@linkplain PlatformComponent platform components} that defines MXBean42* or DynamicMBean to be registered in the platform MBeanServer.43*44* A {@code PlatformMBeanProvider} will implement the {@code getPlatformComponentList()}45* method to return the list of {@code PlatformComponents} it provides.46*/47public abstract class PlatformMBeanProvider {48/**49* {@code PlatformComponent} models MBeans of a management interface supported50* by the platform.51*52* If a PlatformComponent models a singleton MBean, the {@link #getObjectNamePattern()53* ObjectName pattern} must be the {@link54* javax.management.ObjectName#getCanonicalName() canonical name} of that55* singleton MBean. Otherwise, it must be an ObjectName pattern56* that can be used to query the MBeans for this57* PlatformComponent registered in a {@code MBeanServer}.58* <br>59* The {@link #getObjectNamePattern() ObjectName pattern} serves as a unique60* key for identifying the instance of PlatformComponent. It is thus illegal61* for a given {@link PlatformMBeanProvider} to export several instance of62* PlatformComponent with the same63* {@link #getObjectNamePattern() ObjectName pattern} string.64* <br>65* If two different provider instances export a PlatformComponent for the66* same ObjectName pattern, only the PlatformComponent instance of the first67* provider will be taken into account.68*69* @param <T> The higher level interface for which the MBeans modeled by70* this object should be recognized. For instance, for the {@link71* java.lang.management.ManagementFactory#getOperatingSystemMXBean()72* Operating System MXBean}, this should be {@link73* java.lang.management.OperatingSystemMXBean74* java.lang.management.OperatingSystemMXBean}.75*/76public interface PlatformComponent<T> {77/**78* Returns the names of the management interfaces implemented by the79* MBeans modeled by this {@code PlatformComponent}.80*81* @implNote82* When {@link java.lang.management.ManagementFactory#getPlatformMXBean(java.lang.Class)83* ManagementFactory.getPlatformMXBean(mxbeanInterface)} or {@link84* java.lang.management.ManagementFactory#getPlatformMXBeans(java.lang.Class)85* ManagementFactory.getPlatformMXBeans(mxbeanInterface)} are invoked,86* this PlatformComponent instance will match only if the name of the87* given {@code mxbeanInterface} is found in this list.88*89* @return the names of the management interfaces exported by the MBeans90* modeled by this object.91*/92public Set<String> mbeanInterfaceNames();9394/**95* A map from ObjectName string to the MBean instance this96* {@code PlatformComponent} creates.97*98* @implNote99* If {@link #shouldRegister()} is {@code true}, this method100* will be called when the {@link java.lang.management.ManagementFactory101* #getPlatformMBeanServer() Platform MBeanServer} is initialized.102* By default, this method will also be called by {@link103* #getMBeans(java.lang.Class)}, when {@link104* java.lang.management.ManagementFactory#getPlatformMXBean(java.lang.Class)105* ManagementFactory.getPlatformMXBean(mxbeanInterface)} or {@link106* java.lang.management.ManagementFactory#getPlatformMXBeans(java.lang.Class)107* ManagementFactory.getPlatformMXBeans(mxbeanInterface)} are invoked,108* and when the name of the given {@code mxbeanInterface} is contained109* in the names of management interfaces returned by {@link110* #mbeanInterfaceNames()}.111*112* @return A map with, for each MBean, the ObjectName string as key113* and the MBean as value.114*/115public Map<String, T> nameToMBeanMap();116117/**118* An ObjectName pattern uniquely identifies the MBeans119* modeled by this {@code PlatformComponent}.120* If this instance models a singleton MBean, this must be121* the {@link122* javax.management.ObjectName#getCanonicalName() canonical name}123* of that singleton MBean.124*125* @return An ObjectName pattern uniquely identifies the MBeans126* modeled by this instance.127*/128public String getObjectNamePattern();129130/**131* Returns {@code true} if this {@code PlatformComponent} models132* a singleton MBean. By default, {@code true} is assumed.133*134* @return {@code true} if this instance models a singleton MBean.135*/136public default boolean isSingleton() {137return true;138}139140/**141* Returns {@code true} if the MBeans modeled by this {@code PlatformComponent}142* should automatically be registered in the {@link143* java.lang.management.ManagementFactory#getPlatformMBeanServer()144* Platform MBeanServer}. By default, {@code true} is assumed.145*146* @return {@code true} if the MBeans modeled by this instance should147* automatically be registered in the Platform MBeanServer.148*/149public default boolean shouldRegister() {150return true;151}152153/**154* The set of interfaces implemented by the MBeans modeled155* by this {@code PlatformComponent}.156*157* @implNote158* {@link java.lang.management.ManagementFactory#getPlatformManagementInterfaces()159* ManagementFactory.getPlatformManagementInterfaces()} calls this160* method to find the management interfaces supported by the platform.161*162* @return The set of interfaces implemented by the MBeans modeled163* by this instance164*/165public Set<Class<? extends T>> mbeanInterfaces();166167/**168* Return the list of MBeans that implement the given {@code mbeanIntf}169* modeled by this {@code PlatformComponent}. This method returns an170* empty list if no MBean implements the given {@code mbeanIntf}.171*172* @implNote This method will be called when {@link173* java.lang.management.ManagementFactory#getPlatformMXBean(java.lang.Class)174* ManagementFactory.getPlatformMXBean(mbeanIntf)} or {@link175* java.lang.management.ManagementFactory#getPlatformMXBeans(java.lang.Class)176* ManagementFactory.getPlatformMXBeans(mbeanIntf)} are invoked.177* By default it first checks whether the specified {@code mbeanIntf}178* name is contained in the returned list from the {@link #mbeanInterfaceNames()}179* method. If yes, it proceeds and calls180* {@link #mbeans().values()} and filters out all181* MBeans which are not instances of the given {@code mbeanIntf}.182* Otherwise, it returns an empty list.183*184* @param mbeanIntf A management interface.185* @return A (possibly empty) list of MBeans implementing the given186* {@code mbeanIntf}.187*/188public default <I> List<? extends I> getMBeans(Class<I> mbeanIntf) {189List<I> list;190191if (!mbeanInterfaceNames().contains(mbeanIntf.getName())) {192list = Collections.emptyList();193} else {194list = nameToMBeanMap().values().stream()195.filter(mbeanIntf::isInstance)196.map(mbeanIntf::cast)197.collect(Collectors.toList());198}199return list;200}201}202203/**204* Instantiates a new PlatformMBeanProvider.205*206* @throws SecurityException if the subclass (and calling code) does not207* have {@code RuntimePermission("sun.management.spi.PlatformMBeanProvider.subclass")}208*/209protected PlatformMBeanProvider () {210this(checkSubclassPermission());211}212213private PlatformMBeanProvider(Void unused) {214}215216/**217* Returns a list of PlatformComponent instances describing the Platform218* MBeans provided by this provider.219*220* @return a list of PlatformComponent instances describing the Platform221* MBeans provided by this provider.222*/223public abstract List<PlatformComponent<?>> getPlatformComponentList();224225private static Void checkSubclassPermission() {226@SuppressWarnings("removal")227SecurityManager sm = System.getSecurityManager();228if (sm != null) {229sm.checkPermission(new RuntimePermission(PlatformMBeanProvider.class.getName()+".subclass"));230}231return null;232}233}234235236