diff options
author | Harald Musum <musum@verizonmedia.com> | 2019-08-14 08:21:18 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2019-08-14 08:21:18 +0200 |
commit | af8ac01b3883882c792cc872b03aa77ef81a179a (patch) | |
tree | 79e761fd933bcd1b90fa37da720d597fb0c72137 | |
parent | 23566b7d1b574cf21bfdfc587706f2bddebc29c9 (diff) |
Cleanup of config definition name and namespace
Config definition name and namespace are mandatory, so no need
to fallback to default namespace anymore
9 files changed, 144 insertions, 41 deletions
diff --git a/config-application-package/SchemaValidator.java b/config-application-package/SchemaValidator.java new file mode 100644 index 00000000000..35fdc57eeb1 --- /dev/null +++ b/config-application-package/SchemaValidator.java @@ -0,0 +1,121 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.model.application.provider; + +import com.thaiopensource.util.PropertyMap; +import com.thaiopensource.util.PropertyMapBuilder; +import com.thaiopensource.validate.ValidateProperty; +import com.thaiopensource.validate.ValidationDriver; +import com.thaiopensource.validate.rng.CompactSchemaReader; +import com.yahoo.config.application.api.DeployLogger; +import com.yahoo.io.reader.NamedReader; +import com.yahoo.yolean.Exceptions; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; +import java.util.logging.Level; + +/** + * Validates xml files against a schema. + * + * @author Tony Vaagenes + */ +public class SchemaValidator { + private static final int linesOfContextForErrors = 3; + + private final CustomErrorHandler errorHandler = new CustomErrorHandler(); + private final ValidationDriver driver; + private final DeployLogger deployLogger; + + /** + * Initializes the validator by using the given file as schema file + * + * @param schemaFile schema file + * @throws IOException if it is not possible to read schema files + */ + SchemaValidator(File schemaFile, DeployLogger deployLogger) throws IOException, SAXException { + this.deployLogger = deployLogger; + this.driver = new ValidationDriver(PropertyMap.EMPTY, instanceProperties(), CompactSchemaReader.getInstance()); + driver.loadSchema(ValidationDriver.fileInputSource(schemaFile)); + } + + public void validate(File file) throws IOException { + validate(file, file.getName()); + } + + public void validate(File file, String fileName) throws IOException { + validate(ValidationDriver.fileInputSource(file), fileName); + } + + public void validate(Reader reader) throws IOException { + validate(new InputSource(reader), null); + } + + public void validate(NamedReader reader) throws IOException { + validate(new InputSource(reader), reader.getName()); + } + + public void validate(InputSource inputSource, String fileName) throws IOException { + errorHandler.fileName = (fileName == null ? "input" : fileName); + errorHandler.reader = inputSource.getCharacterStream(); + try { + if ( ! driver.validate(inputSource)) { + // Shouldn't happen, error handler should have thrown + throw new RuntimeException("Aborting due to earlier XML errors."); + } + } catch (SAXException e) { + // This should never happen, as it is handled by the ErrorHandler + // installed for the driver. + throw new IllegalArgumentException("XML error in " + errorHandler.fileName + ": " + Exceptions.toMessageString(e)); + } + } + + private PropertyMap instanceProperties() { + PropertyMapBuilder builder = new PropertyMapBuilder(); + builder.put(ValidateProperty.ERROR_HANDLER, errorHandler); + return builder.toPropertyMap(); + } + + private class CustomErrorHandler implements ErrorHandler { + volatile String fileName; + volatile Reader reader; + + public void warning(SAXParseException e) { + deployLogger.log(Level.WARNING, message(e)); + } + + public void error(SAXParseException e) { + throw new IllegalArgumentException(message(e)); + } + + public void fatalError(SAXParseException e) { + throw new IllegalArgumentException(message(e)); + } + + private String message(SAXParseException e) { + return "XML error in " + fileName + ": " + + Exceptions.toMessageString(e) + + " [" + e.getLineNumber() + ":" + e.getColumnNumber() + "]" + + ", input\n" + getErrorContext(e.getLineNumber()); + } + + private String getErrorContext(int lineNumberWithError) { + if (!(reader instanceof StringReader)) return ""; + + return ""; + + + + + + } + + + } + +} diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java index 58318958dfb..d9253d6105b 100644 --- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java +++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java @@ -1,7 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.model.application.provider; -import com.yahoo.config.codegen.CNode; import com.yahoo.config.model.api.ConfigDefinitionRepo; import com.yahoo.io.IOUtils; import com.yahoo.log.LogLevel; @@ -16,7 +15,6 @@ import java.io.IOException; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.stream.Collectors; /** * A global pool of all config definitions that this server knows about. These objects can be shared @@ -48,8 +46,6 @@ public class StaticConfigDefinitionRepo implements ConfigDefinitionRepo { private void addConfigDefinition(File def) { try { ConfigDefinitionKey key = ConfigUtils.createConfigDefinitionKeyFromDefFile(def); - if (key.getNamespace().isEmpty()) - key = new ConfigDefinitionKey(key.getName(), CNode.DEFAULT_NAMESPACE); addConfigDefinition(key, def); } catch (IOException e) { log.log(LogLevel.WARNING, "Exception adding config definition " + def, e); diff --git a/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java b/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java index f90b399d305..a8404b076d4 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java @@ -135,8 +135,7 @@ class InstanceResolver { } static String packageName(ConfigDefinitionKey cKey, PackagePrefix packagePrefix) { - String prefix = packagePrefix.value; - return prefix + (cKey.getNamespace().isEmpty() ? CNode.DEFAULT_NAMESPACE : cKey.getNamespace()); + return packagePrefix.value + cKey.getNamespace(); } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java index d07f6c4e6fd..31231857aae 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/DomConfigPayloadBuilder.java @@ -86,11 +86,15 @@ public class DomConfigPayloadBuilder { } private static boolean validName(String name) { + if (name == null) return false; + Matcher m = namePattern.matcher(name); return m.matches(); } private static boolean validNamespace(String namespace) { + if (namespace == null) return false; + Matcher m = namespacePattern.matcher(namespace); return m.matches(); } diff --git a/config-model/src/test/java/com/yahoo/config/model/ApplicationDeployTest.java b/config-model/src/test/java/com/yahoo/config/model/ApplicationDeployTest.java index ae8e8fe7de5..ec9d631dec5 100644 --- a/config-model/src/test/java/com/yahoo/config/model/ApplicationDeployTest.java +++ b/config-model/src/test/java/com/yahoo/config/model/ApplicationDeployTest.java @@ -5,9 +5,10 @@ import com.google.common.io.Files; import com.yahoo.config.ConfigInstance; import com.yahoo.config.application.api.ApplicationMetaData; import com.yahoo.config.application.api.UnparsedConfigDefinition; -import com.yahoo.config.codegen.CNode; import com.yahoo.config.application.api.ApplicationPackage; -import com.yahoo.config.model.application.provider.*; +import com.yahoo.config.model.application.provider.Bundle; +import com.yahoo.config.model.application.provider.DeployData; +import com.yahoo.config.model.application.provider.FilesApplicationPackage; import com.yahoo.config.model.deploy.DeployState; import com.yahoo.path.Path; import com.yahoo.document.DataType; @@ -338,19 +339,16 @@ public class ApplicationDeployTest { DeployState deployState = new DeployState.Builder().applicationPackage(app).build(); - ConfigDefinition def = deployState.getConfigDefinition(new ConfigDefinitionKey("foo", CNode.DEFAULT_NAMESPACE)).get(); - assertThat(def.getNamespace(), is(CNode.DEFAULT_NAMESPACE)); - - def = deployState.getConfigDefinition(new ConfigDefinitionKey("baz", "xyzzy")).get(); + ConfigDefinition def = deployState.getConfigDefinition(new ConfigDefinitionKey("baz", "xyzzy")).get(); assertThat(def.getNamespace(), is("xyzzy")); def = deployState.getConfigDefinition(new ConfigDefinitionKey("foo", "qux")).get(); assertThat(def.getNamespace(), is("qux")); // A config def without version in filename and version in file header - def = deployState.getConfigDefinition(new ConfigDefinitionKey("xyzzy", CNode.DEFAULT_NAMESPACE)).get(); - assertThat(def.getNamespace(), is(CNode.DEFAULT_NAMESPACE)); - assertThat(def.getName(), is("xyzzy")); + def = deployState.getConfigDefinition(new ConfigDefinitionKey("bar", "xyzzy")).get(); + assertThat(def.getNamespace(), is("xyzzy")); + assertThat(def.getName(), is("bar")); } @Test(expected=IllegalArgumentException.class) diff --git a/config-model/src/test/java/com/yahoo/vespa/model/builder/UserConfigBuilderTest.java b/config-model/src/test/java/com/yahoo/vespa/model/builder/UserConfigBuilderTest.java index 6bfd55ca5de..bbed96d3251 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/builder/UserConfigBuilderTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/builder/UserConfigBuilderTest.java @@ -1,13 +1,13 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.model.builder; -import com.yahoo.test.ArraytypesConfig; import com.yahoo.config.ConfigInstance; import com.yahoo.config.model.application.provider.BaseDeployLogger; +import com.yahoo.config.model.builder.xml.XmlHelper; import com.yahoo.config.model.deploy.ConfigDefinitionStore; -import com.yahoo.test.SimpletypesConfig; import com.yahoo.config.model.producer.UserConfigRepo; -import com.yahoo.config.model.builder.xml.XmlHelper; +import com.yahoo.test.ArraytypesConfig; +import com.yahoo.test.SimpletypesConfig; import com.yahoo.vespa.config.ConfigDefinitionKey; import com.yahoo.vespa.config.ConfigPayload; import com.yahoo.vespa.config.ConfigPayloadBuilder; @@ -22,7 +22,9 @@ import java.io.StringReader; import java.util.Optional; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; /** * @author Ulf Lilleengen diff --git a/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java b/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java index 56ede8897ed..d7653572773 100644 --- a/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java +++ b/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java @@ -2,7 +2,6 @@ package com.yahoo.vespa.config.util; import com.yahoo.collections.Tuple2; -import com.yahoo.config.codegen.CNode; import com.yahoo.io.HexDump; import com.yahoo.io.IOUtils; import com.yahoo.net.HostName; @@ -302,9 +301,6 @@ public class ConfigUtils { */ static ConfigDefinitionKey createConfigDefinitionKeyFromDefContent(String name, byte[] content) { String namespace = ConfigUtils.getDefNamespace(new StringReader(Utf8.toString(content))); - if (namespace.isEmpty()) { - namespace = CNode.DEFAULT_NAMESPACE; - } return new ConfigDefinitionKey(name, namespace); } diff --git a/configgen/src/main/java/com/yahoo/config/codegen/CNode.java b/configgen/src/main/java/com/yahoo/config/codegen/CNode.java index ea88e115530..1c1fb5f5bce 100644 --- a/configgen/src/main/java/com/yahoo/config/codegen/CNode.java +++ b/configgen/src/main/java/com/yahoo/config/codegen/CNode.java @@ -3,17 +3,13 @@ package com.yahoo.config.codegen; import java.util.StringTokenizer; -import static com.yahoo.config.codegen.DefParser.DEFAULT_PACKAGE_PREFIX; - /** - * Abstract superclass for all nodes representing a definition file. + * Abstract superclass for all nodes representing a config definition. * * @author gjoranv */ public abstract class CNode { - public static final String DEFAULT_NAMESPACE = "config"; - // TODO: replace by "type" enum public final boolean isArray; public final boolean isMap; diff --git a/configgen/src/main/java/com/yahoo/config/codegen/JavaClassBuilder.java b/configgen/src/main/java/com/yahoo/config/codegen/JavaClassBuilder.java index 75149d7a50e..40c903ee929 100644 --- a/configgen/src/main/java/com/yahoo/config/codegen/JavaClassBuilder.java +++ b/configgen/src/main/java/com/yahoo/config/codegen/JavaClassBuilder.java @@ -23,11 +23,10 @@ import static com.yahoo.config.codegen.DefParser.DEFAULT_PACKAGE_PREFIX; */ public class JavaClassBuilder implements ClassBuilder { - public static final String INDENTATION = " "; + static final String INDENTATION = " "; private final InnerCNode root; private final NormalizedDefinition nd; - private final String packagePrefix; private final String javaPackage; private final String className; private final File destDir; @@ -35,7 +34,7 @@ public class JavaClassBuilder implements ClassBuilder { public JavaClassBuilder(InnerCNode root, NormalizedDefinition nd, File destDir, String rawPackagePrefix) { this.root = root; this.nd = nd; - this.packagePrefix = (rawPackagePrefix != null) ? rawPackagePrefix : DEFAULT_PACKAGE_PREFIX; + String packagePrefix = (rawPackagePrefix != null) ? rawPackagePrefix : DEFAULT_PACKAGE_PREFIX; this.javaPackage = (root.getPackage() != null) ? root.getPackage() : packagePrefix + root.getNamespace(); this.className = createClassName(root.getName()); this.destDir = destDir; @@ -74,15 +73,7 @@ public class JavaClassBuilder implements ClassBuilder { "import java.io.File;\n" + // "import java.nio.file.Path;\n" + // "import edu.umd.cs.findbugs.annotations.NonNull;\n" + // - getImportFrameworkClasses(root.getNamespace()); - } - - private String getImportFrameworkClasses(String namespace) { - if (CNode.DEFAULT_NAMESPACE.equals(namespace) == false) { - return "import " + packagePrefix + CNode.DEFAULT_NAMESPACE + ".*;"; - } else { - return ""; - } + "import com.yahoo.config.*;"; } // TODO: remove the extra comment line " *" if root.getCommentBlock is empty @@ -96,7 +87,7 @@ public class JavaClassBuilder implements ClassBuilder { " public final static String CONFIG_DEF_MD5 = \"" + root.getMd5() + "\";\n" + // " public final static String CONFIG_DEF_NAME = \"" + root.getName() + "\";\n" + // " public final static String CONFIG_DEF_NAMESPACE = \"" + root.getNamespace() + "\";\n" + // - " public final static String CONFIG_DEF_VERSION = \"" + root.getVersion() + "\";\n" + // TODO: Remove on Vespa 8 + " public final static String CONFIG_DEF_VERSION = \"" + root.getVersion() + "\";\n" + // TODO: Remove in Vespa 8 " public final static String[] CONFIG_DEF_SCHEMA = {\n" + // "" + indentCode(INDENTATION + INDENTATION, getDefSchema()) + "\n" + // " };\n" + // |