<commit_before>/*
 * NOTE: This file is in part based on code from Mort Bay Consulting.
 * The original license statement is also included below.
 *
 * eXist-db Open Source Native XML Database
 * Copyright (C) 2001 The eXist-db Authors
 *
 * <EMAIL>
 * http://www.exist-db.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * ---------------------------------------------------------------------
 *
 * Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
 *
 * 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.exist.start;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * This is an adopted version of the corresponding classes shipped
 * with Jetty. Modified for eXist-db!
 *
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (<EMAIL>)
 * @version $Revision$
 *          TODO:
 *          - finish possible jetty.home locations
 *          - better handling of errors (i.e. when jetty.home cannot be autodetected...)
 *          - include entries from lib _when needed_
 */
public class Main {
    public static final String STANDARD_ENABLED_JETTY_CONFIGS = "standard.enabled-jetty-configs";
    public static final String STANDALONE_ENABLED_JETTY_CONFIGS = "standalone.enabled-jetty-configs";

    private static final int ERROR_CODE_GENERAL = 1;
    private static final int ERROR_CODE_NO_JETTY_CONFIG = 7;
    private static final int ERROR_CODE_RELIABLE_JAVA_DETECTED = 13;

    public static final String CONFIG_DIR_NAME = "etc";

    private static final String PROP_EXIST_START_DEBUG = "exist.start.debug";
    public static final String PROP_EXIST_JETTY_CONFIG = "exist.jetty.config";
    public static final String PROP_EXIST_HOME = "exist.home";
    public static final String PROP_JETTY_HOME = "jetty.home";
    private static final String PROP_LOG4J_CONFIGURATION_FILE = "log4j.configurationFile";
    private static final String PROP_JUL_MANAGER = "java.util.logging.manager";
    private static final String PROP_JAVA_TEMP_DIR = "java.io.tmpdir";

    public static final String ENV_EXIST_JETTY_CONFIG = "EXIST_JETTY_CONFIG";
    public static final String ENV_EXIST_HOME = "EXIST_HOME";
    public static final String ENV_JETTY_HOME = "JETTY_HOME";


    private static Main exist;

    private String _mode = "jetty";
    private boolean _debug = Boolean.getBoolean(PROP_EXIST_START_DEBUG);

    public static void main(final String[] args) {
        try {
            getMain().run(args);

        } catch (final Exception e) {
            e.printStackTrace();
            System.exit(ERROR_CODE_GENERAL);
        }
    }

    /**
     * Singleton Factory Method
     *
     * @return instance of Main class.
     */
    public static Main getMain() {
        if (exist == null) {
            exist = new Main();
        }
        return exist;
    }

    public String getMode() {
        return this._mode;
    }

    private Main() {
    }

    public Main(final String mode) {
        this._mode = mode;
    }

    private static Path getDirectory(final String name) {
        try {
            if (name != null) {
                final Path dir = Paths.get(name).normalize().toAbsolutePath();
                if (Files.isDirectory(dir)) {
                    return dir;
                }
            }
        } catch (final InvalidPathException e) {
            // NOP
        }
        return null;
    }

    private static void invokeMain(final ClassLoader classloader, final String classname, final String[] args)
            throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException, ClassNotFoundException {

        final Class<?> invoked_class = classloader.loadClass(classname);

        final Class<?>[] method_param_types = new Class[1];
        method_param_types[0] = args.getClass();

        final Method main = invoked_class.getDeclaredMethod("main", method_param_types);

        final Object[] method_params = new Object[1];
        method_params[0] = args;
        main.invoke(null, method_params);
    }

    public void run(final String[] args) {
        try {
            runEx(args);
        } catch (final StartException e) {
            System.exit(e.getErrorCode());
        }
    }

