Skip to content

Commit

Permalink
Merge pull request #1089 from eclipse-passage/1088
Browse files Browse the repository at this point in the history
1088 Passage Operator 2.4.0-M3 fails on startup
  • Loading branch information
eparovyshnaya authored May 31, 2022
2 parents cd43e81 + c0220f3 commit d256d76
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.100.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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\
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}

}

0 comments on commit d256d76

Please sign in to comment.