better handling of exceptions in event bus eventhandlers

This commit is contained in:
jmillman 2015-06-22 16:42:44 -04:00
parent c689956bbc
commit 1c16f9e405
3 changed files with 37 additions and 20 deletions

View File

@ -20,20 +20,21 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAdder;
import java.util.logging.Level;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.events.ContentTagAddedEvent;
import org.sleuthkit.autopsy.events.ContentTagDeletedEvent;
import org.sleuthkit.autopsy.imagegallery.ImageGalleryController;
import org.sleuthkit.autopsy.imagegallery.datamodel.Category;
import org.sleuthkit.autopsy.imagegallery.datamodel.DrawableDB;
import org.sleuthkit.datamodel.ContentTag;
import org.sleuthkit.datamodel.TagName;
import org.sleuthkit.datamodel.TskCoreException;
@ -66,7 +67,11 @@ public class CategoryManager {
/**
* Used to distribute {@link CategoryChangeEvent}s
*/
private final EventBus categoryEventBus = new EventBus("Category Event Bus");
private final EventBus categoryEventBus = new AsyncEventBus(Executors.newSingleThreadExecutor(
new BasicThreadFactory.Builder().namingPattern("Category Event Bus").uncaughtExceptionHandler((Thread t, Throwable e) -> {
LOGGER.log(Level.SEVERE, "uncaught exception in event bus handler", e);
}).build()
));
/**
* For performance reasons, keep current category counts in memory. All of

View File

@ -18,19 +18,20 @@
*/
package org.sleuthkit.autopsy.imagegallery.datamodel;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.sleuthkit.autopsy.casemodule.services.TagsManager;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.events.ContentTagAddedEvent;
import org.sleuthkit.autopsy.events.ContentTagDeletedEvent;
import org.sleuthkit.autopsy.imagegallery.datamodel.Category;
import org.sleuthkit.autopsy.imagegallery.datamodel.DrawableFile;
import org.sleuthkit.datamodel.Content;
import org.sleuthkit.datamodel.ContentTag;
import org.sleuthkit.datamodel.TagName;
@ -42,13 +43,20 @@ import org.sleuthkit.datamodel.TskCoreException;
*/
public class DrawableTagsManager {
private static final Logger LOGGER = Logger.getLogger(DrawableTagsManager.class.getName());
private static final String FOLLOW_UP = "Follow Up";
final private Object autopsyTagsManagerLock = new Object();
private TagsManager autopsyTagsManager;
/** Used to distribute {@link TagsChangeEvent}s */
private final EventBus tagsEventBus = new EventBus("Tags Event Bus");
private final EventBus tagsEventBus = new AsyncEventBus(
Executors.newSingleThreadExecutor(
new BasicThreadFactory.Builder().namingPattern("Tags Event Bus").uncaughtExceptionHandler((Thread t, Throwable e) -> {
LOGGER.log(Level.SEVERE, "uncaught exception in event bus handler", e);
}).build()
));
/** The tag name corresponding to the "built-in" tag "Follow Up" */
private TagName followUpTagName;
@ -130,7 +138,7 @@ public class DrawableTagsManager {
.filter(CategoryManager::isCategoryTagName)
.collect(Collectors.toSet());
} catch (TskCoreException | IllegalStateException ex) {
Logger.getLogger(DrawableTagsManager.class.getName()).log(Level.WARNING, "couldn't access case", ex);
LOGGER.log(Level.WARNING, "couldn't access case", ex);
}
return Collections.emptySet();
}
@ -166,7 +174,7 @@ public class DrawableTagsManager {
throw new TskCoreException("tagame exists but wasn't found", ex);
}
} catch (IllegalStateException ex) {
Logger.getLogger(DrawableTagsManager.class.getName()).log(Level.SEVERE, "Case was closed out from underneath", ex);
LOGGER.log(Level.SEVERE, "Case was closed out from underneath", ex);
throw new TskCoreException("Case was closed out from underneath", ex);
}
}

View File

@ -537,15 +537,16 @@ public class GroupManager {
@Subscribe
public void handleTagAdded(ContentTagAddedEvent evt) {
GroupKey<?> groupKey = null;
if (groupBy == DrawableAttribute.TAGS) {
groupKey = new GroupKey<TagName>(DrawableAttribute.TAGS, evt.getTag().getName());
} else if (groupBy == DrawableAttribute.CATEGORY) {
if (groupBy == DrawableAttribute.CATEGORY && CategoryManager.isCategoryTagName(evt.getTag().getName())) {
groupKey = new GroupKey<Category>(DrawableAttribute.CATEGORY, CategoryManager.categoryFromTagName(evt.getTag().getName()));
} else if (groupBy == DrawableAttribute.TAGS && CategoryManager.isNotCategoryTagName(evt.getTag().getName())) {
groupKey = new GroupKey<TagName>(DrawableAttribute.TAGS, evt.getTag().getName());
}
if (groupKey != null) {
final long fileID = evt.getTag().getContent().getId();
DrawableGroup g = getGroupForKey(groupKey);
addFileToGroup(g, groupKey, fileID);
}
}
@SuppressWarnings("AssignmentToMethodParameter")
@ -563,14 +564,17 @@ public class GroupManager {
@Subscribe
public void handleTagDeleted(ContentTagDeletedEvent evt) {
GroupKey<?> groupKey = null;
if (groupBy == DrawableAttribute.TAGS) {
groupKey = new GroupKey<TagName>(DrawableAttribute.TAGS, evt.getTag().getName());
} else if (groupBy == DrawableAttribute.CATEGORY) {
if (groupBy == DrawableAttribute.CATEGORY && CategoryManager.isCategoryTagName(evt.getTag().getName())) {
groupKey = new GroupKey<Category>(DrawableAttribute.CATEGORY, CategoryManager.categoryFromTagName(evt.getTag().getName()));
} else if (groupBy == DrawableAttribute.TAGS && CategoryManager.isNotCategoryTagName(evt.getTag().getName())) {
groupKey = new GroupKey<TagName>(DrawableAttribute.TAGS, evt.getTag().getName());
}
if (groupKey != null) {
final long fileID = evt.getTag().getContent().getId();
DrawableGroup g = removeFromGroup(groupKey, fileID);
}
}
@Subscribe
synchronized public void handleFileRemoved(Collection<Long> removedFileIDs) {