src/jdk.management.jfr/share/classes/jdk/management/jfr/MBeanUtils.java
author kevinw
Thu, 31 Jan 2019 04:49:46 -0800
changeset 53591 bf4c38b9afaf
parent 50745 a390cbb82d47
child 58480 8ca46e186a63
permissions -rw-r--r--
8209951: Problematic sparc intrinsic: com.sun.crypto.provider.CipherBlockChaining Reviewed-by: kvn, thartmann Contributed-by: fairoz.matte@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
package jdk.management.jfr;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
import java.lang.management.ManagementPermission;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
import java.security.Permission;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
import java.time.DateTimeException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
import java.time.Duration;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
import java.time.Instant;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
import java.time.format.DateTimeParseException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
import java.util.List;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
import java.util.function.Function;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
import java.util.stream.Collectors;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
import javax.management.MalformedObjectNameException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
import javax.management.ObjectName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
import jdk.jfr.internal.management.ManagementSupport;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
final class MBeanUtils {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
    private static final Permission monitor = new ManagementPermission("monitor");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
    private static final Permission control = new ManagementPermission("control");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
    static ObjectName createObjectName() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
            return new ObjectName(FlightRecorderMXBean.MXBEAN_NAME);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
        } catch (MalformedObjectNameException mne) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
            throw new Error("Can't happen", mne);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
    static void checkControl() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
        SecurityManager secManager = System.getSecurityManager();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
        if (secManager != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
            secManager.checkPermission(control);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
    static void checkMonitor() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
        SecurityManager secManager = System.getSecurityManager();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
        if (secManager != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
            secManager.checkPermission(monitor);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
    static <T, R> List<R> transformList(List<T> source, Function<T, R> function) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
        return source.stream().map(function).collect(Collectors.toList());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
    static boolean booleanValue(String s) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
        if ("true".equals(s)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
            return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
        if ("false".equals(s)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
            return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
        throw new IllegalArgumentException("Value must be true or false.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
    static Duration duration(String s) throws NumberFormatException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
        if (s == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
            return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
        long l = ManagementSupport.parseTimespan(s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
        if (l == 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
            return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
        return Duration.ofNanos(l);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
    public static Instant parseTimestamp(String s, Instant defaultValue) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
        if (s == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
            return defaultValue;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
            return Instant.parse(s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
        } catch(DateTimeParseException e ) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
            // OK, try with milliseconds since epoch
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
            // before giving up.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
            return Instant.ofEpochMilli(Long.parseLong(s));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
        } catch (NumberFormatException | DateTimeException nfr) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
            throw new IllegalArgumentException("Not a valid timestamp " + s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
    static Long size(String s) throws NumberFormatException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
        long size = Long.parseLong(s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
        if (size < 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
            throw new IllegalArgumentException("Negative size not allowed");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
        return size;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
    public static int parseBlockSize(String string, int defaultSize) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
        if (string == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
            return defaultSize;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
        int size = Integer.parseInt(string);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
        if (size <1)  {
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   125
            throw new IllegalArgumentException("Block size must be at least 1 byte");
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
        return size;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130