1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.util;
17
18 import java.util.Arrays;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 public final class JvmUtil {
23 private static final Pattern VERSION_PREFIX_PATTERN = Pattern.compile("([0-9]+)(\\-?):(.*)");
24
25 private JvmUtil() {
26
27 }
28
29 public static String[] filterJvmOptions(final String[] values) {
30 final int version = getJavaVersion();
31 return Arrays.stream(values).map(s -> {
32 final Matcher matcher = VERSION_PREFIX_PATTERN.matcher(s);
33 if (matcher.matches()) {
34 final int v = Integer.parseInt(matcher.group(1));
35 if ("-".equals(matcher.group(2))) {
36 if (version >= v) {
37 return matcher.group(3);
38 }
39 } else if (v == version) {
40 return matcher.group(3);
41 }
42 return null;
43 } else {
44 return s;
45 }
46 }).filter(s -> s != null).toArray(n -> new String[n]);
47 }
48
49 public static int getJavaVersion() {
50 final String javaVersion = System.getProperty("java.version");
51 int version = 8;
52 if (javaVersion != null) {
53 final String[] split = javaVersion.split("[\\._]");
54 if (split.length > 0) {
55 version = Integer.parseInt(split[0]);
56 if (version == 1 && split.length > 1) {
57 version = Integer.parseInt(split[1]);
58 }
59 }
60 }
61 return version;
62 }
63 }