1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.management.jfr.internal;
  27 
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import java.util.concurrent.Callable;
  33 
  34 import jdk.jfr.internal.management.ManagementSupport;
  35 import jdk.management.jfr.FlightRecorderMXBean;
  36 import jdk.management.jfr.SettingDescriptorInfo;
  37 import sun.management.spi.PlatformMBeanProvider;
  38 
  39 public final class FlightRecorderMXBeanProvider extends PlatformMBeanProvider {
  40 
  41     private final static class SingleMBeanComponent
  42             implements PlatformComponent<FlightRecorderMXBean> {
  43         private final String objectName;
  44         private final Class<FlightRecorderMXBean> mbeanInterface;
  45 
  46         public SingleMBeanComponent(String objectName,
  47                                     Class<FlightRecorderMXBean> mbeanInterface) {
  48             this.objectName = objectName;
  49             this.mbeanInterface = mbeanInterface;
  50         }
  51 
  52         @Override
  53         public Set<String> mbeanInterfaceNames() {
  54             return Collections.singleton(mbeanInterface.getName());
  55         }
  56 
  57         @Override
  58         public Map<String, FlightRecorderMXBean> nameToMBeanMap() {
  59             FlightRecorderMXBean bean = getFlightRecorderMXBean();
  60             if (bean != null) {
  61                 return Collections.singletonMap(objectName, bean);
  62             } else {
  63                 return Collections.emptyMap();
  64             }
  65         }
  66 
  67         @Override
  68         public String getObjectNamePattern() {
  69             return objectName;
  70         }
  71 
  72         @Override
  73         public Set<Class<? extends FlightRecorderMXBean>> mbeanInterfaces() {
  74             return Collections.singleton(mbeanInterface);
  75         }
  76     }
  77 
  78     private static Callable<FlightRecorderMXBean> flightRecorderMXBeanFactory;
  79 
  80     private static volatile FlightRecorderMXBean flightRecorderMXBean;
  81 
  82     private static FlightRecorderMXBean getFlightRecorderMXBean() {
  83         FlightRecorderMXBean bean = flightRecorderMXBean;
  84         if (bean == null) {
  85             SettingDescriptorInfo.from(null); // Sets flightRecorderMXBeanFactory under <clinit> lock
  86             synchronized (flightRecorderMXBeanFactory) {
  87                 bean = flightRecorderMXBean;
  88                 if (bean != null) {
  89                     return bean;
  90                 }
  91                 try {
  92                     bean = flightRecorderMXBean = flightRecorderMXBeanFactory.call();
  93                 } catch (Exception e) {
  94                     ManagementSupport.logError("Could not create Flight Recorder "
  95                             + "instance for MBeanServer. " + e.getMessage());
  96                 }
  97             }
  98         }
  99         return bean;
 100     }
 101 
 102     public static void setFlightRecorderMXBeanFactory(Callable<FlightRecorderMXBean> factory) {
 103         flightRecorderMXBeanFactory = factory;
 104     }
 105 
 106     @Override
 107     public List<PlatformComponent<?>> getPlatformComponentList() {
 108         String objectName = FlightRecorderMXBean.MXBEAN_NAME;
 109         Class<FlightRecorderMXBean> mbeanInterface = FlightRecorderMXBean.class;
 110         return Collections.singletonList(new SingleMBeanComponent(objectName, mbeanInterface));
 111     }
 112 }