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.jfr.internal.management;
  27 
  28 import java.time.Duration;
  29 import java.util.ArrayList;
  30 import java.util.Collections;
  31 import java.util.List;
  32 
  33 import jdk.jfr.EventType;
  34 import jdk.jfr.internal.JVMSupport;
  35 import jdk.jfr.internal.LogLevel;
  36 import jdk.jfr.internal.LogTag;
  37 import jdk.jfr.internal.Logger;
  38 import jdk.jfr.internal.MetadataRepository;
  39 import jdk.jfr.internal.Utils;
  40 import jdk.jfr.internal.instrument.JDKEvents;
  41 
  42 /**
  43  * The management API in module jdk.management.jfr should be built on top of the
  44  * public API in jdk.jfr. Before putting more functionality here, consider if it
  45  * should not be part of the public API, and if not, please provide motivation
  46  *
  47  */
  48 public final class ManagementSupport {
  49 
  50     // Purpose of this method is to expose the event types to the
  51     // FlightRecorderMXBean without instantiating Flight Recorder.
  52     //
  53     // This allows:
  54     //
  55     // 1) discoverability, so event settings can be exposed without the need to
  56     // create a new Recording in FlightrecorderMXBean.
  57     //
  58     // 2) a graphical JMX client to list all attributes to the user, without
  59     // loading JFR memory buffers. This is especially important when there is
  60     // no intent to use Flight Recorder.
  61     //
  62     // An alternative design would be to make FlightRecorder#getEventTypes
  63     // static, but it would the make the API look strange
  64     //
  65     public static List<EventType> getEventTypes() {
  66         // would normally be checked when a Flight Recorder instance is created
  67         Utils.checkAccessFlightRecorder();
  68         if (JVMSupport.isNotAvailable()) {
  69             return new ArrayList<>();
  70         }
  71         JDKEvents.initialize(); // make sure JDK events are available
  72         return Collections.unmodifiableList(MetadataRepository.getInstance().getRegisteredEventTypes());
  73     }
  74 
  75     // Reuse internal code for parsing a timespan
  76     public static long parseTimespan(String s) {
  77         return Utils.parseTimespan(s);
  78     }
  79 
  80     // Reuse internal code for formatting settings
  81     public static final String formatTimespan(Duration dValue, String separation) {
  82         return Utils.formatTimespan(dValue, separation);
  83     }
  84 
  85     // Reuse internal logging mechanism
  86     public static void logError(String message) {
  87         Logger.log(LogTag.JFR, LogLevel.ERROR, message);
  88     }
  89 }