From dfc4173e5bba15ba21e3fc24272c3a36f9e85859 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 19 Oct 2014 21:48:17 +0200 Subject: [PATCH] Fix initialization order issue with DEBUG_ENABLED If a user enabled Smack debug via the property 'smack.debugEnabled', a ConnectionConfiguration could be created where debuggerEnabled is 'false', because Smack is not yet initialized. Also make sure that if the property is not set, it won't overwrite DEBUG_ENABLED = true. Thanks to William Murphy for providing a detailed issue description and supposing a fix. --- .../org/jivesoftware/smack/ConnectionConfiguration.java | 6 ++++++ .../java/org/jivesoftware/smack/SmackInitialization.java | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java index 4f375827d..7e215d9af 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java @@ -45,6 +45,12 @@ import java.util.List; */ public class ConnectionConfiguration implements Cloneable { + static { + // Ensure that Smack is initialized when ConnectionConfiguration is used, or otherwise e.g. + // SmackConfiguration.DEBUG_ENABLED may not be initialized yet. + SmackConfiguration.getVersion(); + } + /** * Hostname of the XMPP server. Usually servers use the same service name as the name * of the server. However, there are some servers like google where host would be diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java index 20d04737c..ff59f37e6 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java @@ -120,7 +120,11 @@ public final class SmackInitialization { // Use try block since we may not have permission to get a system // property (for example, when an applet). try { - SmackConfiguration.DEBUG_ENABLED = Boolean.getBoolean("smack.debugEnabled"); + // Only overwrite DEBUG_ENABLED if it is set via the 'smack.debugEnabled' property. To prevent DEBUG_ENABLED + // = true, which could be set e.g. via a static block from user code, from being overwritten by the property not set + if (Boolean.getBoolean("smack.debugEnabled")) { + SmackConfiguration.DEBUG_ENABLED = true; + } } catch (Exception e) { // Ignore.