Skip to content

Commit

Permalink
Merge branch 'release-2.2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed May 25, 2020
2 parents 0bd86d6 + 367f758 commit 92a6e02
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 24 deletions.
25 changes: 25 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 1
update_configs:
- package_manager: "java:maven"
directory: "/"
update_schedule: "daily"
automerged_updates:
- match:
dependency_name: "org.apache.maven.plugins:*"
- match:
dependency_name: "org.codehaus.mojo:*"
- match:
dependency_name: "org.gaul:modernizer-maven-plugin"
- match:
dependency_name: "org.jacoco:jacoco-maven-plugin"
- match:
dependency_name: "org.checkerframework:checker-qual"
- match:
dependency_name: "org.assertj:assertj-core"
- match:
dependency_name: "org.openjdk.jmh:*"
- match:
dependency_name: "com.akathist.maven.plugins.launch4j:launch4j-maven-plugin"
- match:
dependency_name: "net.nicoulaj.maven.plugins:checksum-maven-plugin"

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ deploy:
- provider: releases
api_key: "${GITHUB_KEY}"
skip_cleanup: true
draft: true
on:
tags: true
repo: "${DEPLOY_REPO}"
Expand Down
2 changes: 1 addition & 1 deletion java-desktop-util-chart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>be.nbb.rd</groupId>
<artifactId>java-desktop-util-parent</artifactId>
<version>2.2.3</version>
<version>2.2.4</version>
</parent>

<artifactId>java-desktop-util-chart</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion java-desktop-util-fa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>be.nbb.rd</groupId>
<artifactId>java-desktop-util-parent</artifactId>
<version>2.2.3</version>
<version>2.2.4</version>
</parent>

<artifactId>java-desktop-util-fa</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion java-desktop-util-os/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>be.nbb.rd</groupId>
<artifactId>java-desktop-util-parent</artifactId>
<version>2.2.3</version>
<version>2.2.4</version>
</parent>

