From e18b53f89116e8153e2023b3da55ee4c52443648 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Tue, 11 Jan 2022 14:44:25 +0100 Subject: Misc cleanup Propagate jar file as method parameter. Fix linting warning on generic cast. Cleanup exception handling. --- .../application/validation/ComponentValidator.java | 44 +++++++++------------- .../validation/ComponentValidatorTest.java | 10 ++--- 2 files changed, 22 insertions(+), 32 deletions(-) (limited to 'config-model') diff --git a/config-model/src/main/java/com/yahoo/vespa/model/application/validation/ComponentValidator.java b/config-model/src/main/java/com/yahoo/vespa/model/application/validation/ComponentValidator.java index 21e396959a7..66fe9308ae7 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/application/validation/ComponentValidator.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/application/validation/ComponentValidator.java @@ -2,11 +2,12 @@ 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.vespa.model.VespaModel; -import com.yahoo.config.application.api.ComponentInfo; -import com.yahoo.config.application.api.DeployLogger; + import java.io.IOException; import java.util.Arrays; import java.util.HashSet; @@ -16,49 +17,39 @@ import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; import java.util.logging.Level; -import java.util.zip.ZipException; /** * A validator for bundles. Uses BND library for some of the validation (not active yet) * * @author hmusum - * @since 2010-11-11 + * @author bjorncs */ public class ComponentValidator extends Validator { - private JarFile jarFile; - public ComponentValidator() { - } - - public ComponentValidator(JarFile jarFile) { - this.jarFile = jarFile; - } + public ComponentValidator() {} @Override public void validate(VespaModel model, DeployState deployState) { ApplicationPackage app = deployState.getApplicationPackage(); for (ComponentInfo info : app.getComponentsInfo(deployState.getVespaVersion())) { try { - this.jarFile = new JarFile(app.getFileReference(Path.fromString(info.getPathRelativeToAppDir()))); - } catch (ZipException e) { - throw new IllegalArgumentException("Error opening jar file '" + info.getPathRelativeToAppDir() + - "'. Please check that this is a valid jar file"); - } catch (IOException e) { - e.printStackTrace(); - } - try { - validateAll(deployState.getDeployLogger()); + Path path = Path.fromString(info.getPathRelativeToAppDir()); + DeployLogger deployLogger = deployState.getDeployLogger(); + deployLogger.log(Level.FINE, String.format("Validating bundle at '%s'", path)); + JarFile jarFile = new JarFile(app.getFileReference(path)); + validateJarFile(deployLogger, jarFile); } catch (IOException e) { - e.printStackTrace(); + throw new IllegalArgumentException( + "Failed to validate JAR file '" + info.getPathRelativeToAppDir() + "'", e); } } } - public void validateAll(DeployLogger deployLogger) throws IOException { - validateOSGIHeaders(deployLogger); + void validateJarFile(DeployLogger deployLogger, JarFile jarFile) throws IOException { + validateOSGIHeaders(deployLogger, jarFile); } - public void validateOSGIHeaders(DeployLogger deployLogger) throws IOException { + public void validateOSGIHeaders(DeployLogger deployLogger, JarFile jarFile) throws IOException { Manifest mf = jarFile.getManifest(); if (mf == null) { throw new IllegalArgumentException("Non-existing or invalid manifest in " + jarFile.getName()); @@ -67,9 +58,8 @@ public class ComponentValidator extends Validator { // Check for required OSGI headers Attributes attributes = mf.getMainAttributes(); HashSet mfAttributes = new HashSet<>(); - for (Object attributeSet : attributes.entrySet()) { - Map.Entry e = (Map.Entry) attributeSet; - mfAttributes.add(e.getKey().toString()); + for (Map.Entry entry : attributes.entrySet()) { + mfAttributes.add(entry.getKey().toString()); } List requiredOSGIHeaders = Arrays.asList( "Bundle-ManifestVersion", "Bundle-Name", "Bundle-SymbolicName", "Bundle-Version"); diff --git a/config-model/src/test/java/com/yahoo/vespa/model/application/validation/ComponentValidatorTest.java b/config-model/src/test/java/com/yahoo/vespa/model/application/validation/ComponentValidatorTest.java index a375621d391..7045af60fc9 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/application/validation/ComponentValidatorTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/application/validation/ComponentValidatorTest.java @@ -21,8 +21,8 @@ public class ComponentValidatorTest { public void basicComponentValidation() throws Exception { // Valid jar file JarFile ok = new JarFile(new File(JARS_DIR + "ok.jar")); - ComponentValidator componentValidator = new ComponentValidator(ok); - componentValidator.validateAll(new BaseDeployLogger()); + ComponentValidator componentValidator = new ComponentValidator(); + componentValidator.validateJarFile(new BaseDeployLogger(), ok); // No manifest validateWithException("nomanifest.jar", "Non-existing or invalid manifest in " + JARS_DIR + "nomanifest.jar"); @@ -31,8 +31,8 @@ public class ComponentValidatorTest { private void validateWithException(String jarName, String exceptionMessage) throws IOException { try { JarFile jarFile = new JarFile(JARS_DIR + jarName); - ComponentValidator componentValidator = new ComponentValidator(jarFile); - componentValidator.validateAll(new BaseDeployLogger()); + ComponentValidator componentValidator = new ComponentValidator(); + componentValidator.validateJarFile(new BaseDeployLogger(), jarFile); assert (false); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), exceptionMessage); @@ -50,7 +50,7 @@ public class ComponentValidatorTest { } }; - new ComponentValidator(new JarFile(JARS_DIR + "snapshot_bundle.jar")).validateAll(logger); + new ComponentValidator().validateJarFile(logger, new JarFile(JARS_DIR + "snapshot_bundle.jar")); assertTrue(buffer.toString().contains("Deploying snapshot bundle")); } } -- cgit v1.2.3