[OPENJPA-2924] BlacklistClassResolver is improved (#118)

* BlacklistClassResolver is improved

* Comments are addressed
This commit is contained in:
Maxim Solodovnik 2024-08-19 11:13:29 +07:00 committed by GitHub
parent c3fff2cb0f
commit ae8c759ffe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 7 deletions

View File

@ -18,23 +18,35 @@
*/ */
package org.apache.openjpa.util; package org.apache.openjpa.util;
import java.util.stream.Stream;
public class BlacklistClassResolver { public class BlacklistClassResolver {
private static final String MATCH_ANY = "*";
public static final BlacklistClassResolver DEFAULT = new BlacklistClassResolver( public static final BlacklistClassResolver DEFAULT = new BlacklistClassResolver(
toArray(System.getProperty( toArray(System.getProperty(
"openjpa.serialization.class.blacklist", "openjpa.serialization.class.blacklist",
"org.codehaus.groovy.runtime.,org.apache.commons.collections4.functors.,org.apache.xalan")), "org.codehaus.groovy.runtime.,org.apache.commons.collections4.functors.,org.apache.xalan")),
toArray(System.getProperty("openjpa.serialization.class.whitelist"))); toArray(System.getProperty("openjpa.serialization.class.whitelist")));
private final boolean allWhite;
private final boolean allBlack;
private final String[] blacklist; private final String[] blacklist;
private final String[] whitelist; private final String[] whitelist;
protected BlacklistClassResolver(final String[] blacklist, final String[] whitelist) { protected BlacklistClassResolver(final String[] blacklist, final String[] whitelist) {
allWhite = Stream.of(whitelist).anyMatch(white -> MATCH_ANY.equals(white));
allBlack = Stream.of(blacklist).anyMatch(black -> MATCH_ANY.equals(black));
this.whitelist = whitelist; this.whitelist = whitelist;
this.blacklist = blacklist; this.blacklist = blacklist;
} }
protected boolean isBlacklisted(final String name) { protected boolean isBlacklisted(final String name) {
return (whitelist != null && !contains(whitelist, name)) || contains(blacklist, name); if (allWhite || contains(whitelist, name)) {
return false;
}
return allBlack || contains(blacklist, name);
} }
public final String check(final String name) { public final String check(final String name) {
@ -45,15 +57,17 @@ public class BlacklistClassResolver {
} }
private static String[] toArray(final String property) { private static String[] toArray(final String property) {
return property == null ? null : property.split(" *, *"); return property == null
? new String[] {}
: Stream.of(property.split(" *, *"))
.filter(item -> item != null && !item.isEmpty())
.toArray(String[]::new);
} }
private static boolean contains(final String[] list, String name) { private static boolean contains(final String[] list, String name) {
if (list != null) { for (final String white : list) {
for (final String white : list) { if (name.startsWith(white)) {
if (name.startsWith(white)) { return true;
return true;
}
} }
} }
return false; return false;