Shinsuke Sugaya 6 лет назад
Родитель
Сommit
0dbeb2c09e

+ 4 - 3
src/main/java/org/codelibs/fess/mylasta/direction/FessProp.java

@@ -55,6 +55,7 @@ import org.codelibs.fess.helper.PermissionHelper;
 import org.codelibs.fess.mylasta.action.FessUserBean;
 import org.codelibs.fess.taglib.FessFunctions;
 import org.codelibs.fess.util.ComponentUtil;
+import org.codelibs.fess.util.JvmUtil;
 import org.codelibs.fess.util.PrunedTag;
 import org.dbflute.optional.OptionalThing;
 import org.elasticsearch.search.sort.SortBuilder;
@@ -679,19 +680,19 @@ public interface FessProp {
     String getJvmCrawlerOptions();
 
     default String[] getJvmCrawlerOptionsAsArray() {
-        return getJvmCrawlerOptions().split("\n");
+        return JvmUtil.filterJvmOptions(getJvmCrawlerOptions().split("\n"));
     }
 
     String getJvmSuggestOptions();
 
     default String[] getJvmSuggestOptionsAsArray() {
-        return getJvmSuggestOptions().split("\n");
+        return JvmUtil.filterJvmOptions(getJvmSuggestOptions().split("\n"));
     }
 
     String getJvmThumbnailOptions();
 
     default String[] getJvmThumbnailOptionsAsArray() {
-        return getJvmThumbnailOptions().split("\n");
+        return JvmUtil.filterJvmOptions(getJvmThumbnailOptions().split("\n"));
     }
 
     String getCrawlerDocumentHtmlPrunedTags();

+ 63 - 0
src/main/java/org/codelibs/fess/util/JvmUtil.java

@@ -0,0 +1,63 @@
+/*
+ * Copyright 2012-2019 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.util;
+
+import java.util.Arrays;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public final class JvmUtil {
+    private static final Pattern VERSION_PREFIX_PATTERN = Pattern.compile("([0-9]+)(\\-?):(.*)");
+
+    private JvmUtil() {
+        // nothing
+    }
+
+    public static String[] filterJvmOptions(final String[] values) {
+        final int version = getJavaVersion();
+        return Arrays.stream(values).map(s -> {
+            final Matcher matcher = VERSION_PREFIX_PATTERN.matcher(s);
+            if (matcher.matches()) {
+                final int v = Integer.parseInt(matcher.group(1));
+                if ("-".equals(matcher.group(2))) {
+                    if (version >= v) {
+                        return matcher.group(3);
+                    }
+                } else if (v == version) {
+                    return matcher.group(3);
+                }
+                return null;
+            } else {
+                return s;
+            }
+        }).filter(s -> s != null).toArray(n -> new String[n]);
+    }
+
+    public static int getJavaVersion() {
+        final String javaVersion = System.getProperty("java.version");
+        int version = 8;
+        if (javaVersion != null) {
+            final String[] split = javaVersion.split("[\\._]");
+            if (split.length > 0) {
+                version = Integer.parseInt(split[0]);
+                if (version == 1 && split.length > 1) {
+                    version = Integer.parseInt(split[1]);
+                }
+            }
+        }
+        return version;
+    }
+}

+ 69 - 0
src/test/java/org/codelibs/fess/util/JvmUtilTest.java

@@ -0,0 +1,69 @@
+/*
+ * Copyright 2012-2019 CodeLibs Project and the Others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific language
+ * governing permissions and limitations under the License.
+ */
+package org.codelibs.fess.util;
+
+import org.codelibs.fess.unit.UnitFessTestCase;
+
+public class JvmUtilTest extends UnitFessTestCase {
+    public void test_getJavaVersion() {
+        System.setProperty("java.version", "1.4.2_19");
+        assertEquals(4, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "1.5.0_15");
+        assertEquals(5, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "1.6.0_34");
+        assertEquals(6, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "1.7.0_25");
+        assertEquals(7, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "1.8.0_171");
+        assertEquals(8, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "9.0.4");
+        assertEquals(9, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "10.0.1");
+        assertEquals(10, JvmUtil.getJavaVersion());
+        System.setProperty("java.version", "11.0.1");
+        assertEquals(11, JvmUtil.getJavaVersion());
+    }
+
+    public void test_filterJvmOptions() {
+        final String[] args = new String[] { //
+                "-X111", //
+                        "8:-X222", //
+                        "10:-X333", //
+                        "11:-X444", //
+                        "8-:-X555", //
+                        "10-:-X666", //
+                        "11-:-X777", //
+                        "12-:-X888", //
+                        "-X999",//
+                };
+
+        System.setProperty("java.version", "1.8.0_171");
+        String[] values = JvmUtil.filterJvmOptions(args);
+        assertEquals("-X111", values[0]);
+        assertEquals("-X222", values[1]);
+        assertEquals("-X555", values[2]);
+        assertEquals("-X999", values[3]);
+
+        System.setProperty("java.version", "11.0.1");
+        values = JvmUtil.filterJvmOptions(args);
+        assertEquals("-X111", values[0]);
+        assertEquals("-X444", values[1]);
+        assertEquals("-X555", values[2]);
+        assertEquals("-X666", values[3]);
+        assertEquals("-X777", values[4]);
+        assertEquals("-X999", values[5]);
+    }
+}