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

575166 License Agreement management: license issuing #878

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -7,19 +7,20 @@ Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.osgi.services;bundle-version="0.0.0",
Require-Bundle: org.apache.logging.log4j;bundle-version="2.8.2",
org.eclipse.osgi;bundle-version="0.0.0",
org.eclipse.osgi.services;bundle-version="0.0.0",
org.eclipse.passage.lic.base;bundle-version="0.0.0",
org.eclipse.passage.lic.email;bundle-version="0.0.0",
org.eclipse.passage.lic.emf;bundle-version="0.0.0",
org.eclipse.passage.lic.equinox;bundle-version="0.0.0",
org.eclipse.passage.lic.keys.model;bundle-version="0.0.0",
org.eclipse.passage.lic.licenses.model;bundle-version="0.0.0";visibility:=reexport,
org.eclipse.passage.lic.users.model;bundle-version="0.0.0",
org.eclipse.passage.loc.agreements.core;bundle-version="0.0.0",
org.eclipse.passage.loc.api;bundle-version="0.0.0",
org.eclipse.passage.loc.products.core;bundle-version="0.0.0",
org.eclipse.passage.loc.users.core;bundle-version="0.0.0",
org.eclipse.passage.lic.base;bundle-version="0.0.0",
org.apache.logging.log4j;bundle-version="2.8.2",
org.eclipse.passage.lic.keys.model;bundle-version="0.0.0"
org.eclipse.passage.loc.users.core;bundle-version="0.0.0"
Export-Package: org.eclipse.passage.loc.internal.licenses;
x-friends:="org.eclipse.passage.loc.licenses.emfforms,
org.eclipse.passage.loc.licenses.ui,
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
<service>
<provide interface="org.eclipse.passage.loc.internal.api.OperatorLicenseService"/>
</service>
<reference bind="bindAgreementRegistry" interface="org.eclipse.passage.loc.internal.agreements.AgreementRegistry" name="AgreementRegistry" unbind="unbindAgreementRegistry"/>
<reference bind="bindEnvironmentInfo" interface="org.eclipse.osgi.service.environment.EnvironmentInfo" name="EnvironmentInfo" unbind="unbindEnvironmentInfo"/>
<reference bind="bindEventAdmin" interface="org.osgi.service.event.EventAdmin" name="EventAdmin" unbind="unbindEventAdmin"/>
<reference bind="bindLicenseRegistry" interface="org.eclipse.passage.loc.internal.licenses.LicenseRegistry" name="LicenseRegistry" unbind="unbindLicenseRegistry"/>
Original file line number Diff line number Diff line change
@@ -30,40 +30,51 @@
import org.eclipse.passage.lic.base.io.UserHomeProductResidence;
import org.eclipse.passage.lic.emf.validation.ErrorMessages;
import org.eclipse.passage.lic.internal.licenses.model.AssignGrantIdentifiers;
import org.eclipse.passage.lic.licenses.LicensePlanDescriptor;
import org.eclipse.passage.lic.licenses.model.api.PersonalLicensePack;
import org.eclipse.passage.loc.internal.agreements.AgreementRegistry;
import org.eclipse.passage.loc.internal.api.IssuedLicense;
import org.eclipse.passage.loc.internal.api.OperatorLicenseEvents;
import org.eclipse.passage.loc.internal.api.OperatorProductService;
import org.eclipse.passage.loc.internal.licenses.LicenseRegistry;
import org.eclipse.passage.loc.internal.licenses.core.i18n.LicensesCoreMessages;
import org.eclipse.passage.loc.internal.licenses.core.issue.PersonalLicenseIssuingProtection;
import org.eclipse.passage.loc.internal.products.ProductRegistry;
import org.eclipse.passage.loc.licenses.trouble.code.LicenseAgreementsAttachFailed;
import org.eclipse.passage.loc.licenses.trouble.code.LicenseIssuingFailed;
import org.eclipse.passage.loc.licenses.trouble.code.LicenseValidationFailed;
import org.osgi.service.event.EventAdmin;

