Skip to content

Commit

Permalink
Fix #265 list licenses (#11993)
Browse files Browse the repository at this point in the history
List licenses in --list-classpath
Add SPDX to all our manifests
handle License and Licence
  • Loading branch information
gregw authored Jul 9, 2024
1 parent c880d93 commit 29d27f7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

import org.eclipse.jetty.start.Props.Prop;
Expand Down Expand Up @@ -135,7 +141,7 @@ private void copyInThread(final InputStream in, final OutputStream out)
}).start();
}

private void dumpClasspathWithVersions(String name, PrintStream out, Classpath classpath)
private void listClasspath(String name, PrintStream out, Classpath classpath)
{
StartLog.endStartLog();
out.println();
Expand All @@ -154,8 +160,93 @@ private void dumpClasspathWithVersions(String name, PrintStream out, Classpath c
int i = 0;
for (Path element : classpath.getElements())
{
out.printf("%2d: %24s | %s\n", i++, getVersion(element), baseHome.toShortForm(element));
String license = getLicenceFromJar(element);
if (license != null && !license.isEmpty())
out.printf("%2d: %24s | %s | %s\n", i++, getVersion(element), baseHome.toShortForm(element), license);
else
out.printf("%2d: %24s | %s\n", i++, getVersion(element), baseHome.toShortForm(element));
}
}

private String getLicenceFromJar(Path jar)
{
if (!Files.exists(jar) || Files.isDirectory(jar) || !Files.isReadable(jar))
return null;
try
{
try (JarFile jarFile = new JarFile(jar.toFile()))
{
Manifest manifest = jarFile.getManifest();
if (manifest != null)
{
String spdxLicense = manifest.getMainAttributes().getValue("SPDX-License-Identifier");
if (spdxLicense != null)
return spdxLicense;

String bundleLicense = manifest.getMainAttributes().getValue("Bundle-License");
if (bundleLicense != null)
return bundleLicense;
}

Optional<String> license = jarFile.stream().filter(Main::isLicenseFile).map(e -> getLicenceFromFile(jarFile, e)).filter(Objects::nonNull).findFirst();
if (license.isPresent())
return license.get();

}
}
catch (Throwable ignored)
{
}
return null;
}

private static boolean isLicenseFile(JarEntry entry)
{
String name = entry.getName();
return name.matches("(?i)^(META-INF/)?LICEN[SC]E.*") || name.matches("(?i)^LICEN[SC]E.*");
}

private String getLicenceFromFile(JarFile jarFile, JarEntry entry)
{
try
{
try (BufferedReader reader = new BufferedReader(new InputStreamReader(jarFile.getInputStream(entry))))
{
String line;
StringBuilder licenseBuilder = new StringBuilder();
int nonEmptyLines = 0;

List<String> links = new ArrayList<>();

while ((line = reader.readLine()) != null)
{
line = line.trim();
if (!line.isEmpty())
{
if (line.contains("SPDX-License-Identifier:"))
return line.substring(line.indexOf(':') + 1).trim();

if (line.startsWith("http:") || line.startsWith("https:"))
links.add(line);

if (nonEmptyLines < 2)
{
licenseBuilder.append(line).append(" ");
nonEmptyLines++;
}
}
}

if (!links.isEmpty())
return links.stream().max(Comparator.comparingInt(String::length)).get();

return nonEmptyLines > 0 ? licenseBuilder.toString().trim() : null;
}
}
catch (Throwable ignored)
{
}
return null;
}

public BaseHome getBaseHome()
Expand Down Expand Up @@ -243,7 +334,7 @@ public void listConfig(PrintStream out, StartArgs args)
// Dump Jetty Properties
jettyEnvironment.dumpProperties(out);
// Dump Jetty Classpath
dumpClasspathWithVersions(jettyEnvironment.getName(), out, jettyEnvironment.getClasspath());
listClasspath(jettyEnvironment.getName(), out, jettyEnvironment.getClasspath());
// Dump Jetty Resolved XMLs
jettyEnvironment.dumpActiveXmls(out);

Expand All @@ -252,7 +343,7 @@ public void listConfig(PrintStream out, StartArgs args)
// Dump Properties
environment.dumpProperties(out);
// Dump Classpath
dumpClasspathWithVersions(environment.getName(), out, environment.getClasspath());
listClasspath(environment.getName(), out, environment.getClasspath());
// Dump Resolved XMLs
environment.dumpActiveXmls(out);
}
Expand Down Expand Up @@ -400,7 +491,7 @@ public void start(StartArgs args) throws IOException, InterruptedException
// Show the version information and return
if (args.isListClasspath())
{
dumpClasspathWithVersions("Jetty", System.out, classpath);
listClasspath("Jetty", System.out, classpath);
}

// Show configuration
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,7 @@
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor>Eclipse Jetty Project</Implementation-Vendor>
<SPDX-License-Identifier>EPL-2.0 OR Apache-2.0</SPDX-License-Identifier>
<url>${jetty.url}</url>
</manifestEntries>
</archive>
Expand Down

0 comments on commit 29d27f7

Please sign in to comment.