<commit_before>/*
 * Copyright (c) JForum Team. All rights reserved.
 *
 * The software in this package is published under the terms of the LGPL
 * license a copy of which has been included with this distribution in the
 * license.txt file.
 *
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import net.jforum.core.UrlPattern;
import net.jforum.core.exceptions.ForumException;
import net.jforum.entities.Config;
import net.jforum.repository.ConfigRepository;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

/**
 * @author <NAME>
 * @author <NAME> <NAME>
 */
public class JForumConfig extends PropertiesConfiguration {
	private Map<String, UrlPattern> urlPatterns = new HashMap<String, UrlPattern>();
	private ConfigRepository configRepository;

	public JForumConfig(ConfigRepository configRepository, HibernateAwareTask hibernateTask) {
		this.setReloadingStrategy(new FileChangedReloadingStrategy());
		this.setDelimiterParsingDisabled(true);

		try {
			loadProps();

			hibernateTask.execute(new HibernateRunnable() {
				public void run() {
					try {
						loadDatabaseProperties();
					}
					catch (Exception e) {
						throw new ForumException(e);
					}
				}
			});
		}
		catch (Exception e) {
			throw new ForumException(e);
		}

		this.loadUrlPatterns();
	}

	public JForumConfig() {
		try {
			loadProps();
		} catch (Exception e) {
			throw new ForumException(e);
		}
	}

	private void loadProps() throws ConfigurationException, Exception {
		this.load(this.getClass().getResourceAsStream("/jforumConfig/SystemGlobals.properties"));
		this.loadCustomProperties();
	}

	private void loadCustomProperties() throws Exception {
		InputStream is = this.getClass().getResourceAsStream("/jforumConfig/jforum-custom.properties");

		if (is != null) {
			Properties custom = new Properties();
			custom.load(is);

			for (Enumeration<?> e = custom.keys(); e.hasMoreElements(); ) {
				String key = (String)e.nextElement();
				this.clearProperty(key);
				this.addProperty(key, custom.get(key));
			}
		}
	}

	private void loadDatabaseProperties() throws Exception{
		if (configRepository != null) {
			List<Config> databasesProperties = this.configRepository.getAll();

			for (Config config : databasesProperties) {
				this.clearProperty(config.getName());
				this.addProperty(config.getName(), config.getValue());
			}
		}
	}

	/**
	 * @see org.apache.commons.configuration.BaseConfiguration#addPropertyDirect(java.lang.String, java.lang.Object)
	 */
	@Override
	protected void addPropertyDirect(String key, Object value) {
		super.addPropertyDirect(key, value);
	}

	public UrlPattern getUrlPattern(String name) {
		return this.urlPatterns.get(name);
	}

	/**
	 * Gets the complete path to the application root directory
	 * @return the path to the root directory
	 */
	public String getApplicationPath() {
		return this.getString(ConfigKeys.APPLICATION_PATH);
	}

	/**
	 * Delegates to {@link #getString(String)}
	 * @param key the key to retrieve
	 * @return the key's value
	 */
	public String getValue(String key) {
		return this.getString(key);
	}

