diff --git a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java index 8feee64e9..a87a8b142 100644 --- a/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java +++ b/bundles/org.eclipse.passage.loc.agreements.emfforms/src/org/eclipse/passage/loc/agreements/emfforms/renderers/AgreementFileRenderer.java @@ -32,9 +32,7 @@ import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding; import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider; import org.eclipse.passage.lic.agreements.model.api.Agreement; -import org.eclipse.passage.lic.api.LicensingException; -import org.eclipse.passage.loc.internal.api.workspace.Agreements; -import org.eclipse.passage.loc.internal.equinox.OperatorGearAware; +import org.eclipse.passage.loc.internal.equinox.AgreementsService; import org.eclipse.passage.loc.workbench.emfforms.renderers.TextWithButtonRenderer; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -90,7 +88,7 @@ private String reside(File file) throws Exception { // rename in already defined name if any: // String name = definedName().orElse(file.getName()); String name = file.getName(); - agreements().located(name).write(Files.readAllBytes(file.toPath())); + new AgreementsService().get().located(name).write(Files.readAllBytes(file.toPath())); return name; } @@ -127,15 +125,6 @@ private Optional definedName() { } } - private Agreements agreements() throws LicensingException { - Optional service = new OperatorGearAware() - .withGear(gear -> Optional.of(gear.workspace().agreements())); - if (!service.isPresent()) { - throw new LicensingException("There is no Agreements service supplied by Operator Workspace"); //$NON-NLS-1$ - } - return service.get(); - } - private Optional agreement() { try { IObservableValue value = getModelValue(); diff --git a/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/equinox/AgreementsService.java b/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/equinox/AgreementsService.java new file mode 100644 index 000000000..f570d7f4e --- /dev/null +++ b/bundles/org.eclipse.passage.loc.api/src/org/eclipse/passage/loc/internal/equinox/AgreementsService.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * 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.equinox; + +import java.util.Optional; + +import org.eclipse.passage.lic.api.LicensingException; +import org.eclipse.passage.loc.internal.api.workspace.Agreements; + +public final class AgreementsService { + + public Agreements get() throws LicensingException { + Optional service = new OperatorGearAware() + .withGear(gear -> Optional.of(gear.workspace().agreements())); + if (!service.isPresent()) { + throw new LicensingException("There is no Agreements service supplied by Operator Workspace"); //$NON-NLS-1$ + } + return service.get(); + } + +} diff --git a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java index f509b6c78..8a1f0562f 100644 --- a/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java +++ b/bundles/org.eclipse.passage.loc.licenses.core/src/org/eclipse/passage/loc/internal/licenses/core/IssuePersonalLicense.java @@ -58,7 +58,12 @@ final class IssuePersonalLicense { } ServiceInvocationResult issue(Supplier template) { - PersonalLicensePack license = adjsut(signed(EcoreUtil.copy(template.get()))); + PersonalLicensePack license = new Builder(template.get())// + .adjusted()// + .guarded()// + .withSignature()// + .withAgreements()// + .build(); Optional errors = new ErrorMessages().apply(license); if (errors.isPresent()) { return new BaseServiceInvocationResult<>(new Trouble(new LicenseValidationFailed(), errors.get())); @@ -69,16 +74,12 @@ ServiceInvocationResult issue(Supplier templ return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), LicensesCoreMessages.LicenseOperatorServiceImpl_error_io, e)); } - LicensedProduct product = new BaseLicensedProduct(// - license.getLicense().getProduct().getIdentifier(), // - license.getLicense().getProduct().getVersion()); + LicensedProduct product = product(license); Path path = new UserHomeProductResidence(product).get(); Path decrypted; try { - decrypted = new PersistedDecoded(path, license)// - .write(license.getLicense().getIdentifier() + new PassageFileExtension.LicenseDecrypted().get()); - events.postEvent(OperatorLicenseEvents.decodedIssued(decrypted.toString())); + decrypted = decrypted(license, path); } catch (LicensingException e) { return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), LicensesCoreMessages.LicenseOperatorServiceImpl_failed_to_save_decoded, e)); @@ -86,28 +87,73 @@ ServiceInvocationResult issue(Supplier templ Path encrypted; try { - encrypted = new PersistedEncoded(product, decrypted, new ProductPassword(products, operator))// - .write(license.getLicense().getIdentifier() + new PassageFileExtension.LicenseEncrypted().get()); + encrypted = encrypted(license, product, decrypted); } catch (LicensingException e) { return new BaseServiceInvocationResult<>(new Trouble(new LicenseIssuingFailed(), LicensesCoreMessages.LicenseOperatorServiceImpl_export_error, e)); } - events.postEvent(OperatorLicenseEvents.encodedIssued(encrypted.toString())); + return result(license, decrypted, encrypted); + } + + private BaseServiceInvocationResult result(PersonalLicensePack license, Path decrypted, + Path encrypted) { return new BaseServiceInvocationResult<>(new BaseIssuedLicense(license, encrypted, decrypted)); } - private PersonalLicensePack adjsut(PersonalLicensePack license) { - Date issueDate = new Date(); - license.getLicense().setIdentifier(UUID.randomUUID().toString()); - license.getLicense().setIssueDate(issueDate); - new AssignGrantIdentifiers().accept(license); - new PersonalLicenseIssuingProtection().accept(license); - return license; + private Path encrypted(PersonalLicensePack license, LicensedProduct product, Path decrypted) + throws LicensingException { + Path encrypted = new PersistedEncoded(product, decrypted, new ProductPassword(products, operator))// + .write(license.getLicense().getIdentifier() + new PassageFileExtension.LicenseEncrypted().get()); + events.postEvent(OperatorLicenseEvents.encodedIssued(encrypted.toString())); + return encrypted; } - private PersonalLicensePack signed(PersonalLicensePack pack) { - new LicenseSignature().accept(pack.getLicense()); - return pack; + private Path decrypted(PersonalLicensePack license, Path path) throws LicensingException { + Path decrypted = new PersistedDecoded(path, license)// + .write(license.getLicense().getIdentifier() + new PassageFileExtension.LicenseDecrypted().get()); + events.postEvent(OperatorLicenseEvents.decodedIssued(decrypted.toString())); + return decrypted; } + private BaseLicensedProduct product(PersonalLicensePack license) { + return new BaseLicensedProduct(// + license.getLicense().getProduct().getIdentifier(), // + license.getLicense().getProduct().getVersion()); + } + + private static final class Builder { + + private final PersonalLicensePack pack; + + Builder(PersonalLicensePack template) { + this.pack = EcoreUtil.copy(template); + } + + Builder withSignature() { + new LicenseSignature().accept(pack.getLicense()); + return this; + } + + Builder withAgreements() { + + // TODO + return this; + } + + Builder adjusted() { + pack.getLicense().setIdentifier(UUID.randomUUID().toString()); + pack.getLicense().setIssueDate(new Date()); + new AssignGrantIdentifiers().accept(pack); + return this; + } + + Builder guarded() { + new PersonalLicenseIssuingProtection().accept(pack); + return this; + } + + PersonalLicensePack build() { + return pack; + } + } }