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

@ -44,7 +44,7 @@ import org.w3c.dom.NodeList;
* Class to represent the settings to be serialized for hash lookup. * Class to represent the settings to be serialized for hash lookup.
*/ */
final class HashLookupSettings implements Serializable { final class HashLookupSettings implements Serializable {
private static final String SERIALIZATION_FILE_NAME = "hashLookup.settings"; //NON-NLS private static final String SERIALIZATION_FILE_NAME = "hashLookup.settings"; //NON-NLS
private static final String SERIALIZATION_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + SERIALIZATION_FILE_NAME; //NON-NLS private static final String SERIALIZATION_FILE_PATH = PlatformUtil.getUserConfigDirectory() + File.separator + SERIALIZATION_FILE_NAME; //NON-NLS
private static final String SET_ELEMENT = "hash_set"; //NON-NLS private static final String SET_ELEMENT = "hash_set"; //NON-NLS
@ -57,10 +57,15 @@ final class HashLookupSettings implements Serializable {
private static final String CONFIG_FILE_NAME = "hashsets.xml"; //NON-NLS private static final String CONFIG_FILE_NAME = "hashsets.xml"; //NON-NLS
private static final String configFilePath = PlatformUtil.getUserConfigDirectory() + File.separator + CONFIG_FILE_NAME; private static final String configFilePath = PlatformUtil.getUserConfigDirectory() + File.separator + CONFIG_FILE_NAME;
private static final Logger logger = Logger.getLogger(HashDbManager.class.getName()); private static final Logger logger = Logger.getLogger(HashDbManager.class.getName());
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,15 +73,22 @@ 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<>();
this.addHashesToList(knownHashSets); this.addHashesToList(knownHashSets);
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,21 +108,38 @@ 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()) {
return readSerializedSettings(); return readSerializedSettings();
} }
return readXmlSettings(); return readXmlSettings();
} }
/**
* 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))) {
@ -121,7 +150,16 @@ final class HashLookupSettings implements Serializable {
throw new HashLookupSettingsException("Could not read hash database settings.", ex); throw new HashLookupSettingsException("Could not read hash database settings.", ex);
} }
} }
/**
* 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,34 +168,30 @@ 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) {
Element setEl = (Element) setsNList.item(i); Element setEl = (Element) setsNList.item(i);
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,
@ -181,10 +213,10 @@ final class HashLookupSettings implements Serializable {
hashSetName = newHashSetName; hashSetName = newHashSetName;
} }
} }
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.
@ -192,19 +224,19 @@ final class HashLookupSettings implements Serializable {
knownFilesType = HashDbManager.HashDb.KnownFilesType.KNOWN.toString(); knownFilesType = HashDbManager.HashDb.KnownFilesType.KNOWN.toString();
updatedSchema = true; updatedSchema = true;
} }
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);
String dbPath; String dbPath;
NodeList pathsNList = setEl.getElementsByTagName(PATH_ELEMENT); NodeList pathsNList = setEl.getElementsByTagName(PATH_ELEMENT);
if (pathsNList.getLength() > 0) { if (pathsNList.getLength() > 0) {
@ -215,19 +247,19 @@ final class HashLookupSettings implements Serializable {
if (null != legacyPathNumber && !legacyPathNumber.isEmpty()) { if (null != legacyPathNumber && !legacyPathNumber.isEmpty()) {
updatedSchema = true; updatedSchema = true;
} }
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));
hashSetNames.add(hashSetName); hashSetNames.add(hashSetName);
} }
if (updatedSchema) { if (updatedSchema) {
String backupFilePath = configFilePath + ".v1_backup"; //NON-NLS String backupFilePath = configFilePath + ".v1_backup"; //NON-NLS
String messageBoxTitle = NbBundle.getMessage(HashLookupSettings.class, String messageBoxTitle = NbBundle.getMessage(HashLookupSettings.class,
@ -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,
@ -284,14 +314,24 @@ final class HashLookupSettings implements Serializable {
* hash lookups. * hash lookups.
*/ */
static final class HashDbInfo implements Serializable { static final class HashDbInfo implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final String hashSetName; private final String hashSetName;
private final HashDbManager.HashDb.KnownFilesType knownFilesType; private final HashDbManager.HashDb.KnownFilesType knownFilesType;
private final boolean searchDuringIngest; private final boolean searchDuringIngest;
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;
@ -352,13 +392,13 @@ final class HashLookupSettings implements Serializable {
* clients of the user-defined file types manager. * clients of the user-defined file types manager.
*/ */
static class HashLookupSettingsException extends Exception { static class HashLookupSettingsException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
HashLookupSettingsException(String message) { HashLookupSettingsException(String message) {
super(message); super(message);
} }
HashLookupSettingsException(String message, Throwable throwable) { HashLookupSettingsException(String message, Throwable throwable) {
super(message, throwable); super(message, throwable);
} }