org.apache.nifi.toolkit.tls.properties.NiFiPropertiesWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nifi-toolkit-tls Show documentation
Show all versions of nifi-toolkit-tls Show documentation
Tooling to make tls configuration easier
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.toolkit.tls.properties;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Class capable of writing out updated NiFi properties. It keeps a list of original lines and a map of updates to apply.
*
* It first writes all the original properties (with updated values if they exist) and then adds any new properties at the end.
*/
public class NiFiPropertiesWriter {
private final List lines;
private final Map updatedValues;
public NiFiPropertiesWriter(List lines) {
this.lines = new ArrayList<>(lines);
this.updatedValues = new HashMap<>();
}
/**
* Sets a property value
*
* @param key the property key
* @param value the property value
*/
public void setPropertyValue(String key, String value) {
updatedValues.put(key, value);
}
/**
* Write an updated nifi.properties to the given OutputStream
*
* @param outputStream the output stream
* @throws IOException if there is an IO error
*/
public void writeNiFiProperties(OutputStream outputStream) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream))) {
Map remainingValues = new HashMap<>(updatedValues);
Set keysSeen = new HashSet<>();
for (String line : lines) {
String key = line.split("=")[0].trim();
boolean outputLine = true;
if (!key.isEmpty() && !key.startsWith("#")) {
if (!keysSeen.add(key)) {
throw new IOException("Found key more than once in nifi.properties: " + key);
}
String value = remainingValues.remove(key);
if (value != null) {
writer.write(key);
writer.write("=");
writer.write(value);
outputLine = false;
}
}
if (outputLine) {
writer.write(line);
}
writer.newLine();
}
for (Map.Entry keyValueEntry : remainingValues.entrySet()) {
writer.write(keyValueEntry.getKey());
writer.write("=");
writer.write(keyValueEntry.getValue());
writer.newLine();
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy