exceptionfactory commented on code in PR #8005: URL: https://github.com/apache/nifi/pull/8005#discussion_r1420621232
########## nifi-nar-bundles/nifi-registry-bundle/nifi-registry-service/src/main/java/org/apache/nifi/schemaregistry/services/InMemoryJsonSchemaRegistry.java: ########## @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.schemaregistry.services; + +import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.SpecVersion; +import org.apache.nifi.annotation.behavior.DynamicProperty; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.schema.access.JsonSchema; +import org.apache.nifi.schema.access.JsonSchemaRegistryComponent; +import org.apache.nifi.schema.access.SchemaNotFoundException; +import org.apache.nifi.schema.access.SchemaVersion; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.apache.commons.lang3.StringUtils.isNotBlank; + +@Tags({"schema", "registry", "json"}) +@CapabilityDescription("Provides a service for registering and accessing schemas. One can register a schema " + + "as a dynamic property where 'name' represents the schema name and 'value' represents the textual " + + "representation of the actual schema following the syntax and semantics of JSON's Schema format. " + + "Empty schemas and schemas only consisting of whitespace are not acceptable schemas." + + "The registry is heterogeneous registry as it can store schemas of multiple schema draft versions. " + + "By default the registry is configured to store schemas of Draft 2020-12. When a schema is added, the version " + + "which is currently is set, is what the schema is saved as.") +@DynamicProperty(name = "Schema name", value = "Schema Content", + description = "Adds a named schema using the JSON string representation of a JSON schema", + expressionLanguageScope = ExpressionLanguageScope.NONE) +public class InMemoryJsonSchemaRegistry extends AbstractControllerService implements JsonSchemaRegistry, JsonSchemaRegistryComponent { + + private static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS = Collections.singletonList(SCHEMA_VERSION); + + private final ConcurrentMap<String, JsonSchema> jsonSchemas; + private final ConcurrentMap<SchemaVersion, JsonSchemaFactory> schemaFactories; + private volatile SchemaVersion schemaVersion; + + public InMemoryJsonSchemaRegistry() { + jsonSchemas = new ConcurrentHashMap<>(); + schemaFactories = Arrays.stream(SchemaVersion.values()) + .collect(Collectors.toConcurrentMap(Function.identity(), + schemaDraftVersion -> JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.fromId(schemaDraftVersion.getUri()).get()))); + schemaVersion = SchemaVersion.valueOf(SCHEMA_VERSION.getDefaultValue()); + } + + @Override + public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) { + if (SCHEMA_VERSION.getName().equals(descriptor.getName()) && !newValue.equals(oldValue)) { + schemaVersion = SchemaVersion.valueOf(newValue); + } else if(descriptor.isDynamic() && isBlank(newValue)) { + jsonSchemas.remove(descriptor.getName()); + } else if (descriptor.isDynamic() && isNotBlank(newValue)) { + try { + final String schemaName = descriptor.getName(); + final JsonSchemaFactory jsonSchemaFactory = schemaFactories.get(schemaVersion); + jsonSchemaFactory.getSchema(newValue); + jsonSchemas.put(schemaName, new JsonSchema(schemaVersion, newValue)); + } catch (final Exception e) { + // not a problem - the service won't be valid and the validation message will indicate what is wrong. Review Comment: The `TestRunner` has methods to add and enable Controller Services, which also populate member variables, like the Logger. It is possible to call the `initialize()` method directly, to provide those values, but using the TestRunner is a better approach. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org