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