Skip to content

Commit

Permalink
#1054 LIC: library-licensed-separately: support license import
Browse files Browse the repository at this point in the history
support actual license import

Signed-off-by: eparovyshnaya <[email protected]>
  • Loading branch information
eparovyshnaya committed Feb 28, 2022
1 parent 6ec463c commit 390a8e5
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.lic.base/schema/library.exsd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

</documentation>
<appinfo>
<meta.attribute kind="java" basedOn=":org.eclipse.passage.lic.internal.base.access.DelegatedLicensingService"/>
<meta.attribute kind="java" basedOn=":org.eclipse.passage.lic.internal.base.access.Library"/>
</appinfo>
</annotation>
</attribute>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* 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.base.diagnostic.code;

import org.eclipse.passage.lic.api.diagnostic.TroubleCode;
import org.eclipse.passage.lic.internal.base.i18n.DiagnosticCodeMessages;

/**
*
* @since 2.3
*/
public final class ForeignLicense extends TroubleCode {

public ForeignLicense() {
super(413, DiagnosticCodeMessages.getString("ForeignLicense.explanation")); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
* <li>411 - no data of a particular type found (info)</li>
* <li>412 - a licensing agreement has not been actively accepted by the product
* user</li>
* <li>413 - foreign license: suggested license file cannot be read with the
* product/library key (bearable)
* </ul>
* </li>
* <li>5xx - reserved</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;

import org.eclipse.passage.lic.api.LicensedProduct;
Expand All @@ -31,16 +32,21 @@
import org.eclipse.passage.lic.base.access.SumOfCertificates;
import org.eclipse.passage.lic.base.diagnostic.SumOfLists;

/**
* Set of libraries registered for the the product
*
* @see Library
*/
public final class Libraries {

private final List<DelegatedLicensingService> libraries;
private final List<Library> libraries;
private final Supplier<LicensedProduct> owner;

public Libraries(Supplier<List<DelegatedLicensingService>> libraries, Supplier<LicensedProduct> owner) {
public Libraries(Supplier<List<Library>> libraries, Supplier<LicensedProduct> owner) {
this(libraries.get(), owner);
}

public Libraries(List<DelegatedLicensingService> libraries, Supplier<LicensedProduct> owner) {
public Libraries(List<Library> libraries, Supplier<LicensedProduct> owner) {
this.libraries = libraries;
this.owner = owner;
}
Expand All @@ -55,8 +61,7 @@ public LicensedProduct product() {

public Optional<ServiceInvocationResult<ExaminationCertificate>> assess() {
return libraries.stream()//
.map(DelegatedLicensingService::assess)
.reduce(new BaseServiceInvocationResult.Sum<>(new SumOfCertificates()));
.map(Library::assess).reduce(new BaseServiceInvocationResult.Sum<>(new SumOfCertificates()));
}

public Optional<ServiceInvocationResult<List<AgreementAcceptanceService>>> agreementsServices(
Expand All @@ -69,14 +74,15 @@ public Optional<ServiceInvocationResult<List<AgreementAcceptanceService>>> agree

public Optional<ServiceInvocationResult<List<LicenseReadingService>>> licenseReadingServices() {
return libraries.stream()//
.map(DelegatedLicensingService::licenseReadingService)//
.map(Library::licenseReadingService)//
.map(this::enlisted)//
.reduce(sum());
}

public void installLicense(Path license) throws IOException {
// TODO Auto-generated method stub

public Optional<ServiceInvocationResult<Boolean>> installLicense(Path license) throws IOException {
return libraries.stream()//
.map(library -> library.installLicense(license))//
.reduce(new Sum<Boolean>(or()));
}

private <T> ServiceInvocationResult<List<T>> enlisted(ServiceInvocationResult<T> origin) {
Expand All @@ -88,4 +94,8 @@ private <T> Sum<List<T>> sum() {
return new Sum<List<T>>(new SumOfLists<T>());
}

private BinaryOperator<Boolean> or() {
return (first, second) -> first || second;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.base.access;

import java.io.IOException;
import java.nio.file.Path;

import org.eclipse.passage.lic.api.LicensedProduct;
Expand All @@ -22,8 +21,12 @@
import org.eclipse.passage.lic.api.agreements.AgreementToAccept;
import org.eclipse.passage.lic.api.conditions.mining.LicenseReadingService;
import org.eclipse.passage.lic.api.restrictions.ExaminationCertificate;
import org.eclipse.passage.lic.base.diagnostic.code.ForeignLicense;

/**
* <p>
* Represent a library's licensing aspect for an owning product.
* </p>
* <p>
* Implementation must be stateless: service is to be instantiated as many times
* as it is appealed to, no caching to perform for scanned extensions.
Expand Down Expand Up @@ -52,27 +55,33 @@
*
* <ul>
*
* <li>1.1. supply it's own assessment results when asked:
* {@code implementing access()}</li>
* <li>1.1. supply it's own assessment results when asked: {@code access()}</li>
*
* <li>1.2. facilitate acceptance of a license agreement, in case it demands
* some for acceptance:
* {@code agreementsService(AgreementToAccept agreement)}</li>
*
* <li>2.1. supply a {@linkplain LicenseReadingService} that will read licensing
* {@linkplain Condition}s from a given file, in case it can be treated a this
* {@linkplain Condition}s from a license file, in case it can be treated a this
* library's license</li>
*
* <li>2.2. provide a way to install a license to path configured for the
* library as license residence.</li>
* <li>2.2. provide a way to install a license (it defined to be this library's
* license) to path configured for the library as <i>license residence</i>:
* {@code installLicense(Path license)}.</li>
*
* </ul>
*
*/
public interface DelegatedLicensingService extends PassageLicenseCoverage {
public interface Library extends PassageLicenseCoverage {

/**
* Library represents its own <i>product</i>.
*/
LicensedProduct product();

/**
* Request a library to perform full license coverage assessment.
*/
@Override
ServiceInvocationResult<ExaminationCertificate> assess();

Expand All @@ -90,10 +99,28 @@ public interface DelegatedLicensingService extends PassageLicenseCoverage {
ServiceInvocationResult<LicenseReadingService> licenseReadingService();

/**
* <p>
* If the given {@code license} relates to the library, it should install to the
* license residence, configured for this library's
* {@code access cycle configuration}
* </p>
*
* <p>
* If the {@code license} file does not belong to the library, report negative
* result with bearable diagnostic of {@linkplain ForeignLicense} trouble code.
* </p>
* <p>
* If the {@code license} file can be read, but was not imported for some
* reason, report negative result with severe trouble in diagnostic.
* </p>
* <p>
* In case the {@code license} has been actually installed, report positive
* result, optionally with whatever informative diagnostic.
* </p>
*
* @return diagnosed result of installation: whether installation of the
* {@code license} has been actually performed or not
*/
void installLicense(Path license) throws IOException;
ServiceInvocationResult<Boolean> installLicense(Path license);

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ InvalidLicenseAttendantFile.license_expired=Invalid content of a license attenda
AbsentLicenseAttendantFile.license_expired=Expected license attendant file is not found
TentativeAccess.explanation=Feature is not covered by a license, but in the same time it's usage is not severely restricted; thus tentative access can be granted.
NoDataOfType.explanation=No data of type found
ForeignLicense.explanation=License file does not belong to the product/library
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
import java.util.List;
import java.util.function.Supplier;

import org.eclipse.passage.lic.internal.base.access.DelegatedLicensingService;
import org.eclipse.passage.lic.internal.base.access.Library;
import org.eclipse.passage.lic.internal.equinox.ServiceExtensions;

@SuppressWarnings("restriction")
public final class RegisteredLibraries implements Supplier<List<DelegatedLicensingService>> {
public final class RegisteredLibraries implements Supplier<List<Library>> {

@Override
public List<DelegatedLicensingService> get() {
return new ServiceExtensions<DelegatedLicensingService>(//
public List<Library> get() {
return new ServiceExtensions<Library>(//
"org.eclipse.passage.lic.base", //$NON-NLS-1$
"library", //$NON-NLS-1$
DelegatedLicensingService.class)//
Library.class)//
.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.jface.dialogs.licensing;

import java.io.IOException;
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.equinox.LicenseReadingServiceRequest;
Expand All @@ -50,6 +54,7 @@
public final class ImportLicenseDialog extends NotificationDialog {

private final DateTimeFormatter dates = DateTimeFormatter.ofPattern("dd-MM-yyyy"); //$NON-NLS-1$
private Libraries libraries = null;
private ButtonConfig action;
private Text path;

Expand Down Expand Up @@ -147,7 +152,7 @@ private void loadLicense(String file) {
ServiceInvocationResult<Collection<ConditionPack>> packs = new LicenseConditions(//
Paths.get(file), //
new LicenseReadingServiceRequest(), //
new Libraries(new RegisteredLibraries(), product::get)).get();
libraries()).get();
if (!packs.data().isPresent()) {
reportError(packs.diagnostic());
return;
Expand Down Expand Up @@ -196,13 +201,44 @@ private void doLicenseImport() {
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()));
okPressed();
}

private Libraries libraries() {
if (libraries == null) {
Optional<LicensedProduct> product = product();
if (!product.isPresent()) {
return null;
}
libraries = new Libraries(new RegisteredLibraries(), product::get);
}
return libraries;
}

private void doLicenseImport(Path license, LicensedProduct product) {
try {
new ExternalLicense(product.get()).install(Paths.get(path.getText().trim()));
} catch (IOException e) {
installLibraryLicense(license);
new ExternalLicense(product).install(license);
} catch (Exception e) {
setErrorMessage(
String.format(ImportLicenseDialogMessages.ImportLicenseDialog_io_error, e.getLocalizedMessage()));
}
okPressed();
}

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$
}
}

}

0 comments on commit 390a8e5

Please sign in to comment.