--- /dev/null 2017-11-09 09:38:01.297999907 +0100 +++ new/src/jdk.management.jfr/share/classes/jdk/management/jfr/internal/FlightRecorderMXBeanProvider.java 2018-04-09 16:28:22.160072963 +0200 @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.management.jfr.internal; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; + +import jdk.jfr.internal.management.ManagementSupport; +import jdk.management.jfr.FlightRecorderMXBean; +import jdk.management.jfr.SettingDescriptorInfo; +import sun.management.spi.PlatformMBeanProvider; + +public final class FlightRecorderMXBeanProvider extends PlatformMBeanProvider { + + private final static class SingleMBeanComponent + implements PlatformComponent { + private final String objectName; + private final Class mbeanInterface; + + public SingleMBeanComponent(String objectName, + Class mbeanInterface) { + this.objectName = objectName; + this.mbeanInterface = mbeanInterface; + } + + @Override + public Set mbeanInterfaceNames() { + return Collections.singleton(mbeanInterface.getName()); + } + + @Override + public Map nameToMBeanMap() { + FlightRecorderMXBean bean = getFlightRecorderMXBean(); + if (bean != null) { + return Collections.singletonMap(objectName, bean); + } else { + return Collections.emptyMap(); + } + } + + @Override + public String getObjectNamePattern() { + return objectName; + } + + @Override + public Set> mbeanInterfaces() { + return Collections.singleton(mbeanInterface); + } + } + + private static Callable flightRecorderMXBeanFactory; + + private static volatile FlightRecorderMXBean flightRecorderMXBean; + + private static FlightRecorderMXBean getFlightRecorderMXBean() { + FlightRecorderMXBean bean = flightRecorderMXBean; + if (bean == null) { + SettingDescriptorInfo.from(null); // Sets flightRecorderMXBeanFactory under lock + synchronized (flightRecorderMXBeanFactory) { + bean = flightRecorderMXBean; + if (bean != null) { + return bean; + } + try { + bean = flightRecorderMXBean = flightRecorderMXBeanFactory.call(); + } catch (Exception e) { + ManagementSupport.logError("Could not create Flight Recorder " + + "instance for MBeanServer. " + e.getMessage()); + } + } + } + return bean; + } + + public static void setFlightRecorderMXBeanFactory(Callable factory) { + flightRecorderMXBeanFactory = factory; + } + + @Override + public List> getPlatformComponentList() { + String objectName = FlightRecorderMXBean.MXBEAN_NAME; + Class mbeanInterface = FlightRecorderMXBean.class; + return Collections.singletonList(new SingleMBeanComponent(objectName, mbeanInterface)); + } +}