From a9a7dd2da8db472e20c3aa44ae7cac274b8ed499 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Feb 2016 19:48:21 +0300 Subject: refactoring --- src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 4 ++-- src/main/java/com/juick/xmpp/s2s/ConnectionOut.java | 16 ++++++++-------- src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | 11 ++++++++--- src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 8 ++++---- 4 files changed, 22 insertions(+), 17 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java index 7b9483f7..6bb81b0d 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -16,6 +16,7 @@ import java.nio.channels.Channels; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -146,8 +147,7 @@ public class ConnectionIn extends Connection implements Runnable { XMPPComponent.removeConnectionIn(this); closeConnection(); } catch (Exception e) { - LOGGER.warning("STREAM " + streamID + " ERROR:" + e.toString()); - e.printStackTrace(); + LOGGER.log(Level.WARNING, "STREAM " + streamID + " ERROR", e); XMPPComponent.removeConnectionIn(this); closeConnection(); } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index 9a8dd0a8..c18ae342 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -9,6 +9,7 @@ import java.net.Socket; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.Channels; import java.nio.channels.CompletionHandler; +import java.util.logging.Level; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -112,21 +113,20 @@ public class ConnectionOut extends Connection implements Runnable { LOGGER.warning("STREAM TO " + to + " " + streamID + " FINISHED"); XMPPComponent.removeConnectionOut(ConnectionOut.this); closeConnection(); - } catch (XmlPullParserException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } catch (Exception e) { - e.printStackTrace(); + LOGGER.log(Level.SEVERE, "s2s out exception", e); + XMPPComponent.removeConnectionOut(ConnectionOut.this); + closeConnection(); } } @Override public void failed(Throwable exc, AsynchronousSocketChannel attachment) { - + LOGGER.log(Level.WARNING, "s2s out failed", exc); + XMPPComponent.removeConnectionOut(ConnectionOut.this); + closeConnection(); } }); - Thread.currentThread().join(); } catch (Exception e) { LOGGER.warning(e.toString()); XMPPComponent.removeConnectionOut(this); @@ -138,7 +138,7 @@ public class ConnectionOut extends Connection implements Runnable { try { sendStanza("" + key + ""); } catch (IOException e) { - LOGGER.warning("STREAM TO " + to + " " + streamID + " ERROR: " + e.toString()); + LOGGER.log(Level.WARNING, "STREAM TO " + to + " " + streamID + " ERROR", e); } } } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 272bc197..73e7a65e 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -10,7 +10,6 @@ import com.juick.xmpp.extensions.XOOB; import com.juick.xmpp.utils.SHA1; import com.juick.xmpp.utils.XmlUtils; import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.InputStreamReader; @@ -28,6 +27,12 @@ import java.util.logging.Level; */ public class ConnectionRouter extends Connection implements Runnable { + private String componentName; + + ConnectionRouter(String componentName) { + this.componentName = componentName; + } + @Override public void run() { LOGGER.info("STREAM ROUTER START"); @@ -43,7 +48,7 @@ public class ConnectionRouter extends Connection implements Runnable { parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); writer = new OutputStreamWriter(Channels.newOutputStream(client)); - String msg = ""; + String msg = ""; writer.write(msg); writer.flush(); @@ -74,7 +79,7 @@ public class ConnectionRouter extends Connection implements Runnable { if (to != null && (tag.equals("message") || tag.equals("presence") || tag.equals("iq"))) { JID jid = new JID(to); if (jid.Host != null) { - if (jid.Host.equals(XMPPComponent.COMPONENTNAME)) { + if (jid.Host.equals(componentName)) { if (tag.equals("message")) { Message xmsg = Message.parse(parser, XMPPComponent.childParsers); LOGGER.info("STREAM ROUTER (PROCESS): " + xmsg.toString()); diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 95641f5e..3720d963 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -27,7 +27,6 @@ public class XMPPComponent implements ServletContextListener { ExecutorService executorService; public static String HOSTNAME = null; - public static String COMPONENTNAME = null; public static String STATSFILE = null; public static ConnectionRouter connRouter; static final List inConnections = Collections.synchronizedList(new ArrayList<>()); @@ -158,15 +157,16 @@ public class XMPPComponent implements ServletContextListener { try { conf.load(sce.getServletContext().getResourceAsStream("WEB-INF/s2s.conf")); HOSTNAME = conf.getProperty("hostname"); - COMPONENTNAME = conf.getProperty("componentname"); + String componentName = conf.getProperty("componentname"); STATSFILE = conf.getProperty("statsfile"); Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); + sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - connRouter = new ConnectionRouter(); + connRouter = new ConnectionRouter(componentName); new Thread(connRouter).start(); new Thread(new ConnectionListener()).start(); new Thread(new CleaningUp()).start(); -- cgit v1.2.3