summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/application/validation/AbstractBundleValidator.java
blob: 12c482f3fdbc398f066081c95e248bd1fde163a8 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.ComponentInfo;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.path.Path;
import com.yahoo.text.XML;
import com.yahoo.vespa.model.application.validation.Validation.Context;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.regex.Pattern;

/**
 * Base class for OSGi bundle validator.
 *
 * @author bjorncs
 */
public abstract class AbstractBundleValidator implements Validator {

    protected interface JarContext {
        void illegal(String error);
        void illegal(String error, Throwable cause);
        DeployState deployState();
        static JarContext of(Context context) {
            return new JarContext() {
                @Override public void illegal(String error) { context.illegal(error); }
                @Override public void illegal(String error, Throwable cause) { context.illegal(error, cause); }
                @Override public DeployState deployState() { return context.deployState(); }
            };
        }
    }

    protected abstract void validateManifest(JarContext context, JarFile jar, Manifest mf);
    protected abstract void validatePomXml(JarContext context, JarFile jar, Document pom);

    @Override
    public final void validate(Context context) {
        ApplicationPackage app = context.deployState().getApplicationPackage();
        for (ComponentInfo info : app.getComponentsInfo(context.deployState().getVespaVersion())) {
            Path path = Path.fromString(info.getPathRelativeToAppDir());
            try {
                context.deployState().getDeployLogger()
                        .log(Level.FINE, String.format("Validating bundle at '%s'", path));
                JarFile jarFile = new JarFile(app.getFileReference(path));
                validateJarFile(JarContext.of(context), jarFile);
            } catch (IOException e) {
                context.illegal("Failed to validate JAR file '" + path.last() + "'", e);
            }
        }
    }

    final void validateJarFile(JarContext context, JarFile jar) throws IOException {
        Manifest manifest = jar.getManifest();
        if (manifest == null) {
            context.illegal("Non-existing or invalid manifest in " + filename(jar));
        }
        validateManifest(context, jar, manifest);
        getPomXmlContent(context::illegal, context.deployState().getDeployLogger(), jar).ifPresent(pom -> validatePomXml(context, jar, pom));
    }

    protected final String filename(JarFile jarFile) { return Paths.get(jarFile.getName()).getFileName().toString(); }

    protected final void forEachPomXmlElement(Document pom, String xpath, Consumer<Element> consumer) throws XPathExpressionException {
        NodeList dependencies = (NodeList) XPathFactory.newDefaultInstance().newXPath()
                .compile("/project/" + xpath)
                .evaluate(pom, XPathConstants.NODESET);
        for (int i = 0; i < dependencies.getLength(); i++) {
            Element element = (Element) dependencies.item(i);
            consumer.accept(element);
        }
    }

    protected final void forEachImportPackage(Manifest mf, Consumer<String> consumer) {
        String importPackage = mf.getMainAttributes().getValue("Import-Package");
        ImportPackageInfo importPackages = new ImportPackageInfo(importPackage);

        for (String packageName : importPackages.packages()) {
            consumer.accept(packageName);
        }
    }

    protected final void log(DeployState state, Level level, String fmt, Object... args) {
        state.getDeployLogger().logApplicationPackage(level, String.format(fmt, args));
    }

    private static final Pattern POM_FILE_LOCATION = Pattern.compile("META-INF/maven/.+?/.+?/pom.xml");
    public Optional<Document> getPomXmlContent(BiConsumer<String, Throwable> context, DeployLogger logger, JarFile jar) {
        return jar.stream()
                .filter(f -> POM_FILE_LOCATION.matcher(f.getName()).matches())
                .findFirst()
                .map(f -> {
                    try {
                        String text = new String(jar.getInputStream(f).readAllBytes());
                        return XML.getDocumentBuilder(false)
                                .parse(new InputSource(new StringReader(text)));
                    } catch (SAXException e) {
                        String message = String.format("Unable to parse pom.xml from %s", filename(jar));
                        logger.log(Level.SEVERE, message);
                        context.accept(message, e);
                    } catch (IOException e) {
                        logger.log(Level.INFO,
                                String.format("Unable to read '%s' from '%s'", f.getName(), jar.getName()));
                    }
                    return null;
                });
    }
}