Skip to content

Commit

Permalink
Merge pull request #1061 from eclipse-passage/1059
Browse files Browse the repository at this point in the history
1059
  • Loading branch information
eparovyshnaya authored Mar 8, 2022
2 parents 9b9fcaa + fa96b5e commit e623a8b
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* 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.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;

/**
* <p>
* Collecting conditions from set of licenses, dedicated for different parties
* (product or its libraries), is accompanied by multiple <i>cannot read this
* license</i> denials, as practically every license is tried against every
* party.
* </p>
* <p>
* These denials are to be treated as <i>bearable</i> as do not contain signs of
* failure.
* </p>
*/
final class CalmedDown implements Supplier<ServiceInvocationResult<Collection<ConditionPack>>> {

private final ServiceInvocationResult<Collection<ConditionPack>> original;

CalmedDown(ServiceInvocationResult<Collection<ConditionPack>> original) {
this.original = original;
}

@Override
public ServiceInvocationResult<Collection<ConditionPack>> get() {
TroubleCode morsel = new ServiceFailedOnMorsel();
List<Trouble> 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<Trouble> severe(List<Trouble> calmed) {
List<Trouble> severe = new ArrayList<>(original.diagnostic().severe());
severe.removeAll(calmed);
return severe;
}

private List<Trouble> bearable(List<Trouble> calmed) {
List<Trouble> bearable = new ArrayList<>(original.diagnostic().bearable());
bearable.addAll(calmed);
return bearable;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* </p>
* <p>
* To be used strictly outside of Access Cycle.
* </p>
*/
public final class LicenseConditions implements Supplier<ServiceInvocationResult<Collection<ConditionPack>>> {

Expand All @@ -53,7 +56,7 @@ public LicenseConditions(//

@Override
public ServiceInvocationResult<Collection<ConditionPack>> get() {
return sum().apply(fromProduct(), fromLibraries());
return new CalmedDown(sum().apply(fromProduct(), fromLibraries())).get();
}

private ServiceInvocationResult<Collection<ConditionPack>> fromProduct() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,21 @@
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;
import java.util.Optional;
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;
Expand Down Expand Up @@ -135,7 +130,14 @@ private void browseAndLoad() {
private List<Path> 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<Path> licenses = new AllLicensesFromFolder(folder).get();
path.setData(licenses);
return licenses;
}

private void loadLicense(List<Path> files) {
Expand Down Expand Up @@ -188,13 +190,17 @@ private Optional<LicensedProduct> product() {
return product.data();
}

@SuppressWarnings("unchecked")
private void doLicenseImport() {
Optional<LicensedProduct> product = product();
if (!product.isPresent()) {
return;
}
List<Path> 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<Path>) licenses, product.get(), libraries, this::setErrorMessage).install();
okPressed();
}

Expand All @@ -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<ServiceInvocationResult<Boolean>> result = libraries.installLicense(license);
if (!result.isPresent()) {
return; // no libraries
}
ServiceInvocationResult<Boolean> 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$
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Path> licenses;
private final Libraries libraries;
private final Consumer<String> error;
private final Optional<LicenseReadingService> service;

LicenseSet(List<Path> licenses, LicensedProduct product, Libraries libraries, Consumer<String> error) {
this.licenses = licenses;
this.product = product;
this.libraries = libraries;
this.error = error;
this.service = productLicenseReadingService();
}

private Optional<LicenseReadingService> productLicenseReadingService() {
return miner().map(miner -> new BaseLicenseReadingService(product, miner));
}

private Optional<MiningEquipment> 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<ServiceInvocationResult<Boolean>> result = libraries.installLicense(license);
if (!result.isPresent()) {
return; // no libraries
}
ServiceInvocationResult<Boolean> 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);
}
}

0 comments on commit e623a8b

Please sign in to comment.