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

RAT-469: Fixes to correctly detect additional valid licenses #413

Merged
merged 25 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a4cc18b
Fixes to correctly detect additional valid licenses
Claudenw Dec 10, 2024
9735c09
reverted ubuntu to 22.04
Claudenw Dec 10, 2024
8987032
reverted change to ubuntu version
Claudenw Dec 10, 2024
2713a15
fixed tools spotbugs issue under java 21
Claudenw Dec 10, 2024
d8b4ce7
RAT-469: Add Itest which should work after the patch is applied
ottlinger Dec 11, 2024
0b95cb6
RAT-469: Add changelog entry
ottlinger Dec 11, 2024
c45e75d
LHF: fix typo
ottlinger Dec 11, 2024
8f0ad6c
removed RELEASE_NOTES from tools build
Claudenw Dec 11, 2024
53772d9
Merge remote-tracking branch 'upstream/Changes_for_invalid_license_er…
Claudenw Dec 11, 2024
8645b12
RAT-469: Ensure inclusion/exclusion rules are logged properly
ottlinger Dec 11, 2024
3e21c56
Merge remote-tracking branch 'refs/remotes/upstream/Changes_for_inval…
Claudenw Dec 11, 2024
63fa15a
Merge remote-tracking branch 'origin/master' into Changes_for_invalid…
ottlinger Dec 12, 2024
ef5c93b
RAT-469: Replace misleading default text as new configuration element…
ottlinger Dec 13, 2024
a4d720e
Cleaned up analysis and verified correctness of LicenseSetFactory
Claudenw Dec 14, 2024
6a5b17c
RAT-469: Minor fixes
ottlinger Dec 15, 2024
b35df25
RAT-469: Remove hungarian notation
ottlinger Dec 15, 2024
83ac502
RAT-469: Minor fixes
ottlinger Dec 17, 2024
aee4042
fixed tests
Claudenw Jan 12, 2025
1e4c7b1
removed dead code
Claudenw Jan 12, 2025
fdac590
Minor cleanups
ottlinger Jan 12, 2025
a886a07
Minor cleanups
ottlinger Jan 12, 2025
16a2319
RAT-469: Minor cleanup
ottlinger Jan 12, 2025
7cba21a
RAT-469: Build failure on GHA-windows
ottlinger Jan 12, 2025
e7883c0
RAT-469: Fix spotbugs issue/encoding
ottlinger Jan 12, 2025
cf9c2f5
RAT-469: Fix spotbugs issue/encoding
ottlinger Jan 12, 2025
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
2 changes: 1 addition & 1 deletion apache-rat-core/src/main/java/org/apache/rat/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static LicenseSetFactory readConfigFiles(final Collection<URI> uris) {
}

LicenseSetFactory result = new LicenseSetFactory(licenses);
approvedLicenseCategories.forEach(result::addLicenseCategory);
approvedLicenseCategories.forEach(result::approveLicenseCategory);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ public void setFrom(final Defaults defaults) {
if (getStyleSheet() == null) {
setStyleSheet(StyleSheets.PLAIN.getStyleSheet());
}

defaults.getStandardExclusion().forEach(this::addExcludedCollection);
}