	/**
	 * Load the urlPatterns
	 */
	private void loadUrlPatterns() {
		FileInputStream fis = null;

		try {
			Properties p = new Properties();
			p.load(this.getClass().getResourceAsStream("/jforumConfig/urlPattern.properties"));

			for (Entry<Object, Object> entry : p.entrySet()) {
				String name = (String)entry.getKey();
				String value = (String)entry.getValue();

				this.urlPatterns.put(name, new UrlPattern(value));
			}
		}
		catch (IOException e) {
			throw new ForumException(e);
		}
		finally {
			if (fis != null) {
				try { fis.close(); } catch (Exception e) {}
			}
		}
	}
}
<commit_msg>in test environment, hibernateTask could be null in JForumConfig constructor<commit_after>/*
 * Copyright (c) JForum Team. All rights reserved.
 *
 * The software in this package is published under the terms of the LGPL
 * license a copy of which has been included with this distribution in the
 * license.txt file.
 *
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import net.jforum.core.UrlPattern;
import net.jforum.core.exceptions.ForumException;
import net.jforum.entities.Config;
import net.jforum.repository.ConfigRepository;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class JForumConfig extends PropertiesConfiguration {
	private Map<String, UrlPattern> urlPatterns = new HashMap<String, UrlPattern>();
	private ConfigRepository configRepository;

	public JForumConfig(ConfigRepository configRepository, HibernateAwareTask hibernateTask) {
		this.setReloadingStrategy(new FileChangedReloadingStrategy());
		this.setDelimiterParsingDisabled(true);

		try {
			loadProps();

			//in test environment, hibernateTask could be null 
			if(hibernateTask != null){
				hibernateTask.execute(new HibernateRunnable() {
					public void run() {
						try {
							loadDatabaseProperties();
						}
						catch (Exception e) {
							throw new ForumException(e);
						}
					}
				});
			}
		}
		catch (Exception e) {
			throw new ForumException(e);
		}

		this.loadUrlPatterns();
	}

	public JForumConfig() {
		try {
			loadProps();
		} catch (Exception e) {
			throw new ForumException(e);
		}
	}

	private void loadProps() throws ConfigurationException, Exception {
		this.load(this.getClass().getResourceAsStream("/jforumConfig/SystemGlobals.properties"));
		this.loadCustomProperties();
	}

	private void loadCustomProperties() throws Exception {
		InputStream is = this.getClass().getResourceAsStream("/jforumConfig/jforum-custom.properties");

		if (is != null) {
			Properties custom = new Properties();
			custom.load(is);

			for (Enumeration<?> e = custom.keys(); e.hasMoreElements(); ) {
				String key = (String)e.nextElement();
				this.clearProperty(key);
				this.addProperty(key, custom.get(key));
			}
		}
	}

	private void loadDatabaseProperties() throws Exception{
		if (configRepository != null) {
			List<Config> databasesProperties = this.configRepository.getAll();

			for (Config config : databasesProperties) {
				this.clearProperty(config.getName());
				this.addProperty(config.getName(), config.getValue());
			}
		}
	}

	/**
	 * @see org.apache.commons.configuration.BaseConfiguration#addPropertyDirect(java.lang.String, java.lang.Object)
	 */
	@Override
	protected void addPropertyDirect(String key, Object value) {
		super.addPropertyDirect(key, value);
	}

	public UrlPattern getUrlPattern(String name) {
		return this.urlPatterns.get(name);
	}

	/**
	 * Gets the complete path to the application root directory
	 * @return the path to the root directory
	 */
	public String getApplicationPath() {
		return this.getString(ConfigKeys.APPLICATION_PATH);
	}

	/**
	 * Delegates to {@link #getString(String)}
	 * @param key the key to retrieve
	 * @return the key's value
	 */
	public String getValue(String key) {
		return this.getString(key);
	}

	/**
	 * Load the urlPatterns
	 */
	private void loadUrlPatterns() {
		FileInputStream fis = null;

		try {
			Properties p = new Properties();
			p.load(this.getClass().getResourceAsStream("/jforumConfig/urlPattern.properties"));

			for (Entry<Object, Object> entry : p.entrySet()) {
				String name = (String)entry.getKey();
				String value = (String)entry.getValue();

				this.urlPatterns.put(name, new UrlPattern(value));
			}
		}
		catch (IOException e) {
			throw new ForumException(e);
		}
		finally {
			if (fis != null) {
				try { fis.close(); } catch (Exception e) {}
			}
		}
	}
}

{'fraction_non_alphanumeric': 0.11858744929611072, 'fraction_numerical': 0.0, 'mean_word_length': 4.15559655596556, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '4035293', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2460, 'n_words': 597}