1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.backend.filebased;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.io.OutputStreamWriter;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import java.nio.file.StandardOpenOption;
21 import org.apache.commons.configuration.ConfigurationException;
22 import org.apache.commons.configuration.PropertiesConfiguration;
23 import org.apache.commons.configuration.event.ConfigurationEvent;
24 import org.apache.commons.configuration.event.ConfigurationListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * We do not count loads and saves as in
30 * {@link org.apache.commons.configuration.builder.AutoSaveListener}, because
31 * ConfigurationListener is not aware of such things
33 class AutoSaveListener implements ConfigurationListener {
35 private static final Logger logger = LoggerFactory.getLogger(AutoSaveListener.class);
37 private final Path path;
38 private final PropertiesConfiguration configuration;
43 * @param path the file path to write to
44 * @param configuration the configuration, where the change events come
45 * from. This is needed as <code>event.getSource()</code> does
48 public AutoSaveListener(Path path, PropertiesConfiguration configuration) {
50 this.configuration = configuration;
54 public void configurationChanged(ConfigurationEvent event) {
55 if (!event.isBeforeUpdate()) {
57 if (!Files.exists(this.path.getParent())) {
58 Files.createDirectories(this.path.getParent());
60 } catch (IOException ce) {
61 AutoSaveListener.logger.error("Could not update properties file", ce);
64 try (OutputStream out = Files.newOutputStream(this.path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
65 OutputStreamWriter writer = new OutputStreamWriter(out);
66 this.configuration.save(writer);
67 } catch (ConfigurationException | IOException ce) {
68 AutoSaveListener.logger.error("Could not update properties file", ce);