Fixed semantics, added documentation.:

This commit is contained in:
Oliver Spohngellert 2016-04-12 16:34:59 -04:00
parent 69890a75cb
commit aa4736fafa
2 changed files with 86 additions and 49 deletions

View File

@ -500,16 +500,13 @@ public class HashDbManager implements PropertyChangeListener {
private void loadHashsetsConfiguration() { private void loadHashsetsConfiguration() {
try { try {
HashLookupSettings settings = HashLookupSettings.readSettings(); HashLookupSettings settings = HashLookupSettings.readSettings();
if (settings != null) {
this.configureSettings(settings); this.configureSettings(settings);
}
} catch (HashLookupSettings.HashLookupSettingsException ex) { } catch (HashLookupSettings.HashLookupSettingsException ex) {
Logger.getLogger(HashDbManager.class.getName()).log(Level.SEVERE, "Could not read Hash lookup settings from disk.", ex); Logger.getLogger(HashDbManager.class.getName()).log(Level.SEVERE, "Could not read Hash lookup settings from disk.", ex);
} }
} }
@Messages({"# {0} - database name", "HashDbManager.noDbPath.message=Couldn't get valid database path for: {0}", @Messages({"# {0} - database name", "HashDbManager.noDbPath.message=Couldn't get valid database path for: {0}"})
"HashDbManager.noOverwrite.message=Could not overwrite hash database settings."})
private void configureSettings(HashLookupSettings settings) { private void configureSettings(HashLookupSettings settings) {
boolean dbInfoRemoved = false; boolean dbInfoRemoved = false;
List<HashDbInfo> hashDbInfoList = settings.getHashDbInfo(); List<HashDbInfo> hashDbInfoList = settings.getHashDbInfo();
@ -536,7 +533,7 @@ public class HashDbManager implements PropertyChangeListener {
try { try {
HashLookupSettings.writeSettings(new HashLookupSettings(this.knownHashSets, this.knownBadHashSets)); HashLookupSettings.writeSettings(new HashLookupSettings(this.knownHashSets, this.knownBadHashSets));
} catch (HashLookupSettings.HashLookupSettingsException ex) { } catch (HashLookupSettings.HashLookupSettingsException ex) {
logger.log(Level.SEVERE, "Could not overwrite hash database settings."); logger.log(Level.SEVERE, "Could not overwrite hash database settings.", ex);
} }
} }
} }

View File

