<commit_before>/*
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2007 "<NAME>" <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 * $Rev$
 * Last modified by $Author$
 * $Date$
 */

package tigase.conf;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import tigase.disco.ServiceEntity;
import tigase.disco.ServiceIdentity;
import tigase.disco.XMPPService;
import tigase.server.AbstractComponentRegistrator;
import tigase.server.Command;
import tigase.server.MessageReceiver;
import tigase.server.MessageRouter;
import tigase.server.ConnectionManager;
import tigase.server.MessageRouterConfig;
import tigase.server.Packet;
import tigase.server.Permissions;
import tigase.server.ServerComponent;
import tigase.util.ClassUtil;
import tigase.util.JIDUtils;
import tigase.xml.Element;
import tigase.xml.XMLUtils;
import tigase.xml.db.Types.DataType;
import tigase.xmpp.Authorization;
import tigase.xmpp.StanzaType;
import tigase.xmpp.PacketErrorTypeException;

import static tigase.server.MessageRouterConfig.DEF_SM_NAME;

/**
 * Class Configurator
 *
 * Created: Tue Nov 22 07:07:11 2005
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Rev$
 */
public class Configurator extends AbstractComponentRegistrator<Configurable>
	implements Configurable, XMPPService {

	private static final String LOGGING_KEY = "logging/";

  private static final Logger log =
		Logger.getLogger("tigase.conf.Configurator");

	private ConfigRepository repository = null;
	//	private Timer delayedTask = new Timer("ConfiguratorTask", true);
	private Map<String, Object> defConfigParams =
		new LinkedHashMap<String, Object>();
	private ServiceEntity serviceEntity = null;
	private ServiceEntity config_list = null;
	private ServiceEntity config_set = null;
	private boolean demoMode = false;
	/**
	 * This variable is used only for configuration at run-time to add new
	 * components....
	 */
	private String routerCompName = null;

	public void setName(String name) {
		super.setName(name);
		serviceEntity = new ServiceEntity(name, "config", "Server configuration");
		serviceEntity.addIdentities(new ServiceIdentity[] {
				new ServiceIdentity("automation", "command-list",
					"Configuration commands")});
		serviceEntity.addFeatures(DEF_FEATURES);
		config_list = new ServiceEntity(name, "list", "List");
		config_list.addIdentities(new ServiceIdentity[] {
				new ServiceIdentity("automation", "command-list",
					"Config listings")});
		config_list.addFeatures(DEF_FEATURES);
		config_set = new ServiceEntity(name, "set", "Set");
		config_set.addIdentities(new ServiceIdentity[] {
				new ServiceIdentity("automation", "command-list",
					"Config settings")});
		config_set.addFeatures(DEF_FEATURES);
		ServiceEntity item = new ServiceEntity(getName(), "--none--",
			"Add new component...");
		item.addFeatures(CMD_FEATURES);
		item.addIdentities(new ServiceIdentity("automation", "command-node",
					"Add new component..."));
		config_set.addItems(item);
		serviceEntity.addItems(config_list, config_set);
	}

	public void parseArgs(final String[] args) {
		defConfigParams.put(GEN_TEST, Boolean.FALSE);
		defConfigParams.put("config-type", GEN_CONFIG_DEF);
    if (args != null && args.length > 0) {
      for (int i = 0; i < args.length; i++) {
				String key = null;
				Object val = null;
				if (args[i].startsWith(GEN_CONFIG)) {
					key = "config-type";	val = args[i];
				}
				if (args[i].startsWith(GEN_TEST)) {
					key = args[i];  val = Boolean.TRUE;
				}
				if (args[i].equals(GEN_USER_DB) || args[i].equals(GEN_USER_DB_URI)
					|| args[i].equals(GEN_AUTH_DB) || args[i].equals(GEN_AUTH_DB_URI)
					|| args[i].equals(GEN_COMP_NAME) || args[i].equals(GEN_COMP_CLASS)
					|| args[i].startsWith(GEN_EXT_COMP) || args[i].equals(GEN_VIRT_HOSTS)
					|| args[i].equals(GEN_ADMINS) || args[i].equals(GEN_DEBUG)
					|| (args[i].startsWith(GEN_CONF) && !args[i].startsWith(GEN_CONFIG))) {
					key = args[i];  val = args[++i];
				}
				if (key != null) {
					defConfigParams.put(key, val);
					//System.out.println("Setting defaults: " + key + "=" + val.toString());
					log.config("Setting defaults: " + key + "=" + val.toString());
				} // end of if (key != null)
      } // end of for (int i = 0; i < args.length; i++)
    }
  }

	public Configurator(String fileName, String[] args) {
		parseArgs(args);
		repository = ConfigRepository.getConfigRepository(fileName);
		defConfigParams.putAll(getAllProperties(null));
	}

	public boolean isCorrectType(ServerComponent component) {<commit_msg>Added support for initial parameters read from property file which is useful especially on windows<commit_after>/*
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2007 "<NAME>" <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 * $Rev$
 * Last modified by $Author$
 * $Date$
 */

package tigase.conf;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import tigase.disco.ServiceEntity;
import tigase.disco.ServiceIdentity;
import tigase.disco.XMPPService;
import tigase.server.AbstractComponentRegistrator;
import tigase.server.Command;
import tigase.server.MessageReceiver;
import tigase.server.MessageRouter;
import tigase.server.ConnectionManager;
import tigase.server.MessageRouterConfig;
import tigase.server.Packet;
import tigase.server.Permissions;
import tigase.server.ServerComponent;
import tigase.util.ClassUtil;
import tigase.util.JIDUtils;
import tigase.xml.Element;
import tigase.xml.XMLUtils;
import tigase.xml.db.Types.DataType;
import tigase.xmpp.Authorization;
import tigase.xmpp.StanzaType;
import tigase.xmpp.PacketErrorTypeException;

import static tigase.server.MessageRouterConfig.DEF_SM_NAME;

/**
 * Class Configurator
 *
 * Created: Tue Nov 22 07:07:11 2005
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Rev$
 */
public class Configurator extends AbstractComponentRegistrator<Configurable>
	implements Configurable, XMPPService {

	public static final String PROPERTY_FILENAME_PROP_KEY = "--property-file";
	private static final String LOGGING_KEY = "logging/";

  private static final Logger log =
		Logger.getLogger("tigase.conf.Configurator");

	private ConfigRepository repository = null;
	//	private Timer delayedTask = new Timer("ConfiguratorTask", true);
	private Map<String, Object> defConfigParams =
		new LinkedHashMap<String, Object>();
	private Properties defProperties = new Properties();
	private ServiceEntity serviceEntity = null;
	private ServiceEntity config_list = null;
	private ServiceEntity config_set = null;
	private boolean demoMode = false;
	/**
	 * This variable is used only for configuration at run-time to add new
	 * components....
	 */
	private String routerCompName = null;

	public void setName(String name) {
		super.setName(name);
		serviceEntity = new ServiceEntity(name, "config", "Server configuration");
		serviceEntity.addIdentities(new ServiceIdentity[] {
				new ServiceIdentity("automation", "command-list",
					"Configuration commands")});
		serviceEntity.addFeatures(DEF_FEATURES);
		config_list = new ServiceEntity(name, "list", "List");
		config_list.addIdentities(new ServiceIdentity[] {
				new ServiceIdentity("automation", "command-list",
					"Config listings")});
		config_list.addFeatures(DEF_FEATURES);
		config_set = new ServiceEntity(name, "set", "Set");
		config_set.addIdentities(new ServiceIdentity[] {
				new ServiceIdentity("automation", "command-list",
					"Config settings")});
		config_set.addFeatures(DEF_FEATURES);
		ServiceEntity item = new ServiceEntity(getName(), "--none--",
			"Add new component...");
		item.addFeatures(CMD_FEATURES);
		item.addIdentities(new ServiceIdentity("automation", "command-node",
					"Add new component..."));
		config_set.addItems(item);
		serviceEntity.addItems(config_list, config_set);
	}

	public void parseArgs(final String[] args) {
		defConfigParams.put(GEN_TEST, Boolean.FALSE);
		defConfigParams.put("config-type", GEN_CONFIG_DEF);
    if (args != null && args.length > 0) {
      for (int i = 0; i < args.length; i++) {
				String key = null;
				Object val = null;
				if (args[i].startsWith(GEN_CONFIG)) {
					key = "config-type";	val = args[i];
				}
				if (args[i].startsWith(GEN_TEST)) {
					key = args[i];  val = Boolean.TRUE;
				}
				if (args[i].equals(GEN_USER_DB) || args[i].equals(GEN_USER_DB_URI)
					|| args[i].equals(GEN_AUTH_DB) || args[i].equals(GEN_AUTH_DB_URI)
					|| args[i].equals(GEN_COMP_NAME) || args[i].equals(GEN_COMP_CLASS)
					|| args[i].startsWith(GEN_EXT_COMP) || args[i].equals(GEN_VIRT_HOSTS)
					|| args[i].equals(GEN_ADMINS) || args[i].equals(GEN_DEBUG)
					|| (args[i].startsWith(GEN_CONF) && !args[i].startsWith(GEN_CONFIG))
					|| args[i].equals(PROPERTY_FILENAME_PROP_KEY)) {
					key = args[i];  val = args[++i];
				}
				if (key != null) {
					defConfigParams.put(key, val);
					//System.out.println("Setting defaults: " + key + "=" + val.toString());
					log.config("Setting defaults: " + key + "=" + val.toString());
				} // end of if (key != null)
      } // end of for (int i = 0; i < args.length; i++)
    }
		String property_filename =
			(String)defConfigParams.get(PROPERTY_FILENAME_PROP_KEY);
		if (property_filename != null) {
			log.config("Loading initial properties from property file: "
				+ property_filename);
			try {
				defProperties.load(new FileReader(property_filename));
				Set<String> prop_keys = defProperties.stringPropertyNames();
				for (String key: prop_keys) {
					if (key.startsWith("--") || key.equals("config-type")) {
						String value = defProperties.getProperty(key);
						defConfigParams.put(key, value);
						defProperties.remove(key);
						log.config("Added default config parameter: ("
							+ key + "=" + value + ")");
					}
				}
			} catch (FileNotFoundException e) {
				log.warning("Given property file was not found: " + property_filename);
			} catch (IOException e) {
				log.log(Level.WARNING, "Can not read property file: "
					+ property_filename, e);
			}
		}
  }

	public Configurator(String fileName, String[] args) {
		parseArgs(args);
		repository = ConfigRepository.getConfigRepository(fileName);
		defConfigParams.putAll(getAllProperties(null));
		Set<String> prop_keys = defProperties.stringPropertyNames();
		for (String key: prop_keys) {
			int idx1 = key.indexOf("/");
			if (idx1 > 0) {
				String root = key.substring(0, idx1);
				String node = key.substring(idx1+1);
				String prop_key = null;
				int idx2 = node.lastIndexOf("/");
				if (idx2 > 0) {
					prop_key = node.substring(idx2+1);
					node = node.substring(0, idx2);
				} else {
					prop_key = node;
					node = null;
				}
				repository.set(root, node, prop_key, defProperties.getProperty(key));
				log.config("Added default config property: ("
					+ key + "=" + defProperties.getProperty(key) + ")");

			} else {
				log.warning("Ignoring default property, component part is missing: " + key);
			}
		}
	}

	public boolean isCorrectType(ServerComponent component) {
{'fraction_non_alphanumeric': 0.12637698898408814, 'fraction_numerical': 0.004742962056303549, 'mean_word_length': 4.4267330842673305, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7547812', 'n_tokens_mistral': 4484, 'n_tokens_neox': 4055, 'n_words': 1017}