<commit_before>/*
 * Created on Oct 5, 2004 by mschilli
 */
package alma.acs.commandcenter.app;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import alma.acs.commandcenter.engine.RunModel;
import alma.acs.commandcenter.util.MiscUtils;
import alma.acs.commandcenter.util.VariableString;
import alma.acs.util.ACSPorts;
import alma.acs.util.AcsLocations;
import alma.entity.xmlbinding.acscommandcenterproject.AcsCommandCenterProject;
import alma.entity.xmlbinding.acscommandcenterproject.types.ModeType;
import alma.entity.xmlbinding.acscommandcenterproject.ContainerT;



/**
 * Makes an AcsCommandCenterProject available as a RunModel.
 * 
 * <p>
 * Some methods may depending on a boolean flag return a runtime value stored in the
 * application (e.g. most recently started manager, or cdb thats most likely to belong to
 * the most recently started manager etc.) or return a value from the  project. </p>
 * 
 * @author mschilli
 */
public class ProjectRunModel implements RunModel {

	
	////////////////////////////////////////////////////////
	/// ---------- Logic of ProjectRunModel ------------ ///
	////////////////////////////////////////////////////////


	// --- construction ---
	
	public ProjectRunModel(AcsCommandCenterProject project) {
		setProject(project);
	}

	
	// --- project ---
	
	protected AcsCommandCenterProject project;

	public void setProject (AcsCommandCenterProject project) {
		this.project = project;
	}

	public AcsCommandCenterProject getProject () {
		return project;
	}
	
	public ModeType getMode() {
		return project.getMode();
	}
	
	// --- thread safety for concurrent starts of containers ---
	
	protected int selectedContainerIndex = 0; //default
	
	public ProjectRunModel createViewOnContainer (int containerIndex) {
		ProjectRunModel ret = new ProjectRunModel(project);
		ret.selectedContainerIndex = containerIndex;
		return ret;
	}
	
	
	// --- internal helpers (also API for Gui) ---
	
