-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept support for native image layers
- Loading branch information
Showing
12 changed files
with
810 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
common/utils/src/main/java/org/graalvm/buildtools/utils/JarMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2003-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.graalvm.buildtools.utils; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
public class JarMetadata { | ||
private final List<String> packageList; | ||
|
||
public JarMetadata(List<String> packageList) { | ||
this.packageList = packageList; | ||
} | ||
|
||
public List<String> getPackageList() { | ||
return packageList; | ||
} | ||
|
||
public static JarMetadata readFrom(Path propertiesFile) { | ||
Properties props = new Properties(); | ||
try (InputStream is = Files.newInputStream(propertiesFile)) { | ||
props.load(is); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Unable to read metadata from properties file " + propertiesFile, e); | ||
} | ||
String packages = (String) props.get("packages"); | ||
List<String> packageList = packages == null ? List.of() : Arrays.asList(packages.split(",")); | ||
return new JarMetadata(packageList); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
common/utils/src/main/java/org/graalvm/buildtools/utils/JarScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2003-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.graalvm.buildtools.utils; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.Writer; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Performs scanning of a JAR file and extracts some metadata in the | ||
* form of a properties file. For now this type only extracts the list | ||
* of packages from a jar. | ||
*/ | ||
public class JarScanner { | ||
/** | ||
* Scans a jar and creates a properties file with metadata about the jar contents. | ||
* @param inputJar the input jar | ||
* @param outputFile the output file | ||
* @throws IOException | ||
*/ | ||
public static void scanJar(Path inputJar, Path outputFile) throws IOException { | ||
try (Writer fileWriter = Files.newBufferedWriter(outputFile); PrintWriter writer = new PrintWriter(fileWriter)) { | ||
Set<String> packageList = new TreeSet<>(); | ||
try (FileSystem jarFileSystem = FileSystems.newFileSystem(inputJar, null)) { | ||
Path root = jarFileSystem.getPath("/"); | ||
try (Stream<Path> files = Files.walk(root)) { | ||
files.forEach(path -> { | ||
if (path.toString().endsWith(".class")) { | ||
Path relativePath = root.relativize(path); | ||
String className = relativePath.toString() | ||
.replace('/', '.') | ||
.replace('\\', '.') | ||
.replace(".class", ""); | ||
var lastDot = className.lastIndexOf("."); | ||
if (lastDot > 0) { | ||
packageList.add(className.substring(0, lastDot)); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
writer.println("packages=" + String.join(",", packageList)); | ||
} catch (IOException ex) { | ||
throw new RuntimeException("Unable to write JAR analysis", ex); | ||
} | ||
} | ||
} |
Oops, something went wrong.