Expand Down Expand Up @@ -612,7 +611,7 @@ public void addApprovedLicenseCategory(final ILicenseFamily approvedILicenseFami
* @param familyCategory the category to add.
*/
public void addApprovedLicenseCategory(final String familyCategory) {
licenseSetFactory.addLicenseCategory(familyCategory);
licenseSetFactory.approveLicenseCategory(familyCategory);
}

/**
Expand Down Expand Up @@ -686,7 +685,7 @@ public void addApprovedLicenseId(final ILicense approvedLicense) {
* @param licenseId the license id to add.
*/
public void addApprovedLicenseId(final String licenseId) {
licenseSetFactory.addLicenseId(licenseId);
licenseSetFactory.approveLicenseId(licenseId);
}

/**
Expand Down Expand Up @@ -805,6 +804,14 @@ public ClaimValidator getClaimValidator() {
return claimValidator;
}

/**
* Gets the enclosed LicenseSetFactory.
* @return the license set factory.
*/
public LicenseSetFactory getLicenseSetFactory() {
return licenseSetFactory;
}

/**
* Validates that the configuration is valid.
* @param logger String consumer to log warning messages to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,58 @@
/**
* Creates default analysers.
*/
public final class DefaultAnalyserFactory {
public final class AnalyserFactory {

private DefaultAnalyserFactory() {
private AnalyserFactory() {
// do not instantiate
}

/**
* Creates an anlayser that adds the approved license predicate to the document metadata.
* <p>
* Note you probably do not want this as it is automatically added to {@link #createConfiguredAnalyser}.
* </p>
* @param approvalPredicate the predicate to approve licenses.
* @return A document analyser that sets the approvalPredicate in document metadata.
*/
public static IDocumentAnalyser createPolicy(final Predicate<ILicense> approvalPredicate) {
return document -> {
if (document != null) {
document.getMetaData().setApprovalPredicate(approvalPredicate);
}
};
}

/**
* Creates an analyser that calls each of the provided analysers in order.
* @param analysers the array of analysers to call.
* @return an analyser that will call all the provided analysers.
*/
public static IDocumentAnalyser createMultiplexer(final IDocumentAnalyser... analysers) {
return document -> {
for (IDocumentAnalyser analyser : analysers) {
analyser.analyse(document);
}
};
}

/**
* Creates a DocumentAnalyser from a collection of ILicenses.
* Creates a DocumentAnalyser from teh report configuraiton.
* @param configuration the ReportConfiguration
* @return A document analyser that uses the provides licenses.
*/
public static IDocumentAnalyser createDefaultAnalyser(final ReportConfiguration configuration) {
Set<ILicense> licenses = configuration.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
public static IDocumentAnalyser createConfiguredAnalyser(final ReportConfiguration configuration) {
LicenseSetFactory licenseSetFactory = configuration.getLicenseSetFactory();
Set<ILicense> licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL);
if (licenses.isEmpty()) {
throw new ConfigurationException("At least one license must be defined");
}
if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
DefaultLog.getInstance().debug("Currently active Licenses are:");
licenses.forEach(DefaultLog.getInstance()::debug);
}
return new DefaultAnalyser(configuration, licenses);
return createMultiplexer(createPolicy(licenseSetFactory.getApprovedLicensePredicate()),
new DefaultAnalyser(configuration, licenses));
}

/**
Expand Down
23 changes: 12 additions & 11 deletions apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
package org.apache.rat.api;

import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.apache.rat.license.ILicense;
import org.apache.rat.license.ILicenseFamily;
import org.apache.rat.utils.DefaultLog;
import org.apache.tika.mime.MediaType;

/**
Expand All @@ -38,7 +36,7 @@ public class MetaData {
/** The list of matched licenses */
private final SortedSet<ILicense> matchedLicenses;
/** The list of License Family Categories that are approved */
private final Set<String> approvedLicenses;
private Predicate<ILicense> approvalPredicate;
/** The charset for this document */
private Charset charset;
/** The media type for this document */
Expand All @@ -53,7 +51,10 @@ public class MetaData {
*/
public MetaData() {
this.matchedLicenses = new TreeSet<>();
this.approvedLicenses = new HashSet<>();
this.approvalPredicate = x -> {
DefaultLog.getInstance().error("Approved Predicate was not set.");
throw new IllegalStateException("Approved Predicate was not set.");
};
}

/**
Expand Down Expand Up @@ -106,11 +107,10 @@ public boolean detectedLicense() {

/**
* Sets the set of approved licenses.
* @param approvedLicenseFamilies the set of approved license families.
* @param approvalPredicate the predicate to validate licenses.
*/
public void setApprovedLicenses(final Set<ILicenseFamily> approvedLicenseFamilies) {
licenses().filter(lic -> approvedLicenseFamilies.contains(lic.getLicenseFamily()))
.forEach(lic -> approvedLicenses.add(lic.getId()));
public void setApprovalPredicate(final Predicate<ILicense> approvalPredicate) {
this.approvalPredicate = approvalPredicate;
}

/**
Expand All @@ -135,7 +135,7 @@ public Stream<ILicense> approvedLicenses() {
* @return {@code true} if the license is in the list of approved licenses, {@code false} otherwise.
*/
public boolean isApproved(final ILicense license) {
return approvedLicenses.contains(license.getId());
return approvalPredicate.test(license);
}

/**
Expand Down Expand Up @@ -197,6 +197,7 @@ public void removeLicenses(final Predicate<ILicense> filter) {

@Override
public String toString() {
return String.format("MetaData[%s license, %s approved]", matchedLicenses.size(), approvedLicenses.size());
return String.format("MetaData[%s license, %s approved]", matchedLicenses.size(),
matchedLicenses.stream().filter(approvalPredicate).count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ExclusionProcessor addIncludedCollection(final StandardCollection collect

/**
* Add the patterns from collections of patterns as excluded patterns.
* @param patterns the strings to that define patterns to be ecxcuded from processing.
* @param patterns the strings to that define patterns to be excluded from processing.
* @return this
*/
public ExclusionProcessor addExcludedPatterns(final Iterable<String> patterns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ boolean matchPath(final String str, final char[][] strDirs, final boolean isCase
} else {
result = SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive);
}
if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
if (result && DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
DefaultLog.getInstance().debug(format("%s match %s -> %s", this, str, result));
}
return result;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* Analyses Documents.
*/
@FunctionalInterface
public interface IDocumentAnalyser {
/**
* Analyse the specified document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void addFamily(final ILicenseFamily.Builder builder) {
* Adds a license family category (id) to the list of approved licenses
* @param familyCategory the category to add.
*/
public void addLicenseCategory(final String familyCategory) {
public void approveLicenseCategory(final String familyCategory) {
approvedLicenseCategories.add(ILicenseFamily.makeCategory(familyCategory));
}

Expand All @@ -254,7 +254,7 @@ public void removeLicenseCategory(final String familyCategory) {
* Adds a license family category (id) to the list of approved licenses
* @param licenseId the license ID to add.
*/
public void addLicenseId(final String licenseId) {
public void approveLicenseId(final String licenseId) {
approvedLicenseIds.add(licenseId);
}

Expand All @@ -275,21 +275,27 @@ private boolean isApprovedCategory(final ILicenseFamily family) {
return approvedLicenseCategories.contains(family.getFamilyCategory()) && !removedLicenseCategories.contains(family.getFamilyCategory());
}

/**
* Gets a predicate to filter for approved licenses.
* @return a predicate that returns {@code true} if the license is approved.
*/
public Predicate<ILicense> getApprovedLicensePredicate() {
return lic -> !removedLicenseIds.contains(lic.getId()) && (approvedLicenseIds.contains(lic.getId()) ||
isApprovedCategory(lic.getLicenseFamily()));
}

/**
* Gets the License objects based on the filter.
* @param filter the types of LicenseFamily objects to return.
* @return a SortedSet of ILicense objects.
*/
public SortedSet<ILicense> getLicenses(final LicenseFilter filter) {
Predicate<ILicense> approved = l -> (isApprovedCategory(l.getLicenseFamily()) ||
approvedLicenseIds.contains(l.getId())) && !removedLicenseIds.contains(l.getId());

switch (filter) {
case ALL:
return Collections.unmodifiableSortedSet(licenses);
case APPROVED:
SortedSet<ILicense> result = new TreeSet<>();
licenses.stream().filter(approved).forEach(result::add);
licenses.stream().filter(getApprovedLicensePredicate()).forEach(result::add);
return result;
case NONE:
default:
Expand Down

This file was deleted.

Loading
Loading