final class IssuePersonalLicense {

private final LicenseRegistry licenses;
private final AgreementRegistry agreements;
private final ProductRegistry products;
private final OperatorProductService operator;
private final EventAdmin events;

IssuePersonalLicense(LicenseRegistry licenses, ProductRegistry products, OperatorProductService operator,
EventAdmin events) {
IssuePersonalLicense(LicenseRegistry licenses, AgreementRegistry agreements, ProductRegistry products,
OperatorProductService operator, EventAdmin events) {
this.licenses = licenses;
this.agreements = agreements;
this.products = products;
this.operator = operator;
this.events = events;
}

ServiceInvocationResult<IssuedLicense> issue(Supplier<PersonalLicensePack> template) {
PersonalLicensePack license = new Builder(template.get())//
.adjusted()//
.guarded()//
.withSignature()//
.withAgreements()//
.build();
PersonalLicensePack license;
try {
license = new Builder(template.get())//
.adjusted()//
.guarded()//
.withSignature()//
.withAgreements()//
.build();
} catch (LicensingException e) {
return new BaseServiceInvocationResult<>(
new Trouble(new LicenseAgreementsAttachFailed(), e.getMessage(), e));
}
Optional<String> errors = new ErrorMessages().apply(license);
if (errors.isPresent()) {
return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors.get()));
@@ -121,7 +132,7 @@ private BaseLicensedProduct product(PersonalLicensePack license) {
license.getLicense().getProduct().getVersion());
}

private static final class Builder {
private final class Builder {

private final PersonalLicensePack pack;

@@ -134,9 +145,8 @@ Builder withSignature() {
return this;
}

Builder withAgreements() {

// TODO
Builder withAgreements() throws LicensingException {
new LiecnseAgreements(agreements).install(plan(), pack.getLicense());
return this;
}

@@ -155,5 +165,9 @@ Builder guarded() {
PersonalLicensePack build() {
return pack;
}

private LicensePlanDescriptor plan() {
return licenses.getLicensePlan(pack.getLicense().getPlan());
}
}
}
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import org.eclipse.passage.lic.licenses.model.api.FloatingLicenseAccess;
import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack;
import org.eclipse.passage.lic.licenses.model.api.PersonalLicensePack;
import org.eclipse.passage.loc.internal.agreements.AgreementRegistry;
import org.eclipse.passage.loc.internal.api.FloatingLicenseRequest;
import org.eclipse.passage.loc.internal.api.IssuedFloatingLicense;
import org.eclipse.passage.loc.internal.api.IssuedLicense;
@@ -44,6 +45,7 @@ public class LicenseOperatorServiceImpl implements OperatorLicenseService {
private ProductRegistry products;
private UserRegistry users;
private LicenseRegistry licenses;
private AgreementRegistry agreements;
private OperatorProductService operator;

@Reference
@@ -90,6 +92,17 @@ public void unbindLicenseRegistry(LicenseRegistry registry) {
}
}

@Reference
public void bindAgreementRegistry(AgreementRegistry registry) {
this.agreements = registry;
}

public void unbindAgreementRegistry(AgreementRegistry registry) {
if (Objects.equals(this.agreements, registry)) {
this.agreements = null;
}
}

@Reference
public void bindUserRegistry(UserRegistry registry) {
this.users = registry;
@@ -125,7 +138,7 @@ public ServiceInvocationResult<IssuedLicense> issueLicensePack(PersonalLicenseRe
Supplier<PersonalLicensePack> pack = (template instanceof PersonalLicensePack) //
? () -> PersonalLicensePack.class.cast(template)//
: () -> createLicensePack(request);
return new IssuePersonalLicense(licenses, products, operator, events).issue(pack);
return new IssuePersonalLicense(licenses, agreements, products, operator, events).issue(pack);
}

@Override
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*******************************************************************************
* 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.loc.internal.licenses.core;

import java.util.Collection;
import java.util.Optional;

import org.eclipse.passage.lic.agreements.AgreementDescriptor;
import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.io.Hashes;
import org.eclipse.passage.lic.api.io.HashesRegistry;
import org.eclipse.passage.lic.licenses.LicensePlanDescriptor;
import org.eclipse.passage.lic.licenses.model.api.AgreementData;
import org.eclipse.passage.lic.licenses.model.api.LicenseRequisites;
import org.eclipse.passage.lic.licenses.model.meta.LicensesFactory;
import org.eclipse.passage.loc.internal.agreements.AgreementRegistry;
import org.eclipse.passage.loc.internal.api.workspace.Agreements;
import org.eclipse.passage.loc.internal.equinox.AgreementsService;
import org.eclipse.passage.loc.internal.equinox.OperatorGearAware;
import org.eclipse.passage.loc.internal.licenses.core.i18n.LicensesCoreMessages;

@SuppressWarnings("restriction")
final class LiecnseAgreements {
eparovyshnaya marked this conversation as resolved.
Show resolved Hide resolved

private final AgreementRegistry registry;

LiecnseAgreements(AgreementRegistry registry) {
this.registry = registry;
}

void install(LicensePlanDescriptor plan, LicenseRequisites license) throws LicensingException {
Agreements service = new AgreementsService().get(); // TODO: cashed field
Hashes hashes = hashes();// TODO: cashed field
for (String identifier : plan.getAgreements()) {
installAgreement(license, registry.agreement(identifier), service, hashes);
}

}

private void installAgreement(LicenseRequisites license, AgreementDescriptor agreement, Agreements service,
Hashes hashes) throws LicensingException {
if (!service.exists(agreement.getFile())) {
throw new LicensingException(String.format(//
LicensesCoreMessages.LicenseOperatorServiceImpl_failed_to_find_agreement_file, //
service.located(agreement.getFile()).info(), //
agreement.getName()));
}
license.getAgreements().add(data(agreement, service, hashes));
}

private AgreementData data(AgreementDescriptor agreement, Agreements service, Hashes hashes)
throws LicensingException {
AgreementData data = LicensesFactory.eINSTANCE.createAgreementData();
data.setIdentifier(agreement.getIdentifier());
data.setName(agreement.getName());
data.setFile(agreement.getFile());
data.setContentType(agreement.getMime());
byte[] content = content(agreement, service);
data.setContent(content);
data.setHashAlgo(hashes.id().toString());
data.setHash(hashes.get(content));
return data;
}

private byte[] content(AgreementDescriptor agreement, Agreements service) throws LicensingException {
try {
return service.located(agreement.getFile()).content();
} catch (Exception e) {
throw new LicensingException(String.format(//
LicensesCoreMessages.LicenseOperatorServiceImpl_failed_to_attach_agreement, //
agreement.getName(), //
service.located(agreement.getFile()).info(), //
e));
}
}

private Hashes hashes() throws LicensingException {
Optional<HashesRegistry> service = new OperatorGearAware().withGear(gear -> Optional.of(gear.hashes()));
if (!service.isPresent()) {
throw new LicensingException("There is no HashesRegistry service supplied by Operator Gear"); //$NON-NLS-1$
}
Collection<Hashes> all = service.get().get().services();
if (all.isEmpty()) {
throw new LicensingException("There is no Hashes service supplied by Operator Gear"); //$NON-NLS-1$
}
return all.iterator().next();
}
}
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ public final class LicenseTroubleCodeMessages extends NLS {
public static String LicenseValidationFailed_explanation;
public static String LicenseIssuingFailed_explanation;
public static String LicenseIssuingIsPartial_explanation;
public static String LicenseAgreementAttachFailed_explanation;

static {
// initialize resource bundle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2020 ArSysOp and others
# Copyright (c) 2020, 2021 ArSysOp and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
@@ -13,3 +13,4 @@
LicenseIssuingFailed_explanation=License issuing failed
LicenseValidationFailed_explanation=License information contains unavoidable errors
LicenseIssuingIsPartial_explanation=License issuing is partially completed
LicenseAgreementAttachFailed_explanation=Failed to attach agreement to license pack
Original file line number Diff line number Diff line change
@@ -48,6 +48,8 @@ public final class LicensesCoreMessages extends NLS {
public static String LicenseOperatorServiceImpl_floating_save_encoded_failed;
public static String LicenseOperatorServiceImpl_floating_save_product_key;
public static String LicenseOperatorServiceImpl_floating_save_encoded_file_error;
public static String LicenseOperatorServiceImpl_failed_to_find_agreement_file;
public static String LicenseOperatorServiceImpl_failed_to_attach_agreement;

static {
// initialize resource bundle
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@ EmfObjectPersisted_failed=Failed to persist emf object %s to %s
LicenseOperatorServiceImpl_error_io=Failed on I/O operation
LicenseOperatorServiceImpl_export_error=License Pack export error
LicenseOperatorServiceImpl_export_success=License pack exported successfully: \n\n %s \n
LicenseOperatorServiceImpl_failed_to_find_agreement_file=Content file [%s] for agreement [%s] is not found
LicenseOperatorServiceImpl_failed_to_attach_agreement=Failed to attach agreement [%s] with content [%s] to license
LicenseOperatorServiceImpl_failed_to_save_decoded=Failed on decoded license file saving
LicenseOperatorServiceImpl_status_invalid_licensing_request=Invalid Licensing Request
LicenseOperatorServiceImpl_w_no_encoding=License Pack has been exported without encoding: \n\n %s \n
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2020, 2021 ArSysOp
eparovyshnaya marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.loc.licenses.trouble.code;

import org.eclipse.passage.lic.api.diagnostic.TroubleCode;
import org.eclipse.passage.loc.internal.licenses.core.i18n.LicenseTroubleCodeMessages;

public final class LicenseAgreementsAttachFailed extends TroubleCode {

public LicenseAgreementsAttachFailed() {
super(903, LicenseTroubleCodeMessages.LicenseAgreementAttachFailed_explanation);
}

}