Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OPENJPA-2924] BlacklistClassResolver is improved #118

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,35 @@
*/
package org.apache.openjpa.util;

import java.util.stream.Stream;

public class BlacklistClassResolver {
private static final String MATCH_ANY = "*";

public static final BlacklistClassResolver DEFAULT = new BlacklistClassResolver(
toArray(System.getProperty(
"openjpa.serialization.class.blacklist",
"org.codehaus.groovy.runtime.,org.apache.commons.collections4.functors.,org.apache.xalan")),
toArray(System.getProperty("openjpa.serialization.class.whitelist")));

private final boolean allWhite;
private final boolean allBlack;
private final String[] blacklist;
private 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.blacklist = blacklist;
}

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) {
Expand All @@ -45,15 +57,17 @@ public final String check(final String name) {
}

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) {
if (list != null) {
for (final String white : list) {
if (name.startsWith(white)) {
return true;
}
for (final String white : list) {
if (name.startsWith(white)) {
return true;
}
}
return false;
Expand Down
Loading