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 return s;
35 }
36 final int v = Integer.parseInt(matcher.group(1));
37 if ("-".equals(matcher.group(2))) {
38 if (version >= v) {
39 return matcher.group(3);
40 }
41 } else if (v == version) {
42 return matcher.group(3);
43 }
44 return null;
45 }).filter(s -> s != null).toArray(n -> new String[n]);
46 }
47
48 public static int getJavaVersion() {
49 final String javaVersion = System.getProperty("java.version");
50 int version = 8;
51 if (javaVersion != null) {
52 final String[] split = javaVersion.split("[\\._]");
53 if (split.length > 0) {
54 version = Integer.parseInt(split[0]);
55 if (version == 1 && split.length > 1) {
56 version = Integer.parseInt(split[1]);
57 }
58 }
59 }
60 return version;
61 }
62 }