<commit_before>/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: <EMAIL>
 *
 * This library 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: version 3 of
 * the License.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive.scheduler.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AbstractIdleService;


/**
 * This class encapsulates the logic to create an HSQLDB instance in server mode.
 * <p>
 * The server manages catalogs. Each catalog depicts an isolated database.
 * <p>
 * When HSQLDB is started in server mode, multiple clients can connect to it 
 * at the same time, which is not the case with the file or in-memory mode.
 *
 * @author ActiveEon Team
 */
public class HsqldbServer extends AbstractIdleService {

    private static final Logger logger = Logger.getLogger(HsqldbServer.class);

    /**
     * Hibernate properties.
     */

    protected static final String PROP_HIBERNATE_CONNECTION_URL = "hibernate.connection.url";

    protected static final String PROP_HIBERNATE_CONNECTION_USERNAME = "hibernate.connection.username";

    @SuppressWarnings("squid:S2068")
    protected static final String PROP_HIBERNATE_CONNECTION_PASSWORD = "<PASSWORD>";

    /*
     * HSQLDB properties.
     */

    protected static final String HSQLDB_DOCUMENTATION_URL = "http://hsqldb.org/doc/guide/dbproperties-chapt.html";

    // custom property used by tests to be able to pass catalog location in Hibernate configuration file
    protected static final String PROP_HSQLDB_PREFIX_CATALOG_PATH = "catalog.path";

    protected static final String PROP_HSQLDB_PREFIX_DBNAME = "server.dbname";

    protected static final String PROP_HSQLDB_PREFIX_SERVER_DATABASE = "server.database";

    // this property is not a common HSQLDB property but a custom one to configure catalog options
    protected static final String PROP_HSQLDB_SERVER_CATALOGS_OPTION_LINE = "server.catalogs.option_line";

    /*
     * Instance variables.
     */

    private int catalogIndex;

    private final String catalogOptionLine;

    private final HsqlProperties hsqlProperties;

    private Server server;

    /**
     * Creates a new instance of HSQLDB server.
     */
    HsqldbServer() {
        catalogOptionLine = "";
        hsqlProperties = new HsqlProperties();
    }

    /**
     * Creates a new instance of HSQLDB server.
     *
     * @param configuration the path to the HSQLDB server properties file.
     *
     * @throws IOException if an error occurs while reading the configuration file.
     */
    public HsqldbServer(Path configuration) throws IOException {

        Properties hsqldbServerProperties = loadProperties(configuration);

        catalogOptionLine = hsqldbServerProperties.getProperty(PROP_HSQLDB_SERVER_CATALOGS_OPTION_LINE);
        hsqldbServerProperties.remove(PROP_HSQLDB_SERVER_CATALOGS_OPTION_LINE);

        hsqlProperties = new HsqlProperties(hsqldbServerProperties);
    }

