Example usage for org.apache.commons.configuration FileConfiguration save

List of usage examples for org.apache.commons.configuration FileConfiguration save

Introduction

In this page you can find the example usage for org.apache.commons.configuration FileConfiguration save.

Prototype

void save() throws ConfigurationException;

Source Link

Document

Save the configuration.

Usage

From source file:gda.util.persistence.LocalParametersTest.java

public void testThreadSafety() throws Exception {
    final File testScratchDir = TestUtils.createClassScratchDirectory(LocalParametersTest.class);
    final String configDir = testScratchDir.getAbsolutePath();

    final String configName = "threadsafety";

    // Delete config from disk, if it exists
    final File configFile = new File(configDir, configName + ".xml");
    configFile.delete();// w  w  w .j  a  va2 s  . com

    final AtomicReference<Exception> error = new AtomicReference<Exception>();

    final int numThreads = 4;
    final long threadRunTimeInMs = 5000;
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(numThreads);

    for (int i = 0; i < numThreads; i++) {
        Thread t = new Thread() {
            @Override
            public void run() {

                try {
                    final FileConfiguration config = LocalParameters.getThreadSafeXmlConfiguration(configDir,
                            configName, true);

                    // Wait for signal to start
                    startLatch.await();

                    final String propertyName = Thread.currentThread().getName();

                    final long startTime = System.currentTimeMillis();
                    while (true) {

                        // Finish if we've exceeded the run time
                        long elapsedTime = System.currentTimeMillis() - startTime;
                        if (elapsedTime >= threadRunTimeInMs) {
                            break;
                        }

                        // Finish if another thread has generated an exception
                        if (error.get() != null) {
                            break;
                        }

                        config.setProperty(propertyName, System.currentTimeMillis());
                        config.save();
                    }
                } catch (Exception e) {
                    e.printStackTrace(System.err);
                    error.set(e);
                }

                finishLatch.countDown();
            }
        };
        t.start();
    }

    // Start all threads
    final long startTime = System.currentTimeMillis();
    startLatch.countDown();

    // Wait for all threads to finish
    finishLatch.await();
    final long endTime = System.currentTimeMillis();
    final long elapsedTime = (endTime - startTime);
    System.out.printf("Finished after %dms%n", elapsedTime);

    // No error should have been thrown
    assertNull("An exception was thrown by one of the threads", error.get());
}

From source file:com.manydesigns.portofino.actions.admin.SettingsAction.java

@Button(list = "settings", key = "update", order = 1, type = Button.TYPE_PRIMARY)
public Resolution update() {
    setupFormAndBean();/*from  w  w  w  .j  av  a2  s  .c  o  m*/
    form.readFromRequest(context.getRequest());
    if (form.validate()) {
        logger.debug("Applying settings to model");
        try {
            FileConfiguration fileConfiguration = (FileConfiguration) configuration;
            Settings settings = new Settings();
            form.writeToObject(settings);
            fileConfiguration.setProperty(PortofinoProperties.APP_NAME, settings.appName);
            fileConfiguration.setProperty(PortofinoProperties.LANDING_PAGE, settings.landingPage);
            fileConfiguration.setProperty(PortofinoProperties.LOGIN_PAGE, settings.loginPage);
            if (!settings.preloadGroovyPages
                    || fileConfiguration.getProperty(PortofinoProperties.GROOVY_PRELOAD_PAGES) != null) {
                fileConfiguration.setProperty(PortofinoProperties.GROOVY_PRELOAD_PAGES,
                        settings.preloadGroovyPages);
            }
            if (!settings.preloadGroovyClasses
                    || fileConfiguration.getProperty(PortofinoProperties.GROOVY_PRELOAD_CLASSES) != null) {
                fileConfiguration.setProperty(PortofinoProperties.GROOVY_PRELOAD_CLASSES,
                        settings.preloadGroovyClasses);
            }
            fileConfiguration.save();
            logger.info("Configuration saved to " + fileConfiguration.getFile().getAbsolutePath());
        } catch (Exception e) {
            logger.error("Configuration not saved", e);
            SessionMessages
                    .addErrorMessage(ElementsThreadLocals.getText("the.configuration.could.not.be.saved"));
            return new ForwardResolution("/m/pageactions/actions/admin/settings.jsp");
        }
        SessionMessages.addInfoMessage(ElementsThreadLocals.getText("configuration.updated.successfully"));
        return new RedirectResolution(this.getClass());
    } else {
        return new ForwardResolution("/m/pageactions/actions/admin/settings.jsp");
    }
}

From source file:org.parosproxy.paros.view.AbstractFrame.java

private void storeLocation() {
    if (frameName == null) {
        return;//from   w  w w .ja  v  a  2s  . co  m
    }

    FileConfiguration config = loadConfig();
    String configName = "ui." + frameName;
    Point location = getLocation();
    config.setAutoSave(true);
    config.setProperty(configName + PROP_LEFT, Integer.toString(location.x));
    config.setProperty(configName + PROP_TOP, Integer.toString(location.y));
    config.setProperty(configName + PROP_WIDTH, Integer.toString(getWidth()));
    config.setProperty(configName + PROP_HEIGHT, Integer.toString(getHeight()));

    try {
        config.save();
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }

    System.out.println("STORE " + frameName + "  " + location.x + "  " + location.y + "  " + getWidth() + "  "
            + getHeight());

}

From source file:umich.ms.batmass.gui.viewers.map2d.options.Map2DOptions.java

public Configuration flushUserConfigToDefault() {
    FileConfiguration userConfig = null;
    try {//from  w  ww .ja v a2  s  .  com
        userConfig = BmConfig.forClass(BaseMap2D.class);
        userConfig.clear();
        userConfig.save();
        userConfig = BmConfig.forClass(BaseMap2D.class);
    } catch (ConfigurationException | IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    defaultConfig = readDefaultConfig();
    return userConfig;
}

From source file:umich.ms.batmass.gui.viewers.map2d.options.Map2DOptionsPanel.java

void store() throws ConfigurationException, IOException {
    updateConfigToUIState();/*from   ww w.j av  a 2s.  c o m*/

    FileConfiguration userConfig = BmConfig.forClass(BaseMap2D.class);
    for (Iterator<String> i = config.getKeys(); i.hasNext();) {
        String key = i.next();
        Object value = config.getProperty(key);
        userConfig.setProperty(key, value);
    }
    userConfig.save();
}