Fasel/fasel-jfx/src/main/java/de/vanitasvitae/fasel/xmpp/XmppConnectionService.java

48 lines
1.5 KiB
Java
Raw Normal View History

2018-09-16 02:24:57 +02:00
package de.vanitasvitae.fasel.xmpp;
import java.io.IOException;
2019-12-12 14:53:17 +01:00
import java.util.Map;
import java.util.WeakHashMap;
2018-09-16 02:24:57 +02:00
import org.jivesoftware.smack.SmackException;
2019-12-12 14:53:17 +01:00
import org.jivesoftware.smack.XMPPConnection;
2018-09-16 02:24:57 +02:00
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
2019-12-12 14:53:17 +01:00
import de.vanitasvitae.fasel.db.entity.Account;
2018-09-16 02:24:57 +02:00
import org.jxmpp.stringprep.XmppStringprepException;
2019-12-12 14:53:17 +01:00
public final class XmppConnectionService {
2018-09-16 02:24:57 +02:00
private final XMPPTCPConnection connection;
2019-12-12 14:53:17 +01:00
private static final Map<Account, XmppConnectionService> CONNECTIONS = new WeakHashMap<>();
public static XmppConnectionService connectionForAccount(Account account) throws XmppStringprepException {
XmppConnectionService connection = CONNECTIONS.get(account);
if (connection == null) {
connection = new XmppConnectionService(account.getUsername(), account.getServiceAddress(), account.getPassword());
CONNECTIONS.put(account, connection);
}
return connection;
}
private XmppConnectionService(String username, String serverAddress, String password)
2018-09-16 02:24:57 +02:00
throws XmppStringprepException {
connection = new XMPPTCPConnection(username, password, serverAddress);
}
2019-12-12 14:53:17 +01:00
public XMPPConnection getConnection() {
return connection;
}
2018-09-16 02:24:57 +02:00
public void login() throws InterruptedException, IOException, SmackException, XMPPException {
connection.connect();
connection.login();
}
public void attachDatabase() {
}
}