From 9580784bbf4d6e6d97e731b92b0f700a2eaade26 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 25 Sep 2014 09:42:55 +0200 Subject: [PATCH] DisabledSmackClasses now also take package names So that whole packages can be disabled. --- .../smack/SmackConfiguration.java | 6 ++- .../smack/SmackInitialization.java | 42 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java index 87c4377d3..d94cbe740 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java @@ -283,7 +283,11 @@ public final class SmackConfiguration { } /** - * Add a class to the disabled smack classes + * Add a class to the disabled smack classes. + *

+ * {@code className} can also be a package name, in this case, the entire + * package is disabled (but can be manually enabled). + *

* * @param className */ 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 e443092dc..20d04737c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackInitialization.java @@ -166,27 +166,37 @@ public final class SmackInitialization { final String startName = parser.getName(); int eventType; String name; - do { + outerloop: do { eventType = parser.next(); name = parser.getName(); if (eventType == XmlPullParser.START_TAG && "className".equals(name)) { String classToLoad = parser.nextText(); - if (SmackConfiguration.disabledSmackClasses.contains(classToLoad)) { - LOGGER.info("Not loading disabled Smack class " + classToLoad); - } - else { - try { - loadSmackClass(classToLoad, optional, classLoader); + for (String disabledClassOrPackage : SmackConfiguration.disabledSmackClasses) { + if (disabledClassOrPackage.equals(classToLoad)) { + // Skip disabled class + continue outerloop; } - catch (Exception e) { - // Don't throw the exception if an exceptions collection is given, instead - // record it there. This is used for unit testing purposes. - if (exceptions != null) { - exceptions.add(e); - } - else { - throw e; - } + int lastDotIndex = disabledClassOrPackage.lastIndexOf('.'); + // Security check to avoid NPEs if someone entered 'foo.bar.' + if (disabledClassOrPackage.length() > lastDotIndex + // disabledClassOrPackage is not an Class + && !Character.isUpperCase(disabledClassOrPackage.charAt(lastDotIndex + 1)) + // classToLoad startsWith the package disabledClassOrPackage disables + && classToLoad.startsWith(disabledClassOrPackage)) { + // Skip the class because the whole package was disabled + continue outerloop; + } + } + + try { + loadSmackClass(classToLoad, optional, classLoader); + } catch (Exception e) { + // Don't throw the exception if an exceptions collection is given, instead + // record it there. This is used for unit testing purposes. + if (exceptions != null) { + exceptions.add(e); + } else { + throw e; } } }