<commit_before>/*
 * Copyright (c) 2008-2017 Haulmont.
 *
 * 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 com.haulmont.gradle.task.db;

import com.haulmont.gradle.hsql.CubaHSQLDBServer;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.taskdefs.WaitFor;
import org.apache.tools.ant.taskdefs.WaitFor.Unit;
import org.apache.tools.ant.taskdefs.condition.Socket;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.TaskAction;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;

public class CubaHsqlStart extends CubaHsqlTask {

    public static final String HSQLDB_SERVER_MAIN = "org.hsqldb.server.Server";

    protected File dbDataDir;
    protected boolean showUi = true;

    @Override
    protected void init() {
        super.init();

        if (dbDataDir == null) {
            File projectDir = getProject().getRootProject().getProjectDir();
            dbDataDir = new File(projectDir, "deploy/hsqldb");
        }
    }

    @TaskAction
    public void startDb() {
        init();

        //noinspection ResultOfMethodCallIgnored
        dbDataDir.mkdirs();

        if (isShowUi() && !GraphicsEnvironment.isHeadless()) {
            URL[] classloaderUrls = ((URLClassLoader) CubaHSQLDBServer.class.getClassLoader()).getURLs();

            String[] paths = new String[classloaderUrls.length];
            for (int i = 0; i < classloaderUrls.length; i++) {
                try {
                    paths[i] = new File(classloaderUrls[i].toURI()).getAbsolutePath();
                } catch (URISyntaxException e) {
                    throw new GradleException("Unable to compose path", e);
                }
            }

            String dbServerClassPath = driverClasspath + File.pathSeparator + StringUtils.join(paths, File.pathSeparator);

            Java java = new Java();
            java.setProject(getProject().getAnt().getProject());
            java.setClassname(CubaHSQLDBServer.class.getName());
            java.createClasspath().setPath(dbServerClassPath);
            java.setFork(true);
            java.setSpawn(true);
            java.setDir(dbDataDir);

            java.createArg()
                    .setLine("" + dbPort + " \"" + dbDataDir.getAbsolutePath() + "\" \"" + dbName + "\"");

            java.execute();
        } else {
            Execute exec = new Execute();
            exec.setAntRun(getProject().getAnt().getProject());
            exec.setWorkingDirectory(dbDataDir);

            String osName = System.getProperty("os.name");
            if (osName.contains("linux")
                    || osName.contains("mac")
                    || osName.contains("nix")) {
                exec.setCommandline(new String[]{
                        "java",
                        "-cp", "\"" + driverClasspath + "\"",
                        HSQLDB_SERVER_MAIN,
                        "--port", String.valueOf(dbPort),
                        "--database.0", "file:\"" + dbName + "\"",
                        "--dbname.0", "\"" + dbName + "\""
                });
            } else {
                exec.setCommandline(new String[]{
                        "cmd.exe", "/C",
                        "java.exe",
                        "-cp", "\"" + driverClasspath + "\"",
                        HSQLDB_SERVER_MAIN,
                        "--port", String.valueOf(dbPort),
                        "--database.0", "file:\"" + dbName + "\"",
                        "--dbname.0", "\"" + dbName + "\""
                });
            }

            try {
                exec.spawn();
            } catch (IOException e) {
                throw new GradleException("Unable to start HSQL server", e);
            }
        }

        Unit UNIT_SECOND = (Unit) Unit.getInstance(Unit.class, "second");

        WaitFor waitFor = new WaitFor();
        waitFor.setProject(getProject().getAnt().getProject());
        waitFor.setMaxWait(10);
        waitFor.setMaxWaitUnit(UNIT_SECOND);
        waitFor.setCheckEvery(1);
        waitFor.setCheckEveryUnit(UNIT_SECOND);

        Socket socket = new Socket();
        socket.setPort(dbPort);
        socket.setServer("localhost");

        waitFor.addSocket(socket);
        waitFor.execute();
    }

    public boolean isShowUi() {
        return showUi;
    }

    public void setShowUi(boolean showUi) {
        this.showUi = showUi;
    }

    public File getDbDataDir() {
        return dbDataDir;
    }

    public void setDbDataDir(File dbDataDir) {
        this.dbDataDir = dbDataDir;
    }
}<commit_msg>PL-9663 Get rid of ClassLoader addUrl trick in HsqlStart / HsqlStop tasks - Fix startDb on Linux<commit_after>/*
 * Copyright (c) 2008-2017 Haulmont.
 *
 * 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 com.haulmont.gradle.task.db;

import com.haulmont.gradle.hsql.CubaHSQLDBServer;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.taskdefs.WaitFor;
import org.apache.tools.ant.taskdefs.WaitFor.Unit;
import org.apache.tools.ant.taskdefs.condition.Socket;
import org.gradle.api.GradleException;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.impldep.org.apache.commons.lang.SystemUtils;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;

public class CubaHsqlStart extends CubaHsqlTask {

    public static final String HSQLDB_SERVER_MAIN = "org.hsqldb.server.Server";

    protected File dbDataDir;
    protected boolean showUi = true;

    @Override
    protected void init() {
        super.init();

        if (dbDataDir == null) {
            File projectDir = getProject().getRootProject().getProjectDir();
            dbDataDir = new File(projectDir, "deploy/hsqldb");
        }
    }

    @TaskAction
    public void startDb() {
        init();

        //noinspection ResultOfMethodCallIgnored
        dbDataDir.mkdirs();

        if (isShowUi() && !GraphicsEnvironment.isHeadless()) {
            URL[] classloaderUrls = ((URLClassLoader) CubaHSQLDBServer.class.getClassLoader()).getURLs();

            String[] paths = new String[classloaderUrls.length];
            for (int i = 0; i < classloaderUrls.length; i++) {
                try {
                    paths[i] = new File(classloaderUrls[i].toURI()).getAbsolutePath();
                } catch (URISyntaxException e) {
                    throw new GradleException("Unable to compose path", e);
                }
            }

            String dbServerClassPath = driverClasspath + File.pathSeparator + StringUtils.join(paths, File.pathSeparator);

            Java java = new Java();
            java.setProject(getProject().getAnt().getProject());
            java.setClassname(CubaHSQLDBServer.class.getName());
            java.createClasspath().setPath(dbServerClassPath);
            java.setFork(true);
            java.setSpawn(true);
            java.setDir(dbDataDir);

            java.createArg()
                    .setLine("" + dbPort + " \"" + dbDataDir.getAbsolutePath() + "\" \"" + dbName + "\"");

            java.execute();
        } else {
            Execute exec = new Execute();
            exec.setAntRun(getProject().getAnt().getProject());
            exec.setWorkingDirectory(dbDataDir);

            if (SystemUtils.IS_OS_WINDOWS) {
                exec.setCommandline(new String[]{
                        "cmd.exe", "/C",
                        "java.exe",
                        "-cp", "\"" + driverClasspath + "\"",
                        HSQLDB_SERVER_MAIN,
                        "--port", String.valueOf(dbPort),
                        "--database.0", "file:\"" + dbName + "\"",
                        "--dbname.0", "\"" + dbName + "\""
                });
            } else {
                exec.setCommandline(new String[]{
                        "java",
                        "-cp", "\"" + driverClasspath + "\"",
                        HSQLDB_SERVER_MAIN,
                        "--port", String.valueOf(dbPort),
                        "--database.0", "file:\"" + dbName + "\"",
                        "--dbname.0", "\"" + dbName + "\""
                });
            }

            try {
                exec.spawn();
            } catch (IOException e) {
                throw new GradleException("Unable to start HSQL server", e);
            }
        }

        Unit UNIT_SECOND = (Unit) Unit.getInstance(Unit.class, "second");

        WaitFor waitFor = new WaitFor();
        waitFor.setProject(getProject().getAnt().getProject());
        waitFor.setMaxWait(10);
        waitFor.setMaxWaitUnit(UNIT_SECOND);
        waitFor.setCheckEvery(1);
        waitFor.setCheckEveryUnit(UNIT_SECOND);

        Socket socket = new Socket();
        socket.setPort(dbPort);
        socket.setServer("localhost");

        waitFor.addSocket(socket);
        waitFor.execute();
    }

    public boolean isShowUi() {
        return showUi;
    }

    public void setShowUi(boolean showUi) {
        this.showUi = showUi;
    }

    public File getDbDataDir() {
        return dbDataDir;
    }

    public void setDbDataDir(File dbDataDir) {
        this.dbDataDir = dbDataDir;
    }
}
{'fraction_non_alphanumeric': 0.12237529691211402, 'fraction_numerical': 0.004370546318289786, 'mean_word_length': 1.9988603988603988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '676348', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2888, 'n_words': 631}