From c492c2108ef25f0ed65441842e2349e3483462b7 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Sat, 14 Oct 2023 14:32:52 -0400 Subject: [PATCH] Add preferences support --- src/qz/installer/provision/Step.java | 52 ++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/qz/installer/provision/Step.java b/src/qz/installer/provision/Step.java index 919ec116e..aa720a45a 100644 --- a/src/qz/installer/provision/Step.java +++ b/src/qz/installer/provision/Step.java @@ -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; @@ -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."); } @@ -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> pairs = new ArrayList<>(); + for(String prop : props) { + AbstractMap.SimpleEntry 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 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 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) {