aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-xmpp/src/main/java/com/juick/components/XMPPServer.java')
-rw-r--r--juick-xmpp/src/main/java/com/juick/components/XMPPServer.java60
1 files changed, 35 insertions, 25 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
index 463d8c2a..e26e0a1e 100644
--- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
+++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
@@ -1,7 +1,10 @@
package com.juick.components;
import com.juick.components.s2s.*;
-import com.juick.xmpp.*;
+import com.juick.util.ThreadHelper;
+import com.juick.xmpp.JID;
+import com.juick.xmpp.Stanza;
+import com.juick.xmpp.StanzaChild;
import com.juick.xmpp.extensions.JuickMessage;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
@@ -23,11 +26,9 @@ import java.util.*;
import java.util.concurrent.ExecutorService;
/**
- *
* @author ugnich
*/
public class XMPPServer implements DisposableBean {
-
private static final Logger logger = LoggerFactory.getLogger(XMPPServer.class);
public ExecutorService service;
@@ -39,15 +40,19 @@ public class XMPPServer implements DisposableBean {
public String keystorePassword;
public List<String> brokenSSLhosts;
public List<String> bannedHosts;
+
private final List<ConnectionIn> inConnections = Collections.synchronizedList(new ArrayList<>());
private final List<ConnectionOut> outConnections = Collections.synchronizedList(new ArrayList<>());
private final List<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<>());
+
public JdbcTemplate jdbc;
final public HashMap<String, StanzaChild> childParsers = new HashMap<>();
+
public XMPPServer(Environment env, ExecutorService service, JdbcTemplate jdbc) {
this.service = service;
this.jdbc = jdbc;
+
logger.info("component initialized");
try {
HOSTNAME = env.getProperty("hostname");
@@ -64,7 +69,9 @@ public class XMPPServer implements DisposableBean {
childParsers.put(JuickMessage.XMLNS, new JuickMessage());
router = new ConnectionRouter(this, componentName, componentPort, env.getProperty("xmpp_password"));
+
service.submit(router);
+
service.submit(() -> {
final ServerSocket listener = new ServerSocket(s2sPort);
logger.info("s2s listener ready");
@@ -79,6 +86,7 @@ public class XMPPServer implements DisposableBean {
}
}
});
+
service.submit(new CleaningUp(this));
} catch (Exception e) {
@@ -86,6 +94,29 @@ public class XMPPServer implements DisposableBean {
}
}
+ @Override
+ public void destroy() throws Exception {
+ synchronized (getOutConnections()) {
+ for (Iterator<ConnectionOut> i = getOutConnections().iterator(); i.hasNext(); ) {
+ ConnectionOut c = i.next();
+ c.closeConnection();
+ i.remove();
+ }
+ }
+
+ synchronized (getInConnections()) {
+ for (Iterator<ConnectionIn> i = getInConnections().iterator(); i.hasNext(); ) {
+ ConnectionIn c = i.next();
+ c.closeConnection();
+ i.remove();
+ }
+ }
+
+ ThreadHelper.shutdownAndAwaitTermination(service);
+
+ logger.info("Xmpp server destroyed");
+ }
+
public void addConnectionIn(ConnectionIn c) {
synchronized (getInConnections()) {
getInConnections().add(c);
@@ -113,7 +144,7 @@ public class XMPPServer implements DisposableBean {
public String getFromCache(String hostname) {
CacheEntry ret = null;
synchronized (getOutCache()) {
- for (Iterator<CacheEntry> i = getOutCache().iterator(); i.hasNext();) {
+ for (Iterator<CacheEntry> i = getOutCache().iterator(); i.hasNext(); ) {
CacheEntry c = i.next();
if (c.hostname != null && c.hostname.equals(hostname)) {
ret = c;
@@ -202,27 +233,6 @@ public class XMPPServer implements DisposableBean {
}
}
- @Override
- public void destroy() {
- synchronized (getOutConnections()) {
- for (Iterator<ConnectionOut> i = getOutConnections().iterator(); i.hasNext();) {
- ConnectionOut c = i.next();
- c.closeConnection();
- i.remove();
- }
- }
-
- synchronized (getInConnections()) {
- for (Iterator<ConnectionIn> i = getInConnections().iterator(); i.hasNext();) {
- ConnectionIn c = i.next();
- c.closeConnection();
- i.remove();
- }
- }
- service.shutdown();
- logger.info("component destroyed");
- }
-
public ConnectionRouter getRouter() {
return router;
}