Skip to content

Commit

Permalink
Add readonly flag to JSON configuration to read only config files
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenHollmann committed Feb 17, 2022
1 parent cd0450e commit cc3a249
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions faroe/json/src/main/java/org/n52/faroe/json/JsonConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class JsonConfiguration implements Destroyable,
private static final String WEB_INF_PATH = "WEB-INF";
private String fileName = DEFAULT_FILE_NAME;
private int writeTimeout = DEFAULT_WRITE_TIMEOUT;
private boolean readonly = false;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final JsonNodeFactory nodeFactory = Json.nodeFactory();
private ObjectNode configuration;
Expand Down Expand Up @@ -170,6 +171,15 @@ public void set(ObjectNode configuration) {
public void setWriteTimeout(int writeTimeout) {
this.writeTimeout = writeTimeout;
}

/**
* Sets the flag to persist or not persist the settings, e.g. settings defined externally
*
* @param readonly the flag to persist settings or not
*/
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}

/**
* Sets the file name of this configuration.
Expand Down Expand Up @@ -229,16 +239,18 @@ public void writeNow() {
* Actually persists the configuration.
*/
private synchronized void persist() {
readLock().lock();
try {
LOG.debug("Writing configuration file");
try (FileOutputStream fos = new FileOutputStream(this.file)) {
Json.print(fos, this.configuration);
if (!readonly) {
readLock().lock();
try {
LOG.debug("Writing configuration file");
try (FileOutputStream fos = new FileOutputStream(this.file)) {
Json.print(fos, this.configuration);
}
} catch (IOException e) {
throw new ConfigurationError("Could not persist configuration", e);
} finally {
readLock().unlock();
}
} catch (IOException e) {
throw new ConfigurationError("Could not persist configuration", e);
} finally {
readLock().unlock();
}
}

Expand Down

0 comments on commit cc3a249

Please sign in to comment.