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

remove com.google.inject #385

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
35 changes: 25 additions & 10 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@
<artifactId>eclipse-api-for-java</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
Expand Down Expand Up @@ -167,6 +157,31 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<id>jar</id>
<configuration>
<bnd><![CDATA[
Bundle-Copyright: Copyright (c) (2019) Contributors to the Eclipse Foundation.
Git-Descriptor: ${system-allow-fail;git describe --dirty --always --abbrev=9}
Git-SHA: ${system-allow-fail;git rev-list -1 --no-abbrev-commit HEAD}
SPDX-License-Identifier: ${project.licenses[0].name}
-noextraheaders: true
-reproducible: true

Export-Package: org.eclipse.dash.licenses*
]]></bnd>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IContentId> ids, Consumer<IContentData> consumer);

default int getWeight() {
return 50;
}
Expand Down
48 changes: 26 additions & 22 deletions core/src/main/java/org/eclipse/dash/licenses/LicenseChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILicenseDataProvider> licenseDataProviders;

private LicenseToolContext ctx;

public LicenseChecker(LicenseToolContext context) {
this.ctx = context;

}

private Stream<ILicenseDataProvider> 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()));
}

/**
Expand All @@ -40,22 +41,25 @@ private Stream<ILicenseDataProvider> getLicenseDataProviders() {
* @return
*/
public Map<IContentId, LicenseData> getLicenseData(Collection<IContentId> ids) {
Map<IContentId, LicenseData> licenseData = ids.stream().map(id -> new LicenseData(id)).collect(
Collectors.toMap(LicenseData::getId, Function.identity(), (existing, replacement) -> existing));
Map<IContentId, LicenseData> licenseData = ids
.stream()
.map(id -> new LicenseData(id))
.collect(
Collectors.toMap(LicenseData::getId, Function.identity(), (existing, replacement) -> existing));

getLicenseDataProviders().forEach(provider -> {
new Batchifier<IContentId>()
.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<IContentId>().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;
Expand Down
32 changes: 18 additions & 14 deletions core/src/main/java/org/eclipse/dash/licenses/LicenseSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,25 +28,30 @@ public class LicenseSupport {

final Logger logger = LoggerFactory.getLogger(LicenseSupport.class);

@Inject
ISettings settings;
@Inject
IHttpClientService httpClientService;

private Map<String, String> 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<String, String> getApprovedLicenses() {
if (approvedLicenses == null) {

ctx.getHttpClientService().get(ctx.getSettings().getApprovedLicensesUrl(), "application/json", response -> {
approvedLicenses = readApprovedLicenses(new InputStreamReader(response));
});
}
return approvedLicenses;
}

private Map<String, String> getApprovedLicenses(Reader contentReader) {
private Map<String, String> readApprovedLicenses(Reader contentReader) {
JsonReader reader = Json.createReader(contentReader);
JsonObject read = (JsonObject) reader.read();

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> validTypes;
private Set<String> validProviders;

private LicenseToolContext ctx;

/**
* The ClearlyDefined API expects a flat array of ids in JSON format in the
* payload of the POST request.
Expand Down Expand Up @@ -141,8 +134,9 @@ private void doQueryClearlyDefined(List<IContentId> 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();
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -266,4 +259,10 @@ public ClearlyDefinedResponseException() {
super();
}
}

@Override
public void init(LicenseToolContext context) {
this.ctx = context;
bootstrap();
}
}
31 changes: 13 additions & 18 deletions core/src/main/java/org/eclipse/dash/licenses/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<IResultsCollector> collectors = new ArrayList<>();

Expand All @@ -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 -> {
Expand Down Expand Up @@ -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);
}
Expand Down
Loading