James Moger
2014-03-21 53a83f67aef203379e43a9ed89ccfcb16c16200e
Implement setting removal for configuration settings
5 files modified
78 ■■■■■ changed files
src/main/java/com/gitblit/FileSettings.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/IStoredSettings.java 22 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/WebXmlSettings.java 30 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/models/ServerSettings.java 4 ●●●● patch | view | raw | blame | history
src/test/java/com/gitblit/tests/mock/MemorySettings.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/FileSettings.java
@@ -103,6 +103,23 @@
        return properties;
    }
    @Override
    public boolean saveSettings() {
        String content = FileUtils.readContent(propertiesFile, "\n");
        for (String key : removals) {
            String regex = "(?m)^(" + regExEscape(key) + "\\s*+=\\s*+)"
                    + "(?:[^\r\n\\\\]++|\\\\(?:\r?\n|\r|.))*+$";
            content = content.replaceAll(regex, "");
        }
        removals.clear();
        FileUtils.writeContent(propertiesFile, content);
        // manually set the forceReload flag because not all JVMs support real
        // millisecond resolution of lastModified. (issue-55)
        forceReload = true;
        return true;
    }
    /**
     * Updates the specified settings in the settings file.
     */
src/main/java/com/gitblit/IStoredSettings.java
@@ -20,6 +20,8 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,6 +39,8 @@
    protected final Logger logger;
    protected final Properties overrides = new Properties();
    protected final Set<String> removals = new TreeSet<String>();
    public IStoredSettings(Class<? extends IStoredSettings> clazz) {
        logger = LoggerFactory.getLogger(clazz);
@@ -353,6 +357,24 @@
    }
    /**
     * Remove a setting.
     *
     * @param key
     */
    public void removeSetting(String key) {
        getSettings().remove(key);
        overrides.remove(key);
        removals.add(key);
    }
    /**
     * Saves the current settings.
     *
     * @param map
     */
    public abstract boolean saveSettings();
    /**
     * Updates the values for the specified keys and persists the entire
     * configuration file.
     *
src/main/java/com/gitblit/WebXmlSettings.java
@@ -80,6 +80,36 @@
    }
    @Override
    public synchronized boolean saveSettings() {
        try {
            Properties props = new Properties();
            // load pre-existing web-configuration
            if (overrideFile.exists()) {
                InputStream is = new FileInputStream(overrideFile);
                props.load(is);
                is.close();
            }
            // put all new settings and persist
            for (String key : removals) {
                props.remove(key);
            }
            removals.clear();
            OutputStream os = new FileOutputStream(overrideFile);
            props.store(os, null);
            os.close();
            // override current runtime settings
            properties.clear();
            properties.putAll(props);
            return true;
        } catch (Throwable t) {
            logger.error("Failed to save settings!", t);
        }
        return false;
    }
    @Override
    public synchronized boolean saveSettings(Map<String, String> settings) {
        try {
            Properties props = new Properties();
src/main/java/com/gitblit/models/ServerSettings.java
@@ -58,4 +58,8 @@
    public boolean hasKey(String key) {
        return settings.containsKey(key);
    }
    public SettingModel remove(String key) {
        return settings.remove(key);
    }
}
src/test/java/com/gitblit/tests/mock/MemorySettings.java
@@ -48,6 +48,11 @@
    }
    @Override
    public boolean saveSettings() {
        return false;
    }
    @Override
    public boolean saveSettings(Map<String, String> updatedSettings) {
        return false;
    }