Autopsy  4.6.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-2017 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  private final MessageReceiver receiver;
60 
76  RemoteEventPublisher(String eventChannelName, LocalEventPublisher localPublisher, MessageServiceConnectionInfo info) throws URISyntaxException, JMSException {
77  try {
78  this.localPublisher = localPublisher;
79  ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(info.getUserName(), info.getPassword(), info.getURI());
80  connection = connectionFactory.createConnection();
81  connection.start();
82  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
83  Topic topic = session.createTopic(eventChannelName);
84  producer = session.createProducer(topic);
85  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
86  consumer = session.createConsumer(topic, "events = '" + ALL_MESSAGE_SELECTOR + "'", true); //NON-NLS
87  receiver = new MessageReceiver();
88  consumer.setMessageListener(receiver);
89  } catch (URISyntaxException | JMSException ex) {
90  logger.log(Level.SEVERE, "Failed to connect to event channel", ex); //NON-NLS
91  try {
92  stop();
93  } catch (JMSException ignored) {
98  }
99  throw ex;
100  }
101  }
102 
109  synchronized void stop() throws JMSException {
110  if (null != producer) {
111  producer.close();
112  }
113  if (null != consumer) {
114  consumer.close();
115  }
116  if (null != session) {
117  session.close();
118  }
119  if (null != connection) {
120  connection.close();
121  }
122  }
123 
129  synchronized void publish(AutopsyEvent event) throws JMSException {
130  ObjectMessage message = session.createObjectMessage();
131  message.setStringProperty("events", ALL_MESSAGE_SELECTOR); //NON-NLS
132  message.setObject(event);
133  producer.send(message);
134  }
135 
140  private final class MessageReceiver implements MessageListener {
141 
148  @Override
149  public void onMessage(Message message) {
150  try {
151  if (message instanceof ObjectMessage) {
152  ObjectMessage objectMessage = (ObjectMessage) message;
153  Object object = objectMessage.getObject();
154  if (object instanceof AutopsyEvent) {
155  AutopsyEvent event = (AutopsyEvent) object;
156  event.setSourceType(AutopsyEvent.SourceType.REMOTE);
157  localPublisher.publish(event);
158  }
159  }
160  } catch (JMSException ex) {
161  logger.log(Level.SEVERE, "Error receiving message", ex); //NON-NLS
162  } catch (Throwable ex) {
163  // Exception firewall.
164  logger.log(Level.SEVERE, "Unexpected error receiving message", ex); //NON-NLS
165  }
166  }
167  }
168 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.