Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / backend / filebased / AutoSaveListener.java
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
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.backend.filebased;
13
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;
20
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;
27
28 /**
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
32  */
33 class AutoSaveListener implements ConfigurationListener {
34         
35         private static final Logger logger = LoggerFactory.getLogger(AutoSaveListener.class);
36         
37         private final Path path;
38         private final PropertiesConfiguration configuration;
39         
40         
41         /**
42          * 
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
46          *            not work
47          */
48         public AutoSaveListener(Path path, PropertiesConfiguration configuration) {
49                 this.path = path;
50                 this.configuration = configuration;
51         }
52         
53         @Override
54         public void configurationChanged(ConfigurationEvent event) {
55                 if (!event.isBeforeUpdate()) {
56                         try {
57                                 if (!Files.exists(this.path.getParent())) {
58                                         Files.createDirectories(this.path.getParent());
59                                 }
60                         } catch (IOException ce) {
61                                 AutoSaveListener.logger.error("Could not update properties file", ce);
62                                 return;
63                         }
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);
69                         }
70                 }
71         }
72 }