Skip to content

Commit

Permalink
[LIC-Base] Simplify IO operations
Browse files Browse the repository at this point in the history
Signed-off-by: Hannes Wellmann <[email protected]>
  • Loading branch information
HannesWell committed Apr 7, 2022
1 parent 4022e64 commit 8badab8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 83 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 @@ -9,20 +9,19 @@
*
* Contributors:
* ArSysOp - initial API and implementation
* Hannes Wellmann (IILS mbH) - Simplify IO operations(#1071)
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.internal.base.i18n.BaseMessages;
Expand All @@ -43,34 +42,20 @@ public FileCollection(Supplier<Path> base, PassageFileExtension extension) {
}

public Collection<Path> get() throws LicensingException {
HunterFiles hunter = new HunterFiles(extensions);
try {
Files.walkFileTree(base.get(), hunter);
try (Stream<Path> files = filesIn(base.get())) {
return files(files);
} catch (IOException e) {
new LicensingException(BaseMessages.getString("FileCollection.failure"), e); //$NON-NLS-1$
throw new LicensingException(BaseMessages.getString("FileCollection.failure"), e); //$NON-NLS-1$
}
return hunter.findings();
}

private static final class HunterFiles extends SimpleFileVisitor<Path> {
private final List<Path> findings = new ArrayList<>();
private final PassageFileExtension extension;

HunterFiles(PassageFileExtension extension) {
this.extension = extension;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(extension.get())) {
findings.add(file);
}
return FileVisitResult.CONTINUE;
}

Collection<Path> findings() {
return findings;
}
private Stream<Path> filesIn(Path path) throws IOException {
return Files.walk(path) //
.filter(Files::isRegularFile);
}

private List<Path> files(Stream<Path> files) {
return files.filter(extensions::ends) //
.collect(Collectors.toList());
}
}
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 @@ -9,12 +9,11 @@
*
* Contributors:
* ArSysOp - initial API and implementation
* Hannes Wellmann (IILS mbH) - Simplify IO operations(#1071)
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
Expand All @@ -32,10 +31,6 @@ public FileContent(Path file) {
}

public byte[] get() throws IOException {
byte[] content = new byte[(int) Files.size(file)];
try (InputStream stream = new FileInputStream(file.toFile())) {
stream.read(content);
}
return content;
return Files.readAllBytes(file);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
* Copyright (c) 2021, 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,7 +12,6 @@
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -44,7 +43,7 @@ public LicensedProduct id() {
@Override
public InputStream productPublicKey() throws LicensingException {
try {
return new FileInputStream(key.toFile());
return Files.newInputStream(key);
} catch (Exception e) {
throw new LicensingException(e);
}
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 @@ -12,8 +12,8 @@
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.function.Supplier;
Expand Down Expand Up @@ -47,7 +47,7 @@ public LicensedProduct id() {
public InputStream productPublicKey() throws LicensingException {
Path path = base.get().resolve(keyFile());
try {
return new FileInputStream(path.toFile());
return Files.newInputStream(path);
} catch (Exception e) {
throw new LicensingException(//
String.format(//
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 @@ -9,24 +9,20 @@
*
* Contributors:
* ArSysOp - initial API and implementation
* Hannes Wellmann (IILS mbH) - Simplify IO operations(#1071)
*******************************************************************************/
package org.eclipse.passage.lic.base.io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
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 Down Expand Up @@ -66,49 +62,36 @@ public Settings(Supplier<Path> base) {
}

public Map<String, Object> get() throws LicensingException {
HunterForSettingsFiles hunter = new HunterForSettingsFiles();
try {
Files.walkFileTree(base.get(), hunter);
try (Stream<Path> files = settingFilesIn(base.get())) {
return properties(files);
} catch (IOException e) {
throw new LicensingException(String.format(BaseMessages.getString("Settings.error_on_reading_settings"), //$NON-NLS-1$
base.get()), e);
}
return hunter.findings();
}

private final class HunterForSettingsFiles extends SimpleFileVisitor<Path> {

private final Map<String, Object> properties;
private final PassageFileExtension extension = new PassageFileExtension.Settings();

public HunterForSettingsFiles() {
this.properties = new HashMap<String, Object>();
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!extension.ends(file)) {
return FileVisitResult.CONTINUE;
}
load(file).stream() //
.forEach(e -> properties.put(//
e.getKey().toString(), //
e.getValue()));
return enough.test(properties) ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
}
private Stream<Path> settingFilesIn(Path path) throws IOException {
return Files.walk(path) //
.filter(Files::isRegularFile) //
.filter(new PassageFileExtension.Settings()::ends);
}

private Set<Entry<Object, Object>> load(Path file) throws IOException {
Properties heap = new Properties();
try (InputStream stream = new FileInputStream(file.toFile())) {
heap.load(stream);
private Map<String, Object> properties(Stream<Path> files) throws IOException {
Map<String, Object> properties = new HashMap<>();
for (Path file : (Iterable<Path>) files::iterator) {
loadAndAdd(properties, file);
if (enough.test(properties)) {
return properties;
}
return heap.entrySet();
}
return properties;
}

Map<String, Object> findings() {
return properties;
private void loadAndAdd(Map<String, Object> properties, Path file) throws IOException {
Properties heap = new Properties();
try (InputStream stream = Files.newInputStream(file)) {
heap.load(stream);
}

heap.forEach((k, v) -> properties.put(k.toString(), v));
}

}

0 comments on commit 8badab8

Please sign in to comment.