19 package org.sleuthkit.autopsy.events;
21 import java.beans.PropertyChangeListener;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.logging.Level;
26 import javax.annotation.concurrent.ThreadSafe;
37 final class LocalEventPublisher {
39 private static final Logger logger = Logger.
getLogger(LocalEventPublisher.class.getName());
40 private final Map<String, Set<PropertyChangeListener>> subscribersByEvent;
46 LocalEventPublisher() {
47 subscribersByEvent =
new ConcurrentHashMap<>();
56 void addSubscriber(Set<String> eventNames, PropertyChangeListener subscriber) {
57 for (String eventName : eventNames) {
58 addSubscriber(eventName, subscriber);
68 void addSubscriber(String eventName, PropertyChangeListener subscriber) {
69 subscribersByEvent.putIfAbsent(eventName, ConcurrentHashMap.<PropertyChangeListener>newKeySet());
70 Set<PropertyChangeListener> subscribers = subscribersByEvent.get(eventName);
71 subscribers.add(subscriber);
80 void removeSubscriber(Set<String> eventNames, PropertyChangeListener subscriber) {
81 for (String eventName : eventNames) {
82 removeSubscriber(eventName, subscriber);
92 void removeSubscriber(String eventName, PropertyChangeListener subscriber) {
93 Set<PropertyChangeListener> subscribers = subscribersByEvent.getOrDefault(eventName, null);
94 if (null != subscribers) {
95 subscribers.remove(subscriber);
105 void publish(AutopsyEvent event) {
106 Set<PropertyChangeListener> subscribers = subscribersByEvent.getOrDefault(event.getPropertyName(), null);
107 if (null != subscribers) {
108 subscribers.forEach((subscriber) -> {
110 subscriber.propertyChange(event);
111 }
catch (Exception ex) {
112 logger.log(Level.SEVERE,
"Exception thrown by subscriber", ex);
synchronized static Logger getLogger(String name)