Autopsy  4.1
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-2015 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.jms.Connection;
24 import javax.jms.DeliveryMode;
25 import javax.jms.JMSException;
26 import javax.jms.Message;
27 import javax.jms.MessageConsumer;
28 import javax.jms.MessageListener;
29 import javax.jms.MessageProducer;
30 import javax.jms.ObjectMessage;
31 import javax.jms.Session;
32 import javax.jms.Topic;
33 import org.apache.activemq.ActiveMQConnectionFactory;
35 
43 final class RemoteEventPublisher {
44 
45  private static final Logger logger = Logger.getLogger(RemoteEventPublisher.class.getName());
46  private static final String ALL_MESSAGE_SELECTOR = "All"; //NON-NLS
47  private final LocalEventPublisher localPublisher;
48  private final Connection connection;
49  private final Session session;
50  private final MessageProducer producer;
51  private final MessageConsumer consumer;
52  private final MessageReceiver receiver;
53 
69  RemoteEventPublisher(String eventChannelName, LocalEventPublisher localPublisher, MessageServiceConnectionInfo info) throws URISyntaxException, JMSException {
70  try {
71  this.localPublisher = localPublisher;
72  ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(info.getUserName(), info.getPassword(), info.getURI());
73  connection = connectionFactory.createConnection();
74  connection.start();
75  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
76  Topic topic = session.createTopic(eventChannelName);
77  producer = session.createProducer(topic);
78  producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
79  consumer = session.createConsumer(topic, "events = '" + ALL_MESSAGE_SELECTOR + "'", true); //NON-NLS
80  receiver = new MessageReceiver();
81  consumer.setMessageListener(receiver);
82  } catch (URISyntaxException | JMSException ex) {
83  logger.log(Level.SEVERE, "Failed to connect to event channel", ex); //NON-NLS
84  try {
85  stop();
86  } catch (JMSException ignored) {
91  }
92  throw ex;
93  }
94  }
95 
102  synchronized void stop() throws JMSException {
103  if (null != producer) {
104  producer.close();
105  }
106  if (null != consumer) {
107  consumer.close();
108  }
109  if (null != session) {
110  session.close();
111  }
112  if (null != connection) {
113  connection.close();
114  }
115  }
116 
122  synchronized void publish(AutopsyEvent event) throws JMSException {
123  ObjectMessage message = session.createObjectMessage();
124  message.setStringProperty("events", ALL_MESSAGE_SELECTOR); //NON-NLS
125  message.setObject(event);
126  producer.send(message);
127  }
128 
133  private final class MessageReceiver implements MessageListener {
134 
141  @Override
142  public void onMessage(Message message) {
143  try {
144  if (message instanceof ObjectMessage) {
145  ObjectMessage objectMessage = (ObjectMessage) message;
146  Object object = objectMessage.getObject();
147  if (object instanceof AutopsyEvent) {
148  AutopsyEvent event = (AutopsyEvent) object;
149  event.setSourceType(AutopsyEvent.SourceType.REMOTE);
150  localPublisher.publish(event);
151  }
152  }
153  } catch (Exception ex) {
154  logger.log(Level.SEVERE, "Error receiving message", ex); //NON-NLS
155  }
156  }
157  }
158 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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