19 package org.sleuthkit.autopsy.events;
21 import java.net.URISyntaxException;
22 import java.util.logging.Level;
23 import javax.annotation.concurrent.GuardedBy;
24 import javax.annotation.concurrent.ThreadSafe;
25 import javax.jms.Connection;
26 import javax.jms.DeliveryMode;
27 import javax.jms.JMSException;
28 import javax.jms.Message;
29 import javax.jms.MessageConsumer;
30 import javax.jms.MessageListener;
31 import javax.jms.MessageProducer;
32 import javax.jms.ObjectMessage;
33 import javax.jms.Session;
34 import javax.jms.Topic;
35 import org.apache.activemq.ActiveMQConnectionFactory;
46 final class RemoteEventPublisher {
48 private static final Logger logger = Logger.
getLogger(RemoteEventPublisher.class.getName());
49 private static final String ALL_MESSAGE_SELECTOR =
"All";
50 private final LocalEventPublisher localPublisher;
52 private final Connection connection;
54 private final Session session;
56 private final MessageProducer producer;
58 private final MessageConsumer consumer;
75 RemoteEventPublisher(String eventChannelName, LocalEventPublisher localPublisher, MessageServiceConnectionInfo info) throws URISyntaxException, JMSException {
77 this.localPublisher = localPublisher;
78 ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory(info.getUserName(), info.getPassword(), info.getURI());
79 connection = connectionFactory.createConnection();
81 session = connection.createSession(
false, Session.AUTO_ACKNOWLEDGE);
82 Topic topic = session.createTopic(eventChannelName);
83 producer = session.createProducer(topic);
84 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
85 consumer = session.createConsumer(topic,
"events = '" + ALL_MESSAGE_SELECTOR +
"'",
true);
86 MessageReceiver receiver =
new MessageReceiver();
87 consumer.setMessageListener(receiver);
88 }
catch (URISyntaxException | JMSException ex) {
89 logger.log(Level.SEVERE,
"Failed to connect to event channel", ex);
92 }
catch (JMSException ignored) {
108 synchronized void stop() throws JMSException {
109 if (null != producer) {
112 if (null != consumer) {
115 if (null != session) {
118 if (null != connection) {
128 synchronized void publish(AutopsyEvent event)
throws JMSException {
129 ObjectMessage message = session.createObjectMessage();
130 message.setStringProperty(
"events", ALL_MESSAGE_SELECTOR);
131 message.setObject(event);
132 producer.send(message);
150 if (message instanceof ObjectMessage) {
151 ObjectMessage objectMessage = (ObjectMessage) message;
152 Object
object = objectMessage.getObject();
154 AutopsyEvent
event = (AutopsyEvent)
object;
155 event.setSourceType(AutopsyEvent.SourceType.REMOTE);
156 localPublisher.publish(event);
159 }
catch (JMSException ex) {
160 logger.log(Level.SEVERE,
"Error receiving message", ex);
161 }
catch (Throwable ex) {
163 logger.log(Level.SEVERE,
"Unexpected error receiving message", ex);
void onMessage(Message message)
synchronized static Logger getLogger(String name)