1 /*
   2  * Copyright (c) 2016, 2019, 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 package jdk.management.jfr;
  26 
  27 import java.lang.management.ManagementPermission;
  28 import java.security.Permission;
  29 import java.time.DateTimeException;
  30 import java.time.Duration;
  31 import java.time.Instant;
  32 import java.time.format.DateTimeParseException;
  33 import java.util.List;
  34 import java.util.function.Function;
  35 import java.util.stream.Collectors;
  36 
  37 import javax.management.MalformedObjectNameException;
  38 import javax.management.ObjectName;
  39 
  40 import jdk.jfr.internal.management.ManagementSupport;
  41 
  42 final class MBeanUtils {
  43 
  44     private static final Permission monitor = new ManagementPermission("monitor");
  45     private static final Permission control = new ManagementPermission("control");
  46 
  47     static ObjectName createObjectName() {
  48         try {
  49             return new ObjectName(FlightRecorderMXBean.MXBEAN_NAME);
  50         } catch (MalformedObjectNameException mne) {
  51             throw new Error("Can't happen", mne);
  52         }
  53     }
  54 
  55     static void checkControl() {
  56         SecurityManager secManager = System.getSecurityManager();
  57         if (secManager != null) {
  58             secManager.checkPermission(control);
  59         }
  60     }
  61 
  62     static void checkMonitor() {
  63         SecurityManager secManager = System.getSecurityManager();
  64         if (secManager != null) {
  65             secManager.checkPermission(monitor);
  66         }
  67     }
  68 
  69     static <T, R> List<R> transformList(List<T> source, Function<T, R> function) {
  70         return source.stream().map(function).collect(Collectors.toList());
  71     }
  72 
  73     static boolean booleanValue(String s) {
  74         if ("true".equals(s)) {
  75             return true;
  76         }
  77         if ("false".equals(s)) {
  78             return false;
  79         }
  80         throw new IllegalArgumentException("Value must be true or false.");
  81     }
  82 
  83     static Duration duration(String s) throws NumberFormatException {
  84         if (s == null) {
  85             return null;
  86         }
  87         long l = ManagementSupport.parseTimespan(s);
  88         if (l == 0) {
  89             return null;
  90         }
  91         return Duration.ofNanos(l);
  92     }
  93 
  94     public static Instant parseTimestamp(String s, Instant defaultValue) {
  95         if (s == null) {
  96             return defaultValue;
  97         }
  98         try {
  99             return Instant.parse(s);
 100         } catch(DateTimeParseException e ) {
 101             // OK, try with milliseconds since epoch
 102             // before giving up.
 103         }
 104         try {
 105             return Instant.ofEpochMilli(Long.parseLong(s));
 106         } catch (NumberFormatException | DateTimeException nfr) {
 107             throw new IllegalArgumentException("Not a valid timestamp " + s);
 108         }
 109     }
 110 
 111     static Long size(String s) throws NumberFormatException {
 112         long size = Long.parseLong(s);
 113         if (size < 0) {
 114             throw new IllegalArgumentException("Negative size not allowed");
 115         }
 116         return size;
 117     }
 118 
 119     public static int parseBlockSize(String string, int defaultSize) {
 120         if (string == null) {
 121             return defaultSize;
 122         }
 123         int size = Integer.parseInt(string);
 124         if (size <1)  {
 125             throw new IllegalArgumentException("Block size msut be at least 1 byte");
 126         }
 127         return size;
 128     }
 129 }
 130