<commit_before>/**
 * openHAB, the open Home Automation Bus.
 * Copyright (C) 2010, openHAB.org <<EMAIL>>
 *
 * See the contributors.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * 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, or (at your option) any later version.
 *
 * 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; if not, see <http://www.gnu.org/licenses>.
 *
 * Additional permission under GNU GPL version 3 section 7
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with Eclipse (or a modified version of that library),
 * containing parts covered by the terms of the Eclipse Public License
 * (EPL), the licensors of this Program grant you additional permission
 * to convey the resulting work.
 */

package org.openhab.config.core.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.openhab.config.core.ConfigConstants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ManagedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class provides a mean to read any kind of configuration data from a shared config
 * file and dispatch it to the different bundles using the {@link ConfigurationAdmin} service.
 * 
 * <p>The name of the configuration file can be provided as a program argument "openhab.configfile".
 * If this argument is not set, the default "configurations/openhab.cfg" will be used.
 * In case the configuration file does not exist, a warning will be logged and no action
 * will be performed.</p>
 * 
 * <p>The format of the configuration file is similar to a standard property file, with the
 * exception that the property name must be prefixed by the service pid of the {@link ManagedService}:</p>
 * <p>&lt;service-pid&gt;:&lt;property&gt;=&lt;value&gt;</p>
 * <p>The prefix "org.openhab" can be omitted on the service pid, it is automatically added if
 * the pid does not contain any "."</p> 
 * 
 * @author <NAME>
 * @since 0.3.0
 *
 */
public class ConfigDispatcher {

	private static final Logger logger = LoggerFactory.getLogger(ConfigDispatcher.class);

	@SuppressWarnings({ "unchecked", "rawtypes" })
	static public void initializeBundleConfigurations() {
		ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) ConfigActivator.configurationAdminTracker.getService();
		if(configurationAdmin!=null) {
			String configFilePath = getConfigurationFilePath();
			File configFile = new File(configFilePath);
			try {
				// we need to remember which configuration needs to be updated because values have changed.
				Map<Configuration, Dictionary> configsToUpdate = new HashMap<Configuration, Dictionary>();
				
				// also cache the already retrieved configurations for each pid
				Map<Configuration, Dictionary> configMap = new HashMap<Configuration, Dictionary>();
				
				List<String> lines = IOUtils.readLines(new FileInputStream(configFile));
				for(String line : lines) {					
					String[] contents = parseLine(configFilePath, line);
					// no valid configuration line, so continue
					if(contents==null) continue;
					String pid = contents[0];
					String property = contents[1];
					String value = contents[2];
					Configuration configuration = configurationAdmin.getConfiguration(pid, null);
					if(configuration!=null) {
						Dictionary configProperties = configMap.get(configuration);
						if(configProperties==null) {
							configProperties = new Properties();
							configMap.put(configuration, configProperties);
						}
						if(!value.equals(configProperties.get(property))) {
							configProperties.put(property, value);
							configsToUpdate.put(configuration, configProperties);
						}
					}
				}
				for(Entry<Configuration, Dictionary> entry : configsToUpdate.entrySet()) {
					entry.getKey().update(entry.getValue());
				}
			} catch (FileNotFoundException e) {
				logger.warn("Main openHAB configuration file '{}' does not exist.", configFilePath);
			} catch (IOException e) {
				logger.error("Main openHAB configuration file '{}' cannot be read.", configFilePath, e);
			}
			
		}		
	}

	private static String[] parseLine(final String filePath, final String line) {
		String trimmedLine = line.trim();
		if(trimmedLine.startsWith("#") || trimmedLine.isEmpty()) {
			return null;
		}
		if(trimmedLine.substring(1).contains(":")) { 
			String pid = StringUtils.substringBefore(line, ":");
			String rest = line.substring(pid.length() + 1);
			if(!pid.contains(".")) {
				pid = "org.openhab." + pid;
			}
			if(!rest.isEmpty() && rest.substring(1).contains("=")) {
				String property = StringUtils.substringBefore(rest, "=");
				String value = rest.substring(property.length() + 1);
				return new String[] { pid.trim(), property.trim(), value.trim() };
			}
		}
		logger.warn("Cannot parse line �{}� of main configuration file �{}�.", line, filePath);
		return null;
	}

	private static String getConfigurationFilePath() {
		String progArg = System.getProperty(ConfigConstants.CONFIG_FILE_PROG_ARGUMENT);
		if(progArg!=null) {
			return progArg;
		} else {
			return ConfigConstants.MAIN_CONFIG_FOLDER + "/" + ConfigConstants.DEFAULT_CONFIG_FILENAME;
		}
	}
}
<commit_msg>added config dispatcher<commit_after>/**
 * openHAB, the open Home Automation Bus.
 * Copyright (C) 2010, openHAB.org <<EMAIL>>
 *
 * See the contributors.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * 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, or (at your option) any later version.
 *
 * 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; if not, see <http://www.gnu.org/licenses>.
 *
 * Additional permission under GNU GPL version 3 section 7
 *
 * If you modify this Program, or any covered work, by linking or
 * combining it with Eclipse (or a modified version of that library),
 * containing parts covered by the terms of the Eclipse Public License
 * (EPL), the licensors of this Program grant you additional permission
 * to convey the resulting work.
 */

