Skip to content

Commit

Permalink
Switch parsePropertyPair(s) to use HashMap instead of ArrayList<Simpl…
Browse files Browse the repository at this point in the history
…eEntry>.
  • Loading branch information
tresf committed May 3, 2024
1 parent 93c3dde commit 37b2838
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/qz/common/PropertyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
/**
* Created by Tres on 12/16/2015.
Expand Down Expand Up @@ -92,4 +93,8 @@ public boolean save() {
}
return success;
}

public synchronized Object setProperty(Map.Entry<String, String> pair) {
return super.setProperty(pair.getKey(), pair.getValue());
}
}
31 changes: 16 additions & 15 deletions src/qz/installer/provision/ProvisionInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static qz.common.Constants.*;
Expand Down Expand Up @@ -142,21 +142,22 @@ private boolean invokeStep(Step step) throws Exception {
public void setCustomPorts(Installer installer) {
for(Step step : steps) {
if(step.getType() == Type.PROPERTY) {
for(AbstractMap.SimpleEntry<String,String> pair : PropertyInvoker.parsePropertyPairs(step)) {
if(pair.getKey().equals(ArgValue.WEBSOCKET_SECURE_PORTS.getMatch())) {
List<Integer> securePorts = PrefsSearch.parseIntegerArray(pair.getValue());
if(!securePorts.isEmpty()) {
installer.setSecurePorts(securePorts);
log.info("Picked up custom secure ports from {}: [{}]", PROVISION_FILE, StringUtils.join(securePorts, ","));
}
} else if(pair.getKey().equals(ArgValue.WEBSOCKET_INSECURE_PORTS.getMatch())) {
List<Integer> insecurePorts = PrefsSearch.parseIntegerArray(pair.getValue());
if(!insecurePorts.isEmpty()) {
installer.setInsecurePorts(insecurePorts);
log.info("Picked up custom insecure ports from {}: [{}]", PROVISION_FILE, StringUtils.join(insecurePorts, ","));
}
HashMap<String, String> pairs = PropertyInvoker.parsePropertyPairs(step);
String foundPorts;
if((foundPorts = pairs.get(ArgValue.WEBSOCKET_SECURE_PORTS.getMatch())) != null) {
List<Integer> securePorts = PrefsSearch.parseIntegerArray(foundPorts);
if(!securePorts.isEmpty()) {
installer.setSecurePorts(securePorts);
log.info("Picked up custom secure ports from {}: [{}]", PROVISION_FILE, StringUtils.join(securePorts, ","));
}
}
}
if((foundPorts = pairs.get(ArgValue.WEBSOCKET_INSECURE_PORTS.getMatch())) != null) {
List<Integer> insecurePorts = PrefsSearch.parseIntegerArray(foundPorts);
if(!insecurePorts.isEmpty()) {
installer.setSecurePorts(insecurePorts);
log.info("Picked up custom insecure ports from {}: [{}]", PROVISION_FILE, StringUtils.join(insecurePorts, ","));
}
}
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/qz/installer/provision/invoker/PropertyInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import java.io.File;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PropertyInvoker implements Invokable {
private Step step;
Expand All @@ -20,10 +21,10 @@ public PropertyInvoker(Step step, PropertyHelper properties) {
}

public boolean invoke() {
ArrayList<AbstractMap.SimpleEntry<String,String>> pairs = parsePropertyPairs(step);
HashMap<String, String> pairs = parsePropertyPairs(step);
if (!pairs.isEmpty()) {
for(AbstractMap.SimpleEntry<String,String> pair : pairs) {
properties.setProperty(pair.getKey(), pair.getValue());
for(Map.Entry<String, String> pair : pairs.entrySet()) {
properties.setProperty(pair);
}
if (properties.save()) {
log.info("Successfully provisioned '{}' '{}'", pairs.size(), step.getType());
Expand Down Expand Up @@ -52,14 +53,18 @@ public static PropertyHelper getPreferences(Step step) {
return new PropertyHelper(FileUtilities.USER_DIR + File.separator + Constants.PREFS_FILE + ".properties");
}

public static ArrayList<AbstractMap.SimpleEntry<String,String>> parsePropertyPairs(Step step) {
ArrayList<AbstractMap.SimpleEntry<String,String>> pairs = new ArrayList<>();
public static HashMap<String, String> parsePropertyPairs(Step step) {
HashMap<String, String> pairs = new HashMap<>();
if(step.getData() != null && !step.getData().trim().isEmpty()) {
String[] props = step.getData().split("\\|");
for(String prop : props) {
AbstractMap.SimpleEntry<String,String> pair = parsePropertyPair(step, prop);
if (pair != null) {
pairs.add(pair);
if(pairs.get(pair.getKey()) != null) {
log.warn("Property {} already exists, replacing [before: {}, after: {}] ",
pair.getKey(), pairs.get(pair.getKey()), pair.getValue());
}
pairs.put(pair.getKey(), pair.getValue());
}
}
} else {
Expand Down

0 comments on commit 37b2838

Please sign in to comment.