001/*
002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.benchmarking.common.jfr;
017
018import de.cuioss.tools.logging.CuiLogger;
019import jdk.jfr.FlightRecorder;
020
021/**
022 * Utility class for detecting and verifying JFR (Java Flight Recorder) support.
023 * <p>
024 * Provides methods to check if JFR is available, enabled, and properly configured
025 * in the current JVM runtime.
026 */
027public final class JfrSupport {
028
029    private static final CuiLogger LOGGER = new CuiLogger(JfrSupport.class);
030
031    private static final boolean JFR_AVAILABLE = checkJfrAvailability();
032
033    private JfrSupport() {
034        // Utility class
035    }
036
037    /**
038     * Checks if JFR is available in the current JVM.
039     * 
040     * @return true if JFR is available, false otherwise
041     */
042    public static boolean isAvailable() {
043        return JFR_AVAILABLE;
044    }
045
046
047    private static boolean checkJfrAvailability() {
048        try {
049            // Check if JFR classes are available
050            Class.forName("jdk.jfr.FlightRecorder");
051            Class.forName("jdk.jfr.Recording");
052            Class.forName("jdk.jfr.Event");
053
054            // Check if FlightRecorder is accessible
055            FlightRecorder.getFlightRecorder();
056
057            return true;
058        } catch (ClassNotFoundException e) {
059            LOGGER.debug("JFR classes not found: %s", e.getMessage());
060            return false;
061        } catch (SecurityException | UnsupportedOperationException | IllegalStateException e) {
062            LOGGER.debug("JFR not available: %s", e.getMessage());
063            return false;
064        }
065    }
066}