Skip to content

Commit

Permalink
#1054 LIC: library-licensed-separately: support license import
Browse files Browse the repository at this point in the history
retarget ImportLicenseDialog from a single license file to a directory

Signed-off-by: eparovyshnaya <[email protected]>
  • Loading branch information
eparovyshnaya committed Feb 28, 2022
1 parent 390a8e5 commit a37fc2b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 24 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 @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.File;
import java.nio.file.Path;
import java.util.function.Supplier;

Expand All @@ -24,6 +25,13 @@ public final boolean ends(Path path) {
return path.getFileName().toString().endsWith(get());
}

/**
* @since 2.3
*/
public final boolean ends(File file) {
return file.getName().endsWith(get());
}

public static final class LicenseEncrypted extends PassageFileExtension {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* 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.jface.dialogs.licensing;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.eclipse.passage.lic.api.ServiceInvocationResult;
import org.eclipse.passage.lic.api.conditions.ConditionPack;
import org.eclipse.passage.lic.base.BaseServiceInvocationResult;
import org.eclipse.passage.lic.base.SumOfCollections;
import org.eclipse.passage.lic.equinox.LicenseReadingServiceRequest;
import org.eclipse.passage.lic.internal.base.access.Libraries;
import org.eclipse.passage.lic.internal.base.conditions.LicenseConditions;

@SuppressWarnings("restriction")
final class AllConditionsFromLicenses implements Supplier<ServiceInvocationResult<Collection<ConditionPack>>> {

private final List<Path> licenses;
private final Libraries libraries;
private final LicenseReadingServiceRequest product;

AllConditionsFromLicenses(List<Path> licenses, Libraries libraries) {
this.licenses = licenses;
this.libraries = libraries;
this.product = new LicenseReadingServiceRequest();
}

@Override
public ServiceInvocationResult<Collection<ConditionPack>> get() {
return licenses.stream()//
.map(this::fromLicense)//
.reduce(new BaseServiceInvocationResult.Sum<>(new SumOfCollections<>()))//
.orElse(new BaseServiceInvocationResult<>(Collections.emptyList()));
}

private ServiceInvocationResult<Collection<ConditionPack>> fromLicense(Path file) {
return new LicenseConditions(file, product, libraries).get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* 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.jface.dialogs.licensing;

import java.io.File;
import java.io.FileFilter;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.eclipse.passage.lic.base.io.PassageFileExtension;

final class AllLicensesFromFolder implements Supplier<List<Path>> {

private final Optional<String> folder;

AllLicensesFromFolder(String folder) {
this(Optional.ofNullable(folder));
}

AllLicensesFromFolder(Optional<String> folder) {
this.folder = folder;
}

@Override
public List<Path> get() {
if (!folder.isPresent()) {
return Collections.emptyList();
}
return Arrays.stream(licenses())//
.map(File::toPath)//
.collect(Collectors.toList());
}

private File[] licenses() {
File host = new File(folder.get());
if (!host.isDirectory()) {
return new File[0];
}
return host.listFiles(licen());
}

private FileFilter licen() {
return new FileFilter() {
private final PassageFileExtension licen = new PassageFileExtension.LicenseEncrypted();

@Override
public boolean accept(File file) {
return licen.ends(file);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
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;
import org.eclipse.passage.lic.internal.base.access.Libraries;
import org.eclipse.passage.lic.internal.base.conditions.LicenseConditions;
import org.eclipse.passage.lic.internal.equinox.access.RegisteredLibraries;
import org.eclipse.passage.lic.internal.jface.i18n.ImportLicenseDialogMessages;
import org.eclipse.passage.lic.jface.resource.LicensingImages;
Expand All @@ -45,7 +43,7 @@
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
Expand Down Expand Up @@ -130,30 +128,24 @@ private String evaluation(Condition condition) {
}

private void browseAndLoad() {
browse().ifPresent(this::loadLicense);
loadLicense(browse());
updateButtonsEnablement();
}

private Optional<String> browse() {
FileDialog dialog = new FileDialog(getShell(), SWT.OPEN | SWT.SHEET);
private List<Path> browse() {
DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.OPEN | SWT.SHEET);
dialog.setText(ImportLicenseDialogMessages.ImportLicenseDialog_browse_dialog_title);
dialog.setFilterPath(path.getText().trim());
dialog.setFilterExtensions(new String[] { "*.licen", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
Optional<String> file = Optional.ofNullable(dialog.open());
file.ifPresent(path::setText);
return file;
return new AllLicensesFromFolder(dialog.open()).get();
}

private void loadLicense(String file) {
private void loadLicense(List<Path> files) {
Optional<LicensedProduct> product = product();
if (!product.isPresent()) {
return;
}
ServiceInvocationResult<Collection<ConditionPack>> packs = new LicenseConditions(//
Paths.get(file), //
new LicenseReadingServiceRequest(), //
libraries()).get();
if (!packs.data().isPresent()) {
ServiceInvocationResult<Collection<ConditionPack>> packs = //
new AllConditionsFromLicenses(files, libraries()).get();
if (!new NoSevereErrors().test(packs.diagnostic())) {
reportError(packs.diagnostic());
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2020, 2021 ArSysOp and others
# Copyright (c) 2020, 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 @@ -10,14 +10,14 @@
# Contributors:
# ArSysOp - initial API and implementation
###############################################################################
ImportLicenseDialog_browse_dialog_title=Import from File
ImportLicenseDialog_browse_dialog_title=Import all licenses from folder
ImportLicenseDialog_column_evaluation=Evaluation
ImportLicenseDialog_column_feature=Feature
ImportLicenseDialog_column_period=Valid
ImportLicenseDialog_import_title=&Import
ImportLicenseDialog_import_tooltip=Copy this license file to a dedicated directory
ImportLicenseDialog_import_tooltip=Copy found license files to dedicated directories
ImportLicenseDialog_io_error=License import failed due to I/O error: %s
ImportLicenseDialog_lic_read_failed=Failed to read the license pointed
ImportLicenseDialog_lic_read_failed=Failed to read the license from the pointed folder
ImportLicenseDialog_path_label=From license file:
ImportLicenseDialog_prelude=Point license file to see what's inside
ImportLicenseDialog_title=Import License
ImportLicenseDialog_prelude=Point a folder with license files to see what's inside them
ImportLicenseDialog_title=Import Licenses

0 comments on commit a37fc2b

Please sign in to comment.