<artifactId>java-desktop-util-os</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2018 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package ec.util.desktop.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
*
* @author Philippe Charles
*/
@lombok.experimental.UtilityClass
class InternalCopyOfRegWrapper {

public static final String COMMAND = "reg";

@NonNull
public Map<String, List<RegValue>> query(@NonNull String keyName, boolean recursive) throws IOException {
Objects.requireNonNull(keyName);
try (BufferedReader reader = InternalCopyofProcessReader.newReader(getArgs(keyName, recursive))) {
return parse(reader);
}
}

String[] getArgs(String keyName, boolean recursive) {
List<String> args = new ArrayList<>();
args.add(COMMAND);
args.add("query");
args.add(keyName);
if (recursive) {
args.add("/s");
}
return args.toArray(new String[0]);
}

Map<String, List<RegValue>> parse(BufferedReader reader) throws IOException {
Map<String, List<RegValue>> result = new LinkedHashMap<>();
String line;
String subKey = null;
List<RegValue> values = null;
while ((line = reader.readLine()) != null) {
if (!line.isEmpty()) {
if (subKey == null) {
subKey = line;
values = new ArrayList<>();
} else {
RegValue regValue = RegValue.parseOrNull(line);
if (regValue != null) {
values.add(regValue);
} else {
result.put(subKey, values);
subKey = line;
values = new ArrayList<>();
}
}
}
}
if (subKey != null) {
result.put(subKey, values);
}
return result;
}

@lombok.Value
public static final class RegValue {

private static final Pattern PATTERN = Pattern.compile("^[ ]{4}(.+)[ ]{4}(REG_(?:SZ|MULTI_SZ|EXPAND_SZ|DWORD|QWORD|BINARY|NONE))[ ]{4}(.*)$");

@Nullable
public static RegValue parseOrNull(@NonNull CharSequence line) {
Matcher m = PATTERN.matcher(line);
return m.matches() ? new RegValue(m.group(1), m.group(2), m.group(3)) : null;
}

@lombok.NonNull
private String name;

@lombok.NonNull
private String dataType;

@lombok.NonNull
private String value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2018 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package ec.util.desktop.impl;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
*
* @author Philippe Charles
*/
@lombok.experimental.UtilityClass
class InternalCopyofProcessReader {

@NonNull
public static BufferedReader newReader(@NonNull String... args) throws IOException {
return newReader(new ProcessBuilder(args).start());
}

@NonNull
public static BufferedReader newReader(@NonNull Process process) throws IOException {
return new BufferedReader(new InputStreamReader(new ProcessInputStream(process), Charset.defaultCharset()));
}

private static final class ProcessInputStream extends InputStream {

@lombok.experimental.Delegate(excludes = Closeable.class)
private final InputStream delegate;

private final Process process;

public ProcessInputStream(Process process) {
this.delegate = process.getInputStream();
this.process = process;
}

@Override
public void close() throws IOException {
try {
readUntilEnd();
waitForEndOfProcess();
} finally {
delegate.close();
}
}

// we need the process to end, else we'll get an illegal Thread State Exception
private void readUntilEnd() throws IOException {
while (delegate.read() != -1) {
}
}

private void waitForEndOfProcess() throws IOException {
try {
process.waitFor();
} catch (InterruptedException ex) {
throw new IOException(ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinReg;
import java.io.IOException;
import java.util.Objects;
import java.util.SortedMap;

/**
Expand All @@ -30,6 +31,8 @@ final class JnaRegistry extends WinRegistry {

@Override
public boolean keyExists(Root root, String key) throws IOException {
Objects.requireNonNull(root);
Objects.requireNonNull(key);
try {
return Advapi32Util.registryKeyExists(convert(root), key);
} catch (Win32Exception | UnsatisfiedLinkError ex) {
Expand All @@ -39,6 +42,9 @@ public boolean keyExists(Root root, String key) throws IOException {

@Override
public Object getValue(Root root, String key, String name) throws IOException {
Objects.requireNonNull(root);
Objects.requireNonNull(key);
Objects.requireNonNull(name);
try {
WinReg.HKEY hkey = convert(root);
return Advapi32Util.registryValueExists(hkey, key, name) ? Advapi32Util.registryGetValue(hkey, key, name) : null;
Expand All @@ -49,6 +55,8 @@ public Object getValue(Root root, String key, String name) throws IOException {

@Override
public SortedMap<String, Object> getValues(Root root, String key) throws IOException {
Objects.requireNonNull(root);
Objects.requireNonNull(key);
try {
WinReg.HKEY hkey = convert(root);
return Advapi32Util.registryKeyExists(hkey, key) ? Advapi32Util.registryGetValues(hkey, key) : Util.EMPTY_SORTED_MAP;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2020 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package ec.util.desktop.impl;

import ec.util.desktop.impl.InternalCopyOfRegWrapper.RegValue;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
*
* @author Philippe Charles
*/
final class RegRegistry extends WinRegistry {

private static final String KEY_SEPARATOR = "\\";

private List<InternalCopyOfRegWrapper.RegValue> getValuesOrNull(WinRegistry.Root root, String key) throws IOException {
Objects.requireNonNull(root);
Objects.requireNonNull(key);
String keyName = root.name() + KEY_SEPARATOR + key;
return InternalCopyOfRegWrapper.query(keyName, false).get(keyName);
}

@Override
public boolean keyExists(WinRegistry.Root root, String key) throws IOException {
List<InternalCopyOfRegWrapper.RegValue> data = getValuesOrNull(root, key);
return data != null;
}

@Override
public Object getValue(WinRegistry.Root root, String key, String name) throws IOException {
List<InternalCopyOfRegWrapper.RegValue> data = getValuesOrNull(root, key);
Objects.requireNonNull(name);
return data != null
? data
.stream()
.filter(regValue -> regValue.getName().equals(name))
.map(regValue -> regValue.getValue())
.findFirst()
.orElse(null)
: null;
}

@Override
public SortedMap<String, Object> getValues(WinRegistry.Root root, String key) throws IOException {
List<InternalCopyOfRegWrapper.RegValue> data = getValuesOrNull(root, key);
return data != null
? data.stream().collect(Collectors.toMap(RegValue::getName, regValue -> (Object) regValue.getValue(), (l, r) -> l, TreeMap::new))
: Collections.emptySortedMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ private static WinRegistry createInstance() {
return new JnaRegistry();
}
// fallback
log.log(Level.INFO, "Using NoOpRegistry");
return noOp();
log.log(Level.INFO, "Using RegRegistry");
return new RegRegistry();
}
}

Expand Down
Loading

0 comments on commit 92a6e02

Please sign in to comment.