From f841a4cf32ed07dadb87697a101fbb8c83bc987f Mon Sep 17 00:00:00 2001 From: Stefan Bischof Date: Thu, 26 Sep 2024 14:26:01 +0200 Subject: [PATCH 1/2] add OSGi Metadata using bnd-maven-plugin Signed-off-by: Stefan Bischof --- core/pom.xml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/core/pom.xml b/core/pom.xml index 5bd94aac..44fb41d6 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -167,6 +167,31 @@ + + biz.aQute.bnd + bnd-maven-plugin + true + + + jar + + + + + jar + + + + From 230f42c02cf6846f799c1ba99c8ae20cc8fed213 Mon Sep 17 00:00:00 2001 From: Stefan Bischof Date: Fri, 27 Sep 2024 14:00:52 +0200 Subject: [PATCH 2/2] remove com.google.inject Signed-off-by: Stefan Bischof --- core/pom.xml | 10 -- .../dash/licenses/ILicenseDataProvider.java | 6 +- .../eclipse/dash/licenses/LicenseChecker.java | 48 +++---- .../eclipse/dash/licenses/LicenseSupport.java | 32 ++--- .../clearlydefined/ClearlyDefinedSupport.java | 31 +++-- .../org/eclipse/dash/licenses/cli/Main.java | 31 ++--- .../context/BaseLicenseToolModule.java | 117 ++++++++++++++++++ .../licenses/context/LicenseToolContext.java | 32 +++++ .../licenses/context/LicenseToolModule.java | 46 +------ .../foundation/EclipseFoundationSupport.java | 32 ++--- .../dash/licenses/http/HttpClientService.java | 24 ++-- .../dash/licenses/review/GitLabSupport.java | 44 +++---- .../validation/EclipseProjectIdValidator.java | 17 +-- .../tests/ClearlyDefinedSupportTests.java | 37 +++--- .../licenses/tests/LicenseCheckerTests.java | 9 +- .../licenses/tests/LicenseSupportTests.java | 7 +- .../tests/util/TestLicenseToolModule.java | 35 ++---- .../dash/licenses/maven/LicenseCheckMojo.java | 8 +- 18 files changed, 328 insertions(+), 238 deletions(-) create mode 100644 core/src/main/java/org/eclipse/dash/licenses/context/BaseLicenseToolModule.java create mode 100644 core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolContext.java diff --git a/core/pom.xml b/core/pom.xml index 44fb41d6..f83ee7d4 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -64,16 +64,6 @@ eclipse-api-for-java 1.0.0 - - com.google.inject - guice - 7.0.0 - - - com.google.inject.extensions - guice-multibindings - 4.2.3 - org.apache.commons commons-csv diff --git a/core/src/main/java/org/eclipse/dash/licenses/ILicenseDataProvider.java b/core/src/main/java/org/eclipse/dash/licenses/ILicenseDataProvider.java index 4cfa5d4b..20b448e4 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/ILicenseDataProvider.java +++ b/core/src/main/java/org/eclipse/dash/licenses/ILicenseDataProvider.java @@ -12,10 +12,14 @@ import java.util.Collection; import java.util.function.Consumer; +import org.eclipse.dash.licenses.context.LicenseToolContext; + public interface ILicenseDataProvider { + void init(LicenseToolContext context); + void queryLicenseData(Collection ids, Consumer consumer); - + default int getWeight() { return 50; } diff --git a/core/src/main/java/org/eclipse/dash/licenses/LicenseChecker.java b/core/src/main/java/org/eclipse/dash/licenses/LicenseChecker.java index b56bf498..7537c388 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/LicenseChecker.java +++ b/core/src/main/java/org/eclipse/dash/licenses/LicenseChecker.java @@ -11,25 +11,26 @@ import java.util.Collection; import java.util.Map; -import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.dash.licenses.LicenseSupport.Status; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.eclipse.dash.licenses.util.Batchifier; -import jakarta.inject.Inject; - public class LicenseChecker { - @Inject - ISettings settings; - @Inject - Set licenseDataProviders; + + private LicenseToolContext ctx; + + public LicenseChecker(LicenseToolContext context) { + this.ctx = context; + + } private Stream getLicenseDataProviders() { // Compare in reverse order. We want the "heaviest" one first. - return licenseDataProviders.stream().sorted((a,b)-> Integer.compare(b.getWeight(), a.getWeight())); + return ctx.getLicenseDataProviders().stream().sorted((a, b) -> Integer.compare(b.getWeight(), a.getWeight())); } /** @@ -40,22 +41,25 @@ private Stream getLicenseDataProviders() { * @return */ public Map getLicenseData(Collection ids) { - Map licenseData = ids.stream().map(id -> new LicenseData(id)).collect( - Collectors.toMap(LicenseData::getId, Function.identity(), (existing, replacement) -> existing)); + Map licenseData = ids + .stream() + .map(id -> new LicenseData(id)) + .collect( + Collectors.toMap(LicenseData::getId, Function.identity(), (existing, replacement) -> existing)); getLicenseDataProviders().forEach(provider -> { - new Batchifier() - .setBatchSize(settings.getBatchSize()) - .setConsumer(batch -> { - provider.queryLicenseData(batch, data -> { - var item = licenseData.get(data.getId()); - if (item != null) item.addContentData(data); - }); - }) - .batchify(ids.stream() - .filter(IContentId::isValid) - .filter(id -> licenseData.get(id).getStatus() != Status.Approved) - .iterator()); + new Batchifier().setBatchSize(ctx.getSettings().getBatchSize()).setConsumer(batch -> { + provider.queryLicenseData(batch, data -> { + var item = licenseData.get(data.getId()); + if (item != null) + item.addContentData(data); + }); + }) + .batchify(ids + .stream() + .filter(IContentId::isValid) + .filter(id -> licenseData.get(id).getStatus() != Status.Approved) + .iterator()); }); return licenseData; diff --git a/core/src/main/java/org/eclipse/dash/licenses/LicenseSupport.java b/core/src/main/java/org/eclipse/dash/licenses/LicenseSupport.java index 872aaa05..3370b899 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/LicenseSupport.java +++ b/core/src/main/java/org/eclipse/dash/licenses/LicenseSupport.java @@ -14,13 +14,12 @@ import java.util.HashMap; import java.util.Map; -import org.eclipse.dash.licenses.http.IHttpClientService; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.eclipse.dash.licenses.spdx.SpdxExpression; import org.eclipse.dash.licenses.spdx.SpdxExpressionParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.inject.Inject; import jakarta.json.Json; import jakarta.json.JsonObject; import jakarta.json.JsonReader; @@ -29,25 +28,30 @@ public class LicenseSupport { final Logger logger = LoggerFactory.getLogger(LicenseSupport.class); - @Inject - ISettings settings; - @Inject - IHttpClientService httpClientService; - private Map approvedLicenses; + private LicenseToolContext ctx; + public enum Status { Approved, Restricted } - @Inject - public void init() { - httpClientService.get(settings.getApprovedLicensesUrl(), "application/json", response -> { - approvedLicenses = getApprovedLicenses(new InputStreamReader(response)); - }); + public LicenseSupport(LicenseToolContext context) { + this.ctx = context; + + } + + private Map getApprovedLicenses() { + if (approvedLicenses == null) { + + ctx.getHttpClientService().get(ctx.getSettings().getApprovedLicensesUrl(), "application/json", response -> { + approvedLicenses = readApprovedLicenses(new InputStreamReader(response)); + }); + } + return approvedLicenses; } - private Map getApprovedLicenses(Reader contentReader) { + private Map readApprovedLicenses(Reader contentReader) { JsonReader reader = Json.createReader(contentReader); JsonObject read = (JsonObject) reader.read(); @@ -89,7 +93,7 @@ public Status getStatus(SpdxExpression expression) { if (expression == null) return Status.Restricted; - if (expression.matchesApproved(approvedLicenses.keySet())) { + if (expression.matchesApproved(getApprovedLicenses().keySet())) { return Status.Approved; } diff --git a/core/src/main/java/org/eclipse/dash/licenses/clearlydefined/ClearlyDefinedSupport.java b/core/src/main/java/org/eclipse/dash/licenses/clearlydefined/ClearlyDefinedSupport.java index afc85891..bc2650c4 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/clearlydefined/ClearlyDefinedSupport.java +++ b/core/src/main/java/org/eclipse/dash/licenses/clearlydefined/ClearlyDefinedSupport.java @@ -22,30 +22,23 @@ import org.eclipse.dash.licenses.IContentData; import org.eclipse.dash.licenses.IContentId; import org.eclipse.dash.licenses.ILicenseDataProvider; -import org.eclipse.dash.licenses.ISettings; import org.eclipse.dash.licenses.LicenseSupport; import org.eclipse.dash.licenses.LicenseSupport.Status; -import org.eclipse.dash.licenses.http.IHttpClientService; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.eclipse.dash.licenses.util.JsonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.inject.Inject; import jakarta.json.stream.JsonParsingException; public class ClearlyDefinedSupport implements ILicenseDataProvider { final Logger logger = LoggerFactory.getLogger(ClearlyDefinedSupport.class); - @Inject - ISettings settings; - @Inject - IHttpClientService httpClientService; - @Inject - LicenseSupport licenseService; - private Set validTypes; private Set validProviders; + private LicenseToolContext ctx; + /** * The ClearlyDefined API expects a flat array of ids in JSON format in the * payload of the POST request. @@ -141,8 +134,9 @@ private void doQueryClearlyDefined(List ids, int start, int end, Con if (start == end) return; - int code = httpClientService - .post(settings.getClearlyDefinedDefinitionsUrl(), "application/json", + int code = ctx + .getHttpClientService() + .post(ctx.getSettings().getClearlyDefinedDefinitionsUrl(), "application/json", JsonUtils.toJson(ids.subList(start, end)), response -> { // FIXME Seems like overkill. AtomicInteger counter = new AtomicInteger(); @@ -209,8 +203,8 @@ private boolean isSupported(IContentId id) { * otherwise. */ public boolean isAccepted(ClearlyDefinedContentData data) { - if (data.getLicenseScore() >= settings.getConfidenceThreshold()) { - if (licenseService.getStatus(data.getLicense()) != LicenseSupport.Status.Approved) + if (data.getLicenseScore() >= ctx.getSettings().getConfidenceThreshold()) { + if (ctx.getLicenseService().getStatus(data.getLicense()) != LicenseSupport.Status.Approved) return false; return !data .discoveredLicenses() @@ -223,10 +217,9 @@ public boolean isAccepted(ClearlyDefinedContentData data) { } boolean isDiscoveredLicenseApproved(String license) { - return licenseService.getStatus(license) == LicenseSupport.Status.Approved; + return ctx.getLicenseService().getStatus(license) == LicenseSupport.Status.Approved; } - @Inject void bootstrap() { /* * FIXME This is a hack. AFAICT, there is no API that answers the list of valid @@ -266,4 +259,10 @@ public ClearlyDefinedResponseException() { super(); } } + + @Override + public void init(LicenseToolContext context) { + this.ctx = context; + bootstrap(); + } } diff --git a/core/src/main/java/org/eclipse/dash/licenses/cli/Main.java b/core/src/main/java/org/eclipse/dash/licenses/cli/Main.java index 975de75b..30ea3989 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/cli/Main.java +++ b/core/src/main/java/org/eclipse/dash/licenses/cli/Main.java @@ -26,14 +26,10 @@ import org.eclipse.dash.licenses.LicenseChecker; import org.eclipse.dash.licenses.context.LicenseToolModule; import org.eclipse.dash.licenses.review.CreateReviewRequestCollector; -import org.eclipse.dash.licenses.review.GitLabSupport; import org.eclipse.dash.licenses.validation.EclipseProjectIdValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.inject.Guice; -import com.google.inject.Injector; - /** * This class provides a CLI entry point to determine licenses for content. The * tool can be invoked in a few different ways, e.g. @@ -79,16 +75,16 @@ void doit(CommandLineSettings settings) { System.exit(0); } - Injector injector = Guice.createInjector(new LicenseToolModule(settings)); + LicenseToolModule module = new LicenseToolModule(settings); if (settings.getProjectId() != null) { - var validator = injector.getInstance(EclipseProjectIdValidator.class); + var validator = new EclipseProjectIdValidator(module); if (!validator.validate(settings.getProjectId(), message -> System.out.println(message))) { System.exit(INTERNAL_ERROR); } } - LicenseChecker checker = injector.getInstance(LicenseChecker.class); + LicenseChecker checker = module.getLicenseChecker(); List collectors = new ArrayList<>(); @@ -108,8 +104,7 @@ void doit(CommandLineSettings settings) { } if (settings.isReview()) { - collectors - .add(new CreateReviewRequestCollector(injector.getInstance(GitLabSupport.class), (id, url) -> {})); + collectors.add(new CreateReviewRequestCollector(module.getGitlab(), (id, url) -> {})); } Arrays.stream(settings.getFileNames()).forEach(name -> { @@ -159,15 +154,15 @@ private IDependencyListReader getReader(String name) throws FileNotFoundExceptio } else { File input = new File(name); if (input.exists()) { - switch (input.getName()) { - case "pnpm-lock.yaml": - return new PnpmPackageLockFileReader(new FileInputStream(input)); - case "package-lock.json": - return new PackageLockFileReader(new FileInputStream(input)); - case "yarn.lock": - return new YarnLockFileReader(new FileReader(input)); - } - return new FlatFileReader(new FileReader(input)); + switch (input.getName()) { + case "pnpm-lock.yaml": + return new PnpmPackageLockFileReader(new FileInputStream(input)); + case "package-lock.json": + return new PackageLockFileReader(new FileInputStream(input)); + case "yarn.lock": + return new YarnLockFileReader(new FileReader(input)); + } + return new FlatFileReader(new FileReader(input)); } else { throw new FileNotFoundException(name); } diff --git a/core/src/main/java/org/eclipse/dash/licenses/context/BaseLicenseToolModule.java b/core/src/main/java/org/eclipse/dash/licenses/context/BaseLicenseToolModule.java new file mode 100644 index 00000000..da6f35cf --- /dev/null +++ b/core/src/main/java/org/eclipse/dash/licenses/context/BaseLicenseToolModule.java @@ -0,0 +1,117 @@ +/************************************************************************* + * Copyright (c) 2021, 2022 The Eclipse Foundation and others. + * + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which accompanies this + * distribution, and is available at https://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + *************************************************************************/ +package org.eclipse.dash.licenses.context; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.function.Consumer; + +import org.eclipse.dash.api.EclipseApi; +import org.eclipse.dash.licenses.ILicenseDataProvider; +import org.eclipse.dash.licenses.IProxySettings; +import org.eclipse.dash.licenses.ISettings; +import org.eclipse.dash.licenses.LicenseChecker; +import org.eclipse.dash.licenses.LicenseSupport; +import org.eclipse.dash.licenses.http.HttpClientService; +import org.eclipse.dash.licenses.http.IHttpClientService; +import org.eclipse.dash.licenses.review.GitLabSupport; + +public class BaseLicenseToolModule implements LicenseToolContext { + + private ISettings settings; + private Optional proxySettings; + + private IHttpClientService httpClientService; + private LicenseChecker licenseChecker; + private EclipseApi eclipseApi; + private LicenseSupport licenseSupport; + private GitLabSupport gitLabSupport; + private List licenseDataProviders = new ArrayList<>(); + + public BaseLicenseToolModule(ISettings settings) { + this(settings, null); + } + + public BaseLicenseToolModule(ISettings settings, IProxySettings proxySettings) { + this.settings = settings; + this.proxySettings = Optional.ofNullable(proxySettings); + this.httpClientService = newHttpClient(); + + this.licenseChecker = new LicenseChecker(this); + this.licenseSupport = new LicenseSupport(this); + + this.eclipseApi = new EclipseApi(new EclipseApi.HttpService() { + @Override + public int get(String url, String contentType, Consumer handler) { + return httpClientService.get(url, contentType, handler); + } + }); + this.gitLabSupport = new GitLabSupport(this); + } + + protected IHttpClientService newHttpClient() { + return new HttpClientService(this); + + } + + @Override + public ISettings getSettings() { + return settings; + } + + @Override + public Optional proxySettings() { + return proxySettings; + } + + @Override + public List getLicenseDataProviders() { + return licenseDataProviders; + } + + @Override + public void bindLicenseDataProviders(ILicenseDataProvider licenseDataProvider) { + licenseDataProvider.init(this); + licenseDataProviders.add(licenseDataProvider); + } + + @Override + public IHttpClientService getHttpClientService() { + return httpClientService; + } + + @Override + public LicenseSupport getLicenseService() { + return licenseSupport; + } + + @Override + public EclipseApi getEclipseApi() { + return eclipseApi; + } + + @Override + public GitLabSupport getGitlab() { + return gitLabSupport; + } + + public LicenseChecker getLicenseChecker() { + return licenseChecker; + } + + @SuppressWarnings("unchecked") + public T getLicenseDataProviderOfType(Class ldpClass) { + return (T) licenseDataProviders.stream().filter(t -> { + return ldpClass.isInstance(t); + }).findFirst().orElse(null); + } +} diff --git a/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolContext.java b/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolContext.java new file mode 100644 index 00000000..3ff88ed6 --- /dev/null +++ b/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolContext.java @@ -0,0 +1,32 @@ +package org.eclipse.dash.licenses.context; + +import java.util.List; +import java.util.Optional; + +import org.eclipse.dash.api.EclipseApi; +import org.eclipse.dash.licenses.ILicenseDataProvider; +import org.eclipse.dash.licenses.IProxySettings; +import org.eclipse.dash.licenses.ISettings; +import org.eclipse.dash.licenses.LicenseSupport; +import org.eclipse.dash.licenses.http.IHttpClientService; +import org.eclipse.dash.licenses.review.GitLabSupport; + +public interface LicenseToolContext { + + ISettings getSettings(); + + Optional proxySettings(); + + List getLicenseDataProviders(); + + void bindLicenseDataProviders(ILicenseDataProvider licenseDataProvider); + + IHttpClientService getHttpClientService(); + + LicenseSupport getLicenseService(); + + EclipseApi getEclipseApi(); + + GitLabSupport getGitlab(); + +} diff --git a/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolModule.java b/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolModule.java index 49b71cfb..05f2ca91 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolModule.java +++ b/core/src/main/java/org/eclipse/dash/licenses/context/LicenseToolModule.java @@ -9,54 +9,21 @@ *************************************************************************/ package org.eclipse.dash.licenses.context; -import java.io.InputStream; -import java.util.function.Consumer; - -import org.eclipse.dash.api.EclipseApi; -import org.eclipse.dash.licenses.ILicenseDataProvider; import org.eclipse.dash.licenses.IProxySettings; import org.eclipse.dash.licenses.ISettings; -import org.eclipse.dash.licenses.LicenseChecker; -import org.eclipse.dash.licenses.LicenseSupport; import org.eclipse.dash.licenses.clearlydefined.ClearlyDefinedSupport; import org.eclipse.dash.licenses.foundation.EclipseFoundationSupport; -import org.eclipse.dash.licenses.http.HttpClientService; -import org.eclipse.dash.licenses.http.IHttpClientService; -import org.eclipse.dash.licenses.review.GitLabSupport; - -import com.google.inject.AbstractModule; -import com.google.inject.multibindings.Multibinder; -import com.google.inject.util.Providers; - -public class LicenseToolModule extends AbstractModule { - private ISettings settings; - private IProxySettings proxySettings; +public class LicenseToolModule extends BaseLicenseToolModule { public LicenseToolModule(ISettings settings) { this(settings, null); } public LicenseToolModule(ISettings settings, IProxySettings proxySettings) { - this.settings = settings; - this.proxySettings = proxySettings; - } + super(settings, proxySettings); - @Override - protected void configure() { - HttpClientService httpClientService = new HttpClientService(); - bind(IHttpClientService.class).toInstance(httpClientService); - bind(ISettings.class).toInstance(settings); - bind(LicenseChecker.class).toInstance(new LicenseChecker()); - bind(EclipseApi.class).toInstance(new EclipseApi(new EclipseApi.HttpService() { - @Override - public int get(String url, String contentType, Consumer handler) { - return httpClientService.get(url, contentType, handler); - } - })); - - var licenseDataProviders = Multibinder.newSetBinder(binder(), ILicenseDataProvider.class); - licenseDataProviders.addBinding().toInstance(new EclipseFoundationSupport() { + bindLicenseDataProviders(new EclipseFoundationSupport() { @Override public int getWeight() { return 100; @@ -64,11 +31,8 @@ public int getWeight() { }); if (!"skip".equals(settings.getClearlyDefinedDefinitionsUrl())) { - licenseDataProviders.addBinding().to(ClearlyDefinedSupport.class); + bindLicenseDataProviders(new ClearlyDefinedSupport()); } - - bind(LicenseSupport.class).toInstance(new LicenseSupport()); - bind(GitLabSupport.class).toInstance(new GitLabSupport()); - bind(IProxySettings.class).toProvider(Providers.of(proxySettings)); } + } diff --git a/core/src/main/java/org/eclipse/dash/licenses/foundation/EclipseFoundationSupport.java b/core/src/main/java/org/eclipse/dash/licenses/foundation/EclipseFoundationSupport.java index b83708ba..25a18100 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/foundation/EclipseFoundationSupport.java +++ b/core/src/main/java/org/eclipse/dash/licenses/foundation/EclipseFoundationSupport.java @@ -16,13 +16,10 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; -import jakarta.inject.Inject; - import org.eclipse.dash.licenses.IContentData; import org.eclipse.dash.licenses.IContentId; import org.eclipse.dash.licenses.ILicenseDataProvider; -import org.eclipse.dash.licenses.ISettings; -import org.eclipse.dash.licenses.http.IHttpClientService; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,19 +31,16 @@ import jakarta.json.JsonReader; public class EclipseFoundationSupport implements ILicenseDataProvider { - @Inject - ISettings settings; - @Inject - IHttpClientService httpClientService; final Logger logger = LoggerFactory.getLogger(EclipseFoundationSupport.class); + private LicenseToolContext ctx; @Override public void queryLicenseData(Collection ids, Consumer consumer) { if (ids.isEmpty()) return; - String url = settings.getLicenseCheckUrl(); + String url = ctx.getSettings().getLicenseCheckUrl(); if (url.isBlank()) { logger.debug("Bypassing Eclipse Foundation."); return; @@ -56,7 +50,7 @@ public void queryLicenseData(Collection ids, Consumer String form = encodeRequestPayload(ids); - int code = httpClientService.post(url, "application/x-www-form-urlencoded", form, response -> { + int code = ctx.getHttpClientService().post(url, "application/x-www-form-urlencoded", form, response -> { AtomicInteger counter = new AtomicInteger(); JsonReader reader = Json.createReader(new StringReader(response)); @@ -66,8 +60,9 @@ public void queryLicenseData(Collection ids, Consumer if (approved != null) approved.forEach((key, each) -> { FoundationData data = new FoundationData(each.asJsonObject()); - logger.debug("EF approved: {} ({}) score: {} {} {}", data.getId(), data.getRule(), data.getScore(), - data.getLicense(), data.getAuthority()); + logger + .debug("EF approved: {} ({}) score: {} {} {}", data.getId(), data.getRule(), + data.getScore(), data.getLicense(), data.getAuthority()); consumer.accept(data); counter.incrementAndGet(); }); @@ -76,8 +71,9 @@ public void queryLicenseData(Collection ids, Consumer if (restricted != null) restricted.forEach((key, each) -> { FoundationData data = new FoundationData(each.asJsonObject()); - logger.debug("EF restricted: {} score: {} {} {}", data.getId(), data.getScore(), data.getLicense(), - data.getAuthority()); + logger + .debug("EF restricted: {} score: {} {} {}", data.getId(), data.getScore(), + data.getLicense(), data.getAuthority()); consumer.accept(data); counter.incrementAndGet(); }); @@ -101,7 +97,7 @@ private String encodeRequestPayload(Collection ids) { private JsonObject buildRequestPayload(Collection ids) { JsonObjectBuilder request = Json.createObjectBuilder(); - var projectId = settings.getProjectId(); + var projectId = ctx.getSettings().getProjectId(); if (projectId != null) { logger.debug("Querying for project {}.", projectId); request.add("project", projectId); @@ -132,4 +128,10 @@ private JsonArray buildJsonPayload(Collection ids) { return builder.build(); } + + @Override + public void init(LicenseToolContext context) { + this.ctx = context; + + } } diff --git a/core/src/main/java/org/eclipse/dash/licenses/http/HttpClientService.java b/core/src/main/java/org/eclipse/dash/licenses/http/HttpClientService.java index 6afef390..13090a97 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/http/HttpClientService.java +++ b/core/src/main/java/org/eclipse/dash/licenses/http/HttpClientService.java @@ -23,14 +23,9 @@ import java.time.Duration; import java.util.Collections; import java.util.Map; -import java.util.Optional; import java.util.function.Consumer; -import jakarta.inject.Inject; -import jakarta.inject.Provider; - -import org.eclipse.dash.licenses.IProxySettings; -import org.eclipse.dash.licenses.ISettings; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,19 +34,18 @@ public class HttpClientService implements IHttpClientService { final static int MAX_TRIES = 10; - @Inject - ISettings settings; + private LicenseToolContext ctx; - /** Optional HTTP proxy settings. */ - @Inject - Provider proxySettings; + public HttpClientService(LicenseToolContext context) { + this.ctx = context; + } @Override public int post(String url, String contentType, String payload, Consumer handler) { try { var tries = 0; while (true) { - Duration timeout = Duration.ofSeconds(settings.getTimeout()); + Duration timeout = Duration.ofSeconds(ctx.getSettings().getTimeout()); HttpRequest request = HttpRequest .newBuilder(URI.create(url)) .header("Content-Type", contentType) @@ -83,7 +77,7 @@ public boolean remoteFileExists(String url) { .newBuilder(URI.create(url)) .method("HEAD", HttpRequest.BodyPublishers.noBody()); - Duration timeout = Duration.ofSeconds(settings.getTimeout()); + Duration timeout = Duration.ofSeconds(ctx.getSettings().getTimeout()); HttpRequest request = reqBuilder.timeout(timeout).build(); HttpClient httpClient = getHttpClient(timeout); @@ -111,7 +105,7 @@ public int get(String url, String contentType, Map headers, Cons headers.forEach((key, value) -> reqBuilder.header(key, value)); - Duration timeout = Duration.ofSeconds(settings.getTimeout()); + Duration timeout = Duration.ofSeconds(ctx.getSettings().getTimeout()); HttpRequest request = reqBuilder.timeout(timeout).build(); HttpClient httpClient = getHttpClient(timeout); @@ -139,7 +133,7 @@ protected HttpClient getHttpClient(Duration timeout) { .followRedirects(HttpClient.Redirect.ALWAYS); // Configure proxy, if any - Optional.ofNullable(this.proxySettings.get()).ifPresent(proxySettings -> proxySettings.configure(builder)); + ctx.proxySettings().ifPresent(proxySettings -> proxySettings.configure(builder)); return builder.build(); } diff --git a/core/src/main/java/org/eclipse/dash/licenses/review/GitLabSupport.java b/core/src/main/java/org/eclipse/dash/licenses/review/GitLabSupport.java index 5323cfaa..850eb90d 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/review/GitLabSupport.java +++ b/core/src/main/java/org/eclipse/dash/licenses/review/GitLabSupport.java @@ -9,18 +9,15 @@ *************************************************************************/ package org.eclipse.dash.licenses.review; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; -import jakarta.inject.Inject; -import jakarta.inject.Provider; - import org.eclipse.dash.licenses.IContentId; -import org.eclipse.dash.licenses.IProxySettings; -import org.eclipse.dash.licenses.ISettings; import org.eclipse.dash.licenses.LicenseData; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.eclipse.dash.licenses.util.GitUtils; import org.gitlab4j.api.GitLabApi; import org.gitlab4j.api.GitLabApiException; @@ -28,20 +25,17 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Maps; - public class GitLabSupport { final Logger logger = LoggerFactory.getLogger(GitLabSupport.class); private static final int MAXIMUM_REVIEWS = 100; - @Inject - ISettings settings; + private LicenseToolContext ctx; + + public GitLabSupport(LicenseToolContext context) { + this.ctx = context; + } - /** Optional HTTP proxy settings. */ - @Inject - Provider proxySettings; - public void createReviews(List needsReview, BiConsumer monitor) { execute(connection -> { var count = 0; @@ -68,7 +62,8 @@ public void createReviews(List needsReview, BiConsumer needsReview, BiConsumer callable) { - Map clientConfig = null; - IProxySettings proxySettings = this.proxySettings.get(); - if (proxySettings != null) { + Map clientConfig = new HashMap<>(); + + ctx.proxySettings().ifPresent(proxySettings -> { // Configure GitLab API for the proxy server - clientConfig = Maps.newHashMap(); proxySettings.configureJerseyClient(clientConfig); - } + }); - try (GitLabApi gitLabApi = new GitLabApi(settings.getIpLabHostUrl(), settings.getIpLabToken(), clientConfig)) { - callable.accept(new GitLabConnection(gitLabApi, settings.getIpLabRepositoryPath())); + try (GitLabApi gitLabApi = new GitLabApi(ctx.getSettings().getIpLabHostUrl(), ctx.getSettings().getIpLabToken(), + clientConfig)) { + callable.accept(new GitLabConnection(gitLabApi, ctx.getSettings().getIpLabRepositoryPath())); } } } diff --git a/core/src/main/java/org/eclipse/dash/licenses/validation/EclipseProjectIdValidator.java b/core/src/main/java/org/eclipse/dash/licenses/validation/EclipseProjectIdValidator.java index 2d5f3f0b..6fbf064b 100644 --- a/core/src/main/java/org/eclipse/dash/licenses/validation/EclipseProjectIdValidator.java +++ b/core/src/main/java/org/eclipse/dash/licenses/validation/EclipseProjectIdValidator.java @@ -11,21 +11,22 @@ import java.util.function.Consumer; -import jakarta.inject.Inject; - import org.eclipse.dash.api.EclipseApi; -import org.eclipse.dash.licenses.review.GitLabSupport; +import org.eclipse.dash.licenses.context.LicenseToolContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EclipseProjectIdValidator { final Logger logger = LoggerFactory.getLogger(EclipseProjectIdValidator.class); - @Inject - EclipseApi eclipseApi; + private LicenseToolContext ctx; + + private EclipseApi eclipseApi; - @Inject - GitLabSupport gitlab; + public EclipseProjectIdValidator(LicenseToolContext context) { + this.ctx = context; + eclipseApi = ctx.getEclipseApi(); + } public boolean validate(String id, Consumer output) { var project = eclipseApi.getProject(id); @@ -33,7 +34,7 @@ public boolean validate(String id, Consumer output) { output.accept("The specified project cannot be found. You must provide a valid Eclipse project id."); output.accept("Specify the project as [tlp].[name] (e.g., technology.dash)"); try { - gitlab.execute(connection -> { + ctx.getGitlab().execute(connection -> { var user = connection.getUserId(); var account = eclipseApi.getAccount(user); if (account.exists() || account.isCommitter()) { diff --git a/core/src/test/java/org/eclipse/dash/licenses/tests/ClearlyDefinedSupportTests.java b/core/src/test/java/org/eclipse/dash/licenses/tests/ClearlyDefinedSupportTests.java index eede9df4..a1435070 100644 --- a/core/src/test/java/org/eclipse/dash/licenses/tests/ClearlyDefinedSupportTests.java +++ b/core/src/test/java/org/eclipse/dash/licenses/tests/ClearlyDefinedSupportTests.java @@ -31,25 +31,23 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import com.google.inject.Guice; -import com.google.inject.Injector; - class ClearlyDefinedSupportTests { private ClearlyDefinedSupport clearlyDefined; @BeforeEach void setup() { - Injector injector = Guice.createInjector(new TestLicenseToolModule()); - clearlyDefined = injector.getInstance(ClearlyDefinedSupport.class); + TestLicenseToolModule testLicenseToolModule = new TestLicenseToolModule(); + clearlyDefined = testLicenseToolModule.getLicenseDataProviderOfType(ClearlyDefinedSupport.class); } @Test void testMatchApproved() { List results = new ArrayList<>(); - clearlyDefined.queryLicenseData(Collections.singleton(ContentId.getContentId("npm/npmjs/-/write/1.0.3")), - data -> results.add(data)); + clearlyDefined + .queryLicenseData(Collections.singleton(ContentId.getContentId("npm/npmjs/-/write/1.0.3")), + data -> results.add(data)); assertEquals(1, results.size()); @@ -65,9 +63,9 @@ void testMatchApproved() { @Test void testMatchRestricted() { List results = new ArrayList<>(); - clearlyDefined.queryLicenseData( - Collections.singleton(ContentId.getContentId("npm/npmjs/@yarnpkg/lockfile/1.1.0")), - data -> results.add(data)); + clearlyDefined + .queryLicenseData(Collections.singleton(ContentId.getContentId("npm/npmjs/@yarnpkg/lockfile/1.1.0")), + data -> results.add(data)); assertEquals(1, results.size()); @@ -91,8 +89,9 @@ void testEmptyRequest() { @Test void testWithUnsupported() { List results = new ArrayList<>(); - clearlyDefined.queryLicenseData(Collections.singleton(ContentId.getContentId("p2/eclipseplugin/-/write/0.2.0")), - data -> results.add(data)); + clearlyDefined + .queryLicenseData(Collections.singleton(ContentId.getContentId("p2/eclipseplugin/-/write/0.2.0")), + data -> results.add(data)); assertTrue(results.isEmpty()); } @@ -114,9 +113,10 @@ void testMultiple() { }; // @formatter:on - clearlyDefined.queryLicenseData( - Arrays.stream(packages).map(each -> ContentId.getContentId(each)).collect(Collectors.toList()), - data -> results.add(data)); + clearlyDefined + .queryLicenseData( + Arrays.stream(packages).map(each -> ContentId.getContentId(each)).collect(Collectors.toList()), + data -> results.add(data)); assertEquals(8, results.size()); } @@ -139,9 +139,10 @@ void testMultipleWithFailure() { }; // @formatter:on - clearlyDefined.queryLicenseData( - Arrays.stream(packages).map(each -> ContentId.getContentId(each)).collect(Collectors.toList()), - data -> results.add(data)); + clearlyDefined + .queryLicenseData( + Arrays.stream(packages).map(each -> ContentId.getContentId(each)).collect(Collectors.toList()), + data -> results.add(data)); assertEquals(7, results.size()); } diff --git a/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseCheckerTests.java b/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseCheckerTests.java index 9dfb5ebe..58d8008c 100644 --- a/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseCheckerTests.java +++ b/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseCheckerTests.java @@ -24,17 +24,16 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.google.inject.Guice; -import com.google.inject.Injector; - class LicenseCheckerTests { private LicenseChecker licenseChecker; @BeforeEach void setup() { - Injector injector = Guice.createInjector(new TestLicenseToolModule()); - licenseChecker = injector.getInstance(LicenseChecker.class); + + TestLicenseToolModule testLicenseToolModule = new TestLicenseToolModule(); + licenseChecker = testLicenseToolModule.getLicenseChecker(); + } @Test diff --git a/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseSupportTests.java b/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseSupportTests.java index 5a0e2b1e..949ac5a3 100644 --- a/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseSupportTests.java +++ b/core/src/test/java/org/eclipse/dash/licenses/tests/LicenseSupportTests.java @@ -16,17 +16,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import com.google.inject.Guice; -import com.google.inject.Injector; - class LicenseSupportTests { private LicenseSupport licenseSupport; @BeforeEach void setup() { - Injector injector = Guice.createInjector(new TestLicenseToolModule()); - licenseSupport = injector.getInstance(LicenseSupport.class); + TestLicenseToolModule testLicenseToolModule = new TestLicenseToolModule(); + licenseSupport = testLicenseToolModule.getLicenseService(); } /** diff --git a/core/src/test/java/org/eclipse/dash/licenses/tests/util/TestLicenseToolModule.java b/core/src/test/java/org/eclipse/dash/licenses/tests/util/TestLicenseToolModule.java index ebcbc948..7372585d 100644 --- a/core/src/test/java/org/eclipse/dash/licenses/tests/util/TestLicenseToolModule.java +++ b/core/src/test/java/org/eclipse/dash/licenses/tests/util/TestLicenseToolModule.java @@ -18,46 +18,36 @@ import java.util.function.Consumer; import java.util.stream.Collectors; -import org.eclipse.dash.licenses.ILicenseDataProvider; import org.eclipse.dash.licenses.ISettings; -import org.eclipse.dash.licenses.LicenseChecker; -import org.eclipse.dash.licenses.LicenseSupport; import org.eclipse.dash.licenses.clearlydefined.ClearlyDefinedSupport; +import org.eclipse.dash.licenses.context.BaseLicenseToolModule; import org.eclipse.dash.licenses.foundation.EclipseFoundationSupport; import org.eclipse.dash.licenses.http.IHttpClientService; -import com.google.inject.AbstractModule; -import com.google.inject.multibindings.Multibinder; - import jakarta.json.Json; import jakarta.json.JsonArray; import jakarta.json.JsonReader; import jakarta.json.JsonString; -public class TestLicenseToolModule extends AbstractModule { +public class TestLicenseToolModule extends BaseLicenseToolModule { - private ISettings settings; + private static ISettings settings = new ISettings() { + }; public TestLicenseToolModule() { - this.settings = new ISettings() { - }; - } - - @Override - protected void configure() { - bind(ISettings.class).toInstance(settings); - bind(IHttpClientService.class).toInstance(getHttpClientService()); - bind(LicenseChecker.class).toInstance(new LicenseChecker()); - bind(LicenseSupport.class).toInstance(new LicenseSupport()); - - var licenseDataProviders = Multibinder.newSetBinder(binder(), ILicenseDataProvider.class); - licenseDataProviders.addBinding().toInstance(new EclipseFoundationSupport() { + super(settings); + bindLicenseDataProviders(new EclipseFoundationSupport() { @Override public int getWeight() { return 100; } }); - licenseDataProviders.addBinding().toInstance(new ClearlyDefinedSupport()); + bindLicenseDataProviders(new ClearlyDefinedSupport()); + } + + @Override + protected IHttpClientService newHttpClient() { + return getHttpClientService(); } public IHttpClientService getHttpClientService() { @@ -208,4 +198,5 @@ public boolean remoteFileExists(String url) { } }; } + } diff --git a/maven-plugin/src/main/java/org/eclipse/dash/licenses/maven/LicenseCheckMojo.java b/maven-plugin/src/main/java/org/eclipse/dash/licenses/maven/LicenseCheckMojo.java index e5f1b205..57860513 100644 --- a/maven-plugin/src/main/java/org/eclipse/dash/licenses/maven/LicenseCheckMojo.java +++ b/maven-plugin/src/main/java/org/eclipse/dash/licenses/maven/LicenseCheckMojo.java @@ -226,16 +226,16 @@ public void execute() throws MojoExecutionException, MojoFailureException { NeedsReviewCollector needsReviewCollector = new NeedsReviewCollector(); collectors.add(needsReviewCollector); - Injector injector = Guice.createInjector(new LicenseToolModule(settings, createProxySettings())); + LicenseToolModule module=new LicenseToolModule(settings, createProxySettings()); if (settings.getProjectId() != null) { - var validator = injector.getInstance(EclipseProjectIdValidator.class); + var validator = new EclipseProjectIdValidator(module); if (!validator.validate(settings.getProjectId(), message -> getLog().error(message))) { throw new MojoExecutionException("Invalid project id."); } } - LicenseChecker checker = injector.getInstance(LicenseChecker.class); + LicenseChecker checker = module.getLicenseChecker(); summary.getParentFile().mkdirs(); reviewSummary.getParentFile().mkdirs(); @@ -247,7 +247,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { collectors.add(new CSVCollector(summaryOut)); if (iplabToken != null && projectId != null) { - collectors.add(new CreateReviewRequestCollector(injector.getInstance(GitLabSupport.class), + collectors.add(new CreateReviewRequestCollector(module.getGitlab(), (id, url) -> reviewSummaryOut.println("[" + id + "](" + url + ")"))); } else if (iplabToken != null) { getLog().info(