View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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          // nothing
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  }