Skip to content

Commit

Permalink
code refactoring - class and variable renaming for clarity
Browse files Browse the repository at this point in the history
Rename LastRun class and a couple of variables, "properties" and "lastRun" in Config class for improving readability.
  • Loading branch information
ashitsalesforce committed Sep 19, 2024
1 parent 762205a commit bab72a8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
97 changes: 40 additions & 57 deletions src/main/java/com/salesforce/dataloader/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> parameterOverridesMap;
Expand Down Expand Up @@ -380,7 +378,7 @@ public class Config {
/**
* The <code>lastRun</code> is for last run statistics file
*/
private LastRun lastRun;
private LastRunProperties lastRunProperties;
/**
* <code>encrypter</code> 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.
Expand Down Expand Up @@ -505,7 +503,7 @@ public class Config {
* @see #save()
*/
private Config(String filename, Map<String, String> overridesMap) throws ConfigInitializationException, IOException {
properties = new LinkedProperties();
loadedProperties = new LinkedProperties();
this.filename = filename;

File configFile = new File(this.filename);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}


Expand Down Expand Up @@ -904,10 +902,10 @@ public Map<String, String> 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
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}
Expand All @@ -1009,10 +1007,9 @@ private void postLoad(Map<?, ?> propMap, boolean isConfigFilePropsMap) throws Co
initEncryption((Map<String, String>) 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) {
Expand All @@ -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<String, String> propMap = (Map<String, String>)values;
// initialize encryption
Expand Down Expand Up @@ -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<String> list = new ArrayList<String>();
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
*
Expand Down Expand Up @@ -1206,42 +1189,42 @@ 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);
}
}

// do not save read-only properties that were not specified
// 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<String, String>)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.
}
}
}

removeUnsupportedProperties();
removeDecryptedProperties();
removeCLIOptionsFromProperties();
removeEmptyProperties(this.properties);
removeEmptyProperties(this.loadedProperties);

FileOutputStream out = null;
try {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String> keys = this.properties.stringPropertyNames();
Set<String> keys = this.loadedProperties.stringPropertyNames();
Field[] allFields = Config.class.getDeclaredFields();
for (Field field : allFields) {
if (field.getName().startsWith("CLI_OPTION_")) {
Expand All @@ -1336,7 +1319,7 @@ private void removeCLIOptionsFromProperties() {
}
for (String key : keys) {
if (key.equalsIgnoreCase(fieldVal)) {
this.properties.remove(key);
this.loadedProperties.remove(key);
}
}
}
Expand All @@ -1352,7 +1335,7 @@ private void removeEmptyProperties(Properties props) {
* Save statistics from the last run
*/
public void saveLastRun() throws IOException {
lastRun.save();
lastRunProperties.save();
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
* @author Alex Warshavsky
* @since 8.0
*/
public class LastRun extends Properties {
public class LastRunProperties extends Properties {
/**
* Comment for <code>serialVersionUID</code>
*/
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$
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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$
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/salesforce/dataloader/util/DAORowUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit bab72a8

Please sign in to comment.