Added canConnect() method to the MessageServiceConnectionInfo class

This commit is contained in:
Eugene Livis 2015-07-16 09:42:05 -04:00
parent ef48080439
commit ba8dd33a06
2 changed files with 22 additions and 29 deletions

View File

@ -204,7 +204,7 @@ public class ServicesMonitor {
private String checkServiceStatus(String service) {
try {
if (service.equals(ServiceName.REMOTE_CASE_DATABASE.toString())) {
if (canConnectToRemoteDb()) {
if (UserPreferences.getDatabaseConnectionInfo().canConnect()) {
setServiceStatus(ServiceName.REMOTE_CASE_DATABASE.toString(), ServiceStatus.UP.toString(), "");
return ServiceStatus.UP.toString();
} else {
@ -221,7 +221,7 @@ public class ServicesMonitor {
return ServiceStatus.DOWN.toString();
}
} else if (service.equals(ServiceName.MESSAGING.toString())) {
if (canConnectToMessagingService()) {
if (UserPreferences.getMessageServiceConnectionInfo().canConnect()) {
setServiceStatus(ServiceName.MESSAGING.toString(), ServiceStatus.UP.toString(), "");
return ServiceStatus.UP.toString();
} else {
@ -297,33 +297,6 @@ public class ServicesMonitor {
public void removeSubscriber(PropertyChangeListener subscriber) {
eventPublisher.removeSubscriber(serviceNames, subscriber);
}
/**
* Verifies connection to remote database.
*
* @return True if connection can be established, false otherwise.
*/
private boolean canConnectToRemoteDb() {
return UserPreferences.getDatabaseConnectionInfo().canConnect();
}
/**
* Verifies connection to messaging service.
*
* @return True if connection can be established, false otherwise.
*/
private boolean canConnectToMessagingService() {
MessageServiceConnectionInfo msgInfo = UserPreferences.getMessageServiceConnectionInfo();
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(msgInfo.getUserName(), msgInfo.getPassword(), msgInfo.getURI());
Connection connection = connectionFactory.createConnection();
connection.start();
connection.close();
return true;
} catch (URISyntaxException | JMSException ex) {
return false;
}
}
/**
* Verifies connectivity to all services.

View File

@ -21,6 +21,10 @@ package org.sleuthkit.autopsy.events;
import java.net.URI;
import java.net.URISyntaxException;
import javax.annotation.concurrent.Immutable;
import javax.jms.Connection;
import javax.jms.JMSException;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.sleuthkit.autopsy.core.UserPreferences;
/**
* Connection info for a Java Message Service (JMS) provider. Thread-safe.
@ -99,4 +103,20 @@ public final class MessageServiceConnectionInfo {
return new URI(String.format(MESSAGE_SERVICE_URI, host, port));
}
/**
* Verifies connection to messaging service.
*
* @return True if connection can be established, false otherwise.
*/
public boolean canConnect() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(getUserName(), getPassword(), getURI());
Connection connection = connectionFactory.createConnection();
connection.start();
connection.close();
return true;
} catch (URISyntaxException | JMSException ex) {
return false;
}
}
}