package org.openhab.config.core.internal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.openhab.config.core.ConfigConstants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ManagedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class provides a mean to read any kind of configuration data from a shared config
 * file and dispatch it to the different bundles using the {@link ConfigurationAdmin} service.
 * 
 * <p>The name of the configuration file can be provided as a program argument "openhab.configfile".
 * If this argument is not set, the default "configurations/openhab.cfg" will be used.
 * In case the configuration file does not exist, a warning will be logged and no action
 * will be performed.</p>
 * 
 * <p>The format of the configuration file is similar to a standard property file, with the
 * exception that the property name must be prefixed by the service pid of the {@link ManagedService}:</p>
 * <p>&lt;service-pid&gt;:&lt;property&gt;=&lt;value&gt;</p>
 * <p>The prefix "org.openhab" can be omitted on the service pid, it is automatically added if
 * the pid does not contain any "."</p> 
 * 
 * @author <NAME>
 * @since 0.3.0
 *
 */
public class ConfigDispatcher {

	private static final Logger logger = LoggerFactory.getLogger(ConfigDispatcher.class);

	@SuppressWarnings({ "unchecked", "rawtypes" })
	static public void initializeBundleConfigurations() {
		ConfigurationAdmin configurationAdmin = (ConfigurationAdmin) ConfigActivator.configurationAdminTracker.getService();
		if(configurationAdmin!=null) {
			String configFilePath = getConfigurationFilePath();
			File configFile = new File(configFilePath);
			try {
				// we need to remember which configuration needs to be updated because values have changed.
				Map<Configuration, Dictionary> configsToUpdate = new HashMap<Configuration, Dictionary>();
				
				// also cache the already retrieved configurations for each pid
				Map<Configuration, Dictionary> configMap = new HashMap<Configuration, Dictionary>();
				
				List<String> lines = IOUtils.readLines(new FileInputStream(configFile));
				for(String line : lines) {					
					String[] contents = parseLine(configFilePath, line);
					// no valid configuration line, so continue
					if(contents==null) continue;
					String pid = contents[0];
					String property = contents[1];
					String value = contents[2];
					Configuration configuration = configurationAdmin.getConfiguration(pid, null);
					if(configuration!=null) {
						Dictionary configProperties = configMap.get(configuration);
						if(configProperties==null) {
							configProperties = new Properties();
							configMap.put(configuration, configProperties);
						}
						if(!value.equals(configProperties.get(property))) {
							configProperties.put(property, value);
							configsToUpdate.put(configuration, configProperties);
						}
					}
				}
				for(Entry<Configuration, Dictionary> entry : configsToUpdate.entrySet()) {
					entry.getKey().update(entry.getValue());
				}
			} catch (FileNotFoundException e) {
				logger.warn("Main openHAB configuration file '{}' does not exist.", configFilePath);
			} catch (IOException e) {
				logger.error("Main openHAB configuration file '{}' cannot be read.", configFilePath, e);
			}
			
		}		
	}

	private static String[] parseLine(final String filePath, final String line) {
		String trimmedLine = line.trim();
		if(trimmedLine.startsWith("#") || trimmedLine.isEmpty()) {
			return null;
		}
		if(trimmedLine.substring(1).contains(":")) { 
			String pid = StringUtils.substringBefore(line, ":");
			String rest = line.substring(pid.length() + 1);
			if(!pid.contains(".")) {
				pid = "org.openhab." + pid;
			}
			if(!rest.isEmpty() && rest.substring(1).contains("=")) {
				String property = StringUtils.substringBefore(rest, "=");
				String value = rest.substring(property.length() + 1);
				return new String[] { pid.trim(), property.trim(), value.trim() };
			}
		}
		logger.warn("Cannot parse line '{}' of main configuration file '{}'.", line, filePath);
		return null;
	}

	private static String getConfigurationFilePath() {
		String progArg = System.getProperty(ConfigConstants.CONFIG_FILE_PROG_ARGUMENT);
		if(progArg!=null) {
			return progArg;
		} else {
			return ConfigConstants.MAIN_CONFIG_FOLDER + "/" + ConfigConstants.DEFAULT_CONFIG_FILENAME;
		}
	}
}

{'fraction_non_alphanumeric': 0.1027719241368131, 'fraction_numerical': 0.0030799157075701088, 'mean_word_length': 3.711340206185567, 'pattern_counts': {'":': 4, '<': 39, '<?xml version=': 0, '>': 39, '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': '1120726', 'n_tokens_mistral': 3744, 'n_tokens_neox': 3268, 'n_words': 1126}