From 1d837a835c04078ec66325e9fb9de21ac401874e Mon Sep 17 00:00:00 2001
From: Alexander Alexeev
Date: Wed, 30 Nov 2016 15:10:15 +0700
Subject: prevent string concatenation for disabled loggers
---
.../main/java/com/juick/components/XMPPServer.java | 3 +-
.../java/com/juick/components/s2s/Connection.java | 4 +--
.../com/juick/components/s2s/ConnectionIn.java | 33 +++++++++++-----------
.../com/juick/components/s2s/ConnectionOut.java | 27 +++++++++---------
.../com/juick/components/s2s/ConnectionRouter.java | 15 +++++-----
5 files changed, 43 insertions(+), 39 deletions(-)
(limited to 'juick-xmpp/src/main/java/com/juick')
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 a2f593ee..7be2400a 100644
--- a/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
+++ b/juick-xmpp/src/main/java/com/juick/components/XMPPServer.java
@@ -202,7 +202,8 @@ public class XMPPServer implements AutoCloseable {
try {
connOut.sendStanza(xml);
} catch (IOException e) {
- logger.warn("STREAM TO " + connOut.to + " " + connOut.streamID + " ERROR: " + e.toString());
+ logger.warn("STREAM TO {} {} ERROR: {}",
+ connOut.to, connOut.streamID, e);
}
return;
}
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java b/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java
index 700293df..4a39ffb5 100644
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java
+++ b/juick-xmpp/src/main/java/com/juick/components/s2s/Connection.java
@@ -87,7 +87,7 @@ public class Connection {
public void sendStanza(String xml) throws IOException {
if (streamID != null) {
- logger.trace("OUT: " + xml + "\n");
+ logger.trace("OUT: {}\n", xml);
}
writer.write(xml);
writer.flush();
@@ -98,7 +98,7 @@ public class Connection {
public void closeConnection() {
if (streamID != null) {
- logger.info(String.format("CLOSING STREAM %s", streamID));
+ logger.info("CLOSING STREAM {}", streamID);
}
try {
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java
index 3220b074..2f007008 100644
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java
+++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionIn.java
@@ -21,7 +21,6 @@ import java.util.List;
import java.util.UUID;
/**
- *
* @author ugnich
*/
public class ConnectionIn extends Connection implements Runnable {
@@ -43,7 +42,7 @@ public class ConnectionIn extends Connection implements Runnable {
@Override
public void run() {
- LOGGER.info("STREAM FROM ? " + streamID + " START");
+ LOGGER.info("STREAM FROM ? {} START", streamID);
try {
parser.next(); // stream:stream
updateTsRemoteData();
@@ -75,7 +74,7 @@ public class ConnectionIn extends Connection implements Runnable {
if (tag.equals("result") && parser.getNamespace().equals(NS_DB)) {
String dfrom = parser.getAttributeValue(null, "from");
String to = parser.getAttributeValue(null, "to");
- LOGGER.info("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK");
+ LOGGER.info("STREAM FROM {} TO {} {} ASKING FOR DIALBACK", dfrom, to, streamID);
if (dfrom.endsWith(xmpp.HOSTNAME) && (dfrom.equals(xmpp.HOSTNAME) || dfrom.endsWith("." + xmpp.HOSTNAME))) {
break;
}
@@ -106,10 +105,10 @@ public class ConnectionIn extends Connection implements Runnable {
}
if (valid) {
sendStanza("");
- LOGGER.info("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY VALID");
+ LOGGER.info("STREAM FROM {} {} DIALBACK VERIFY VALID", vfrom, streamID);
} else {
sendStanza("");
- LOGGER.warn("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY INVALID");
+ LOGGER.warn("STREAM FROM {} {} DIALBACK VERIFY INVALID", vfrom, streamID);
}
} else if (tag.equals("presence") && checkFromTo(parser)) {
Presence p = Presence.parse(parser, null);
@@ -120,7 +119,7 @@ public class ConnectionIn extends Connection implements Runnable {
updateTsRemoteData();
Message msg = Message.parse(parser, xmpp.childParsers);
if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) {
- LOGGER.info("STREAM " + streamID + ": " + msg.toString());
+ LOGGER.info("STREAM {}: {}", streamID, msg);
if (!bot.incomingMessage(msg)) {
xmpp.getRouter().sendStanza(msg.toString());
}
@@ -130,11 +129,11 @@ 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)) {
- LOGGER.info("STREAM " + streamID + ": " + xml);
+ LOGGER.info("STREAM {}: {}", streamID, xml);
xmpp.getRouter().sendStanza(xml);
}
} else if (sc != null && !isSecured() && tag.equals("starttls")) {
- LOGGER.info("STREAM " + streamID + " SECURING");
+ LOGGER.info("STREAM {} SECURING", streamID);
sendStanza("");
try {
socket = sc.getSocketFactory().createSocket(socket, socket.getInetAddress().getHostAddress(),
@@ -142,10 +141,10 @@ public class ConnectionIn extends Connection implements Runnable {
((SSLSocket) socket).setUseClientMode(false);
((SSLSocket) socket).startHandshake();
setSecured(true);
- LOGGER.info("STREAM " + streamID + " SECURED");
+ LOGGER.info("STREAM {} SECURED", streamID);
restartParser();
} catch (SSLException sex) {
- LOGGER.warn("STREAM " + streamID + " SSL ERROR");
+ LOGGER.warn("STREAM {} SSL ERROR {}", streamID, sex);
sendStanza("");
xmpp.removeConnectionIn(this);
closeConnection();
@@ -153,20 +152,21 @@ public class ConnectionIn extends Connection implements Runnable {
} else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) {
sendOpenStream(null, true);
} else {
- LOGGER.info("STREAM " + streamID + ": " + XmlUtils.parseToString(parser, true));
+ if (LOGGER.isInfoEnabled()) // prevent call parseToString if logger disabled
+ LOGGER.info("STREAM {}: {}", streamID, XmlUtils.parseToString(parser, true));
}
}
- LOGGER.warn("STREAM " + streamID + " FINISHED");
+ LOGGER.warn("STREAM {} FINISHED", streamID);
xmpp.removeConnectionIn(this);
closeConnection();
} catch (EOFException | SocketException ex) {
- LOGGER.info(String.format("STREAM %s CLOSED (dirty)", streamID));
+ LOGGER.info("STREAM {} CLOSED (dirty)", streamID);
xmpp.removeConnectionIn(this);
closeConnection();
} catch (HostUnknownException e) {
LOGGER.warn(e.getMessage());
} catch (Exception e) {
- LOGGER.warn("STREAM " + streamID + " ERROR", e);
+ LOGGER.warn("STREAM {} ERROR {}", streamID, e);
xmpp.removeConnectionIn(this);
closeConnection();
}
@@ -195,10 +195,10 @@ public class ConnectionIn extends Connection implements Runnable {
sendStanza("");
if (type.equals("valid")) {
from.add(sfrom);
- LOGGER.info("STREAM FROM " + sfrom + " " + streamID + " READY");
+ LOGGER.info("STREAM FROM {} {} READY", sfrom, streamID);
}
} catch (IOException e) {
- LOGGER.warn("STREAM FROM " + sfrom + " " + streamID + " ERROR: " + e.toString());
+ LOGGER.warn("STREAM FROM {} {} ERROR: {}", sfrom, streamID, e);
}
}
@@ -219,6 +219,7 @@ public class ConnectionIn extends Connection implements Runnable {
}
return false;
}
+
class HostUnknownException extends Exception {
public HostUnknownException(String message) {
super(message);
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
index c15a2a25..5b1da316 100644
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
+++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionOut.java
@@ -58,7 +58,7 @@ public class ConnectionOut extends Connection implements Runnable {
@Override
public void run() {
- logger.info("STREAM TO " + to + " START");
+ logger.info("STREAM TO {} START", to);
try {
socket = new Socket();
socket.connect(DNSQueries.getServerAddress(to));
@@ -72,7 +72,7 @@ public class ConnectionOut extends Connection implements Runnable {
throw new Exception("STREAM TO " + to + " INVALID FIRST PACKET");
}
- logger.info("STREAM TO " + to + " " + streamID + " OPEN");
+ logger.info("STREAM TO {} {} OPEN", to, streamID);
xmpp.addConnectionOut(ConnectionOut.this);
boolean xmppversionnew = parser.getAttributeValue(null, "version") != null;
if (!xmppversionnew) {
@@ -90,16 +90,16 @@ public class ConnectionOut extends Connection implements Runnable {
String type = parser.getAttributeValue(null, "type");
if (type != null && type.equals("valid")) {
streamReady = true;
- logger.info("STREAM TO " + to + " " + streamID + " READY");
+ logger.info("STREAM TO {} {} READY", to, streamID);
String cache = xmpp.getFromCache(to);
if (cache != null) {
- logger.info("STREAM TO " + to + " " + streamID + " SENDING CACHE");
+ logger.info("STREAM TO {} {} SENDING CACHE", to, streamID);
sendStanza(cache);
}
} else {
- logger.info("STREAM TO " + to + " " + streamID + " DIALBACK FAIL");
+ logger.info("STREAM TO {} {} DIALBACK FAIL", to, streamID);
}
XmlUtils.skip(parser);
} else if (tag.equals("verify") && parser.getNamespace().equals(NS_DB)) {
@@ -116,7 +116,7 @@ public class ConnectionOut extends Connection implements Runnable {
} else if (tag.equals("features") && parser.getNamespace().equals(NS_STREAM)) {
StreamFeatures features = StreamFeatures.parse(parser);
if (sc != null && !isSecured() && features.STARTTLS >= 0 && !xmpp.brokenSSLhosts.contains(to)) {
- logger.info("STREAM TO " + to + " " + streamID + " SECURING");
+ logger.info("STREAM TO {} {} SECURING", to, streamID);
sendStanza("");
} else {
processDialback();
@@ -127,11 +127,11 @@ public class ConnectionOut extends Connection implements Runnable {
socket.getPort(), true);
((SSLSocket) socket).startHandshake();
setSecured(true);
- logger.info("STREAM " + streamID + " SECURED");
+ logger.info("STREAM {} SECURED", streamID);
restartParser();
sendOpenStream();
} catch (SSLException sex) {
- logger.error(String.format("s2s ssl error: %s %s", to, streamID), sex);
+ logger.error("s2s ssl error: {} {}, error {}", to, streamID, sex);
sendStanza("");
xmpp.removeConnectionOut(this);
closeConnection();
@@ -139,19 +139,20 @@ public class ConnectionOut extends Connection implements Runnable {
} else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) {
streamID = parser.getAttributeValue(null, "id");
} else {
- logger.info("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true));
+ if (logger.isInfoEnabled()) // prevent parseToString call if logger disabled
+ logger.info("STREAM TO {} {} : {}", to, streamID, XmlUtils.parseToString(parser, true));
}
}
- logger.warn("STREAM TO " + to + " " + streamID + " FINISHED");
+ logger.warn("STREAM TO {} {} FINISHED", to, streamID);
xmpp.removeConnectionOut(ConnectionOut.this);
closeConnection();
} catch (EOFException | SocketException eofex) {
- logger.info(String.format("STREAM %s %s CLOSED (dirty)", to, streamID));
+ logger.info("STREAM {} {} CLOSED (dirty)", to, streamID);
xmpp.removeConnectionOut(ConnectionOut.this);
closeConnection();
} catch (Exception e) {
- logger.error(String.format("s2s out exception: %s %s", to, streamID), e);
+ logger.error("s2s out exception: {} {}, exception {}", to, streamID, e);
xmpp.removeConnectionOut(ConnectionOut.this);
closeConnection();
}
@@ -161,7 +162,7 @@ public class ConnectionOut extends Connection implements Runnable {
try {
sendStanza("" + key + "");
} catch (IOException e) {
- logger.warn("STREAM TO " + to + " " + streamID + " ERROR", e);
+ logger.warn("STREAM TO {} {} ERROR {}", to, streamID, e);
}
}
}
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java
index 5af00329..9272a3d0 100644
--- a/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java
+++ b/juick-xmpp/src/main/java/com/juick/components/s2s/ConnectionRouter.java
@@ -30,7 +30,6 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
/**
- *
* @author ugnich
*/
public class ConnectionRouter extends Connection implements Runnable {
@@ -100,7 +99,7 @@ public class ConnectionRouter extends Connection implements Runnable {
if (jid.Host.equals(componentName)) {
if (tag.equals("message")) {
Message xmsg = Message.parse(parser, xmpp.childParsers);
- logger.info("STREAM ROUTER (PROCESS): " + xmsg.toString());
+ logger.info("STREAM ROUTER (PROCESS): {}", xmsg);
JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS);
if (jmsg != null) {
if (jid.Username != null && jid.Username.equals("recomm")) {
@@ -116,23 +115,25 @@ public class ConnectionRouter extends Connection implements Runnable {
}
} else if (jid.Host.endsWith(xmpp.HOSTNAME) && (jid.Host.equals(xmpp.HOSTNAME) || jid.Host.endsWith("." + xmpp.HOSTNAME))) {
String xml = XmlUtils.parseToString(parser, true);
- logger.info("STREAM ROUTER: " + xml);
+ logger.info("STREAM ROUTER: {}", xml);
} else {
String xml = XmlUtils.parseToString(parser, true);
- logger.info("STREAM ROUTER (OUT): " + xml);
+ logger.info("STREAM ROUTER (OUT): {}", xml);
xmpp.sendOut(jid.Host, xml);
}
} else {
- logger.info("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true));
+ if (logger.isInfoEnabled()) // prevent parseToString for disabled logs
+ logger.info("STREAM ROUTER (NO TO): {}", XmlUtils.parseToString(parser, true));
}
} else {
- logger.info("STREAM ROUTER: " + XmlUtils.parseToString(parser, true));
+ if (logger.isInfoEnabled()) // prevent parseToString for disabled logs
+ logger.info("STREAM ROUTER: {}", XmlUtils.parseToString(parser, true));
}
}
logger.warn("STREAM ROUTER FINISHED");
} catch (Exception e) {
- logger.warn("router error, reconnection " + e.toString());
+ logger.warn("router error, reconnection ", e);
execution.recordFailure(e);
}
}
--
cgit v1.2.3