Skip to content

Commit

Permalink
Add preferences support
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Oct 14, 2023
1 parent 2b87351 commit c492c21
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/qz/installer/provision/Step.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package qz.installer.provision;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import qz.common.Constants;
import qz.common.PropertyHelper;
import qz.utils.FileUtilities;
import qz.utils.ShellUtilities;
import qz.utils.SystemUtilities;
Expand Down Expand Up @@ -181,10 +181,10 @@ public boolean invoke() throws Exception {
return provisionCert();
case SCRIPT:
return provisionScript();
case PREFERENCE:
return provisionPreferences();
case PROPERTY:
// TODO: How should we pass this parameter in?
case PREFERENCE:
// TODO: Calculate this
default:
throw new UnsupportedOperationException("Type " + type + " is not yet supported.");
}
Expand Down Expand Up @@ -235,6 +235,52 @@ private boolean provisionScript() throws Exception {
return success;
}

private boolean provisionPreferences() {
if(this.data != null && !this.data.trim().isEmpty()) {
String[] props = this.data.split("\\|");
ArrayList<AbstractMap.SimpleEntry<String,String>> pairs = new ArrayList<>();
for(String prop : props) {
AbstractMap.SimpleEntry<String,String> pair = parsePropertyPair(prop);
if (pair != null) {
pairs.add(pair);
}
}
if (!pairs.isEmpty()) {
PropertyHelper prefs = new PropertyHelper(FileUtilities.USER_DIR + File.separator + Constants.PREFS_FILE + ".properties");
for(AbstractMap.SimpleEntry<String,String> pair : pairs) {
prefs.setProperty(pair.getKey(), pair.getValue());
}
if (prefs.save()) {
log.info("Successfully provisioned {} {}", pairs.size(), this.type);
return true;
}
log.error("An error occurred saving properties to preferences file {}", this);
}
} else {
log.error("Skipping {} Step, Data is null or empty", type);
}
return false;
}

private AbstractMap.SimpleEntry<String, String> parsePropertyPair(String prop) {
if(prop.contains("=")) {
String[] pair = prop.split("=", 2);
if (!pair[0].trim().isEmpty()) {
if (!pair[1].trim().isEmpty()) {
return new AbstractMap.SimpleEntry<>(pair[0], pair[1]);
} else {
log.warn("Skipping {} '{}', property value is malformed", type, prop);
}
} else {
log.warn("Skipping {} '{}', property name is malformed", type, prop);
}
} else {
log.warn("Skipping {} '{}', property is malformed", type, prop);
}

return null;
}

private File resourceToFile() throws IOException {
InputStream in = relativeClass.getResourceAsStream(this.data);
if(in == null) {
Expand Down

0 comments on commit c492c21

Please sign in to comment.