<commit_before>/* 
* Copyright 2014 <NAME>
*
* 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 net.objecthunter.larch.weedfs;

import net.objecthunter.larch.helpers.InputStreamLoggerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Helper class for starting and monitoring a WeedFs Master node process
 */
public class WeedFsMaster{
    private static final Logger log = LoggerFactory.getLogger(WeedFsMaster.class);

    @Autowired
    Environment env;
    private Process masterProcess;
    private InputStreamLoggerTask loggerTask;

    @PostConstruct
    public void runMaster() {
        /* check if the master dir exists and create if neccessary */
        final File dir = new File(env.getProperty("weedfs.master.dir"));
        if (!dir.exists()) {
            log.info("creating WeedFS master directory at " + dir.getAbsolutePath());
            if (!dir.mkdir()) {
                throw new IllegalArgumentException("Unable to create master directory. Please check the configuration");
            }
        }
        if (!dir.canRead() || !dir.canWrite()) {
            log.error("Unable to create master directory. The application was not initialiazed correctly");
            throw new IllegalArgumentException("Unable to use master directory. Please check the configuration");
        }
        if (env.getProperty("weedfs.binary") == null) {
            throw new IllegalArgumentException("The WeedFs Binary path has to be set");
        }
        final File binary = new File(env.getProperty("weedfs.binary"));
        if (!binary.exists()) {
            throw new IllegalArgumentException(new FileSystemNotFoundException("The weedfs binary can not be found at " + binary.getAbsolutePath()));
        }
        if (!binary.canExecute()) {
            throw new IllegalArgumentException("The weedfs binary at " + binary.getAbsolutePath() + " can not be executed");
        }
        try {
            log.info("starting WeedFS master");

            final List<String> command = Arrays.asList(
                    env.getProperty("weedfs.binary"),
                    "master",
                    "-mdir=" + env.getProperty("weedfs.master.dir"),
                    "-port=" + env.getProperty("weedfs.master.port")<commit_msg>added check for disabled weedfs master node<commit_after>/* 
* Copyright 2014 <NAME>
*
* 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 net.objecthunter.larch.weedfs;

import net.objecthunter.larch.helpers.InputStreamLoggerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystemNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Helper class for starting and monitoring a WeedFs Master node process
 */
public class WeedFsMaster{
    private static final Logger log = LoggerFactory.getLogger(WeedFsMaster.class);

    @Autowired
    Environment env;
    private Process masterProcess;
    private InputStreamLoggerTask loggerTask;

    @PostConstruct
    public void init() {
        if (env.getProperty("weedfs.master.enabled") != null && !Boolean.parseBoolean(env.getProperty("weedfs.master.enabled"))) {
            // no weedfs master node is needed
            return;
        }
        /* check if the master dir exists and create if neccessary */
        final File dir = new File(env.getProperty("weedfs.master.dir"));
        if (!dir.exists()) {
            log.info("creating WeedFS master directory at " + dir.getAbsolutePath());
            if (!dir.mkdir()) {
                throw new IllegalArgumentException("Unable to create master directory. Please check the configuration");
            }
        }
        if (!dir.canRead() || !dir.canWrite()) {
            log.error("Unable to create master directory. The application was not initialiazed correctly");
            throw new IllegalArgumentException("Unable to use master directory. Please check the configuration");
        }
        if (env.getProperty("weedfs.binary") == null) {
            throw new IllegalArgumentException("The WeedFs Binary path has to be set");
        }
        final File binary = new File(env.getProperty("weedfs.binary"));
        if (!binary.exists()) {
            throw new IllegalArgumentException(new FileSystemNotFoundException("The weedfs binary can not be found at " + binary.getAbsolutePath()));
        }
        if (!binary.canExecute()) {
            throw new IllegalArgumentException("The weedfs binary at " + binary.getAbsolutePath() + " can not be executed");
        }
        try {
            log.info("starting WeedFS master");

            final List<String> command = Arrays.asList(
                    env.getProperty("weedfs.binary"),
                    "master",
                    "-mdir=" + env.getProperty("weedfs.master.dir"),
                    "-port=" + env.getProperty("weedfs.master.port")
{'fraction_non_alphanumeric': 0.09846760734334699, 'fraction_numerical': 0.0030344409042633897, 'mean_word_length': 3.0591133004926108, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5760386', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1656, 'n_words': 546}