summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/config/Config.java
blob: fea75e03ace1ce1db8e08faf4c268fac87e6a757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.grow.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Manage configuration for an application.
 *
 * Config reads one or more property files as the application config. Duplicate
 * properties loaded later override properties loaded earlier. Config has the
 * concept of a domain to distinguish settings for development and production.
 * The default domain is prod for production. Domain can be any String such as
 * dev for development or test for testing.
 *
 * The property files are processed like java.util.Properties except that the
 * keys are specified as DOMAIN.KEY. An asterisk (*) can be used in place of a
 * domain to indicate it should apply to all domains. If a domain specific entry
 * exists for the current domain, it will override any global config.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class Config {
    private static final Logger LOG = Logger.getLogger(Config.class);

    private String mDomain;
    private Properties mProperties;

    /**
     * Construct a new Config object. Domain defaults to prod.
     */
    public Config() {
        mDomain = "prod";
        mProperties = new Properties();

    }

    /**
     * Change the domain from the default string "prod".
     *
     * @param domain The new domain.
     */
    public void setDomain(String domain) {
        LOG.info("Setting Config domain to " + domain);
        mDomain = domain;
    }

    /**
     * Load properties from a file.
     * Any exceptions are logged and suppressed.
     */
    public void updateConfig(String propertyFilename) {
        final File propFile = new File(propertyFilename);

        LOG.info("Loading properties from " + propFile);

        try {
            InputStream in = new FileInputStream(propFile);
            updateConfig(in);

        } catch (IOException e) {
            LOG.error("Could not load properties file: " + e.getMessage(), e);
        }
    }

    /**
     * Load properties from an InputStream.
     * This method closes the InputStream when it completes.
     *
     * @param in The InputStream
     */
    public void updateConfig(InputStream in) throws IOException {
        LOG.info("Loading properties from InputStream");
        mProperties.load(in);
        in.close();
    }

    /**
     * Get a String from the config.
     *
     * @return The config value or null if it is not found.
     */
    public String getString(String key) {
        return getString(key, null);
    }

    /**
     * Get a String from the config.
     *
     * @return The config value or defaultValue if it can not be found.
     */
    public String getString(final String key, final String defaultValue) {
        String result;

        final String domainKey = mDomain + "." + key;
        result = mProperties.getProperty(domainKey);
        if (result != null) {
            LOG.debug("Reading config for key = { " + key + " }. Got result = { " + result + " }");
            return result;
        }

        final String globalKey = "*." + key;
        result = mProperties.getProperty(globalKey);
        if (result != null) {
            LOG.debug("Reading config for key = { " + key + " }. Got result = { " + result + " }");
            return result;
        }

        LOG.debug("Reading config for key = { " + key + " }. Got default value = { " + defaultValue + " }");
        return defaultValue;
    }

    /**
     * Get an integer from the config.
     *
     * @return The config value or Integer.MIN_VALUE if the key is not present or the
     *         config can not be parsed.
     */
    public int getInt(String key) {
        return getInt(key, Integer.MIN_VALUE);
    }

    /**
     * Get an integer from the config.
     *
     * @return The config value or defaultValue if the key is not present or the
     *         config can not be parsed.
     */
    public int getInt(String key, int defaultValue) {
        final String propertyValue = getString(key);

        if (propertyValue != null) {
            try {
                final int result = Integer.valueOf(propertyValue);
                return result;

            } catch (NumberFormatException e) {
                LOG.warn("Expected property to be an integer: "
                        + key + " = { " + propertyValue + " }");
            }
        }

        return defaultValue;
    }
}