aboutsummaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/java/com/yahoo/container/plugin/bundle/AnalyzeBundle.java
blob: 0626c786822e26f9f4b33434f8d12369b9ff591c (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.bundle;

import com.yahoo.container.plugin.osgi.ExportPackageParser;
import com.yahoo.container.plugin.osgi.ExportPackages.Export;
import com.yahoo.container.plugin.util.JarFiles;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

/**
 * "Public package" in this context means a package that is either exported or declared as "Global-Package"
 * in a bundle's manifest.
 *
 * @author Tony Vaagenes
 * @author ollivir
 */
public class AnalyzeBundle {
    public static class PublicPackages {
        public final List<Export> exports;
        public final List<String> globals;

        PublicPackages(List<Export> exports, List<String> globals) {
            this.exports = exports;
            this.globals = globals;
        }

        public Set<String> exportedPackageNames() {
            return exports.stream()
                    .map(Export::getPackageNames)
                    .flatMap(Collection::stream)
                    .collect(Collectors.toSet());
        }

    }

    public static PublicPackages publicPackagesAggregated(Collection<File> jarFiles) {
        List<Export> exports = new ArrayList<>();
        List<String> globals = new ArrayList<>();

        for (File jarFile : jarFiles) {
            PublicPackages pp = publicPackages(jarFile);
            exports.addAll(pp.exports);
            globals.addAll(pp.globals);
        }
        return new PublicPackages(exports, globals);
    }

    public static PublicPackages publicPackages(File jarFile) {
        try {
            Optional<Manifest> jarManifest = JarFiles.getManifest(jarFile);
            if (jarManifest.isPresent()) {
                Manifest manifest = jarManifest.get();
                if (isOsgiManifest(manifest)) {
                    return new PublicPackages(parseExports(manifest), parseGlobals(manifest));
                }
            }
            return new PublicPackages(Collections.emptyList(), Collections.emptyList());
        } catch (Exception e) {
            throw new RuntimeException(String.format("Invalid manifest in bundle '%s'", jarFile.getPath()), e);
        }
    }

    public static Optional<String> bundleSymbolicName(File jarFile) {
        return JarFiles.getManifest(jarFile).flatMap(AnalyzeBundle::getBundleSymbolicName);
    }

    private static List<Export> parseExportsFromAttribute(Manifest manifest, String attributeName) {
        return getMainAttributeValue(manifest, attributeName).map(ExportPackageParser::parseExports).orElseGet(() -> new ArrayList<>());
    }

    private static List<Export> parseExports(Manifest jarManifest) {
        return parseExportsFromAttribute(jarManifest, "Export-Package");
    }

    private static List<String> parseGlobals(Manifest manifest) {
        List<Export> globals = parseExportsFromAttribute(manifest, "Global-Package");

        for (Export export : globals) {
            if (export.getParameters().isEmpty() == false) {
                throw new RuntimeException("Parameters not valid for Global-Package.");
            }
        }

        return globals.stream().flatMap(g -> g.getPackageNames().stream()).collect(Collectors.toList());
    }

    private static Optional<String> getMainAttributeValue(Manifest manifest, String attributeName) {
        return Optional.ofNullable(manifest.getMainAttributes().getValue(attributeName));
    }

    private static boolean isOsgiManifest(Manifest mf) {
        return getBundleSymbolicName(mf).isPresent();
    }

    private static Optional<String> getBundleSymbolicName(Manifest mf) {
        return getMainAttributeValue(mf, "Bundle-SymbolicName");
    }
}