aboutsummaryrefslogtreecommitdiffstats
path: root/container-dev-builder/tools/src/main/java/com/yahoo/container/dev/builder/PreinstalledBundleResolver.java
blob: 33717615d3ab1519643ee1cef05a0538b9082bf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.dev.builder;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.jar.Manifest;

public class PreinstalledBundleResolver {

    private static final Path MANIFEST_MF = Paths.get("MANIFEST.MF");
    private static final String X_JDISC_PREINSTALL_BUNDLE = "X-JDisc-Preinstall-Bundle";
    private static final String REMOVABLE_SUFFIX = ".jar";
    private static final String REMOVABLE_ASSEMBLY_ID = "-jar-with-dependencies";

    public static void main(final String[] args) throws Throwable {
        Files.walkFileTree(Paths.get(args[0]), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                if (!attrs.isRegularFile()) {
                    return FileVisitResult.CONTINUE;
                }
                if (!file.getFileName().equals(MANIFEST_MF)) {
                    return FileVisitResult.CONTINUE;
                }
                final String preinstall = new Manifest(Files.newInputStream(file))
                        .getMainAttributes()
                        .getValue(X_JDISC_PREINSTALL_BUNDLE);
                if (preinstall == null) {
                    return FileVisitResult.CONTINUE;
                }
                for (String bundle : preinstall.split(",")) {
                    printDependency(args[1], bundle);
                }
                return super.visitFile(file, attrs);
            }
        });
    }

    private static void printDependency(String pomFormat, String bundle) throws IOException {
        bundle = bundle.trim();
        if (bundle.isEmpty()) {
            return;
        }
        if (bundle.endsWith(REMOVABLE_SUFFIX)) {
            bundle = bundle.substring(0, bundle.length() - REMOVABLE_SUFFIX.length());
        }
        if (bundle.endsWith(REMOVABLE_ASSEMBLY_ID)) {
            bundle = bundle.substring(0, bundle.length() - REMOVABLE_ASSEMBLY_ID.length());
        }
        Path pom = Paths.get(String.format(pomFormat, bundle));
        if (!Files.exists(pom)) {
            return;
        }
        Model model;
        try {
            model = new MavenXpp3Reader().read(Files.newBufferedReader(pom, StandardCharsets.UTF_8));
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            return;
        }
        model.setPomFile(pom.toFile());
        final MavenProject project = new MavenProject(model);
        System.out.println(project.getGroupId() + ":" +
                           project.getArtifactId() + ":jar:" +
                           project.getVersion() + ":compile");
    }
}