@ -61,6 +61,11 @@ final class HashLookupSettings implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final List<HashDbInfo> hashDbInfoList; private final List<HashDbInfo> hashDbInfoList;
/**
* Constructs a settings object to be serialized for hash lookups
*
* @param hashDbInfoList The list of hash db info.
*/
HashLookupSettings(List<HashDbInfo> hashDbInfoList) { HashLookupSettings(List<HashDbInfo> hashDbInfoList) {
this.hashDbInfoList = hashDbInfoList; this.hashDbInfoList = hashDbInfoList;
} }
@ -68,8 +73,8 @@ final class HashLookupSettings implements Serializable {
/** /**
* Constructs a settings object to be serialized for hash lookups * Constructs a settings object to be serialized for hash lookups
* *
* @param knownHashSets * @param knownHashSets The list known hash sets for the settings.
* @param knownBadHashSets * @param knownBadHashSets The list of known bad hash sets for the settings.
*/ */
HashLookupSettings(List<HashDbManager.HashDb> knownHashSets, List<HashDbManager.HashDb> knownBadHashSets) throws HashLookupSettingsException { HashLookupSettings(List<HashDbManager.HashDb> knownHashSets, List<HashDbManager.HashDb> knownBadHashSets) throws HashLookupSettingsException {
hashDbInfoList = new ArrayList<>(); hashDbInfoList = new ArrayList<>();
@ -77,6 +82,13 @@ final class HashLookupSettings implements Serializable {
this.addHashesToList(knownBadHashSets); this.addHashesToList(knownBadHashSets);
} }
/**
* Adds each HashDb to the settings.
*
* @param hashSetList The list of HashDb to add to the settings
*
* @throws * pacannot be obtained
*/
private void addHashesToList(List<HashDbManager.HashDb> hashSetList) throws HashLookupSettingsException { private void addHashesToList(List<HashDbManager.HashDb> hashSetList) throws HashLookupSettingsException {
for (HashDbManager.HashDb hashDb : hashSetList) { for (HashDbManager.HashDb hashDb : hashSetList) {
try { try {
@ -96,12 +108,20 @@ final class HashLookupSettings implements Serializable {
/** /**
* Gets the list of hash db info that this settings contains * Gets the list of hash db info that this settings contains
* *
* @return the hashDbInfoList * @return The list of hash databse info
*/ */
List<HashDbInfo> getHashDbInfo() { List<HashDbInfo> getHashDbInfo() {
return hashDbInfoList; return hashDbInfoList;
} }
/**
* Reads the settings from the disk.
*
* @return The settings object representing what was read.
*
* @throws HashLookupSettingsException When there is a problem reading the
* settings.
*/
static HashLookupSettings readSettings() throws HashLookupSettingsException { static HashLookupSettings readSettings() throws HashLookupSettingsException {
File fileSetFile = new File(SERIALIZATION_FILE_PATH); File fileSetFile = new File(SERIALIZATION_FILE_PATH);
if (fileSetFile.exists()) { if (fileSetFile.exists()) {
@ -111,6 +131,15 @@ final class HashLookupSettings implements Serializable {
} }
/**
* Reads the serialization settings from the disk
*
* @return Settings object representing what is saved in the serialization
* file.
*
* @throws HashLookupSettingsException If there's a problem importing the
* settings
*/
private static HashLookupSettings readSerializedSettings() throws HashLookupSettingsException { private static HashLookupSettings readSerializedSettings() throws HashLookupSettingsException {
try { try {
try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(SERIALIZATION_FILE_PATH))) { try (NbObjectInputStream in = new NbObjectInputStream(new FileInputStream(SERIALIZATION_FILE_PATH))) {
@ -122,6 +151,15 @@ final class HashLookupSettings implements Serializable {
} }
} }
/**
* Reads the xml settings from the disk
*
* @return Settings object representing what is saved in the xml file, or an
* empty settings if there is no xml file.
*
* @throws HashLookupSettingsException If there's a problem importing the
* settings
*/
private static HashLookupSettings readXmlSettings() throws HashLookupSettingsException { private static HashLookupSettings readXmlSettings() throws HashLookupSettingsException {
File xmlFile = new File(configFilePath); File xmlFile = new File(configFilePath);
if (xmlFile.exists()) { if (xmlFile.exists()) {
@ -130,26 +168,22 @@ final class HashLookupSettings implements Serializable {
// Open the XML document that implements the configuration file. // Open the XML document that implements the configuration file.
final Document doc = XMLUtil.loadDoc(HashDbManager.class, configFilePath); final Document doc = XMLUtil.loadDoc(HashDbManager.class, configFilePath);
if (doc == null) { if (doc == null) {
return null; throw new HashLookupSettingsException("Could not open xml document.");
} }
// Get the root element. // Get the root element.
Element root = doc.getDocumentElement(); Element root = doc.getDocumentElement();
if (root == null) { if (root == null) {
logger.log(Level.SEVERE, "Error loading hash sets: invalid file format."); //NON-NLS throw new HashLookupSettingsException("Error loading hash sets: invalid file format.");
return null;
} }
// Get the hash set elements. // Get the hash set elements.
NodeList setsNList = root.getElementsByTagName(SET_ELEMENT); NodeList setsNList = root.getElementsByTagName(SET_ELEMENT);
int numSets = setsNList.getLength(); int numSets = setsNList.getLength();
if (numSets == 0) {
logger.log(Level.WARNING, "No element hash_set exists."); //NON-NLS
}
// Create HashDbInfo objects for each hash set element. Throws on malformed xml. // Create HashDbInfo objects for each hash set element. Throws on malformed xml.
String attributeErrorMessage = " attribute was not set for hash_set at index {0}, cannot make instance of HashDb class"; //NON-NLS String attributeErrorMessage = "Missing %s attribute"; //NON-NLS
String elementErrorMessage = " element was not set for hash_set at index {0}, cannot make instance of HashDb class"; //NON-NLS String elementErrorMessage = "Empty %s element"; //NON-NLS
List<String> hashSetNames = new ArrayList<>(); List<String> hashSetNames = new ArrayList<>();
List<HashDbInfo> hashDbInfoList = new ArrayList<>(); List<HashDbInfo> hashDbInfoList = new ArrayList<>();
for (int i = 0; i < numSets; ++i) { for (int i = 0; i < numSets; ++i) {
@ -157,7 +191,7 @@ final class HashLookupSettings implements Serializable {
String hashSetName = setEl.getAttribute(SET_NAME_ATTRIBUTE); String hashSetName = setEl.getAttribute(SET_NAME_ATTRIBUTE);
if (hashSetName.isEmpty()) { if (hashSetName.isEmpty()) {
throw new HashLookupSettingsException(SEND_INGEST_MESSAGES_ATTRIBUTE + attributeErrorMessage); throw new HashLookupSettingsException(String.format(attributeErrorMessage, SET_NAME_ATTRIBUTE));
} }
// Handle configurations saved before duplicate hash set names were not permitted. // Handle configurations saved before duplicate hash set names were not permitted.
@ -168,9 +202,7 @@ final class HashLookupSettings implements Serializable {
++suffix; ++suffix;
newHashSetName = hashSetName + suffix; newHashSetName = hashSetName + suffix;
} while (hashSetNames.contains(newHashSetName)); } while (hashSetNames.contains(newHashSetName));
logger.log(Level.INFO, NbBundle.getMessage(HashLookupSettings.class, logger.log(Level.INFO, "Duplicate hash set name " + hashSetName + " found.\nReplacing with " + newHashSetName + ".");
"HashDbManager.replacingDuplicateHashsetNameMsg",
hashSetName, newHashSetName));
if (RuntimeProperties.coreComponentsAreActive()) { if (RuntimeProperties.coreComponentsAreActive()) {
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
NbBundle.getMessage(HashLookupSettings.class, NbBundle.getMessage(HashLookupSettings.class,
@ -184,7 +216,7 @@ final class HashLookupSettings implements Serializable {
String knownFilesType = setEl.getAttribute(SET_TYPE_ATTRIBUTE); String knownFilesType = setEl.getAttribute(SET_TYPE_ATTRIBUTE);
if (knownFilesType.isEmpty()) { if (knownFilesType.isEmpty()) {
throw new HashLookupSettingsException(SEND_INGEST_MESSAGES_ATTRIBUTE + attributeErrorMessage); throw new HashLookupSettingsException(String.format(attributeErrorMessage, SET_TYPE_ATTRIBUTE));
} }
// Handle legacy known files types. // Handle legacy known files types.
@ -195,13 +227,13 @@ final class HashLookupSettings implements Serializable {
final String searchDuringIngest = setEl.getAttribute(SEARCH_DURING_INGEST_ATTRIBUTE); final String searchDuringIngest = setEl.getAttribute(SEARCH_DURING_INGEST_ATTRIBUTE);
if (searchDuringIngest.isEmpty()) { if (searchDuringIngest.isEmpty()) {
throw new HashLookupSettingsException(SEND_INGEST_MESSAGES_ATTRIBUTE + attributeErrorMessage); throw new HashLookupSettingsException(String.format(attributeErrorMessage, SEND_INGEST_MESSAGES_ATTRIBUTE));
} }
Boolean searchDuringIngestFlag = Boolean.parseBoolean(searchDuringIngest); Boolean searchDuringIngestFlag = Boolean.parseBoolean(searchDuringIngest);
final String sendIngestMessages = setEl.getAttribute(SEND_INGEST_MESSAGES_ATTRIBUTE); final String sendIngestMessages = setEl.getAttribute(SEND_INGEST_MESSAGES_ATTRIBUTE);
if (searchDuringIngest.isEmpty()) { if (searchDuringIngest.isEmpty()) {
throw new HashLookupSettingsException(SEND_INGEST_MESSAGES_ATTRIBUTE + attributeErrorMessage); throw new HashLookupSettingsException(String.format(attributeErrorMessage, SEND_INGEST_MESSAGES_ATTRIBUTE));
} }
Boolean sendIngestMessagesFlag = Boolean.parseBoolean(sendIngestMessages); Boolean sendIngestMessagesFlag = Boolean.parseBoolean(sendIngestMessages);
@ -218,10 +250,10 @@ final class HashLookupSettings implements Serializable {
dbPath = pathEl.getTextContent(); dbPath = pathEl.getTextContent();
if (dbPath.isEmpty()) { if (dbPath.isEmpty()) {
throw new HashLookupSettingsException(SEND_INGEST_MESSAGES_ATTRIBUTE + attributeErrorMessage); throw new HashLookupSettingsException(String.format(elementErrorMessage, PATH_ELEMENT));
} }
} else { } else {
throw new HashLookupSettingsException(SEND_INGEST_MESSAGES_ATTRIBUTE + attributeErrorMessage); throw new HashLookupSettingsException(String.format(elementErrorMessage, PATH_ELEMENT));
} }
hashDbInfoList.add(new HashDbInfo(hashSetName, HashDbManager.HashDb.KnownFilesType.valueOf(knownFilesType), hashDbInfoList.add(new HashDbInfo(hashSetName, HashDbManager.HashDb.KnownFilesType.valueOf(knownFilesType),
searchDuringIngestFlag, sendIngestMessagesFlag, dbPath)); searchDuringIngestFlag, sendIngestMessagesFlag, dbPath));
@ -236,9 +268,7 @@ final class HashLookupSettings implements Serializable {
"HashDbManager.baseMessage.updatedFormatHashDbConfig"); "HashDbManager.baseMessage.updatedFormatHashDbConfig");
try { try {
FileUtils.copyFile(new File(configFilePath), new File(backupFilePath)); FileUtils.copyFile(new File(configFilePath), new File(backupFilePath));
logger.log(Level.INFO, NbBundle.getMessage(HashLookupSettings.class, logger.log(Level.INFO, baseMessage + "\nA backup copy of the old configuration has been saved as\n" + backupFilePath);
"HashDbManager.savedBackupOfOldConfigMsg",
baseMessage, backupFilePath));
if (RuntimeProperties.coreComponentsAreActive()) { if (RuntimeProperties.coreComponentsAreActive()) {
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
NbBundle.getMessage(HashLookupSettings.class, NbBundle.getMessage(HashLookupSettings.class,
@ -292,6 +322,16 @@ final class HashLookupSettings implements Serializable {
private final boolean sendIngestMessages; private final boolean sendIngestMessages;
private final String path; private final String path;
/**
* Constructs a HashDbInfo object
*
* @param hashSetName The name of the hash set
* @param knownFilesType The known files type
* @param searchDuringIngest Whether or not the db is searched during
* ingest
* @param sendIngestMessages Whether or not ingest messages are sent
* @param path The path to the db
*/
HashDbInfo(String hashSetName, HashDbManager.HashDb.KnownFilesType knownFilesType, boolean searchDuringIngest, boolean sendIngestMessages, String path) { HashDbInfo(String hashSetName, HashDbManager.HashDb.KnownFilesType knownFilesType, boolean searchDuringIngest, boolean sendIngestMessages, String path) {
this.hashSetName = hashSetName; this.hashSetName = hashSetName;
this.knownFilesType = knownFilesType; this.knownFilesType = knownFilesType;