Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
RemoteEventPublisher.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.events;
20 
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;
37 
45 @ThreadSafe
46 final class RemoteEventPublisher {
47 
48  private static final Logger logger = Logger.getLogger(RemoteEventPublisher.class.getName());
49  private static final String ALL_MESSAGE_SELECTOR = "All"; //NON-NLS
50  private final LocalEventPublisher localPublisher; // LocalEventPublisher is thread-safe
51  @GuardedBy("this")
52  private final Connection connection;
53  @GuardedBy("this")
54  private final Session session;
55  @GuardedBy("this")
56  private final MessageProducer producer;
57  @GuardedBy("this")
58  private final MessageConsumer consumer;
59 
75  RemoteEventPublisher(String eventChannelName, LocalEventPublisher localPublisher, MessageServiceConnectionInfo info) throws URISyntaxException, JMSException {
76  try {
77  this.localPublisher = localPublisher;
78  ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(info.getUserName(), info.getPassword(), info.getURI());
79  connection = connectionFactory.createConnection();
80  connection.start();
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); //NON-NLS
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); //NON-NLS
90  try {
91  stop();
92  } catch (JMSException ignored) {
97  }
98  throw ex;
99  }
100  }
101 
108  synchronized void stop() throws JMSException {
109  if (null != producer) {
110  producer.close();
111  }
112  if (null != consumer) {
113  consumer.close();
114  }
115  if (null != session) {
116  session.close();
117  }
118  if (null != connection) {
119  connection.close();
120  }
121  }
122 
128  synchronized void publish(AutopsyEvent event) throws JMSException {
129  ObjectMessage message = session.createObjectMessage();
130  message.setStringProperty("events", ALL_MESSAGE_SELECTOR); //NON-NLS
131  message.setObject(event);
132  producer.send(message);
133  }
134 
139  private final class MessageReceiver implements MessageListener {
140 
147  @Override
148  public void onMessage(Message message) {
149  try {
150  if (message instanceof ObjectMessage) {
151  ObjectMessage objectMessage = (ObjectMessage) message;
152  Object object = objectMessage.getObject();
153  if (object instanceof AutopsyEvent) {
154  AutopsyEvent event = (AutopsyEvent) object;
155  event.setSourceType(AutopsyEvent.SourceType.REMOTE);
156  localPublisher.publish(event);
157  }
158  }
159  } catch (JMSException ex) {
160  logger.log(Level.SEVERE, "Error receiving message", ex); //NON-NLS
161  } catch (Throwable ex) {
162  // Exception firewall.
163  logger.log(Level.SEVERE, "Unexpected error receiving message", ex); //NON-NLS
164  }
165  }
166  }
167 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.