Skip to content

Commit

Permalink
Refactoring: Introduction of CurationDataHandle to enable additional …
Browse files Browse the repository at this point in the history
…ways to represent/reference curation data (#254)
  • Loading branch information
ohecker authored Apr 5, 2024
1 parent 25be0d7 commit 697305f
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private Statistics processApplicationComponent(ApplicationComponent ac) {
ComponentInfoData componentInfoData = null;
try {
for (ComponentInfoProvider cia : this.componentInfoAdapters) {
componentInfoCandidate = cia.getComponentInfo(ac.getPackageUrl(), this.curationDataSelector);
componentInfoCandidate = cia.getComponentInfo(ac.getPackageUrl(),
new SelectorCurationDataHandle(this.curationDataSelector));
if (componentInfoCandidate != null) {
componentInfo = componentInfoCandidate;
// stop querying further adapters if some info was returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public interface ComponentInfoProvider {
* {@link ComponentInfo} object.
*
* @param packageUrl The identifier of the package for which information is requested
* @param curationDataSelector identifies which source should be used for the curation data. <code>null</code>
* indicates that the default should be used.
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the data for the component. If the bean implementing this interface is deactivated (e.g. via some feature
* flag configuration) this method will return <code>null</code>. Otherwise a non-<code>null</code> object
* will be refurned. If there is no actual data available for the requested component calling
* {@link ComponentInfo#getComponentInfoData()} on the returned object will return <code>null</code>.
* @throws ComponentInfoAdapterException if there was an exception when reading the data. In case that there is no
* data available no exception will be thrown. Instead <code>null</code> will be returned in such a case.
*/
ComponentInfo getComponentInfo(String packageUrl, String curationDataSelector) throws ComponentInfoAdapterException;
ComponentInfo getComponentInfo(String packageUrl, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.devonfw.tools.solicitor.componentinfo;

/**
* A handle which represents/references curation data.
*/
public interface CurationDataHandle {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.devonfw.tools.solicitor.componentinfo;

/**
* An implementation of a {@link CurationDataHandle} which references curation data via a dataSelector. This
* dataSelector is a string which might e.g. denote a branch in a Git repository. Further information (like e.g. the
* PackageURL) is needed to retrieve the curation data.
*/
public class SelectorCurationDataHandle implements CurationDataHandle {

private String curationDataSelector;

/**
* The constructor.
*
* @param curationDataSelector the curationDataSelector which is references the curation data. <code>null</code>
* indicates that the default should be used. <code>"none"</code> indicates that no curation should be applied.
*/
public SelectorCurationDataHandle(String curationDataSelector) {

this.curationDataSelector = curationDataSelector;

}

/**
* Gets the curationDataSelctor encapsulated by this handle.
*
* @return the curationDataSelector
*/
public String getCurationDataSelector() {

return this.curationDataSelector;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.devonfw.tools.solicitor.componentinfo.ComponentInfo;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.CurationDataHandle;

/**
* A {@link ComponentInfoCurator} curates the {@link ComponentInfo} data.
Expand All @@ -13,11 +14,11 @@ public interface ComponentInfoCurator {
* Curates the given {@link ComponentInfo}.
*
* @param componentInfo the componentInfo to curate
* @param curationDataSelector identifies which source should be used for the curation data. <code>null</code>
* indicates that the default should be used.
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the curated component info or - if no curation are done - the incoming object
* @throws ComponentInfoAdapterException if the curation could not be read
*/
ComponentInfo curate(ComponentInfo componentInfo, String curationDataSelector) throws ComponentInfoAdapterException;
ComponentInfo curate(ComponentInfo componentInfo, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.devonfw.tools.solicitor.componentinfo.ComponentContentProvider;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfo;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.CurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.DataStatusValue;
import com.devonfw.tools.solicitor.componentinfo.DefaultComponentInfoImpl;
import com.devonfw.tools.solicitor.componentinfo.DefaultLicenseInfoImpl;
Expand Down Expand Up @@ -48,18 +49,16 @@ public ComponentInfoCuratorImpl(CurationProvider curationProvider,
* and the curation.
*
* @param componentInfo the componentInfo to curate
* @param curationDataSelector identifies which source should be used for the curation data. <code>null</code>
* indicates that the default should be used. The special value "none" indicates that no curations will be
* applied.
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the curated component info
* @throws ComponentInfoAdapterException if the curation could not be read
*/
@Override
public ComponentInfo curate(ComponentInfo componentInfo, String curationDataSelector)
public ComponentInfo curate(ComponentInfo componentInfo, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException {

ComponentInfoCuration foundCuration = this.curationProvider.findCurations(componentInfo.getPackageUrl(),
curationDataSelector);
curationDataHandle);
if (foundCuration != null) {
DefaultComponentInfoImpl componentInfoImpl = new DefaultComponentInfoImpl(componentInfo);
applyFoundCurations(componentInfoImpl, foundCuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.devonfw.tools.solicitor.componentinfo.ComponentInfo;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapter;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.CurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.DataStatusValue;
import com.devonfw.tools.solicitor.componentinfo.DefaultComponentInfoImpl;
import com.devonfw.tools.solicitor.componentinfo.LicenseInfo;
Expand Down Expand Up @@ -50,24 +51,22 @@ public CuratingComponentInfoAdapter(FilteredComponentInfoProvider filteredCompon
* data as a {@link ComponentInfo} object.
*
* @param packageUrl The identifier of the package for which information is requested
* @param curationDataSelector Identifies which source should be used for the curation data. <code>null</code>
* indicates that the default should be used. Use "none" to indicate that no curation should be applied.
* @param curationDataHandle Identifies which source should be used for the curation data.
* @return the data derived from the scancode results after applying any defined curation.
* @throws ComponentInfoAdapterException if there was an exception when reading the data. In case that there is no
* data available no exception will be thrown. Instead <code>null</code> will be return in such a case.
*/
@Override
public ComponentInfo getComponentInfo(String packageUrl, String curationDataSelector)
public ComponentInfo getComponentInfo(String packageUrl, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException {

if (isFeatureActive()) {

ComponentInfo componentInfo = this.filteredComponentInfoProvider.getComponentInfo(packageUrl,
curationDataSelector);
ComponentInfo componentInfo = this.filteredComponentInfoProvider.getComponentInfo(packageUrl, curationDataHandle);
if (componentInfo == null || componentInfo.getComponentInfoData() == null) {
return componentInfo;
}
componentInfo = this.componentInfoCurator.curate(componentInfo, curationDataSelector);
componentInfo = this.componentInfoCurator.curate(componentInfo, curationDataHandle);

componentInfo = checkForIssues(componentInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.devonfw.tools.solicitor.componentinfo.ComponentInfo;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.CurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.curation.model.ComponentInfoCuration;

/**
Expand All @@ -14,16 +15,13 @@ public interface CurationProvider {
* Return the curation data for a given package.
*
* @param packageUrl identifies the package
* @param curationDataSelector identifies which source should be used for the curation data. <code>null</code>
* indicates that the default should be used. The special value "none" indicates that no curations should be
* returned.
* @return the curation data if it exists. <code>null</code> if no curation exist for the package or the
* curationDataSelector was given as "none".
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the curation data if it exists. <code>null</code> if no curation exist for the package.
* @throws ComponentInfoAdapterException if something unexpected happens
* @throws ComponentInfoAdapterNonExistingCurationDataSelectorException if the specified curationDataSelector could
* not be resolved
*/
ComponentInfoCuration findCurations(String packageUrl, String curationDataSelector)
ComponentInfoCuration findCurations(String packageUrl, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException, ComponentInfoAdapterNonExistingCurationDataSelectorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.devonfw.tools.solicitor.common.LogMessages;
import com.devonfw.tools.solicitor.common.packageurl.AllKindsPackageURLHandler;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.CurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.SelectorCurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.curation.model.ComponentInfoCuration;
import com.devonfw.tools.solicitor.componentinfo.curation.model.CurationList;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -56,16 +58,17 @@ public SingleFileCurationProvider(AllKindsPackageURLHandler packageURLHandler) {
* Return the curation data for a given package.
*
* @param packageUrl identifies the package
* @param curationDataSelector identifies which source should be used for the curation data. The value "none"
* indicates that no curations should be returned.
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the curation data if it exists. <code>null</code> if no curation exist for the package or the
* curationDataSelector was given as "none".
* @throws ComponentInfoAdapterException if something unexpected happens
*/
@Override
public ComponentInfoCuration findCurations(String packageUrl, String curationDataSelector)
public ComponentInfoCuration findCurations(String packageUrl, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException {

// SelectorCurationDataHandle is the only implementation supported here.
String curationDataSelector = ((SelectorCurationDataHandle) curationDataHandle).getCurationDataSelector();
// Return null if curationDataSelector is "none"
if ("none".equalsIgnoreCase(curationDataSelector)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.devonfw.tools.solicitor.common.packageurl.AllKindsPackageURLHandler;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfo;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.CurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.DataStatusValue;
import com.devonfw.tools.solicitor.componentinfo.DefaultComponentInfoImpl;
import com.devonfw.tools.solicitor.componentinfo.curation.CurationProvider;
Expand Down Expand Up @@ -93,14 +94,13 @@ public void setMinLicensefileNumberOfLines(int minLicensefileNumberOfLines) {
* Read scancode information for the given package from local file storage.
*
* @param packageUrl The package url of the package
* @param curationDataSelector identifies which source should be used for the curation data. <code>null</code>
* indicates that the default should be used. "none" indicates that no curations should be applied.
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the read scancode information
* @throws ComponentInfoAdapterException if there was an exception when reading the data. In case that there is no
* data available no exception will be thrown. Instead <code>null</code> will be returned in such a case.
*/
@Override
public ComponentInfo getComponentInfo(String packageUrl, String curationDataSelector)
public ComponentInfo getComponentInfo(String packageUrl, CurationDataHandle curationDataHandle)
throws ComponentInfoAdapterException {

ScancodeRawComponentInfo rawScancodeData;
Expand All @@ -114,7 +114,7 @@ public ComponentInfo getComponentInfo(String packageUrl, String curationDataSele
}

ScancodeComponentInfo componentScancodeInfos = parseAndMapScancodeJson(packageUrl, rawScancodeData,
curationDataSelector);
curationDataHandle);
addSupplementedData(rawScancodeData, componentScancodeInfos);
LOG.debug("Scancode info for package {}: {} license, {} copyrights, {} NOTICE files", packageUrl,
componentScancodeInfos.getComponentInfoData().getLicenses().size(),
Expand All @@ -140,13 +140,12 @@ private void addSupplementedData(ScancodeRawComponentInfo rawScancodeData,
*
* @param packageUrl package URL of the package
* @param rawScancodeData raw scancode data
* @param curationDataSelector identifies which source should be used for the curation data. If the value of
* curationdataselector equals "none," no curations will be applied.
* @param curationDataHandle identifies which source should be used for the curation data.
* @return the ScancodeComponentInfo
* @throws ComponentInfoAdapterException if there was an issue during parsing
*/
private ScancodeComponentInfo parseAndMapScancodeJson(String packageUrl, ScancodeRawComponentInfo rawScancodeData,
String curationDataSelector) throws ComponentInfoAdapterException {
CurationDataHandle curationDataHandle) throws ComponentInfoAdapterException {

ScancodeComponentInfo componentScancodeInfos = new ScancodeComponentInfo(this.minLicenseScore,
this.minLicensefileNumberOfLines);
Expand All @@ -158,7 +157,7 @@ private ScancodeComponentInfo parseAndMapScancodeJson(String packageUrl, Scancod
ScancodeComponentInfoData scancodeComponentInfoData = componentScancodeInfos.getComponentInfoData();

// Get the curation for a given packageUrl
ComponentInfoCuration componentInfoCuration = this.curationProvider.findCurations(packageUrl, curationDataSelector);
ComponentInfoCuration componentInfoCuration = this.curationProvider.findCurations(packageUrl, curationDataHandle);

// Get all excludedPaths in this curation
List<String> excludedPaths = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.devonfw.tools.solicitor.common.packageurl.AllKindsPackageURLHandler;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.SelectorCurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.curation.SingleFileCurationProvider;
import com.devonfw.tools.solicitor.componentinfo.curation.model.ComponentInfoCuration;

Expand Down Expand Up @@ -44,9 +45,11 @@ void testFindCurationsWithSelectorNull() throws ComponentInfoAdapterException {

ComponentInfoCuration result;

result = this.objectUnderTest.findCurations("pkg:maven/somenamespace/[email protected]", null);
result = this.objectUnderTest.findCurations("pkg:maven/somenamespace/[email protected]",
new SelectorCurationDataHandle(null));
assertEquals("https://scancode-licensedb.aboutcode.org/apache-2.0.LICENSE", result.getLicenses().get(0).getUrl());
result = this.objectUnderTest.findCurations("pkg:maven/somenamespace/[email protected]", null);
result = this.objectUnderTest.findCurations("pkg:maven/somenamespace/[email protected]",
new SelectorCurationDataHandle(null));
assertEquals("https://scancode-licensedb.aboutcode.org/bsd-simplified.LICENSE",
result.getLicenses().get(0).getUrl());
}
Expand All @@ -62,7 +65,8 @@ void testFindCurationsWithSelectorNone() throws ComponentInfoAdapterException {

ComponentInfoCuration result;

result = this.objectUnderTest.findCurations("pkg:maven/somenamespace/[email protected]", "none");
result = this.objectUnderTest.findCurations("pkg:maven/somenamespace/[email protected]",
new SelectorCurationDataHandle("none"));
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.devonfw.tools.solicitor.common.packageurl.AllKindsPackageURLHandler;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfo;
import com.devonfw.tools.solicitor.componentinfo.ComponentInfoAdapterException;
import com.devonfw.tools.solicitor.componentinfo.SelectorCurationDataHandle;
import com.devonfw.tools.solicitor.componentinfo.curation.SingleFileCurationProvider;

/**
Expand Down Expand Up @@ -57,7 +58,8 @@ public void testGetComponentInfoWithoutCurations() throws ComponentInfoAdapterEx

// when
ComponentInfo scancodeComponentInfo = this.filteredScancodeComponentInfoProvider.getComponentInfo(
"pkg:maven/com.devonfw.tools/[email protected]", "someCurationSelector");
"pkg:maven/com.devonfw.tools/[email protected]",
new SelectorCurationDataHandle("someCurationSelector"));

// then
assertNotNull(scancodeComponentInfo.getComponentInfoData());
Expand All @@ -83,7 +85,8 @@ public void testGetComponentInfoWithCurationsAndExclusions() throws ComponentInf
.setCurationsFileName("src/test/resources/scancodefileadapter/curations_with_exclusions.yaml");
// when
ComponentInfo scancodeComponentInfo = this.filteredScancodeComponentInfoProvider.getComponentInfo(
"pkg:maven/com.devonfw.tools/[email protected]", "someCurationSelector");
"pkg:maven/com.devonfw.tools/[email protected]",
new SelectorCurationDataHandle("someCurationSelector"));

// then
assertNotNull(scancodeComponentInfo.getComponentInfoData());
Expand All @@ -110,7 +113,8 @@ public void testGetComponentInfoWithCurationsAndWithoutExclusions() throws Compo

// when
ComponentInfo scancodeComponentInfo = this.filteredScancodeComponentInfoProvider.getComponentInfo(
"pkg:maven/com.devonfw.tools/[email protected]", "someCurationSelector");
"pkg:maven/com.devonfw.tools/[email protected]",
new SelectorCurationDataHandle("someCurationSelector"));

// then
assertNotNull(scancodeComponentInfo.getComponentInfoData());
Expand Down
Loading

0 comments on commit 697305f

Please sign in to comment.