<commit_before>package brooklyn.config;

import groovy.lang.Closure;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brooklyn.config.ConfigKey.HasConfigKey;
import brooklyn.event.basic.BasicConfigKey;
import brooklyn.util.MutableMap;
import brooklyn.util.ResourceUtils;
import brooklyn.util.flags.TypeCoercions;

import com.google.common.base.Predicate;
import com.google.common.base.Throwables;

/** utils for accessing command-line and system-env properties;
 * doesn't resolve anything (unless an execution context is supplied)
 * and treats ConfigKeys as of type object when in doubt,
 * or string when that is likely wanted (e.g. {@link #getFirst(Map, String...)}
 * <p>
 * TODO methods in this class are not thread safe.
 * intention is that they are set during startup and not modified thereafter. */
@SuppressWarnings("rawtypes")
public class BrooklynProperties extends LinkedHashMap implements StringConfigMap {

    private static final long serialVersionUID = -945875483083108978L;
    protected static final Logger LOG = LoggerFactory.getLogger(BrooklynProperties.class);

    public static class Factory {
        public static BrooklynProperties newEmpty() {
            return new BrooklynProperties();
        }
        /** @deprecated since 0.4.0 use newDefault or newEm=pty */
        public static BrooklynProperties newWithSystemAndEnvironment() {
            return newDefault();
        }

        public static BrooklynProperties newDefault() {
            BrooklynProperties properties = new BrooklynProperties();

            addGlobalProperties(properties);

            properties.addEnvironmentVars();

            properties.addSystemProperties();

            return properties;
        }

        private static void addGlobalProperties(BrooklynProperties p) {
            File globalPropertiesFile = new File(
                    p.getFirst(MutableMap.of("defaultIfNone", "/"), "user.home", "HOME")+File.separatorChar+".brooklyn"+File.separatorChar+"brooklyn.properties");

            if (globalPropertiesFile.exists()) {
                p.addFrom(globalPropertiesFile);
            }
        }
    }

    protected BrooklynProperties() {
    }

    @SuppressWarnings("unchecked")
    public BrooklynProperties addEnvironmentVars() {
        putAll(System.getenv());
        return this;
    }
    @SuppressWarnings("unchecked")
    public BrooklynProperties addSystemProperties() {
        putAll(System.getProperties());
        return this;
    }

    @SuppressWarnings("unchecked")
    public BrooklynProperties addFrom(InputStream i) {
        Properties p = new Properties();
        try {
            p.load(i);
        } catch (IOException e) {<commit_msg>Resolved issue where the user.home is determined based on the wrong properties. It should always make use of the System.properties<commit_after>package brooklyn.config;

import groovy.lang.Closure;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brooklyn.config.ConfigKey.HasConfigKey;
import brooklyn.event.basic.BasicConfigKey;
import brooklyn.util.MutableMap;
import brooklyn.util.ResourceUtils;
import brooklyn.util.flags.TypeCoercions;

import com.google.common.base.Predicate;
import com.google.common.base.Throwables;

/** utils for accessing command-line and system-env properties;
 * doesn't resolve anything (unless an execution context is supplied)
 * and treats ConfigKeys as of type object when in doubt,
 * or string when that is likely wanted (e.g. {@link #getFirst(Map, String...)}
 * <p>
 * TODO methods in this class are not thread safe.
 * intention is that they are set during startup and not modified thereafter. */
@SuppressWarnings("rawtypes")
public class BrooklynProperties extends LinkedHashMap implements StringConfigMap {

    private static final long serialVersionUID = -945875483083108978L;
    protected static final Logger LOG = LoggerFactory.getLogger(BrooklynProperties.class);

    public static class Factory {
        public static BrooklynProperties newEmpty() {
            return new BrooklynProperties();
        }
        /** @deprecated since 0.4.0 use newDefault or newEm=pty */
        public static BrooklynProperties newWithSystemAndEnvironment() {
            return newDefault();
        }

        public static BrooklynProperties newDefault() {
            BrooklynProperties properties = new BrooklynProperties();

            addGlobalProperties(properties);

            properties.addEnvironmentVars();

            properties.addSystemProperties();

            return properties;
        }

        private static void addGlobalProperties(BrooklynProperties p) {
            String userHome = System.getProperty("user.home");
            File globalPropertiesFile = new File(userHome+File.separatorChar+".brooklyn"+File.separatorChar+"brooklyn.properties");

            if (globalPropertiesFile.exists()) {
                p.addFrom(globalPropertiesFile);
            }
        }
    }

    protected BrooklynProperties() {
    }

    @SuppressWarnings("unchecked")
    public BrooklynProperties addEnvironmentVars() {
        putAll(System.getenv());
        return this;
    }
    @SuppressWarnings("unchecked")
    public BrooklynProperties addSystemProperties() {
        putAll(System.getProperties());
        return this;
    }

    @SuppressWarnings("unchecked")
    public BrooklynProperties addFrom(InputStream i) {
        Properties p = new Properties();
        try {
            p.load(i);
        } catch (IOException e) {
{'fraction_non_alphanumeric': 0.08856447688564477, 'fraction_numerical': 0.00746147607461476, 'mean_word_length': 3.3514467184191954, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4151138', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1578, 'n_words': 429}