	public String deriveIRfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveIRPort();
		String host = deriveMgrHostfromCommonSettings();
		String ret = AcsLocations.convertToInterfaceRepositoryLocation(host, port);
		return ret;
	}

	public String deriveNSfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveNamingServicePort();
		String host = deriveMgrHostfromCommonSettings();
		String ret = AcsLocations.convertToNameServiceLocation(host, port);
		return ret;
	}
	
	public String deriveCDBfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveCDBPort();
		String host = deriveMgrHostfromCommonSettings();
		String ret = AcsLocations.convertToCdbLocation(host, port);
		return ret;
	}
		
	public String deriveMgrHostfromCommonSettings() {
		String host;
		if (ModeType.REMOTE.equals(project.getMode())) {
			host = read(project.getRemoteHost());
		} else {
			host = ACSPorts.getIP();
		}
		return host;
	}

	public String deriveMgrPortfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveManagerPort();
		return port;
	}

	
	// --- private helpers ---

	protected int deriveInstanceFromCommonSettings() {
		return MiscUtils.parseInt(read(project.getScriptBase()));
	}
	
	protected int selectedContainerIndex() {
		return this.selectedContainerIndex; 
	}
	
	protected ContainerT selectedContainer() {
		int selectedContainerIndex = selectedContainerIndex();
		return project.getContainers().getContainer(selectedContainerIndex);
	}

	
	// --- support for variables ---
	
	protected String read (String stringWithVars) {
		return new VariableString(stringWithVars, false).toString(getVariables());
	}
	
	protected Map variables = null;

	public Map getVariables() {
		if (variables == null) {
			readDefaultVariables();
		}
		return variables;
	}

	/**
	 * The default allocation of the variables map is (an excerpt of) the System Properties. 
	 */
	public void readDefaultVariables() {
		
		Map m = Collections.synchronizedMap(new HashMap());
		
		for (Iterator it = System.getProperties().entrySet().iterator(); it.hasNext() ;) {
			Map.Entry e = (Map.Entry) it.next();
			String n = (String)e.getKey(); 
			if (n.startsWith("java.") || n.startsWith("sun.") || n.startsWith("os.") 
					|| n.startsWith("file.") || n.startsWith("awt.") || n.startsWith("org.omg.")
					|| n.startsWith("jacorb.") || n.startsWith("path.") || n.startsWith("line.")
					|| n.startsWith("user.country") || n.startsWith("user.language") || n.startsWith("user.timezone")
					|| n.startsWith("user.variant")) {
				// skip
			} else {
				m.put(n, e.getValue());
			}
		}
		variables = m;
	}	
	
   /**
    * Use given properties table for variable resolution
    */
	public void readVariables (Properties p) {
		 Map m = Collections.synchronizedMap(new HashMap(p));<commit_msg>filters out some more third-party system properties<commit_after>/*
 * Created on Oct 5, 2004 by mschilli
 */
package alma.acs.commandcenter.app;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import alma.acs.commandcenter.engine.RunModel;
import alma.acs.commandcenter.util.MiscUtils;
import alma.acs.commandcenter.util.VariableString;
import alma.acs.util.ACSPorts;
import alma.acs.util.AcsLocations;
import alma.entity.xmlbinding.acscommandcenterproject.AcsCommandCenterProject;
import alma.entity.xmlbinding.acscommandcenterproject.types.ModeType;
import alma.entity.xmlbinding.acscommandcenterproject.ContainerT;



/**
 * Makes an AcsCommandCenterProject available as a RunModel.
 * 
 * <p>
 * Some methods may depending on a boolean flag return a runtime value stored in the
 * application (e.g. most recently started manager, or cdb thats most likely to belong to
 * the most recently started manager etc.) or return a value from the  project. </p>
 * 
 * @author mschilli
 */
public class ProjectRunModel implements RunModel {

	
	////////////////////////////////////////////////////////
	/// ---------- Logic of ProjectRunModel ------------ ///
	////////////////////////////////////////////////////////


	// --- construction ---
	
	public ProjectRunModel(AcsCommandCenterProject project) {
		setProject(project);
	}

	
	// --- project ---
	
	protected AcsCommandCenterProject project;

	public void setProject (AcsCommandCenterProject project) {
		this.project = project;
	}

	public AcsCommandCenterProject getProject () {
		return project;
	}
	
	public ModeType getMode() {
		return project.getMode();
	}
	
	// --- thread safety for concurrent starts of containers ---
	
	protected int selectedContainerIndex = 0; //default
	
	public ProjectRunModel createViewOnContainer (int containerIndex) {
		ProjectRunModel ret = new ProjectRunModel(project);
		ret.selectedContainerIndex = containerIndex;
		return ret;
	}
	
	
	// --- internal helpers (also API for Gui) ---
	
	public String deriveIRfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveIRPort();
		String host = deriveMgrHostfromCommonSettings();
		String ret = AcsLocations.convertToInterfaceRepositoryLocation(host, port);
		return ret;
	}

	public String deriveNSfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveNamingServicePort();
		String host = deriveMgrHostfromCommonSettings();
		String ret = AcsLocations.convertToNameServiceLocation(host, port);
		return ret;
	}
	
	public String deriveCDBfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveCDBPort();
		String host = deriveMgrHostfromCommonSettings();
		String ret = AcsLocations.convertToCdbLocation(host, port);
		return ret;
	}
		
	public String deriveMgrHostfromCommonSettings() {
		String host;
		if (ModeType.REMOTE.equals(project.getMode())) {
			host = read(project.getRemoteHost());
		} else {
			host = ACSPorts.getIP();
		}
		return host;
	}

	public String deriveMgrPortfromCommonSettings() {
		int basePort = deriveInstanceFromCommonSettings();
		String port = ACSPorts.globalInstance(basePort).giveManagerPort();
		return port;
	}

	
	// --- private helpers ---

	protected int deriveInstanceFromCommonSettings() {
		return MiscUtils.parseInt(read(project.getScriptBase()));
	}
	
	protected int selectedContainerIndex() {
		return this.selectedContainerIndex; 
	}
	
	protected ContainerT selectedContainer() {
		int selectedContainerIndex = selectedContainerIndex();
		return project.getContainers().getContainer(selectedContainerIndex);
	}

	
	// --- support for variables ---
	
	protected String read (String stringWithVars) {
		return new VariableString(stringWithVars, false).toString(getVariables());
	}
	
	protected Map variables = null;

	public Map getVariables() {
		if (variables == null) {
			readDefaultVariables();
		}
		return variables;
	}

	/**
	 * The default allocation of the variables map is (an excerpt of) the System Properties. 
	 */
	public void readDefaultVariables() {
		
		Map m = Collections.synchronizedMap(new HashMap());
		
		for (Iterator it = System.getProperties().entrySet().iterator(); it.hasNext() ;) {
			Map.Entry e = (Map.Entry) it.next();
			String n = (String)e.getKey(); 
			if (n.startsWith("java.") || n.startsWith("sun.") || n.startsWith("os.") 
					|| n.startsWith("file.") || n.startsWith("awt.") || n.startsWith("org.omg.")
					|| n.startsWith("jacorb.") || n.startsWith("path.") || n.startsWith("line.")
					|| n.startsWith("user.country") || n.startsWith("user.language") || n.startsWith("user.timezone")
					|| n.startsWith("user.variant") || n.startsWith("deployment.") || n.startsWith("javaplugin.")
					|| n.startsWith("jnlpx.") || n.startsWith("org.apache.")) {
				// skip
			} else {
				m.put(n, e.getValue());
			}
		}
		variables = m;
	}	
	
   /**
    * Use given properties table for variable resolution
    */
	public void readVariables (Properties p) {
		 Map m = Collections.synchronizedMap(new HashMap(p));
{'fraction_non_alphanumeric': 0.14128120766402166, 'fraction_numerical': 0.0011612154054577125, 'mean_word_length': 3.732142857142857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '2501949', 'n_tokens_mistral': 3202, 'n_tokens_neox': 2913, 'n_words': 703}