From 58fb4a6394cb19ee04e65d0e921cdf0ae6fdb6e3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Jan 2016 21:43:38 +0300 Subject: s2s: logging --- src/main/java/com/juick/xmpp/s2s/Connection.java | 22 ++++------- src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 27 ++++++------- .../com/juick/xmpp/s2s/ConnectionListener.java | 5 ++- .../java/com/juick/xmpp/s2s/ConnectionOut.java | 23 +++++------ .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 22 +++++------ src/main/java/com/juick/xmpp/s2s/Shutdown.java | 45 ---------------------- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 34 +++++++++++++--- 7 files changed, 74 insertions(+), 104 deletions(-) delete mode 100644 src/main/java/com/juick/xmpp/s2s/Shutdown.java (limited to 'src') diff --git a/src/main/java/com/juick/xmpp/s2s/Connection.java b/src/main/java/com/juick/xmpp/s2s/Connection.java index 699e52bf..6d32abbe 100644 --- a/src/main/java/com/juick/xmpp/s2s/Connection.java +++ b/src/main/java/com/juick/xmpp/s2s/Connection.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Date; +import java.util.logging.Logger; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.xmlpull.mxp1.MXParser; @@ -16,6 +17,8 @@ import org.xmlpull.v1.XmlPullParser; */ public class Connection { + protected static final Logger LOGGER = Logger.getLogger(Connection.class.getName()); + public String streamID; public long tsCreated = 0; public long tsLocalData = 0; @@ -29,15 +32,6 @@ public class Connection { tsCreated = System.currentTimeMillis(); } - public void logXml(String xml) { - try { - FileWriter logFile = new FileWriter(XMPPComponent.LOGFILE, true); - logFile.write(new Date().toString() + "\t" + streamID + "\t" + xml); - logFile.close(); - } catch (IOException e) { - } - } - public void logParser() { if (streamID == null) { return; @@ -47,12 +41,12 @@ public class Connection { tag += " " + parser.getAttributeName(i) + "=\"" + parser.getAttributeValue(i) + "\""; } tag += ">...\n"; - logXml(tag); + LOGGER.fine(tag); } public void sendStanza(String xml) throws IOException { - if (XMPPComponent.LOGFILE != null && streamID != null) { - logXml("OUT: " + xml + "\n"); + if (streamID != null) { + LOGGER.fine("OUT: " + xml + "\n"); } writer.write(xml); writer.flush(); @@ -62,8 +56,8 @@ public class Connection { } void closeConnection() { - if (XMPPComponent.LOGFILE != null && streamID != null) { - logXml("CLOSING STREAM\n"); + if (streamID != null) { + LOGGER.info(String.format("CLOSING STREAM %s", streamID)); } try { diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java index f484c9db..fad97c16 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -37,7 +37,7 @@ public class ConnectionIn extends Connection implements Runnable { @Override public void run() { - System.out.println("STREAM FROM ? " + streamID + " START"); + LOGGER.info("STREAM FROM ? " + streamID + " START"); try { parser.setInput(new InputStreamReader(socket.getInputStream())); writer = new OutputStreamWriter(socket.getOutputStream()); @@ -67,10 +67,7 @@ public class ConnectionIn extends Connection implements Runnable { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } - - if (XMPPComponent.LOGFILE != null) { - logParser(); - } + logParser(); packetsRemote++; @@ -78,7 +75,7 @@ public class ConnectionIn extends Connection implements Runnable { if (tag.equals("db:result")) { String dfrom = parser.getAttributeValue(null, "from"); String to = parser.getAttributeValue(null, "to"); - System.out.println("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK"); + LOGGER.info("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK"); if (dfrom.endsWith(XMPPComponent.HOSTNAME) && (dfrom.equals(XMPPComponent.HOSTNAME) || dfrom.endsWith("." + XMPPComponent.HOSTNAME))) { break; } @@ -109,10 +106,10 @@ public class ConnectionIn extends Connection implements Runnable { } if (valid) { sendStanza(""); - System.out.println("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY VALID"); + LOGGER.info("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY VALID"); } else { sendStanza(""); - System.err.println("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY INVALID"); + LOGGER.warning("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY INVALID"); } } else if (tag.equals("presence") && checkFromTo(parser)) { Presence p = Presence.parse(parser, null); @@ -123,7 +120,7 @@ public class ConnectionIn extends Connection implements Runnable { updateTsRemoteData(); Message msg = Message.parse(parser, XMPPComponent.childParsers); if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) { - System.out.println("STREAM " + streamID + ": " + msg.toString()); + LOGGER.info("STREAM " + streamID + ": " + msg.toString()); if (!JuickBot.incomingMessage(msg)) { XMPPComponent.connRouter.sendStanza(msg.toString()); } @@ -133,14 +130,14 @@ public class ConnectionIn extends Connection implements Runnable { String type = parser.getAttributeValue(null, "type"); String xml = XmlUtils.parseToString(parser, true); if (type == null || !type.equals(Iq.Type.error)) { - System.out.println("STREAM " + streamID + ": " + xml); + LOGGER.info("STREAM " + streamID + ": " + xml); XMPPComponent.connRouter.sendStanza(xml); } } else { - System.out.println("STREAM " + streamID + ": " + XmlUtils.parseToString(parser, true)); + LOGGER.info("STREAM " + streamID + ": " + XmlUtils.parseToString(parser, true)); } } - System.err.println("STREAM " + streamID + " FINISHED"); + LOGGER.warning("STREAM " + streamID + " FINISHED"); XMPPComponent.removeConnectionIn(this); closeConnection(); } catch (EOFException ex) { @@ -148,7 +145,7 @@ public class ConnectionIn extends Connection implements Runnable { XMPPComponent.removeConnectionIn(this); closeConnection(); } catch (Exception e) { - System.err.println("STREAM " + streamID + " ERROR:" + e.toString()); + LOGGER.warning("STREAM " + streamID + " ERROR:" + e.toString()); e.printStackTrace(); XMPPComponent.removeConnectionIn(this); closeConnection(); @@ -164,10 +161,10 @@ public class ConnectionIn extends Connection implements Runnable { sendStanza(""); if (type.equals("valid")) { from.add(sfrom); - System.out.println("STREAM FROM " + sfrom + " " + streamID + " READY"); + LOGGER.info("STREAM FROM " + sfrom + " " + streamID + " READY"); } } catch (IOException e) { - System.err.println("STREAM FROM " + sfrom + " " + streamID + " ERROR: " + e.toString()); + LOGGER.warning("STREAM FROM " + sfrom + " " + streamID + " ERROR: " + e.toString()); } } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java index 314be116..f9b788c4 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java @@ -5,6 +5,7 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.logging.Logger; /** * @@ -12,6 +13,8 @@ import java.util.concurrent.Executors; */ public class ConnectionListener implements Runnable { + private static final Logger LOGGER = Logger.getLogger(ConnectionListener.class.getName()); + ExecutorService executorService = Executors.newCachedThreadPool(); @Override @@ -25,7 +28,7 @@ public class ConnectionListener implements Runnable { executorService.submit(conn); } } catch (IOException e) { - System.out.println("IOException on socket listen: " + e.toString()); + LOGGER.info("IOException on socket listen: " + e.toString()); } } } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index 2626b926..0205349a 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -32,7 +32,7 @@ public class ConnectionOut extends Connection implements Runnable { @Override public void run() { - System.out.println("STREAM TO " + to + " START"); + LOGGER.info("STREAM TO " + to + " START"); try { HostnamePort addr = DNSQueries.getServerAddress(to); @@ -51,7 +51,7 @@ public class ConnectionOut extends Connection implements Runnable { throw new Exception("STREAM TO " + to + " INVALID FIRST PACKET"); } - System.out.println("STREAM TO " + to + " " + streamID + " OPEN"); + LOGGER.info("STREAM TO " + to + " " + streamID + " OPEN"); XMPPComponent.addConnectionOut(this); if (checkSID != null) { @@ -64,26 +64,23 @@ public class ConnectionOut extends Connection implements Runnable { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } - - if (XMPPComponent.LOGFILE != null) { - logParser(); - } + logParser(); String tag = parser.getName(); if (tag.equals("db:result")) { String type = parser.getAttributeValue(null, "type"); if (type != null && type.equals("valid")) { streamReady = true; - System.out.println("STREAM TO " + to + " " + streamID + " READY"); + LOGGER.info("STREAM TO " + to + " " + streamID + " READY"); String cache = XMPPComponent.getFromCache(to); if (cache != null) { - System.out.println("STREAM TO " + to + " " + streamID + " SENDING CACHE"); + LOGGER.info("STREAM TO " + to + " " + streamID + " SENDING CACHE"); sendStanza(cache); } } else { - System.out.println("STREAM TO " + to + " " + streamID + " DIALBACK FAIL"); + LOGGER.info("STREAM TO " + to + " " + streamID + " DIALBACK FAIL"); } XmlUtils.skip(parser); } else if (tag.equals("db:verify")) { @@ -98,15 +95,15 @@ public class ConnectionOut extends Connection implements Runnable { } XmlUtils.skip(parser); } else { - System.out.println("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true)); + LOGGER.info("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true)); } } - System.err.println("STREAM TO " + to + " " + streamID + " FINISHED"); + LOGGER.warning("STREAM TO " + to + " " + streamID + " FINISHED"); XMPPComponent.removeConnectionOut(this); closeConnection(); } catch (Exception e) { - System.err.println(e.toString()); + LOGGER.warning(e.toString()); XMPPComponent.removeConnectionOut(this); closeConnection(); } @@ -116,7 +113,7 @@ public class ConnectionOut extends Connection implements Runnable { try { sendStanza("" + key + ""); } catch (IOException e) { - System.err.println("STREAM TO " + to + " " + streamID + " ERROR: " + e.toString()); + LOGGER.warning("STREAM TO " + to + " " + streamID + " ERROR: " + e.toString()); } } } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index d8ce0daf..4a1f6b16 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -26,7 +26,7 @@ public class ConnectionRouter extends Connection implements Runnable { @Override public void run() { - System.out.println("STREAM ROUTER START"); + LOGGER.info("STREAM ROUTER START"); try { socket = new Socket("localhost", 5347); @@ -53,7 +53,7 @@ public class ConnectionRouter extends Connection implements Runnable { throw new Exception("NO HANDSHAKE"); } XmlUtils.skip(parser); - System.out.println("STREAM ROUTER OPEN"); + LOGGER.info("STREAM ROUTER OPEN"); while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() != XmlPullParser.START_TAG) { @@ -68,7 +68,7 @@ public class ConnectionRouter extends Connection implements Runnable { if (jid.Host.equals(XMPPComponent.COMPONENTNAME)) { if (tag.equals("message")) { Message xmsg = Message.parse(parser, XMPPComponent.childParsers); - System.out.println("STREAM ROUTER (PROCESS): " + xmsg.toString()); + LOGGER.info("STREAM ROUTER (PROCESS): " + xmsg.toString()); JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS); if (jmsg != null) { if (jid.Username != null && jid.Username.equals("recomm")) { @@ -84,23 +84,23 @@ public class ConnectionRouter extends Connection implements Runnable { } } else if (jid.Host.endsWith(XMPPComponent.HOSTNAME) && (jid.Host.equals(XMPPComponent.HOSTNAME) || jid.Host.endsWith("." + XMPPComponent.HOSTNAME))) { String xml = XmlUtils.parseToString(parser, true); - System.out.println("STREAM ROUTER: " + xml); + LOGGER.info("STREAM ROUTER: " + xml); } else { String xml = XmlUtils.parseToString(parser, true); - System.out.println("STREAM ROUTER (OUT): " + xml); + LOGGER.info("STREAM ROUTER (OUT): " + xml); XMPPComponent.sendOut(jid.Host, xml); } } else { - System.out.println("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true)); + LOGGER.info("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true)); } } else { - System.out.println("STREAM ROUTER: " + XmlUtils.parseToString(parser, true)); + LOGGER.info("STREAM ROUTER: " + XmlUtils.parseToString(parser, true)); } } - System.err.println("STREAM ROUTER FINISHED"); + LOGGER.warning("STREAM ROUTER FINISHED"); } catch (Exception e) { - System.err.println("STREAM ROUTER PARSE ERROR: " + e.toString()); + LOGGER.warning("STREAM ROUTER PARSE ERROR: " + e.toString()); } System.exit(0); } @@ -111,8 +111,8 @@ public class ConnectionRouter extends Connection implements Runnable { writer.write(xml); writer.flush(); } catch (IOException e) { - System.err.println("STREAM ROUTER ERROR: " + xml); - System.err.println("STREAM ROUTER ERROR: " + e.toString()); + LOGGER.warning("STREAM ROUTER ERROR: " + xml); + LOGGER.warning("STREAM ROUTER ERROR: " + e.toString()); System.exit(0); } } diff --git a/src/main/java/com/juick/xmpp/s2s/Shutdown.java b/src/main/java/com/juick/xmpp/s2s/Shutdown.java deleted file mode 100644 index fb3543f8..00000000 --- a/src/main/java/com/juick/xmpp/s2s/Shutdown.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.juick.xmpp.s2s; - -import java.sql.SQLException; -import java.util.Iterator; - -/** - * - * @author ugnich - */ -public class Shutdown extends Thread { - - @Override - public void run() { - System.out.println("SHUTTING DOWN"); - - synchronized (XMPPComponent.outConnections) { - for (Iterator i = XMPPComponent.outConnections.iterator(); i.hasNext();) { - ConnectionOut c = i.next(); - c.closeConnection(); - i.remove(); - } - } - - synchronized (XMPPComponent.inConnections) { - for (Iterator i = XMPPComponent.inConnections.iterator(); i.hasNext();) { - ConnectionIn c = i.next(); - c.closeConnection(); - i.remove(); - } - } - - XMPPComponent.connRouter.closeConnection(); - - synchronized (XMPPComponent.sqlSync) { - if (XMPPComponent.sql != null) { - try { - XMPPComponent.sql.close(); - XMPPComponent.sql = null; - } catch (SQLException e) { - System.err.println("SQL ERROR: " + e); - } - } - } - } -} diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index ff4ec3e6..bb5f5ef7 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -29,7 +29,6 @@ public class XMPPComponent implements ServletContextListener { public static String HOSTNAME = null; public static String COMPONENTNAME = null; - public static String LOGFILE = null; public static String STATSFILE = null; public static ConnectionRouter connRouter; static final List inConnections = Collections.synchronizedList(new ArrayList<>()); @@ -125,7 +124,7 @@ public class XMPPComponent implements ServletContextListener { try { connOut.sendStanza(xml); } catch (IOException e) { - System.err.println("STREAM TO " + connOut.to + " " + connOut.streamID + " ERROR: " + e.toString()); + LOGGER.warning("STREAM TO " + connOut.to + " " + connOut.streamID + " ERROR: " + e.toString()); } return; } @@ -161,14 +160,11 @@ public class XMPPComponent implements ServletContextListener { conf.load(new FileInputStream("/etc/juick/s2s.conf")); HOSTNAME = conf.getProperty("hostname"); COMPONENTNAME = conf.getProperty("componentname"); - LOGFILE = conf.getProperty("logfile"); 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", "")); - Runtime.getRuntime().addShutdownHook(new Shutdown()); - childParsers.put(JuickMessage.XMLNS, new JuickMessage()); connRouter = new ConnectionRouter(); @@ -185,6 +181,34 @@ public class XMPPComponent implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent sce) { + synchronized (XMPPComponent.outConnections) { + for (Iterator i = XMPPComponent.outConnections.iterator(); i.hasNext();) { + ConnectionOut c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + synchronized (XMPPComponent.inConnections) { + for (Iterator i = XMPPComponent.inConnections.iterator(); i.hasNext();) { + ConnectionIn c = i.next(); + c.closeConnection(); + i.remove(); + } + } + + XMPPComponent.connRouter.closeConnection(); + + synchronized (XMPPComponent.sqlSync) { + if (XMPPComponent.sql != null) { + try { + XMPPComponent.sql.close(); + XMPPComponent.sql = null; + } catch (SQLException e) { + LOGGER.warning("SQL ERROR: " + e); + } + } + } // Now deregister JDBC drivers in this context's ClassLoader: // Get the webapp's ClassLoader ClassLoader cl = Thread.currentThread().getContextClassLoader(); -- cgit v1.2.3