From bab72a801961ea5b107710fa89c70d0ee61b2835 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Thu, 19 Sep 2024 09:42:15 -0700 Subject: [PATCH] code refactoring - class and variable renaming for clarity Rename LastRun class and a couple of variables, "properties" and "lastRun" in Config class for improving readability. --- .../action/visitor/DAOLoadVisitor.java | 6 +- .../salesforce/dataloader/config/Config.java | 97 ++++++++----------- .../{LastRun.java => LastRunProperties.java} | 6 +- .../dataloader/process/ProcessRunner.java | 4 +- .../dataloader/ui/AdvancedSettingsDialog.java | 4 +- .../dataloader/util/DAORowUtil.java | 4 +- .../process/DatabaseProcessTest.java | 4 +- 7 files changed, 54 insertions(+), 71 deletions(-) rename src/main/java/com/salesforce/dataloader/config/{LastRun.java => LastRunProperties.java} (95%) diff --git a/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java b/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java index 8e5ab81c..0378eccd 100644 --- a/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java +++ b/src/main/java/com/salesforce/dataloader/action/visitor/DAOLoadVisitor.java @@ -45,7 +45,7 @@ import com.salesforce.dataloader.client.PartnerClient; import com.salesforce.dataloader.client.SessionInfo; import com.salesforce.dataloader.config.Config; -import com.salesforce.dataloader.config.LastRun; +import com.salesforce.dataloader.config.LastRunProperties; import com.salesforce.dataloader.config.Messages; import com.salesforce.dataloader.controller.Controller; import com.salesforce.dataloader.dao.DataReader; @@ -455,13 +455,13 @@ protected void setLastRunProperties(Object[] results) throws LoadException { // set the last processed row number in the config (*_lastRun.properties) file int currentProcessed; try { - currentProcessed = getConfig().getInt(LastRun.LAST_LOAD_BATCH_ROW); + currentProcessed = getConfig().getInt(LastRunProperties.LAST_LOAD_BATCH_ROW); } catch (ParameterLoadException e) { // if there's a problem getting last batch row, start at the beginning currentProcessed = 0; } currentProcessed += results.length; - getConfig().setValue(LastRun.LAST_LOAD_BATCH_ROW, currentProcessed); + getConfig().setValue(LastRunProperties.LAST_LOAD_BATCH_ROW, currentProcessed); try { getConfig().saveLastRun(); } catch (IOException e) { diff --git a/src/main/java/com/salesforce/dataloader/config/Config.java b/src/main/java/com/salesforce/dataloader/config/Config.java index db3a2116..e2560afb 100644 --- a/src/main/java/com/salesforce/dataloader/config/Config.java +++ b/src/main/java/com/salesforce/dataloader/config/Config.java @@ -54,12 +54,10 @@ import java.util.Calendar; import java.util.Collections; import java.util.Date; -import java.util.Enumeration; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.StringJoiner; @@ -184,7 +182,7 @@ public class Config { /** * The mapping from preference name to preference value (represented as strings). */ - private Properties properties = new LinkedProperties(); + private Properties loadedProperties = new LinkedProperties(); private Properties readOnlyPropertiesFromPropertiesFile = new LinkedProperties(); private Map parameterOverridesMap; @@ -380,7 +378,7 @@ public class Config { /** * The lastRun is for last run statistics file */ - private LastRun lastRun; + private LastRunProperties lastRunProperties; /** * encrypter is a utility used internally in the config for reading/writing * encrypted values. Right now, the list of encrypted values is known to this class only. @@ -505,7 +503,7 @@ public class Config { * @see #save() */ private Config(String filename, Map overridesMap) throws ConfigInitializationException, IOException { - properties = new LinkedProperties(); + loadedProperties = new LinkedProperties(); this.filename = filename; File configFile = new File(this.filename); @@ -558,15 +556,15 @@ private void initializeLastRun(String lastRunFileNamePrefix) { lastRunDir = this.configDir; } - this.lastRun = new LastRun(lastRunFileName, lastRunDir, getBoolean(Config.ENABLE_LAST_RUN_OUTPUT)); + this.lastRunProperties = new LastRunProperties(lastRunFileName, lastRunDir, getBoolean(Config.ENABLE_LAST_RUN_OUTPUT)); // Need to initialize last run date if it's present neither in config or override - lastRun.setDefault(LastRun.LAST_RUN_DATE, getString(INITIAL_LAST_RUN_DATE)); + lastRunProperties.setDefault(LastRunProperties.LAST_RUN_DATE, getString(INITIAL_LAST_RUN_DATE)); try { - this.lastRun.load(); + this.lastRunProperties.load(); } catch (IOException e) { logger.warn(Messages.getFormattedString("LastRun.errorLoading", new String[]{ - this.lastRun.getFullPath(), e.getMessage()}), e); + this.lastRunProperties.getFullPath(), e.getMessage()}), e); } } @@ -683,7 +681,7 @@ private void setDefaults() { * @param name the name of the key */ public boolean contains(String name) { - return properties.containsKey(name) || lastRun.hasParameter(name) && lastRun.containsKey(name); + return loadedProperties.containsKey(name) || lastRunProperties.hasParameter(name) && lastRunProperties.containsKey(name); } /** @@ -840,7 +838,7 @@ public String getConfigFilename(String configFileProperty) { } public String getLastRunFilename() { - return this.lastRun == null ? null : this.lastRun.getFullPath(); + return this.lastRunProperties == null ? null : this.lastRunProperties.getFullPath(); } @@ -904,10 +902,10 @@ public Map getMap(String name) throws ParameterLoadException { private String getParamValue(String name) { String propValue; - if (lastRun != null && lastRun.hasParameter(name)) { - propValue = lastRun.getProperty(name); + if (lastRunProperties != null && lastRunProperties.hasParameter(name)) { + propValue = lastRunProperties.getProperty(name); } else { - propValue = properties != null ? properties.getProperty(name) : null; + propValue = loadedProperties != null ? loadedProperties.getProperty(name) : null; } // check if a property's value is configured when it used to be a pilot property @@ -932,8 +930,8 @@ private String getParamValue(String name) { * @param out the print stream */ public void list(PrintStream out) { - properties.list(out); - lastRun.list(out); + loadedProperties.list(out); + lastRunProperties.list(out); } /** @@ -942,8 +940,8 @@ public void list(PrintStream out) { * @param out the print writer */ public void list(PrintWriter out) { - properties.list(out); - lastRun.list(out); + loadedProperties.list(out); + lastRunProperties.list(out); } /** @@ -979,7 +977,7 @@ private void load(InputStream in) throws ConfigInitializationException, IOExcept Properties propsFromFile = new LinkedProperties(); propsFromFile.load(in); removeEmptyProperties(propsFromFile); - properties.putAll(propsFromFile); + loadedProperties.putAll(propsFromFile); for (String roprop : READ_ONLY_PROPERTY_NAMES) { if (propsFromFile.containsKey(roprop)) { this.readOnlyPropertiesFromPropertiesFile.put( @@ -993,7 +991,7 @@ private void load(InputStream in) throws ConfigInitializationException, IOExcept throw e; } // paramter post-processing - postLoad(properties, true); + postLoad(loadedProperties, true); dirty = false; } @@ -1009,10 +1007,9 @@ private void postLoad(Map propMap, boolean isConfigFilePropsMap) throws Co initEncryption((Map) propMap); // decrypt encrypted values - decryptPasswordProperty(propMap, PASSWORD); - decryptPasswordProperty(propMap, PROXY_PASSWORD); - decryptPasswordProperty(propMap, OAUTH_ACCESSTOKEN); - decryptPasswordProperty(propMap, OAUTH_REFRESHTOKEN); + for (String encryptedProp : ENCRYPTED_PROPERTY_NAMES) { + decryptAndCacheProperty(propMap, encryptedProp); + } // Do not load unsupported properties and CLI options even if they are specified in config.properties file if (isConfigFilePropsMap) { @@ -1021,7 +1018,7 @@ private void postLoad(Map propMap, boolean isConfigFilePropsMap) throws Co } } - private void decryptPasswordProperty(Map values, String propertyName) throws ConfigInitializationException { + private void decryptAndCacheProperty(Map values, String propertyName) throws ConfigInitializationException { @SuppressWarnings("unchecked") Map propMap = (Map)values; // initialize encryption @@ -1152,20 +1149,6 @@ public boolean needsSaving() { return dirty; } - /** - * Returns an enumeration of all preferences known to this config - * - * @return an array of preference names - */ - public String[] preferenceNames() { - ArrayList list = new ArrayList(); - Enumeration en = properties.propertyNames(); - while (en.hasMoreElements()) { - list.add((String) en.nextElement()); - } - return list.toArray(new String[list.size()]); - } - /** * Puts a set of values from a map into config * @@ -1206,12 +1189,12 @@ public void save() throws IOException, GeneralSecurityException { } Properties inMemoryProperties = new LinkedProperties(); - inMemoryProperties.putAll(this.properties); + inMemoryProperties.putAll(this.loadedProperties); // do not save properties set through parameter overrides if (this.parameterOverridesMap != null) { for (String propertyName : this.parameterOverridesMap.keySet()) { - this.properties.remove(propertyName); + this.loadedProperties.remove(propertyName); } } @@ -1219,21 +1202,21 @@ public void save() throws IOException, GeneralSecurityException { // in properties file for (String roprop : READ_ONLY_PROPERTY_NAMES) { if (!this.readOnlyPropertiesFromPropertiesFile.containsKey(roprop)) { - this.properties.remove(roprop); + this.loadedProperties.remove(roprop); } } for (String encryptedProp : ENCRYPTED_PROPERTY_NAMES) { - if (this.properties.containsKey(encryptedProp)) { - Map propMap = (Map)this.properties; + if (this.loadedProperties.containsKey(encryptedProp)) { + Map propMap = (Map)this.loadedProperties; try { @SuppressWarnings("unchecked") String propValue = encryptProperty(encrypter, (Map)propMap, encryptedProp, isBatchMode()); - this.properties.put(encryptedProp, propValue); + this.loadedProperties.put(encryptedProp, propValue); } catch (ParameterLoadException e) { - this.properties.remove(encryptedProp); // Encryption attempt failed. Do not save. + this.loadedProperties.remove(encryptedProp); // Encryption attempt failed. Do not save. } } } @@ -1241,7 +1224,7 @@ public void save() throws IOException, GeneralSecurityException { removeUnsupportedProperties(); removeDecryptedProperties(); removeCLIOptionsFromProperties(); - removeEmptyProperties(this.properties); + removeEmptyProperties(this.loadedProperties); FileOutputStream out = null; try { @@ -1252,10 +1235,10 @@ public void save() throws IOException, GeneralSecurityException { out.close(); } // restore original property values - properties = inMemoryProperties; + loadedProperties = inMemoryProperties; } // save last run statistics - lastRun.save(); + lastRunProperties.save(); } public void setAuthEndpoint(String authEndpoint) { @@ -1313,11 +1296,11 @@ private void removeUnsupportedProperties() { } private void removeDecryptedProperties() { - this.properties.entrySet().removeIf(entry -> (entry.getKey().toString().endsWith(DECRYPTED_SUFFIX))); + this.loadedProperties.entrySet().removeIf(entry -> (entry.getKey().toString().endsWith(DECRYPTED_SUFFIX))); } private void removeCLIOptionsFromProperties() { - Set keys = this.properties.stringPropertyNames(); + Set keys = this.loadedProperties.stringPropertyNames(); Field[] allFields = Config.class.getDeclaredFields(); for (Field field : allFields) { if (field.getName().startsWith("CLI_OPTION_")) { @@ -1336,7 +1319,7 @@ private void removeCLIOptionsFromProperties() { } for (String key : keys) { if (key.equalsIgnoreCase(fieldVal)) { - this.properties.remove(key); + this.loadedProperties.remove(key); } } } @@ -1352,7 +1335,7 @@ private void removeEmptyProperties(Properties props) { * Save statistics from the last run */ public void saveLastRun() throws IOException { - lastRun.save(); + lastRunProperties.save(); } /** @@ -1364,7 +1347,7 @@ public void saveLastRun() throws IOException { * @throws java.io.IOException if there is a problem saving this store */ private void save(OutputStream out, String header) throws IOException { - properties.store(out, header); + loadedProperties.store(out, header); dirty = false; } @@ -1469,10 +1452,10 @@ private void setProperty(String name, String newValue, boolean skipIfAlreadySet) private void doSetPropertyAndUpdateConfig(String name, String oldValue, String newValue) { this.dirty = true; configChanged(name, oldValue, newValue); - if (lastRun != null && lastRun.hasParameter(name)) { - lastRun.put(name, newValue); + if (lastRunProperties != null && lastRunProperties.hasParameter(name)) { + lastRunProperties.put(name, newValue); } else { - properties.put(name, newValue); + loadedProperties.put(name, newValue); } } diff --git a/src/main/java/com/salesforce/dataloader/config/LastRun.java b/src/main/java/com/salesforce/dataloader/config/LastRunProperties.java similarity index 95% rename from src/main/java/com/salesforce/dataloader/config/LastRun.java rename to src/main/java/com/salesforce/dataloader/config/LastRunProperties.java index 48776b82..a2419875 100644 --- a/src/main/java/com/salesforce/dataloader/config/LastRun.java +++ b/src/main/java/com/salesforce/dataloader/config/LastRunProperties.java @@ -37,13 +37,13 @@ * @author Alex Warshavsky * @since 8.0 */ -public class LastRun extends Properties { +public class LastRunProperties extends Properties { /** * Comment for serialVersionUID */ private static final long serialVersionUID = 1L; - private static Logger logger = LogManager.getLogger(LastRun.class); + private static Logger logger = LogManager.getLogger(LastRunProperties.class); // last run statistics public static final String LAST_LOAD_BATCH_ROW = "process.lastLoadBatchRow"; //$NON-NLS-1$ @@ -58,7 +58,7 @@ public class LastRun extends Properties { /** * Initialize lastRun with filename, this is needed if last run output is not enabled (yet) */ - public LastRun(String filename, String filePath, boolean outputEnabled) { + public LastRunProperties(String filename, String filePath, boolean outputEnabled) { super(); // init properties this.filename = filename; this.filePath = filePath; diff --git a/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java b/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java index f0ed9488..7f9c5ee0 100644 --- a/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java +++ b/src/main/java/com/salesforce/dataloader/process/ProcessRunner.java @@ -58,7 +58,7 @@ import com.salesforce.dataloader.action.progress.ILoaderProgress; import com.salesforce.dataloader.action.progress.NihilistProgressAdapter; import com.salesforce.dataloader.config.Config; -import com.salesforce.dataloader.config.LastRun; +import com.salesforce.dataloader.config.LastRunProperties; import com.salesforce.dataloader.config.Messages; import com.salesforce.dataloader.controller.Controller; import com.salesforce.dataloader.exception.ControllerInitializationException; @@ -168,7 +168,7 @@ public synchronized void run(ILoaderProgress monitor) throws Exception { // save last successful run date // FIXME look into a better place so that long runs don't skew this - config.setValue(LastRun.LAST_RUN_DATE, Calendar.getInstance().getTime()); + config.setValue(LastRunProperties.LAST_RUN_DATE, Calendar.getInstance().getTime()); config.saveLastRun(); } else { logger.fatal(Messages.getString("Process.loginError")); //$NON-NLS-1$ diff --git a/src/main/java/com/salesforce/dataloader/ui/AdvancedSettingsDialog.java b/src/main/java/com/salesforce/dataloader/ui/AdvancedSettingsDialog.java index d9bc8898..a158267f 100644 --- a/src/main/java/com/salesforce/dataloader/ui/AdvancedSettingsDialog.java +++ b/src/main/java/com/salesforce/dataloader/ui/AdvancedSettingsDialog.java @@ -28,7 +28,7 @@ package com.salesforce.dataloader.ui; import com.salesforce.dataloader.config.Config; -import com.salesforce.dataloader.config.LastRun; +import com.salesforce.dataloader.config.LastRunProperties; import com.salesforce.dataloader.controller.Controller; import com.salesforce.dataloader.util.AppUtil; import com.salesforce.dataloader.util.LoggingUtil; @@ -788,7 +788,7 @@ public void verifyText(VerifyEvent event) { data.horizontalSpan = 2; blankAgain.setLayoutData(data); - String lastBatch = getController().getConfig().getString(LastRun.LAST_LOAD_BATCH_ROW); + String lastBatch = getController().getConfig().getString(LastRunProperties.LAST_LOAD_BATCH_ROW); if (lastBatch.equals("")) { //$NON-NLS-1$ lastBatch = "0"; //$NON-NLS-1$ } diff --git a/src/main/java/com/salesforce/dataloader/util/DAORowUtil.java b/src/main/java/com/salesforce/dataloader/util/DAORowUtil.java index 048722e1..f7ec71c9 100644 --- a/src/main/java/com/salesforce/dataloader/util/DAORowUtil.java +++ b/src/main/java/com/salesforce/dataloader/util/DAORowUtil.java @@ -128,13 +128,13 @@ public void skipRowToStartOffset(Config cfg, DataReader rdr, ILoaderProgress mon throws LoadException { try { - cfg.setValue(LastRun.LAST_LOAD_BATCH_ROW, 0); + cfg.setValue(LastRunProperties.LAST_LOAD_BATCH_ROW, 0); rowToStart(cfg, rdr); if (updateProgress) { // set the last processed value to the starting row int currentRow = rdr.getCurrentRowNumber(); if (mon != null && currentRow > 0) mon.worked(currentRow); - cfg.setValue(LastRun.LAST_LOAD_BATCH_ROW, currentRow); + cfg.setValue(LastRunProperties.LAST_LOAD_BATCH_ROW, currentRow); cfg.saveLastRun(); } } catch (final DataAccessObjectException e) { diff --git a/src/test/java/com/salesforce/dataloader/process/DatabaseProcessTest.java b/src/test/java/com/salesforce/dataloader/process/DatabaseProcessTest.java index 69cb1bae..9457906d 100644 --- a/src/test/java/com/salesforce/dataloader/process/DatabaseProcessTest.java +++ b/src/test/java/com/salesforce/dataloader/process/DatabaseProcessTest.java @@ -42,7 +42,7 @@ import com.salesforce.dataloader.TestVariant; import com.salesforce.dataloader.action.OperationInfo; import com.salesforce.dataloader.config.Config; -import com.salesforce.dataloader.config.LastRun; +import com.salesforce.dataloader.config.LastRunProperties; import com.salesforce.dataloader.controller.Controller; import com.salesforce.dataloader.dao.database.DatabaseReader; import com.salesforce.dataloader.dao.database.DatabaseTestUtil; @@ -147,7 +147,7 @@ private void testUpsertAccountsDb(Map args, int numRows, boolean if (args == null) args = getTestConfig(); args.put(Config.PROCESS_NAME, processName); Config.DATE_FORMATTER.parse(startTime); - args.put(LastRun.LAST_RUN_DATE, startTime); + args.put(LastRunProperties.LAST_RUN_DATE, startTime); args.put(Config.OPERATION, OperationInfo.upsert.name()); runUpsertProcess(args, isInsert ? numRows : 0, isInsert ? 0 : numRows);