<commit_before>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016-2018 Grakn Labs Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.bootup;

import ai.grakn.GraknConfigKey;
import ai.grakn.bootup.graknengine.Grakn;
import ai.grakn.engine.GraknConfig;
import ai.grakn.util.REST;
import ai.grakn.util.SimpleURI;

import javax.ws.rs.core.UriBuilder;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * @author <NAME>
 */
public class GraknProcess extends AbstractProcessHandler {

    protected final Path homePath;
    protected final Path configPath;
    private final GraknConfig graknConfig;

    private static final long GRAKN_STARTUP_TIMEOUT_S = 300;
    public static final Path GRAKN_PID = Paths.get(File.separator,"tmp","grakn.pid");

    public GraknProcess(Path homePath, Path configPath) {
        this.homePath = homePath;
        this.configPath = configPath;
        this.graknConfig = GraknConfig.read(configPath.toFile());
    }

    protected Class graknClass() {
        return Grakn.class;
    }

    public void start() {
        boolean graknIsRunning = processIsRunning(GRAKN_PID);
        if(graknIsRunning) {
            System.out.println(graknClass().getSimpleName()+" is already running");
        } else {
            graknStartProcess();
        }
    }

    protected String getClassPathFrom(Path home){
        FilenameFilter jarFiles = (dir, name) -> name.toLowerCase().endsWith(".jar");
        File folder = new File(home + File.separator+"services"+File.separator+"lib"); // services/lib folder
        File[] values = folder.listFiles(jarFiles);
        if(values==null) {
            throw new RuntimeException("No libraries found: cannot run "+graknClass().getSimpleName());
        }
        Stream<File> jars = Stream.of(values);
        File conf = new File(home + File.separator+"conf"+File.separator); // /conf
        File graknLogback = new File(home + File.separator+"services"+File.separator+"grakn"+File.separator); // services/grakn lib
        return ":"+Stream.concat(jars, Stream.of(conf, graknLogback))
                .filter(f -> !f.getName().contains("slf4j-log4j12"))
                .map(File::getAbsolutePath)
                .sorted() // we need to sort otherwise it doesn't load logback configuration properly
                .collect(Collectors.joining(":"));
    }

    private void graknStartProcess() {
        System.out.print("Starting "+graknClass().getSimpleName()+"...");
        System.out.flush();

        String command = commandToRun();
        executeAndWait(new String[]{
                "/bin/sh",<commit_msg>fix grakn logback (#2630)<commit_after>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016-2018 <NAME> Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.bootup;

import ai.grakn.GraknConfigKey;
import ai.grakn.bootup.graknengine.Grakn;
import ai.grakn.engine.GraknConfig;
import ai.grakn.util.REST;
import ai.grakn.util.SimpleURI;

import javax.ws.rs.core.UriBuilder;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 * @author <NAME>
 */
public class GraknProcess extends AbstractProcessHandler {

    protected final Path homePath;
    protected final Path configPath;
    private final GraknConfig graknConfig;

    private static final long GRAKN_STARTUP_TIMEOUT_S = 300;
    public static final Path GRAKN_PID = Paths.get(File.separator,"tmp","grakn.pid");

    public GraknProcess(Path homePath, Path configPath) {
        this.homePath = homePath;
        this.configPath = configPath;
        this.graknConfig = GraknConfig.read(configPath.toFile());
    }

    protected Class graknClass() {
        return Grakn.class;
    }

    public void start() {
        boolean graknIsRunning = processIsRunning(GRAKN_PID);
        if(graknIsRunning) {
            System.out.println(graknClass().getSimpleName()+" is already running");
        } else {
            graknStartProcess();
        }
    }

    protected String getClassPathFrom(Path home){
        FilenameFilter jarFiles = (dir, name) -> name.toLowerCase().endsWith(".jar");
        File folder = new File(home + File.separator+"services"+File.separator+"lib"); // services/lib folder
        File[] values = folder.listFiles(jarFiles);
        if(values==null) {
            throw new RuntimeException("No libraries found: cannot run "+graknClass().getSimpleName());
        }
        Stream<File> jars = Stream.of(values);
        File conf = new File(home + File.separator+"conf"+File.separator); // /conf
        File graknLogback = new File(home + File.separator+"services"+File.separator+"grakn"+File.separator + "server"+File.separator); // services/grakn/server lib
        return ":"+Stream.concat(jars, Stream.of(conf, graknLogback))
                .filter(f -> !f.getName().contains("slf4j-log4j12"))
                .map(File::getAbsolutePath)
                .sorted() // we need to sort otherwise it doesn't load logback configuration properly
                .collect(Collectors.joining(":"));
    }

    private void graknStartProcess() {
        System.out.print("Starting "+graknClass().getSimpleName()+"...");
        System.out.flush();

        String command = commandToRun();
        executeAndWait(new String[]{
                "/bin/sh",
{'fraction_non_alphanumeric': 0.11248768126143883, 'fraction_numerical': 0.005068281008024778, 'mean_word_length': 3.2160237388724036, 'pattern_counts': {'":': 4, '<': 10, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6270281', 'n_tokens_mistral': 2153, 'n_tokens_neox': 1996, 'n_words': 570}