001package co.codewizards.cloudstore.core.config;
002
003import java.util.List;
004import java.util.Map;
005import java.util.Properties;
006import java.util.regex.Matcher;
007import java.util.regex.Pattern;
008
009import co.codewizards.cloudstore.core.appid.AppIdRegistry;
010
011/**
012 * Configuration of CloudStore supporting inheritance of settings.
013 * <p>
014 * Obtain an instance via one of the following methods:
015 * <ul>
016 * <li>{@link ConfigImpl#getInstance()}
017 * <li>{@link ConfigImpl#getInstanceForDirectory(co.codewizards.cloudstore.core.oio.File)}
018 * <li>{@link ConfigImpl#getInstanceForFile(co.codewizards.cloudstore.core.oio.File)}
019 * </ul>
020 * <p>
021 * There is one {@code Config} instance available (lazily created, cached temporarily) for every
022 * directory and every file in a repository. Each {@code Config} inherits the settings from the
023 * parent-directory, if not explicitly overwritten.
024 * <p>
025 * The configuration is based on {@link Properties} files. Every property file is optional. If it
026 * does not exist, all settings are inherited. If it does exist, only those properties contained in
027 * the file are overriden. All properties not contained in the file are still inherited. Inheritance
028 * is thus applicable on every individual property.
029 * <p>
030 * Modifications, deletions, creations of properties files are detected during runtime (pretty immediately).
031 * Note, that this detection is based on the files' timestamps. Since most file systems have a granularity
032 * of 1 second (some even 2) for the last-modified-timestamp, multiple modifications in the same second might
033 * not be detected.
034 * <p>
035 * There is a global properties file in the user's home directory (or wherever {@link ConfigDir}
036 * points to): <code>&#36;{user.home}/.cloudstore/cloudstore.properties</code>
037 * <p>
038 * Additionally, every directory can optionally contain the following files:
039 * <ol>
040 * <li><code>.cloudstore.properties</code>
041 * <li><code>cloudstore.properties</code>
042 * <li><code>.&#36;{anyFileName}.cloudstore.properties</code>
043 * <li><code>&#36;{anyFileName}.cloudstore.properties</code>
044 * </ol>
045 * <p>
046 * The files 1. and 2. are applicable to the entire directory and all sub-directories and files in it.
047 * Usually, on GNU/Linux people will prefer 1., but when using Windows, files starting with a "." are
048 * sometimes a bit hard to deal with. Therefore, we support both. The file 2. overrides the settings of file 1..
049 * <p>
050 * The files 3. and 4. are applicable only to the file <code>&#36;{anyFileName}</code>. Thus, if you want
051 * to set special behaviour for the file <code>example.db</code> only, you can create the file
052 * <code>.example.db.cloudstore.properties</code> in the same directory.
053 *
054 * @author Marco หงุ่ยตระกูล-Schulze - marco at codewizards dot co
055 */
056public interface Config {
057        String APP_ID_SIMPLE_ID = AppIdRegistry.getInstance().getAppIdOrFail().getSimpleId();
058
059        String PROPERTIES_FILE_NAME_SUFFIX = ".properties";
060
061        String PROPERTIES_FILE_NAME_FOR_DIRECTORY_LOCAL = '.' + APP_ID_SIMPLE_ID + ".local" + PROPERTIES_FILE_NAME_SUFFIX;
062
063        String PROPERTIES_FILE_NAME_FOR_DIRECTORY = '.' + APP_ID_SIMPLE_ID + PROPERTIES_FILE_NAME_SUFFIX;
064
065        String PROPERTIES_FILE_NAME_PARENT_PREFIX = "parent.";
066
067        String PROPERTIES_FILE_NAME_PARENT = "parent" + PROPERTIES_FILE_NAME_SUFFIX;
068
069        /**
070         * Prefix used for system properties overriding configuration entries.
071         * <p>
072         * Every property in the configuration (i.e. in its properties files) can be overridden
073         * by a corresponding system property. The system property must be prefixed.
074         * <p>
075         * For example, to override the configuration property with the key "deferrableExecutor.timeout",
076         * you can pass the system property "cloudstore.deferrableExecutor.timeout" to the JVM. If the
077         * system property exists, the configuration is not consulted, but the system property value is
078         * used as shortcut.
079         * <p>
080         * Additionally, it is possible to override configuration entries via OS environment variables.
081         * Since an env var's name must not contain a dot ("."), all dots are replaced by underscores ("_").
082         */
083        String SYSTEM_PROPERTY_PREFIX = APP_ID_SIMPLE_ID + '.';
084
085        /**
086         * Gets the property identified by the given key.
087         * <p>
088         * This method directly delegates to {@link Properties#getProperty(String, String)}.
089         * Thus, an empty String in the internal {@code Properties} is returned instead of the
090         * given {@code defaultValue}. The {@code defaultValue} is only returned, if neither
091         * the internal {@code Properties} of this {@code Config} nor any of its parents contains
092         * the entry.
093         * <p>
094         * <b>Important:</b> This is often not the desired behaviour. You might want to use
095         * {@link #getPropertyAsNonEmptyTrimmedString(String, String)} instead!
096         * <p>
097         * Every property can be overwritten by a system property prefixed with {@value #SYSTEM_PROPERTY_PREFIX}.
098         * If - for example - the key "updater.force" is to be read and a system property
099         * named "cloudstore.updater.force" is set, this system property is returned instead!
100         * @param key the key identifying the property. Must not be <code>null</code>.
101         * @param defaultValue the default value to fall back to, if neither this {@code Config}'s
102         * internal {@code Properties} nor any of its parents contains a matching entry.
103         * May be <code>null</code>.
104         * @return the property's value. Never <code>null</code> unless {@code defaultValue} is <code>null</code>.
105         * @see #getPropertyAsNonEmptyTrimmedString(String, String)
106         */
107        String getProperty(final String key, final String defaultValue);
108
109        /**
110         * Gets the property identified by the given key; <b>not</b> taking inheritance into account.
111         * <p>
112         * This method corresponds to {@link #getProperty(String, String)}, but it does not fall back
113         * to any inherited value.
114         * <p>
115         * <b>Important:</b> This method should never be used in order to control the behaviour of the
116         * application! It is intended for use of administrative tools / UIs which need to read/write
117         * directly.
118         *
119         * @param key the key identifying the property. Must not be <code>null</code>.
120         * @return the property's value. <code>null</code>, if the property is not set.
121         * @see #setDirectProperty(String, String)
122         */
123        String getDirectProperty(final String key);
124
125        /**
126         * Sets the property identified by the given key; <b>not</b> taking inheritance into account.
127         * @param key the key identifying the property. Must not be <code>null</code>.
128         * @param value the property's value. <code>null</code> removes the property from this concrete
129         * configuration instance.
130         * @see #getDirectProperty(String)
131         */
132        void setDirectProperty(final String key, final String value);
133
134        /**
135         * Gets the property identified by the given key; {@linkplain String#trim() trimmed}.
136         * <p>
137         * In contrast to {@link #getProperty(String, String)}, this method falls back to the given
138         * {@code defaultValue}, if the internal {@code Properties} contains an empty {@code String}
139         * (after trimming) as value for the given {@code key}.
140         * <p>
141         * It therefore means that a value set to an empty {@code String} in the properties file means
142         * to use the program's default instead. It is therefore consistent with
143         * {@link #getPropertyAsLong(String, long)} and all other {@code getPropertyAs...(...)}
144         * methods.
145         * <p>
146         * The same rules apply to the fall-back-strategy from system property to environment variable and
147         * finally config files.
148         * <p>
149         * Every property can be overwritten by a system property prefixed with {@value #SYSTEM_PROPERTY_PREFIX}.
150         * If - for example - the key "updater.force" is to be read and a system property
151         * named "cloudstore.updater.force" is set, this system property is returned instead!
152         * @param key the key identifying the property. Must not be <code>null</code>.
153         * @param defaultValue the default value to fall back to, if neither this {@code Config}'s
154         * internal {@code Properties} nor any of its parents contains a matching entry or
155         * if this entry's value is an empty {@code String}.
156         * May be <code>null</code>.
157         * @return the property's value. Never <code>null</code> unless {@code defaultValue} is <code>null</code>.
158         */
159        String getPropertyAsNonEmptyTrimmedString(final String key, final String defaultValue);
160
161        long getPropertyAsLong(final String key, final long defaultValue);
162
163        long getPropertyAsPositiveOrZeroLong(final String key, final long defaultValue);
164
165        int getPropertyAsInt(final String key, final int defaultValue);
166
167        int getPropertyAsPositiveOrZeroInt(final String key, final int defaultValue);
168
169        /**
170         * Gets the property identified by the given key.
171         * @param key the key identifying the property. Must not be <code>null</code>.
172         * @param defaultValue the default value to fall back to, if neither this {@code Config}'s
173         * internal {@code Properties} nor any of its parents contains a matching entry or
174         * if this entry's value does not match any possible enum value. Must not be <code>null</code>.
175         * If a <code>null</code> default value is required, use {@link #getPropertyAsEnum(String, Class, Enum)}
176         * instead!
177         * @return the property's value. Never <code>null</code>.
178         * @see #getPropertyAsEnum(String, Class, Enum)
179         * @see #getPropertyAsNonEmptyTrimmedString(String, String)
180         */
181        <E extends Enum<E>> E getPropertyAsEnum(final String key, final E defaultValue);
182
183        /**
184         * Gets the property identified by the given key.
185         * @param key the key identifying the property. Must not be <code>null</code>.
186         * @param enumClass the enum's type. Must not be <code>null</code>.
187         * @param defaultValue the default value to fall back to, if neither this {@code Config}'s
188         * internal {@code Properties} nor any of its parents contains a matching entry or
189         * if this entry's value does not match any possible enum value. May be <code>null</code>.
190         * @return the property's value. Never <code>null</code> unless {@code defaultValue} is <code>null</code>.
191         * @see #getPropertyAsEnum(String, Enum)
192         * @see #getPropertyAsNonEmptyTrimmedString(String, String)
193         */
194        <E extends Enum<E>> E getPropertyAsEnum(final String key, final Class<E> enumClass, final E defaultValue);
195
196        boolean getPropertyAsBoolean(final String key, final boolean defaultValue);
197
198        /**
199         * Gets a version number that is guaranteed to be changed whenever the underlying files change.
200         * <p>
201         * It is <i>not</i> guaranteed to be incremented! Depending on the underlying change, a newer
202         * version number might be less than a previous version number! In most cases, however, the
203         * version number actually grows with each change. Code must not rely on this, but it is a
204         * helpful assumption for debugging.
205         * @return a version number that is guaranteed to be changed whenever the underlying files change.
206         */
207        long getVersion();
208
209        /**
210         * Gets all config-property-keys matching the given regular expression.
211         * <p>
212         * Just like {@link #getProperty(String, String)}, this method takes inheritance into account:
213         * It collects keys from the current {@code Config} instance and all its parents, recursively.
214         * <p>
215         * Note, that {@link Matcher#matches()} is used, i.e. the {@code regex} must match the entire
216         * config-key.
217         * <p>
218         * The given {@code regex} may contain capturing groups, whose results are returned in the {@code Map}'s
219         * values. Note, that the entire match (which is by convention the group 0) is ignored in the values,
220         * because it is the {@code Map}'s key, already. Hence, the first entry in the {@code Map}'s values
221         * (with index 0) corresponds to the first capturing group in the regular expression.
222         * <p>
223         * For example, let's say the regex is "ignore\[([^]]*)\]\.(.*)" and the following matching properties
224         * exist:
225         * <ul>
226         * <li>ignore[backup-file].namePattern=*.bak</li>
227         * <li>ignore[backup-file].enabled=false</li>
228         * <li>ignore[class-file].namePattern=*.class</li>
229         * </ul>
230         * <p>
231         * This leads to a resulting map with the following entries:
232         * <ul>
233         * <li>key: "ignore[backup-file].namePattern", value: "backup-file", "namePattern"</li>
234         * <li>key: "ignore[backup-file].enabled", value: "backup-file", "enabled"</li>
235         * <li>key: "ignore[class-file].namePattern", value: "class-file", "namePattern"</li>
236         * </ul>
237         * <p>
238         * @param regex the regular expression to look for. Must not be <code>null</code>.
239         * @return the keys found together with capturing groups. Never <code>null</code>.
240         */
241        Map<String, List<String>> getKey2GroupsMatching(Pattern regex);
242}