diff --git a/thirdparty/ClasspathSimplification/.gitignore b/thirdparty/ClasspathSimplification/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..afac81c9575ee1461bc5c18a8336b1289be2e827 --- /dev/null +++ b/thirdparty/ClasspathSimplification/.gitignore @@ -0,0 +1,2 @@ +/target/* +!/target/ClasspathSimplification-1.0-jar-with-dependencies.jar \ No newline at end of file diff --git a/thirdparty/ClasspathSimplification/pom.xml b/thirdparty/ClasspathSimplification/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..96c6f9f78402d4c43f4371d56eb55f379e5b5566 --- /dev/null +++ b/thirdparty/ClasspathSimplification/pom.xml @@ -0,0 +1,50 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.sleuthkit.autopsy.classpathsimplication</groupId> + <artifactId>ClasspathSimplification</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.source>17</maven.compiler.source> + <maven.compiler.target>17</maven.compiler.target> + <exec.mainClass>org.sleuthkit.autopsy.classpathsimplication.classpathsimplification.ClasspathSimplification</exec.mainClass> + </properties> + <dependencies> + <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> + <dependency> + <groupId>org.apache.ant</groupId> + <artifactId>ant</artifactId> + <version>1.10.12</version> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>org.sleuthkit.autopsy.classpathsimplification.ClasspathSimplification</mainClass> + </manifest> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + <executions> + <execution> + <id>make-assembly</id> <!-- this is used for inheritance merges --> + <phase>package</phase> <!-- bind to the packaging phase --> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file diff --git a/thirdparty/ClasspathSimplification/src/main/java/org/sleuthkit/autopsy/classpathsimplification/ClasspathSimplification.java b/thirdparty/ClasspathSimplification/src/main/java/org/sleuthkit/autopsy/classpathsimplification/ClasspathSimplification.java new file mode 100644 index 0000000000000000000000000000000000000000..5d0792978f572d1c35fd938aa240c3ca155714b0 --- /dev/null +++ b/thirdparty/ClasspathSimplification/src/main/java/org/sleuthkit/autopsy/classpathsimplification/ClasspathSimplification.java @@ -0,0 +1,96 @@ +/* + * Autopsy Forensic Browser + * + * Copyright 2022 Basis Technology Corp. + * Contact: carrier <at> sleuthkit <dot> org + * + * 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 + * + * http://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.sleuthkit.autopsy.classpathsimplification; + +import java.io.File; +import java.lang.System.Logger.Level; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +/** + * Simplifies a class path from a list of jar files separated by ':' to a list + * of directories ending of format '/dir/path/to/jars/*' + */ +public class ClasspathSimplification extends Task { + + // split on ':' but not 'C:\' + private static Pattern CLASS_PATH_REGEX = Pattern.compile("((C:\\\\)?.+?)(:|$)"); + + /** + * @param args The command line arguments. + */ + public static void main(String[] args) { + System.out.println(getSimplifiedClasspath(args.length < 1 ? null : args[0])); + } + + String originalClassPath; + String outputprop; + + public void setClasspath(String originalClassPath) { + this.originalClassPath = originalClassPath; + } + + public void setOutputprop(String outputprop) { + this.outputprop = outputprop; + } + + @Override + public void execute() throws BuildException { + if (outputprop != null && !outputprop.trim().isEmpty()) { + log("Simplifying path..."); + String simplified = getSimplifiedClasspath(originalClassPath); + getProject().setProperty(outputprop, simplified); + } else { + log("No output property provided!", Level.WARNING.getSeverity()); + } + } + + /** + * Simplifies a class path from a list of jar files separated by ':' to a + * list of directories ending of format '/dir/path/to/jars/*' + * + * @param origPath The original path with jar file paths separated by ':' + * @return The parent folders ending with '*' separated by ':'. + */ + public static String getSimplifiedClasspath(String origPath) { + Set<String> directories = new HashSet<>(); + if (origPath == null) { + return ""; + } + + Matcher pathMatch = CLASS_PATH_REGEX.matcher(origPath); + while (pathMatch.find()) { + String thisPath = pathMatch.group(1).trim(); + if (thisPath.toLowerCase().endsWith(".jar")) { + directories.add(Paths.get(thisPath).getParent().toAbsolutePath().toString()); + } + } + + return directories.stream() + .sorted((a, b) -> a.compareToIgnoreCase(b)) + .map(path -> path.endsWith(File.separator) ? path + "*" : path + File.separator + "*") + .collect(Collectors.joining(":")); + } +} diff --git a/thirdparty/ClasspathSimplification/target/ClasspathSimplification-1.0-jar-with-dependencies.jar b/thirdparty/ClasspathSimplification/target/ClasspathSimplification-1.0-jar-with-dependencies.jar new file mode 100644 index 0000000000000000000000000000000000000000..e798d3b2712ef37e5d11f517b9d93f07f688035a Binary files /dev/null and b/thirdparty/ClasspathSimplification/target/ClasspathSimplification-1.0-jar-with-dependencies.jar differ