DisabledSmackClasses now also take package names

So that whole packages can be disabled.
This commit is contained in:
Florian Schmaus 2014-09-25 09:42:55 +02:00
parent b0f774a2cf
commit 9580784bbf
2 changed files with 31 additions and 17 deletions

View File

@ -283,7 +283,11 @@ public final class SmackConfiguration {
} }
/** /**
* Add a class to the disabled smack classes * Add a class to the disabled smack classes.
* <p>
* {@code className} can also be a package name, in this case, the entire
* package is disabled (but can be manually enabled).
* </p>
* *
* @param className * @param className
*/ */

View File

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