diff --git a/Core/src/org/sleuthkit/autopsy/events/AutopsyEventException.java b/Core/src/org/sleuthkit/autopsy/events/AutopsyEventException.java new file mode 100644 index 0000000000..ae946e349f --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/events/AutopsyEventException.java @@ -0,0 +1,55 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2015 Basis Technology Corp. + * Contact: carrier sleuthkit org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.events; + +import javax.annotation.concurrent.Immutable; + +/** + * Provides a generic events system exception for clients of the events package. + */ +@Immutable +public final class AutopsyEventException extends Exception { + + /** + * Constructs a new exception with null as its message. + */ + AutopsyEventException() { + super(); + } + + /** + * Constructs a new exception with the specified message. + * + * @param message The message. + */ + AutopsyEventException(String message) { + super(message); + } + + /** + * Constructs a new exception with the specified message and cause. + * + * @param message The message. + * @param cause The cause. + */ + AutopsyEventException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/Core/src/org/sleuthkit/autopsy/events/AutopsyEventPublisher.java b/Core/src/org/sleuthkit/autopsy/events/AutopsyEventPublisher.java new file mode 100644 index 0000000000..04821df349 --- /dev/null +++ b/Core/src/org/sleuthkit/autopsy/events/AutopsyEventPublisher.java @@ -0,0 +1,142 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2015 Basis Technology Corp. + * Contact: carrier sleuthkit org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.sleuthkit.autopsy.events; + +import java.beans.PropertyChangeListener; +import java.net.URISyntaxException; +import java.util.Set; +import java.util.logging.Level; +import javax.jms.JMSException; +import org.sleuthkit.autopsy.core.UserPreferences; +import org.sleuthkit.autopsy.coreutils.Logger; + +/** + * Provides thread-safe support for publishing events to registered subscribers + * on both this Autopsy node and other Autopsy nodes. Subscribers are + * constrained to be PropertyChangeListeners to integrate with the legacy use of + * JavaBeans PropertyChangeEvents and PropertyChangeListeners as an application + * event system. + */ +public final class AutopsyEventPublisher { + + private static final Logger logger = Logger.getLogger(AutopsyEventPublisher.class.getName()); + private final LocalEventPublisher localPublisher; + private RemoteEventPublisher remotePublisher; + + /** + * Constructs an object for publishing events to registered subscribers on + * both this Autopsy node and other Autopsy nodes. Communication with other + * nodes is not turned on by default - call openRemoteEventChannel() after + * construction. + */ + public AutopsyEventPublisher() { + localPublisher = new LocalEventPublisher(); + } + + /** + * Opens the event channel used for publishing events to and receiving + * events from other Autopsy nodes. Only one channel may be open at a time. + * + * @param channelName The name of the event channel. + * @throws AutopsyEventException if the channel was not opened. + */ + synchronized public void openRemoteEventChannel(String channelName) throws AutopsyEventException { + if (null != remotePublisher) { + closeRemoteEventChannel(); + } + try { + remotePublisher = new RemoteEventPublisher(channelName, localPublisher, UserPreferences.getMessageServiceConnectionInfo()); + } catch (URISyntaxException | JMSException ex) { + String message = "Failed to open remote event channel"; //NON-NLS + logger.log(Level.SEVERE, message, ex); + throw new AutopsyEventException(message, ex); + } + } + + /** + * Closes the event channel used for publishing events to and receiving + * events from other Autopsy nodes. + */ + synchronized public void closeRemoteEventChannel() { + if (null != remotePublisher) { + try { + remotePublisher.stop(); + } catch (JMSException ex) { + logger.log(Level.SEVERE, "Error closing remote event channel", ex); //NON-NLS + } + remotePublisher = null; + } + } + + /** + * Adds an event subscriber to this publisher. + * + * @param eventNames The events the subscriber is interested in. + * @param subscriber The subscriber to add. + */ + synchronized public void addSubscriber(Set eventNames, PropertyChangeListener subscriber) { + localPublisher.addSubscriber(eventNames, subscriber); + } + + /** + * Adds an event subscriber to this publisher. + * + * @param eventName The event the subscriber is interested in. + * @param subscriber The subscriber to add. + */ + synchronized public void addSubscriber(String eventName, PropertyChangeListener subscriber) { + localPublisher.addSubscriber(eventName, subscriber); + } + + /** + * Removes an event subscriber from this publisher. + * + * @param eventNames The events the subscriber is no longer interested in. + * @param subscriber The subscriber to remove. + */ + synchronized public void removeSubscriber(Set eventNames, PropertyChangeListener subscriber) { + localPublisher.removeSubscriber(eventNames, subscriber); + } + + /** + * Removes an event subscriber from this publisher. + * + * @param eventNames The event the subscriber is no longer interested in. + * @param subscriber The subscriber to remove. + */ + synchronized public void removeSubscriber(String eventName, PropertyChangeListener subscriber) { + localPublisher.removeSubscriber(eventName, subscriber); + } + + /** + * Publishes an event. + * + * @param event The event to publish. + * @throws JMSException + */ + synchronized public void publish(AutopsyEvent event) throws JMSException { + event.setSourceType(AutopsyEvent.SourceType.LOCAL); + localPublisher.publish(event); + if (null != remotePublisher) { + event.setSourceType(AutopsyEvent.SourceType.REMOTE); + remotePublisher.send(event); + } + } + +}