<commit_before>/* 
 * 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 org.ngrinder.sm;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileDescriptor;
import java.net.InetAddress;
import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

/**
 * nGrinder security manager.
 *
 * @author <NAME>
 * @author Tobi
 * @since 3.0
 */
@SuppressWarnings("Duplicates")
public class NGrinderSecurityManager extends SecurityManager {
	private static final String NGRINDER_CONTROLLER_DEFAULT_FOLDER = ".ngrinder";
	private static final String NGRINDER_CONTROLLER_TEMP_FOLDER = "tmp";
	private static final String NGRINDER_CONTEXT_CONTROLLER = "controller";

	private String workDirectory = System.getProperty("user.dir");
	private String controllerHomeDir = "";
	private String controllerHomeTmpDir = "";
	private String ngrinderContext = "";

	private final String pythonPath = System.getProperty("python.path");
	private final String pythonHome = System.getProperty("python.home");
	private final String pythonCache = System.getProperty("python.cachedir");
	private final String etcHosts = System.getProperty("ngrinder.etc.hosts", "");
	private final String consoleIP = System.getProperty("ngrinder.console.ip", "127.0.0.1");
	private final List<String> allowedHost = new ArrayList<String>();
	private final List<String> writeAllowedDirectory = new ArrayList<String>();
	private final List<String> deleteAllowedDirectory = new ArrayList<String>();

	{
		this.init();
	}

	void init() {
		ngrinderContext = System.getProperty("ngrinder.context", "agent");
		if (isControllerContext()) {
			controllerHomeDir = resolveControllerHomeDir();
			controllerHomeTmpDir = (controllerHomeDir + File.separator + NGRINDER_CONTROLLER_TEMP_FOLDER);
		}
		this.initAccessOfDirectories();
		this.initAccessOfHosts();
	}

	private String resolveControllerHomeDir() {
		String userHomeFromEnv = System.getenv("NGRINDER_HOME");
		String userHomeFromProperty = System.getProperty("ngrinder.home");
		String userHome = StringUtils.defaultIfEmpty(userHomeFromProperty, userHomeFromEnv);
		if (StringUtils.isEmpty(userHome)) {
			userHome = System.getProperty("user.home") + File.separator + NGRINDER_CONTROLLER_DEFAULT_FOLDER;
		} else if (StringUtils.startsWith(userHome, "~" + File.separator)) {
			userHome = System.getProperty("user.home") + File.separator + userHome.substring(2);
		} else if (StringUtils.startsWith(userHome, "." + File.separator)) {
			userHome = System.getProperty("user.dir") + File.separator + userHome.substring(2);
		}
		return FilenameUtils.normalize(userHome);
	}<commit_msg>Avoid recursive update error on security mode<commit_after>/* 
 * 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 org.ngrinder.sm;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileDescriptor;
import java.net.InetAddress;
import java.security.AllPermission;
import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

/**
 * nGrinder security manager.
 *
 * @author <NAME>
 * @author Tobi
 * @since 3.0
 */
@SuppressWarnings("Duplicates")
public class NGrinderSecurityManager extends SecurityManager {
	private static final String NGRINDER_CONTROLLER_DEFAULT_FOLDER = ".ngrinder";
	private static final String NGRINDER_CONTROLLER_TEMP_FOLDER = "tmp";
	private static final String NGRINDER_CONTEXT_CONTROLLER = "controller";

	private String workDirectory = System.getProperty("user.dir");
	private String controllerHomeDir = "";
	private String controllerHomeTmpDir = "";
	private String ngrinderContext = "";

	private final String pythonPath = System.getProperty("python.path");
	private final String pythonHome = System.getProperty("python.home");
	private final String pythonCache = System.getProperty("python.cachedir");
	private final String etcHosts = System.getProperty("ngrinder.etc.hosts", "");
	private final String consoleIP = System.getProperty("ngrinder.console.ip", "127.0.0.1");
	private final List<String> allowedHost = new ArrayList<String>();
	private final List<String> writeAllowedDirectory = new ArrayList<String>();
	private final List<String> deleteAllowedDirectory = new ArrayList<String>();

	{
		this.init();
	}

	public NGrinderSecurityManager() {
		this.checkPermission(new AllPermission());
	}

	void init() {
		ngrinderContext = System.getProperty("ngrinder.context", "agent");
		if (isControllerContext()) {
			controllerHomeDir = resolveControllerHomeDir();
			controllerHomeTmpDir = (controllerHomeDir + File.separator + NGRINDER_CONTROLLER_TEMP_FOLDER);
		}
		this.initAccessOfDirectories();
		this.initAccessOfHosts();
	}

	private String resolveControllerHomeDir() {
		String userHomeFromEnv = System.getenv("NGRINDER_HOME");
		String userHomeFromProperty = System.getProperty("ngrinder.home");
		String userHome = StringUtils.defaultIfEmpty(userHomeFromProperty, userHomeFromEnv);
		if (StringUtils.isEmpty(userHome)) {
			userHome = System.getProperty("user.home") + File.separator + NGRINDER_CONTROLLER_DEFAULT_FOLDER;
		} else if (StringUtils.startsWith(userHome, "~" + File.separator)) {
			userHome = System.getProperty("user.home") + File.separator + userHome.substring(2);
		} else if (StringUtils.startsWith(userHome, "." + File.separator)) {
			userHome = System.getProperty("user.dir") + File.separator + userHome.substring(2);
		}
		return FilenameUtils.normalize(userHome);
	}
{'fraction_non_alphanumeric': 0.10917697358375325, 'fraction_numerical': 0.004275461902580546, 'mean_word_length': 5.427870461236506, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '4557798', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1889, 'n_words': 492}