<commit_before>/*******************************************************************************
 * (C) Copyright 2016 <NAME> and <NAME>
 *  
 * This file is part of STEP
 *  
 * STEP 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.
 *  
 * STEP 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 STEP.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package step.commons.processmanager;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import step.commons.conf.Configuration;
import step.commons.helpers.FileHelper;

public class ManagedProcess {

	private static final Logger logger = LoggerFactory.getLogger(ManagedProcess.class);
	
	private final ProcessBuilder builder;
		
	private static File logFolder;
	
	private File processOutputLog;
	
	private File processErrorLog;
	
	private final String id;
	
	private Process process;
	
	private File executionDirectory;
	
	public ManagedProcess(String command, String name) throws ManagedProcessException {
		this(tokenize(command), name);
	}
	
	public ManagedProcess(String name, String... command) throws ManagedProcessException {
		this(Arrays.asList(command), name);
	}
	
	public ManagedProcess(List<String> commands, String name) throws ManagedProcessException {
		super();

		String logdir = Configuration.getInstance().getProperty("managedprocesses.logdir");
		logFolder = new File(logdir!=null?logdir:".");
		
		UUID uuid = UUID.randomUUID();
		this.id = name+"_"+uuid;
		builder = new ProcessBuilder(commands);
		
		executionDirectory = new File(logFolder+"/" + id);
		if(!executionDirectory.exists()) {
			if(!executionDirectory.mkdir()) {
				throw new InvalidParameterException("Unable to create log folder for process " + id + ". Please ensure that the folder specified by the parameter adapters.log in the configuration repository exists and is writable.");
			}
		}
	}
	
	public static File getLogFolder() {
		return logFolder;
	}<commit_msg>Adding constructor to ManagedProcess to define executionDirectory<commit_after>/*******************************************************************************
 * (C) Copyright 2016 <NAME> and <NAME>
 *  
 * This file is part of STEP
 *  
 * STEP 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.
 *  
 * STEP 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 STEP.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package step.commons.processmanager;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import step.commons.conf.Configuration;
import step.commons.helpers.FileHelper;

public class ManagedProcess {

	private static final Logger logger = LoggerFactory.getLogger(ManagedProcess.class);
	
	private final ProcessBuilder builder;
		
	private static File logFolder;
	
	private File processOutputLog;
	
	private File processErrorLog;
	
	private final String id;
	
	private Process process;
	
	private File executionDirectory;
	
	public ManagedProcess(String command, String name) throws ManagedProcessException {
		this(tokenize(command), name);
	}
	
	public ManagedProcess(String name, String... command) throws ManagedProcessException {
		this(Arrays.asList(command), name);
	}
	
	public ManagedProcess(List<String> commands, String name) throws ManagedProcessException {
		this(null, commands, name);
	}
	
	public ManagedProcess(File executionDirectory, List<String> commands, String name) throws ManagedProcessException {
		super();

		String logdir = Configuration.getInstance().getProperty("managedprocesses.logdir");
		logFolder = new File(logdir!=null?logdir:".");
		
		UUID uuid = UUID.randomUUID();
		this.id = name+"_"+uuid;
		builder = new ProcessBuilder(commands);
		
		if(executionDirectory==null) {
			executionDirectory = new File(logFolder+"/" + id);
			if(!executionDirectory.exists()) {
				if(!executionDirectory.mkdir()) {
					throw new InvalidParameterException("Unable to create log folder for process " + id + ". Please ensure that the folder specified by the parameter adapters.log in the configuration repository exists and is writable.");
				}
			}	
		}
		this.executionDirectory = executionDirectory;
	}
	
	public static File getLogFolder() {
		return logFolder;
	}
{'fraction_non_alphanumeric': 0.13352272727272727, 'fraction_numerical': 0.0022095959595959595, 'mean_word_length': 4.995269631031221, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3316767', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1609, 'n_words': 555}