From 4b9f665b7c7a22e3c8d03a9136c829dd4fddf560 Mon Sep 17 00:00:00 2001
From: Elena Parovyshnaia
Date: Tue, 8 Mar 2022 15:45:09 +0300
Subject: [PATCH 1/2] #1059 Prepare sourcebase for 2.3.0 release
improve libraries scenario: read all conditions from license pack, do
not treat denials as failures
---
.../internal/base/conditions/CalmedDown.java | 72 +++++++++++++++++++
.../base/conditions/LicenseConditions.java | 5 +-
2 files changed, 76 insertions(+), 1 deletion(-)
create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java
diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java
new file mode 100644
index 000000000..391b162cd
--- /dev/null
+++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java
@@ -0,0 +1,72 @@
+/*******************************************************************************
+ * Copyright (c) 2021 ArSysOp
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * https://www.eclipse.org/legal/epl-2.0/.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * ArSysOp - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.passage.lic.internal.base.conditions;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import org.eclipse.passage.lic.api.ServiceInvocationResult;
+import org.eclipse.passage.lic.api.conditions.ConditionPack;
+import org.eclipse.passage.lic.api.diagnostic.Trouble;
+import org.eclipse.passage.lic.api.diagnostic.TroubleCode;
+import org.eclipse.passage.lic.base.BaseServiceInvocationResult;
+import org.eclipse.passage.lic.base.diagnostic.BaseDiagnostic;
+import org.eclipse.passage.lic.base.diagnostic.code.ServiceFailedOnMorsel;
+
+/**
+ *
+ * Collecting conditions from set of licenses, dedicated for different parties
+ * (product or its libraries), is accompanied by multiple cannot read this
+ * license denials, as practically every license is tried against every
+ * party.
+ *
+ *
+ * These denials are to be treated as bearable as do not contain signs of
+ * failure.
+ *
+ */
+final class CalmedDown implements Supplier>> {
+
+ private final ServiceInvocationResult> original;
+
+ CalmedDown(ServiceInvocationResult> original) {
+ this.original = original;
+ }
+
+ @Override
+ public ServiceInvocationResult> get() {
+ TroubleCode morsel = new ServiceFailedOnMorsel();
+ List calmed = original.diagnostic().severe().stream()//
+ .filter(trouble -> trouble.code().equals(morsel))//
+ .collect(Collectors.toList());
+ return new BaseServiceInvocationResult<>(//
+ new BaseDiagnostic(severe(calmed), bearable(calmed)), //
+ original.data());
+ }
+
+ private List severe(List calmed) {
+ List severe = new ArrayList<>(original.diagnostic().severe());
+ severe.removeAll(calmed);
+ return severe;
+ }
+
+ private List bearable(List calmed) {
+ List bearable = new ArrayList<>(original.diagnostic().bearable());
+ bearable.addAll(calmed);
+ return bearable;
+ }
+
+}
diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/LicenseConditions.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/LicenseConditions.java
index bb202bd63..e06cbdc89 100644
--- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/LicenseConditions.java
+++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/LicenseConditions.java
@@ -35,6 +35,9 @@
* A license file can belong either to a product under licensing, or to any
* library it exploits. Only appropriate component can read it's licenses.
*
+ *
+ * To be used strictly outside of Access Cycle.
+ *
*/
public final class LicenseConditions implements Supplier>> {
@@ -53,7 +56,7 @@ public LicenseConditions(//
@Override
public ServiceInvocationResult> get() {
- return sum().apply(fromProduct(), fromLibraries());
+ return new CalmedDown(sum().apply(fromProduct(), fromLibraries())).get();
}
private ServiceInvocationResult> fromProduct() {
From fa96b5ec641108e031eff9021a966b355e77e0ad Mon Sep 17 00:00:00 2001
From: Elena Parovyshnaia
Date: Tue, 8 Mar 2022 16:23:46 +0300
Subject: [PATCH 2/2] #1059 Prepare sourcebase for 2.3.0 release
improve libraries scenario: import only related linceses
---
.../internal/base/conditions/CalmedDown.java | 2 +-
.../licensing/ImportLicenseDialog.java | 46 +++-----
.../jface/dialogs/licensing/LicenseSet.java | 103 ++++++++++++++++++
3 files changed, 118 insertions(+), 33 deletions(-)
create mode 100644 bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/LicenseSet.java
diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java
index 391b162cd..70724a503 100644
--- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java
+++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/CalmedDown.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2021 ArSysOp
+ * Copyright (c) 2022 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
diff --git a/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/ImportLicenseDialog.java b/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/ImportLicenseDialog.java
index 2d8aedc16..0f2168b44 100644
--- a/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/ImportLicenseDialog.java
+++ b/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/ImportLicenseDialog.java
@@ -13,9 +13,7 @@
package org.eclipse.passage.lic.internal.jface.dialogs.licensing;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.time.format.DateTimeFormatter;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -23,16 +21,13 @@
import java.util.stream.Collectors;
import org.eclipse.passage.lic.api.LicensedProduct;
-import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.ServiceInvocationResult;
import org.eclipse.passage.lic.api.conditions.Condition;
import org.eclipse.passage.lic.api.conditions.ConditionPack;
import org.eclipse.passage.lic.api.conditions.ValidityPeriod;
import org.eclipse.passage.lic.api.diagnostic.Diagnostic;
import org.eclipse.passage.lic.base.conditions.BaseValidityPeriodClosed;
-import org.eclipse.passage.lic.base.diagnostic.DiagnosticExplained;
import org.eclipse.passage.lic.base.diagnostic.NoSevereErrors;
-import org.eclipse.passage.lic.base.io.ExternalLicense;
import org.eclipse.passage.lic.equinox.EquinoxPassage;
import org.eclipse.passage.lic.internal.base.access.Libraries;
import org.eclipse.passage.lic.internal.equinox.access.RegisteredLibraries;
@@ -135,7 +130,14 @@ private void browseAndLoad() {
private List browse() {
DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.OPEN | SWT.SHEET);
dialog.setText(ImportLicenseDialogMessages.ImportLicenseDialog_browse_dialog_title);
- return new AllLicensesFromFolder(dialog.open()).get();
+ String folder = dialog.open();
+ if (folder == null) {
+ return Collections.emptyList();
+ }
+ path.setText(folder);
+ List licenses = new AllLicensesFromFolder(folder).get();
+ path.setData(licenses);
+ return licenses;
}
private void loadLicense(List files) {
@@ -188,13 +190,17 @@ private Optional product() {
return product.data();
}
+ @SuppressWarnings("unchecked")
private void doLicenseImport() {
Optional product = product();
if (!product.isPresent()) {
return;
}
- List files = Arrays.asList(Paths.get(path.getText().trim())); // TODO: scan folder for *.licen-files
- files.forEach(file -> doLicenseImport(file, product.get()));
+ Object licenses = path.getData();
+ if (licenses == null) {
+ return;
+ }
+ new LicenseSet((List) licenses, product.get(), libraries, this::setErrorMessage).install();
okPressed();
}
@@ -209,28 +215,4 @@ private Libraries libraries() {
return libraries;
}
- private void doLicenseImport(Path license, LicensedProduct product) {
- try {
- installLibraryLicense(license);
- new ExternalLicense(product).install(license);
- } catch (Exception e) {
- setErrorMessage(
- String.format(ImportLicenseDialogMessages.ImportLicenseDialog_io_error, e.getLocalizedMessage()));
- }
- }
-
- private void installLibraryLicense(Path license) throws Exception {
- Optional> result = libraries.installLicense(license);
- if (!result.isPresent()) {
- return; // no libraries
- }
- ServiceInvocationResult status = result.get();
- Diagnostic diagnostic = status.diagnostic();
- System.out.println("Import license license: " + license); //$NON-NLS-1$
- System.out.println(new DiagnosticExplained(diagnostic).get());
- if (!new NoSevereErrors().test(diagnostic)) {
- throw new LicensingException(String.format("License file [%s] failed to be imported", license)); //$NON-NLS-1$
- }
- }
-
}
diff --git a/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/LicenseSet.java b/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/LicenseSet.java
new file mode 100644
index 000000000..017074e43
--- /dev/null
+++ b/bundles/org.eclipse.passage.lic.jface/src/org/eclipse/passage/lic/internal/jface/dialogs/licensing/LicenseSet.java
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * Copyright (c) 2022 ArSysOp
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * https://www.eclipse.org/legal/epl-2.0/.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * ArSysOp - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.passage.lic.internal.jface.dialogs.licensing;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+import java.util.function.Consumer;
+
+import org.eclipse.passage.lic.api.LicensedProduct;
+import org.eclipse.passage.lic.api.LicensingException;
+import org.eclipse.passage.lic.api.ServiceInvocationResult;
+import org.eclipse.passage.lic.api.conditions.mining.LicenseReadingService;
+import org.eclipse.passage.lic.api.conditions.mining.MiningEquipment;
+import org.eclipse.passage.lic.api.diagnostic.Diagnostic;
+import org.eclipse.passage.lic.base.conditions.mining.BaseLicenseReadingService;
+import org.eclipse.passage.lic.base.diagnostic.DiagnosticExplained;
+import org.eclipse.passage.lic.base.diagnostic.NoSevereErrors;
+import org.eclipse.passage.lic.base.io.ExternalLicense;
+import org.eclipse.passage.lic.equinox.SuppliedFrameworkAware;
+import org.eclipse.passage.lic.internal.base.access.Libraries;
+import org.eclipse.passage.lic.internal.jface.i18n.ImportLicenseDialogMessages;
+
+@SuppressWarnings("restriction")
+final class LicenseSet {
+ private final LicensedProduct product;
+ private final List licenses;
+ private final Libraries libraries;
+ private final Consumer error;
+ private final Optional service;
+
+ LicenseSet(List licenses, LicensedProduct product, Libraries libraries, Consumer error) {
+ this.licenses = licenses;
+ this.product = product;
+ this.libraries = libraries;
+ this.error = error;
+ this.service = productLicenseReadingService();
+ }
+
+ private Optional productLicenseReadingService() {
+ return miner().map(miner -> new BaseLicenseReadingService(product, miner));
+ }
+
+ private Optional miner() {
+ return new SuppliedFrameworkAware()
+ .withFramework(framework -> framework.accessCycleConfiguration().miningEquipment());
+ }
+
+ void install() {
+ licenses.forEach(file -> doLicenseImport(file));
+ }
+
+ private void doLicenseImport(Path license) {
+ try {
+ installLibraryLicense(license);
+ installProductLicense(license);
+ } catch (Exception e) {
+ error.accept(
+ String.format(ImportLicenseDialogMessages.ImportLicenseDialog_io_error, e.getLocalizedMessage()));
+ }
+ }
+
+ private void installProductLicense(Path license) throws IOException {
+ if (!productRelevantLicense(license)) {
+ return;
+ }
+ new ExternalLicense(product).install(license);
+ }
+
+ private void installLibraryLicense(Path license) throws Exception {
+ Optional> result = libraries.installLicense(license);
+ if (!result.isPresent()) {
+ return; // no libraries
+ }
+ ServiceInvocationResult status = result.get();
+ Diagnostic diagnostic = status.diagnostic();
+ System.out.println("Import license license: " + license); //$NON-NLS-1$
+ System.out.println(new DiagnosticExplained(diagnostic).get());
+ if (!new NoSevereErrors().test(diagnostic)) {
+ throw new LicensingException(String.format("License file [%s] failed to be imported", license)); //$NON-NLS-1$
+ }
+ }
+
+ private boolean productRelevantLicense(Path license) {
+ if (!service.isPresent()) {
+ return false;
+ }
+ return service.get().read(license).data()//
+ .map(conditions -> !conditions.isEmpty())//
+ .orElse(Boolean.FALSE);
+ }
+}