blob: f202cca22424d31462fa09850fe6c2105b694968 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2020 Agence spatiale canadienne / Canadian Space Agency
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pierre Allard - initial API and implementation
*
* SPDX-License-Identifier: EPL-1.0
******************************************************************************/
package org.eclipse.apogy.addons.mqtt.impl;
import org.eclipse.apogy.addons.mqtt.ApogyAddonsMQTTFacade;
import org.eclipse.apogy.addons.mqtt.ApogyAddonsMQTTPackage;
import org.eclipse.apogy.addons.mqtt.MQTTClient;
import org.eclipse.apogy.addons.mqtt.MQTTClientConnectionOptions;
import org.eclipse.apogy.addons.mqtt.MQTTClientState;
import org.eclipse.apogy.addons.mqtt.ui.PasswordDialog;
import org.eclipse.apogy.common.emf.ApogyCommonTransactionFacade;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MQTTBrokerConnectionInfoCustomImpl extends MQTTBrokerConnectionInfoImpl
{
private static final Logger Logger = LoggerFactory.getLogger(MQTTBrokerConnectionInfoCustomImpl.class);
protected boolean requireValidation = true;
@Override
public void setUserName(String newUserName) {
super.setUserName(newUserName);
requireValidation = true;
}
@Override
public void setPassword(String newPassword) {
super.setPassword(newPassword);
requireValidation = true;
}
@Override
public void setPort(int newPort)
{
super.setPort(newPort);
requireValidation = true;
}
@Override
public String getUserPassword()
{
String passwd = password;
if(passwd == null || passwd.length() == 0)
{
try
{
PasswordDialog dialog;
dialog = new PasswordDialog(Display.getDefault().getActiveShell(), MQTTBrokerConnectionInfoCustomImpl.this);
if(dialog.open() == Window.OK)
{
passwd = dialog.getPassword();
ApogyCommonTransactionFacade.INSTANCE.basicSet(MQTTBrokerConnectionInfoCustomImpl.this, ApogyAddonsMQTTPackage.Literals.MQTT_BROKER_CONNECTION_INFO__PASSWORD, passwd, true);
}
else
{
return password;
}
// Validate the connection.
int tryCount = 1;
while (!validate() && tryCount < 3)
{
// Show Error message
String message = "The specified user name / password pair is invalid.";
int triesLeft = 3 - tryCount;
if(triesLeft != 0)
{
message += " You have " + triesLeft + " try left.";
MessageDialog messageDialog = new MessageDialog(Display.getDefault().getActiveShell(), "Invalid user name / password ", null,
message, MessageDialog.ERROR,
new String[] {"OK"}, 0);
messageDialog.open();
// Open the get password dialog again.c
dialog = new PasswordDialog(Display.getDefault().getActiveShell(), MQTTBrokerConnectionInfoCustomImpl.this);
if(dialog.open() == Window.OK)
{
passwd = dialog.getPassword();
ApogyCommonTransactionFacade.INSTANCE.basicSet(MQTTBrokerConnectionInfoCustomImpl.this, ApogyAddonsMQTTPackage.Literals.MQTT_BROKER_CONNECTION_INFO__PASSWORD, passwd, true);
}
}
tryCount++;
}
}
catch (Exception e)
{
// If the call was not made from the UI Thread.
CustomRunnable runnable = new CustomRunnable();
Display.getDefault().asyncExec(runnable);
while(!runnable.done)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
}
}
}
return password;
}
@Override
public String getEffectiveUserName()
{
if(isUseSystemUserName())
{
return System.getProperty("user.name");
}
else
{
return getUserName();
}
}
@Override
public boolean validate()
{
if(requireValidation)
{
// Attempts to connect.
String userName = this.getEffectiveUserName();
String password = getUserPassword();
String brokerURL = getBrokerHostName();
int port = getPort();
MQTTClient client = null;
try
{
MQTTClientConnectionOptions options = ApogyAddonsMQTTFacade.INSTANCE.createMQTTClientConnectionOptions(userName, password, true, false);
client = ApogyAddonsMQTTFacade.INSTANCE.createMQTTClient(" MQTTBrokerConnectionInfo_Validate", brokerURL, port, null, options);
client.setVerbose(false);
client.setEnableServerAutomaticReconnect(false);
client.start();
// Wait a bit for connection.
long startTime = System.currentTimeMillis();
while(client.getState() != MQTTClientState.CONNECTED && (System.currentTimeMillis() - startTime) < 10000)
{
Thread.sleep(100);
}
ApogyCommonTransactionFacade.INSTANCE.basicSet(MQTTBrokerConnectionInfoCustomImpl.this, ApogyAddonsMQTTPackage.Literals.MQTT_BROKER_CONNECTION_INFO__VALID, (client.getState() == MQTTClientState.CONNECTED), true);
client.stop();
}
catch (Exception e)
{
Logger.error("Connection failed with Exception.", e);
ApogyCommonTransactionFacade.INSTANCE.basicSet(MQTTBrokerConnectionInfoCustomImpl.this, ApogyAddonsMQTTPackage.Literals.MQTT_BROKER_CONNECTION_INFO__VALID, false, true);
// Stops client if applicable.
if(client != null)
{
try
{
client.stop();
}
catch (Exception e2)
{
}
}
}
requireValidation = false;
}
return isValid();
}
private class CustomRunnable implements Runnable
{
public boolean done = false;
public CustomRunnable()
{
done = false;
}
@Override
public void run()
{
try
{
PasswordDialog dialog;
dialog = new PasswordDialog(Display.getDefault().getActiveShell(), MQTTBrokerConnectionInfoCustomImpl.this);
if(dialog.open() == Window.OK)
{
String passwd = dialog.getPassword();
ApogyCommonTransactionFacade.INSTANCE.basicSet(MQTTBrokerConnectionInfoCustomImpl.this, ApogyAddonsMQTTPackage.Literals.MQTT_BROKER_CONNECTION_INFO__PASSWORD, passwd, true);
}
}
catch (Exception e)
{
}
done = true;
}
}
}