From 7ec4f2cff2ad36ec3e7d0e4dbb0b653f5845e2cf Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia <elena.parovyshnaya@gmail.com> Date: Tue, 31 May 2022 19:28:21 +0300 Subject: [PATCH 1/2] 1088 Passage Operator 2.4.0-M3 fails on startup make sure search-path suppliers return rightful path to search in --- .../mining/UserHomeResidentConditions.java | 9 +++- .../passage/lic/base/io/ExistingFolder.java | 42 ++++++++++++++++++ .../passage/lic/base/io/FileCollection.java | 20 ++++++--- .../eclipse/passage/lic/base/io/Settings.java | 24 +++++------ .../base/i18n/BaseMessages.properties | 2 +- .../ConfigurationResidentConditions.java | 9 +++- .../InstallationResidentConditions.java | 9 +++- .../lic/oshi/HardwareAssessmentService.java | 4 +- .../META-INF/MANIFEST.MF | 2 +- .../OSGI-INF/l10n/bundle.properties | 4 +- .../base/tests/io/ExistingFolderTest.java | 43 +++++++++++++++++++ .../base/tests/io/FileCollectionTest.java | 10 ++++- .../base/tests/io/NotExistingFolder.java | 30 +++++++++++++ 13 files changed, 175 insertions(+), 33 deletions(-) create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/ExistingFolder.java create mode 100644 tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/ExistingFolderTest.java create mode 100644 tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/NotExistingFolder.java diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/conditions/mining/UserHomeResidentConditions.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/conditions/mining/UserHomeResidentConditions.java index 35d41ec8c..b98871e3f 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/conditions/mining/UserHomeResidentConditions.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/conditions/mining/UserHomeResidentConditions.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 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 @@ -17,6 +17,7 @@ import org.eclipse.passage.lic.api.LicensedProduct; import org.eclipse.passage.lic.api.conditions.mining.MiningEquipment; +import org.eclipse.passage.lic.base.io.ExistingFolder; import org.eclipse.passage.lic.base.io.LicensingFolder; import org.eclipse.passage.lic.base.io.PassageFileExtension; import org.eclipse.passage.lic.base.io.PathFromLicensedProduct; @@ -40,7 +41,11 @@ public UserHomeResidentConditions(MiningEquipment equipment, PassageFileExtensio @Override protected Supplier<Path> base(LicensedProduct product) { - return new PathFromLicensedProduct(new LicensingFolder(new UserHomePath()), product); + return new ExistingFolder(// + new PathFromLicensedProduct(// + new LicensingFolder(// + new UserHomePath()), // + product)); } } diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/ExistingFolder.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/ExistingFolder.java new file mode 100644 index 000000000..198295cfb --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/ExistingFolder.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * 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.io; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Supplier; + +import org.eclipse.passage.lic.internal.base.Cached; + +public final class ExistingFolder implements Supplier<Path> { + + private final Cached<Supplier<Path>, Path> folder; + + public ExistingFolder(Supplier<Path> folder) { + this.folder = new Cached<>(folder, this::existing); + } + + @Override + public Path get() { + return folder.get(); + } + + private Path existing(Supplier<Path> origin) { + Path path = origin.get(); + if (!Files.exists(path)) { + path.toFile().mkdirs(); // do not use nio + } + return path; + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java index c815f525d..9327ac347 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/FileCollection.java @@ -27,35 +27,43 @@ import org.eclipse.passage.lic.internal.base.i18n.BaseMessages; /** + * Collects regular files of the given {@code extension} starting from the given + * {@code base} path recursively. No particular order is guaranteed. + * * @since 2.1 */ public final class FileCollection { private final Supplier<Path> base; - private final PassageFileExtension extensions; + private final PassageFileExtension extension; + /** + * @param base expected to supply path to an existing directory + */ public FileCollection(Supplier<Path> base, PassageFileExtension extension) { Objects.requireNonNull(base, "FileCollection::base path"); //$NON-NLS-1$ Objects.requireNonNull(extension, "FileCollection::extension"); //$NON-NLS-1$ this.base = base; - this.extensions = extension; + this.extension = extension; } public Collection<Path> get() throws LicensingException { - try (Stream<Path> all = filesIn(base.get())) { + try (Stream<Path> all = files(base.get())) { return filtered(all); } catch (IOException e) { - throw new LicensingException(BaseMessages.getString("FileCollection.failure"), e); //$NON-NLS-1$ + throw new LicensingException( + String.format(BaseMessages.getString("FileCollection.failure"), extension.get(), base.get()), //$NON-NLS-1$ + e); } } - private Stream<Path> filesIn(Path path) throws IOException { + private Stream<Path> files(Path path) throws IOException { return Files.walk(path) // .filter(Files::isRegularFile); } private List<Path> filtered(Stream<Path> files) { - return files.filter(extensions::ends) // + return files.filter(extension::ends) // .collect(Collectors.toList()); } } diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java index 7bbbb1e77..025882918 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/base/io/Settings.java @@ -17,12 +17,12 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.function.Predicate; import java.util.function.Supplier; -import java.util.stream.Stream; import org.eclipse.passage.lic.api.LicensingException; import org.eclipse.passage.lic.internal.base.i18n.BaseMessages; @@ -45,9 +45,6 @@ public final class Settings { /** * Tell me where to start searching and when to stop - * - * @param base - * @param enough */ public Settings(Supplier<Path> base, Predicate<Map<String, Object>> enough) { this.base = base; @@ -62,23 +59,22 @@ public Settings(Supplier<Path> base) { } public Map<String, Object> get() throws LicensingException { - try (Stream<Path> files = settingFilesIn(base.get())) { - return properties(files); + try { + return properties(settingFiles()); } catch (IOException e) { - throw new LicensingException(String.format(BaseMessages.getString("Settings.error_on_reading_settings"), //$NON-NLS-1$ - base.get()), e); + throw new LicensingException(// + String.format(BaseMessages.getString("Settings.error_on_reading_settings"), base.get()), //$NON-NLS-1$ + e); } } - private Stream<Path> settingFilesIn(Path path) throws IOException { - return Files.walk(path) // - .filter(Files::isRegularFile) // - .filter(new PassageFileExtension.Settings()::ends); + private Collection<Path> settingFiles() throws LicensingException { + return new FileCollection(base, new PassageFileExtension.Settings()).get(); } - private Map<String, Object> properties(Stream<Path> files) throws IOException { + private Map<String, Object> properties(Collection<Path> files) throws IOException { Map<String, Object> properties = new HashMap<>(); - for (Path file : (Iterable<Path>) files::iterator) { + for (Path file : files) { loadAndAdd(properties, file); if (enough.test(properties)) { return properties; diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/i18n/BaseMessages.properties b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/i18n/BaseMessages.properties index 584f78b53..4ad4e8b6a 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/i18n/BaseMessages.properties +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/i18n/BaseMessages.properties @@ -14,7 +14,7 @@ LicenseReadingTool.error_no_key_keeper=License for %s cannot be read: there is no key keeper for the product configuration LicenseReadingTool.error_no_stream_codec=License for %s cannot be read: there is no stream codec for the product configuration LicenseReadingTool.error_no_transport=License for %s cannot be read: there is no transport for content type %s -FileCollection.failure=Files collecting failed +FileCollection.failure=Fail to collect files of [%s] extension from [%s] folder JointRegistry.retrieve_absent=No service for id %s can be found among %d following registries: \n\t%s KeyValuePairs.default_error=Failed to parse string to properties MiningTool.error_mining_file=Error on mining conditions from file %s diff --git a/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/ConfigurationResidentConditions.java b/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/ConfigurationResidentConditions.java index ce4e13a85..a759746a7 100644 --- a/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/ConfigurationResidentConditions.java +++ b/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/ConfigurationResidentConditions.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 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 @@ -18,6 +18,7 @@ import org.eclipse.passage.lic.api.LicensedProduct; import org.eclipse.passage.lic.api.conditions.mining.MiningEquipment; import org.eclipse.passage.lic.base.conditions.mining.LocalConditions; +import org.eclipse.passage.lic.base.io.ExistingFolder; import org.eclipse.passage.lic.base.io.LicensingFolder; import org.eclipse.passage.lic.base.io.PathFromLicensedProduct; import org.eclipse.passage.lic.equinox.io.ConfigurationPath; @@ -37,7 +38,11 @@ public ConfigurationResidentConditions(MiningEquipment equipment) { @Override protected Supplier<Path> base(LicensedProduct product) { - return new PathFromLicensedProduct(new LicensingFolder(new ConfigurationPath()), product); + return new ExistingFolder(// + new PathFromLicensedProduct(// + new LicensingFolder(// + new ConfigurationPath()), // + product)); } } diff --git a/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/InstallationResidentConditions.java b/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/InstallationResidentConditions.java index 95c73b4e8..7a7279f76 100644 --- a/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/InstallationResidentConditions.java +++ b/bundles/org.eclipse.passage.lic.equinox/src/org/eclipse/passage/lic/equinox/conditions/InstallationResidentConditions.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 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 @@ -18,6 +18,7 @@ import org.eclipse.passage.lic.api.LicensedProduct; import org.eclipse.passage.lic.api.conditions.mining.MiningEquipment; import org.eclipse.passage.lic.base.conditions.mining.LocalConditions; +import org.eclipse.passage.lic.base.io.ExistingFolder; import org.eclipse.passage.lic.base.io.LicensingFolder; import org.eclipse.passage.lic.base.io.PathFromLicensedProduct; import org.eclipse.passage.lic.equinox.io.InstallationPath; @@ -37,7 +38,11 @@ public InstallationResidentConditions(MiningEquipment equipment) { @Override protected Supplier<Path> base(LicensedProduct product) { - return new PathFromLicensedProduct(new LicensingFolder(new InstallationPath()), product); + return new ExistingFolder(// + new PathFromLicensedProduct(// + new LicensingFolder(// + new InstallationPath()), // + product)); } } diff --git a/bundles/org.eclipse.passage.lic.oshi/src/org/eclipse/passage/lic/oshi/HardwareAssessmentService.java b/bundles/org.eclipse.passage.lic.oshi/src/org/eclipse/passage/lic/oshi/HardwareAssessmentService.java index 70f0e8c94..d59917a0a 100644 --- a/bundles/org.eclipse.passage.lic.oshi/src/org/eclipse/passage/lic/oshi/HardwareAssessmentService.java +++ b/bundles/org.eclipse.passage.lic.oshi/src/org/eclipse/passage/lic/oshi/HardwareAssessmentService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 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 @@ -27,7 +27,7 @@ public final class HardwareAssessmentService implements ExpressionTokenAssessmentService { private final EvaluationType type = new EvaluationType.Hardware(); - private RuntimeEnvironmentRegistry environments; + private final RuntimeEnvironmentRegistry environments; public HardwareAssessmentService(RuntimeEnvironmentRegistry environments) { this.environments = environments; diff --git a/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF index 996c177ed..b9f21e067 100644 --- a/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Automatic-Module-Name: org.eclipse.passage.lic.base.tests Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.passage.lic.base.tests -Bundle-Version: 1.0.1.qualifier +Bundle-Version: 1.0.2.qualifier Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright diff --git a/tests/org.eclipse.passage.lic.base.tests/OSGI-INF/l10n/bundle.properties b/tests/org.eclipse.passage.lic.base.tests/OSGI-INF/l10n/bundle.properties index 60ea18214..49f3896fc 100644 --- a/tests/org.eclipse.passage.lic.base.tests/OSGI-INF/l10n/bundle.properties +++ b/tests/org.eclipse.passage.lic.base.tests/OSGI-INF/l10n/bundle.properties @@ -1,6 +1,6 @@ #Properties file for org.eclipse.passage.lic.base.tests ############################################################################### -# Copyright (c) 2018, 2021 ArSysOp and others +# Copyright (c) 2018, 2022 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 @@ -14,7 +14,7 @@ Bundle-Name = Passage LIC Base Tests Bundle-Vendor = Eclipse Passage -Bundle-Copyright = Copyright (c) 2018, 2021 ArSysOp and others.\n\ +Bundle-Copyright = Copyright (c) 2018, 2022 ArSysOp and others.\n\ \n\ This program and the accompanying materials are made\n\ available under the terms of the Eclipse Public License 2.0\n\ diff --git a/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/ExistingFolderTest.java b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/ExistingFolderTest.java new file mode 100644 index 000000000..49c997a53 --- /dev/null +++ b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/ExistingFolderTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * 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.tests.io; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeFalse; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.function.Supplier; + +import org.eclipse.passage.lic.base.io.ExistingFolder; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public final class ExistingFolderTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void createsIfDoesNotExist() { + Supplier<Path> faulty = notExistingFolder(); + assumeFalse(Files.exists(faulty.get())); + assertTrue(Files.exists(new ExistingFolder(faulty).get())); + } + + private Supplier<Path> notExistingFolder() { + return new NotExistingFolder(folder.getRoot().toPath()); + } + +} diff --git a/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/FileCollectionTest.java b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/FileCollectionTest.java index 2d10a73a6..a561e6ff9 100644 --- a/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/FileCollectionTest.java +++ b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/FileCollectionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2021 ArSysOp + * Copyright (c) 2020, 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 @@ -68,6 +68,14 @@ public void canTraverseSingleFile() throws LicensingException { single.iterator().next().getFileName().toString()); } + @Test(expected = LicensingException.class) + public void failsWhenWronglyConfigured() throws LicensingException { + new FileCollection(// + new NotExistingFolder(folder.getRoot().toPath()), // + new PassageFileExtension.PublicKey()// + ).get(); + } + @Test public void traverseOnlyDownTheScope() throws LicensingException { assertTrue(new FileCollection(this::outOfScope, new PassageFileExtension.LicenseEncrypted()).get().isEmpty()); diff --git a/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/NotExistingFolder.java b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/NotExistingFolder.java new file mode 100644 index 000000000..7453c1287 --- /dev/null +++ b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/io/NotExistingFolder.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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.tests.io; + +import java.nio.file.Path; +import java.util.function.Supplier; + +final class NotExistingFolder implements Supplier<Path> { + + private final Path base; + + NotExistingFolder(Path base) { + this.base = base; + } + + @Override + public Path get() { + return base.resolve(Long.toHexString(System.currentTimeMillis())); + } + +} From c0220f309a1459ff18dae4baea5f14404a29096c Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia <elena.parovyshnaya@gmail.com> Date: Tue, 31 May 2022 19:40:57 +0300 Subject: [PATCH 2/2] 1088 Passage Operator 2.4.0-M3 fails on startup update bundle version properly Signed-off-by: eparovyshnaia <elena.parovyshnaya@gmail.com> --- tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF index b9f21e067..172558008 100644 --- a/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.passage.lic.base.tests/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Automatic-Module-Name: org.eclipse.passage.lic.base.tests Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.passage.lic.base.tests -Bundle-Version: 1.0.2.qualifier +Bundle-Version: 1.0.100.qualifier Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright