Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,27 @@
import java.util.regex.Pattern;

public class Artifact {
private final String module;
private final Set<String> versions;
private final String directory;
private final boolean latest;
private final boolean override;
private final Pattern defaultForPattern;

public Artifact(Set<String> versions, String directory,
public Artifact(String module, Set<String> versions, String directory,
boolean latest, boolean override, String defaultFor) {
this.module = module;
this.versions = versions;
this.directory = directory;
this.latest = latest;
this.override = override;
this.defaultForPattern = (defaultFor == null ? null : Pattern.compile(defaultFor));
}

public String getModule() {
return module;
}

public Set<String> getVersions() {
return versions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,22 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SingleModuleJsonVersionToConfigDirectoryIndex implements VersionToConfigDirectoryIndex {
private final Path moduleRoot;
private final List<Artifact> artifacts;
private final Map<String, List<Artifact>> index;

public SingleModuleJsonVersionToConfigDirectoryIndex(Path moduleRoot) {
this.moduleRoot = moduleRoot;
this.artifacts = parseIndexFile(moduleRoot);
this.index = parseIndexFile(moduleRoot);
}

private List<Artifact> parseIndexFile(Path rootPath) {
private Map<String, List<Artifact>> parseIndexFile(Path rootPath) {
Path indexFile = rootPath.resolve("index.json");
try {
String fileContent = Files.readString(indexFile);
Expand All @@ -73,7 +75,8 @@ private List<Artifact> parseIndexFile(Path rootPath) {
for (int i = 0; i < json.length(); i++) {
entries.add(fromJson(json.getJSONObject(i)));
}
return entries;
return entries.stream()
.collect(Collectors.groupingBy(Artifact::getModule));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -118,23 +121,27 @@ public Optional<DirectoryConfiguration> findLatestConfigurationFor(String groupI

private Optional<DirectoryConfiguration> findConfigurationFor(String groupId, String artifactId, String version,
Predicate<? super Artifact> predicate) {
if (artifacts.isEmpty()) {
String module = groupId + ":" + artifactId;
List<Artifact> artifacts = index.get(module);
if (artifacts == null) {
return Optional.empty();
}
return artifacts.stream()
.filter(artifact -> artifact.getModule().equals(module))
.filter(predicate)
.findFirst()
.map(artifact -> new DirectoryConfiguration(groupId, artifactId, version,
moduleRoot.resolve(artifact.getDirectory()), artifact.isOverride()));
}

private Artifact fromJson(JSONObject json) {
String module = json.optString("module", null);
Set<String> testVersions = readTestedVersions(json.optJSONArray("tested-versions"));
String directory = json.optString("metadata-version", null);
boolean latest = json.optBoolean("latest");
boolean override = json.optBoolean("override");
String defaultFor = json.optString("default-for", null);
return new Artifact(testVersions, directory, latest, override, defaultFor);
return new Artifact(module, testVersions, directory, latest, override, defaultFor);
}

private Set<String> readTestedVersions(JSONArray array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,77 +40,36 @@
*/
package org.graalvm.reachability.internal.index.modules;

import com.github.openjson.JSONArray;
import com.github.openjson.JSONObject;
import org.graalvm.reachability.internal.UncheckedIOException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* Module-to-config index which:
* - Resolves the primary module directory by conventional layout (groupId/artifactId),
* - Reads requires from the inner metadata/group/artifact/index.json and adds their conventional directories.
* This is the default index from module to configuration directory, which first
* looks into the JSON index file, and if a module isn't found there, would try
* to find it in the standard FS location.
*/
public class FileSystemModuleToConfigDirectoryIndex implements ModuleToConfigDirectoryIndex {
private final Path rootPath;
private final JsonModuleToConfigDirectoryIndex jsonIndex;
private final StandardLocationModuleToConfigDirectoryIndex fsIndex;

public FileSystemModuleToConfigDirectoryIndex(Path rootPath) {
this.rootPath = rootPath;
this.jsonIndex = new JsonModuleToConfigDirectoryIndex(rootPath);
this.fsIndex = new StandardLocationModuleToConfigDirectoryIndex(rootPath);
}

/**
* Returns the directories containing the candidate configurations for the given module.
* <p>
* - Always includes the conventional module directory if present: rootPath/groupId/artifactId
* - Additionally includes conventional directories of any modules listed in "requires" of the inner index.json
* - Only a single-level requires expansion is performed
* Returns the directory containing the candidate configurations for the given module.
*
* @param groupId the group of the module
* @param artifactId the artifact of the module
* @return the configuration directory
*/
@Override
public Set<Path> findConfigurationDirectories(String groupId, String artifactId) {
Path base = rootPath.resolve(groupId + "/" + artifactId);
if (!Files.isDirectory(base)) {
return Collections.emptySet();
}

Path indexFile = base.resolve("index.json");
if (Files.isRegularFile(indexFile)) {
Set<Path> result = new LinkedHashSet<>();
// Always include the base directory so its index.json is parsed,
// even if it doesn't contain configuration files itself.
result.add(base);
try {
String content = Files.readString(indexFile);
JSONArray entries = new JSONArray(content);
for (int i = 0; i < entries.length(); i++) {
JSONObject entry = entries.getJSONObject(i);
JSONArray requires = entry.optJSONArray("requires");
if (requires == null) {
continue;
}
for (int j = 0; j < requires.length(); j++) {
String req = requires.getString(j);
int sep = req.indexOf(':');
if (sep > 0) {
String reqGroup = req.substring(0, sep);
String reqArtifact = req.substring(sep + 1);
Path reqDir = rootPath.resolve(reqGroup + "/" + reqArtifact);
if (Files.isDirectory(reqDir)) {
result.add(reqDir);
}
}
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return result;
Set<Path> fromIndex = jsonIndex.findConfigurationDirectories(groupId, artifactId);
if (!fromIndex.isEmpty()) {
return fromIndex;
}

return Collections.singleton(base);
return fsIndex.findConfigurationDirectories(groupId, artifactId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.reachability.internal.index.modules;

import com.github.openjson.JSONArray;
import com.github.openjson.JSONObject;
import org.graalvm.reachability.internal.UncheckedIOException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JsonModuleToConfigDirectoryIndex implements ModuleToConfigDirectoryIndex {
private final Path rootPath;
private final Map<String, Set<Path>> index;

public JsonModuleToConfigDirectoryIndex(Path rootPath) {
this.rootPath = rootPath;
this.index = parseIndexFile(rootPath);
}

private Map<String, Set<Path>> parseIndexFile(Path rootPath) {
Path indexFile = rootPath.resolve("index.json");
try {
String fileContent = Files.readString(indexFile);
JSONArray json = new JSONArray(fileContent);
List<ModuleEntry> entries = new ArrayList<>();
for (int i = 0; i < json.length(); i++) {
entries.add(fromJson(json.getJSONObject(i)));
}
Map<String, List<ModuleEntry>> moduleToEntries = entries.stream()
.collect(Collectors.groupingBy(ModuleEntry::getModule));
Map<String, Set<Path>> index = new HashMap<>(moduleToEntries.size());
for (Map.Entry<String, List<ModuleEntry>> entry : moduleToEntries.entrySet()) {
String key = entry.getKey();
Set<Path> dirs = entry.getValue()
.stream()
.flatMap(module -> Stream.concat(
Stream.of(module.getModuleDirectory()),
module.getRequires().stream().flatMap(e -> {
List<ModuleEntry> moduleEntries = moduleToEntries.get(e);
if (moduleEntries == null) {
throw new IllegalStateException("Module " + module.getModule() + " requires module " + e + " which is not found in index");
}
return moduleEntries.stream().map(ModuleEntry::getModuleDirectory);
})
))
.filter(Objects::nonNull)
.map(rootPath::resolve)
.collect(Collectors.toSet());
index.put(key, dirs);
}
return index;
} catch (IOException e) {
throw new UncheckedIOException(e);
}

}

private ModuleEntry fromJson(JSONObject json) {
String module = json.optString("module", null);
String moduleDirectory = json.optString("directory", null);
List<String> requires = readRequires(json.optJSONArray("requires"));
return new ModuleEntry(module, moduleDirectory, requires);
}

private List<String> readRequires(JSONArray array) {
List<String> requires = new ArrayList<>();
if (array != null) {
for (int i = 0; i < array.length(); i++) {
requires.add(array.getString(i));
}
}
return requires;
}

/**
* Returns the directory containing the candidate configurations for the given module.
*
* @param groupId the group of the module
* @param artifactId the artifact of the module
* @return the configuration directory
*/
@Override
public Set<Path> findConfigurationDirectories(String groupId, String artifactId) {
String key = groupId + ":" + artifactId;
if (!index.containsKey(key)) {
return Collections.emptySet();
}
return index.get(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.reachability.internal.index.modules;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;

public class StandardLocationModuleToConfigDirectoryIndex implements ModuleToConfigDirectoryIndex {
private final Path rootPath;

public StandardLocationModuleToConfigDirectoryIndex(Path rootPath) {
this.rootPath = rootPath;
}

@Override
public Set<Path> findConfigurationDirectories(String groupId, String artifactId) {
Path candidate = rootPath.resolve(groupId + "/" + artifactId);
if (Files.isDirectory(candidate)) {
return Collections.singleton(candidate);
}
return Collections.emptySet();
}
}
Loading
Loading