    private void checkForCorrectJavaVersions() throws StartException {
        final String jreVersion = System.getProperty("java.version", "UNKNOWN");
        if (!jreVersion.startsWith("1.8") && !jreVersion.startsWith("9.")
                && !jreVersion.startsWith("10.") && !jreVersion.startsWith("11.")) {
            System.err.println("*****************************************************");
            System.err.println("Warning: Unreliable Java version has been detected!");
            System.err.println();
            System.err.println("OpenJDK versions 12 and higher currently suffer from a");
            System.err.println("critical bug in the JIT compiler that will cause");
            System.err.println("data loss in eXist-db.");
            System.err.println();
            System.err.println("The problem has been reported to the OpenJDK community.");
            System.err.println();
            System.err.println("For more information, see:");
            System.err.println("- https://bugs.openjdk.java.net/browse/JDK-8253191");
            System.err.println("- https://github.com/eXist-db/exist/issues/3375");
            System.err.println();
            System.err.println("The detected version of Java on your system is " + jreVersion + ".");
            System.err.println();
            System.err.println("To prevent data loss, eXist-db will not be started.");
            System.err.println("Until this OpenJDK bug is resolved, we urge you to");
            System.err.println("switch to LTS versions Java 8 or 11.");
            System.err.println("*****************************************************");<commit_msg>Update code to block java 12,13,14,15 only Update of error message OpenJDK <12 and >15 will silently pass.<commit_after>/*
 * NOTE: This file is in part based on code from Mort Bay Consulting.
 * The original license statement is also included below.
 *
 * eXist-db Open Source Native XML Database
 * Copyright (C) 2001 The eXist-db Authors
 *
 * <EMAIL>
 * http://www.exist-db.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * ---------------------------------------------------------------------
 *
 * Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
 *
 * 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.exist.start;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * This is an adopted version of the corresponding classes shipped
 * with Jetty. Modified for eXist-db!
 *
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (<EMAIL>)
 * @version $Revision$
 *          TODO:
 *          - finish possible jetty.home locations
 *          - better handling of errors (i.e. when jetty.home cannot be autodetected...)
 *          - include entries from lib _when needed_
 */
public class Main {
    public static final String STANDARD_ENABLED_JETTY_CONFIGS = "standard.enabled-jetty-configs";
    public static final String STANDALONE_ENABLED_JETTY_CONFIGS = "standalone.enabled-jetty-configs";

    private static final int ERROR_CODE_GENERAL = 1;
    private static final int ERROR_CODE_NO_JETTY_CONFIG = 7;
    private static final int ERROR_CODE_RELIABLE_JAVA_DETECTED = 13;

    public static final String CONFIG_DIR_NAME = "etc";

    private static final String PROP_EXIST_START_DEBUG = "exist.start.debug";
    public static final String PROP_EXIST_JETTY_CONFIG = "exist.jetty.config";
    public static final String PROP_EXIST_HOME = "exist.home";
    public static final String PROP_JETTY_HOME = "jetty.home";
    private static final String PROP_LOG4J_CONFIGURATION_FILE = "log4j.configurationFile";
    private static final String PROP_JUL_MANAGER = "java.util.logging.manager";
    private static final String PROP_JAVA_TEMP_DIR = "java.io.tmpdir";

    public static final String ENV_EXIST_JETTY_CONFIG = "EXIST_JETTY_CONFIG";
    public static final String ENV_EXIST_HOME = "EXIST_HOME";
    public static final String ENV_JETTY_HOME = "JETTY_HOME";


    private static Main exist;

    private String _mode = "jetty";
    private boolean _debug = Boolean.getBoolean(PROP_EXIST_START_DEBUG);

    public static void main(final String[] args) {
        try {
            getMain().run(args);

        } catch (final Exception e) {
            e.printStackTrace();
            System.exit(ERROR_CODE_GENERAL);
        }
    }

    /**
     * Singleton Factory Method
     *
     * @return instance of Main class.
     */
    public static Main getMain() {
        if (exist == null) {
            exist = new Main();
        }
        return exist;
    }

    public String getMode() {
        return this._mode;
    }

    private Main() {
    }

    public Main(final String mode) {
        this._mode = mode;
    }

    private static Path getDirectory(final String name) {
        try {
            if (name != null) {
                final Path dir = Paths.get(name).normalize().toAbsolutePath();
                if (Files.isDirectory(dir)) {
                    return dir;
                }
            }
        } catch (final InvalidPathException e) {
            // NOP
        }
        return null;
    }

    private static void invokeMain(final ClassLoader classloader, final String classname, final String[] args)
            throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException, ClassNotFoundException {

        final Class<?> invoked_class = classloader.loadClass(classname);

        final Class<?>[] method_param_types = new Class[1];
        method_param_types[0] = args.getClass();

        final Method main = invoked_class.getDeclaredMethod("main", method_param_types);

        final Object[] method_params = new Object[1];
        method_params[0] = args;
        main.invoke(null, method_params);
    }

    public void run(final String[] args) {
        try {
            runEx(args);
        } catch (final StartException e) {
            System.exit(e.getErrorCode());
        }
    }

    private void checkForCorrectJavaVersions() throws StartException {

        final String jreVersion = System.getProperty("java.version", "UNKNOWN");
        final String majorVersion = jreVersion.split("\\.")[0];
        final String[] blockedMajorVersions= {"UNKNOWN", "12", "13", "14", "15"};

        if (!jreVersion.startsWith("1.8") && Arrays.asList(blockedMajorVersions).contains(majorVersion)) {
            System.err.println("*****************************************************");
            System.err.println("Warning: Unreliable Java version has been detected!");
            System.err.println();
            System.err.println("OpenJDK versions 12-15 suffer from a critical bug in the");
            System.err.println("JIT compiler that will cause data loss in eXist-db.");
            System.err.println();
            System.err.println("The problem has been reported to the OpenJDK community.");
            System.err.println();
            System.err.println("For more information, see:");
            System.err.println("- https://bugs.openjdk.java.net/browse/JDK-8253191");
            System.err.println("- https://github.com/eXist-db/exist/issues/3375");
            System.err.println();
            System.err.println("The detected version of Java on your system is " + jreVersion + ".");
            System.err.println();
            System.err.println("To prevent data loss, eXist-db will not be started.");
            System.err.println("We urge you to switch to LTS versions Java 8 or 11.");
            System.err.println("*****************************************************");
{'fraction_non_alphanumeric': 0.11692537111096048, 'fraction_numerical': 0.009625160984206602, 'mean_word_length': 2.7811378780112763, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6981492', 'n_tokens_mistral': 4082, 'n_tokens_neox': 3916, 'n_words': 1313}