    public void addCatalog(Path defaultLocation, Path hibernateConfiguration) throws IOException {
        Properties hibernateProperties = loadProperties(hibernateConfiguration);

        String connectionUrl = hibernateProperties.getProperty(PROP_HIBERNATE_CONNECTION_URL);

        String catalogLocation = identifyCatalogLocationFromConnectionUrl(connectionUrl);
        String catalogName = identifyCatalogNameFromConnectionUrl(connectionUrl);
        String catalogPassword = <PASSWORD>(PROP_HIBERNATE_CONNECTION_PASSWORD);
        String catalogUsername = hibernateProperties.getProperty(PROP_HIBERNATE_CONNECTION_USERNAME);

        Path catalogPath;
        if (catalogLocation == null) {
            // Catalog path references a file.
            // Double resolve is used to get a dedicated folder with the catalog name.
            catalogPath = defaultLocation.resolve(catalogName).resolve(catalogName);
        } else {
            catalogPath = Paths.get(catalogLocation);
        }

        addCatalog(catalogPath, catalogName, catalogUsername, catalogPassword);<commit_msg>Fix check for hsqldb (#3401)<commit_after>/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: <EMAIL>
 *
 * This library 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: version 3 of
 * the License.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive.scheduler.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AbstractIdleService;


/**
 * This class encapsulates the logic to create an HSQLDB instance in server mode.
 * <p>
 * The server manages catalogs. Each catalog depicts an isolated database.
 * <p>
 * When HSQLDB is started in server mode, multiple clients can connect to it 
 * at the same time, which is not the case with the file or in-memory mode.
 *
 * @author ActiveEon Team
 */
public class HsqldbServer extends AbstractIdleService {

    private static final Logger logger = Logger.getLogger(HsqldbServer.class);

    /**
     * Hibernate properties.
     */

    protected static final String PROP_HIBERNATE_CONNECTION_URL = "hibernate.connection.url";

    protected static final String PROP_HIBERNATE_CONNECTION_USERNAME = "hibernate.connection.username";

    @SuppressWarnings("squid:S2068")
    protected static final String PROP_HIBERNATE_CONNECTION_PASSWORD = "<PASSWORD>";

    /*
     * HSQLDB properties.
     */

    protected static final String HSQLDB_DOCUMENTATION_URL = "http://hsqldb.org/doc/guide/dbproperties-chapt.html";

    // custom property used by tests to be able to pass catalog location in Hibernate configuration file
    protected static final String PROP_HSQLDB_PREFIX_CATALOG_PATH = "catalog.path";

    protected static final String PROP_HSQLDB_PREFIX_DBNAME = "server.dbname";

    protected static final String PROP_HSQLDB_PREFIX_SERVER_DATABASE = "server.database";

    // this property is not a common HSQLDB property but a custom one to configure catalog options
    protected static final String PROP_HSQLDB_SERVER_CATALOGS_OPTION_LINE = "server.catalogs.option_line";

    /*
     * Instance variables.
     */

    private int catalogIndex;

    private final String catalogOptionLine;

    private final HsqlProperties hsqlProperties;

    private Server server;

    /**
     * Creates a new instance of HSQLDB server.
     */
    HsqldbServer() {
        catalogOptionLine = "";
        hsqlProperties = new HsqlProperties();
    }

    /**
     * Creates a new instance of HSQLDB server.
     *
     * @param configuration the path to the HSQLDB server properties file.
     *
     * @throws IOException if an error occurs while reading the configuration file.
     */
    public HsqldbServer(Path configuration) throws IOException {

        Properties hsqldbServerProperties = loadProperties(configuration);

        catalogOptionLine = hsqldbServerProperties.getProperty(PROP_HSQLDB_SERVER_CATALOGS_OPTION_LINE);
        hsqldbServerProperties.remove(PROP_HSQLDB_SERVER_CATALOGS_OPTION_LINE);

        hsqlProperties = new HsqlProperties(hsqldbServerProperties);
    }

    public void addCatalog(Path defaultLocation, Path hibernateConfiguration) throws IOException {
        Properties hibernateProperties = loadProperties(hibernateConfiguration);

        String connectionUrl = hibernateProperties.getProperty(PROP_HIBERNATE_CONNECTION_URL);

        if (connectionUrl.toLowerCase().contains("hsqldb")) {

            String catalogLocation = identifyCatalogLocationFromConnectionUrl(connectionUrl);
            String catalogName = identifyCatalogNameFromConnectionUrl(connectionUrl);
            String catalogPassword = hibernateProperties.getProperty(PROP_HIBERNATE_CONNECTION_PASSWORD);
            String catalogUsername = hibernateProperties.getProperty(PROP_HIBERNATE_CONNECTION_USERNAME);

            Path catalogPath;
            if (catalogLocation == null) {
                // Catalog path references a file.
                // Double resolve is used to get a dedicated folder with the catalog name.
                catalogPath = defaultLocation.resolve(catalogName).resolve(catalogName);
            } else {
                catalogPath = Paths.get(catalogLocation);
            }

            addCatalog(catalogPath, catalogName, catalogUsername, catalogPassword);
        }

{'fraction_non_alphanumeric': 0.06920610974919857, 'fraction_numerical': 0.0035828776164435225, 'mean_word_length': 3.685070671378092, 'pattern_counts': {'":': 0, '<': 14, '<?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': '6444800', 'n_tokens_mistral': 2893, 'n_tokens_neox': 2763, 'n_words': 915}