sergehuber commented on a change in pull request #353: URL: https://github.com/apache/unomi/pull/353#discussion_r744555747
########## File path: services/src/main/java/org/apache/unomi/services/impl/schemas/SchemaRegistryImpl.java ########## @@ -0,0 +1,224 @@ +/* + * 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.unomi.services.impl.schemas; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.networknt.schema.*; +import com.networknt.schema.uri.URIFetcher; +import org.apache.commons.lang3.StringUtils; +import org.apache.unomi.api.SchemaType; +import org.apache.unomi.api.services.ProfileService; +import org.apache.unomi.api.services.SchemaRegistry; +import org.apache.unomi.persistence.spi.CustomObjectMapper; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.SynchronousBundleListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URL; +import java.util.*; +import java.util.stream.Collectors; + +public class SchemaRegistryImpl implements SchemaRegistry, SynchronousBundleListener { + + private static final String URI = "https://json-schema.org/draft/2019-09/schema"; + + private static final Logger logger = LoggerFactory.getLogger(SchemaRegistryImpl.class.getName()); + + private final Map<Long, List<SchemaType>> schemaTypesByBundle = new HashMap<>(); + private final Map<String, SchemaType> schemaTypesById = new HashMap<>(); + + private final Map<String, JsonSchema> jsonSchemasById = new LinkedHashMap<>(); + + private final Map<String, Long> bundleIdBySchemaId = new HashMap<>(); + + private BundleContext bundleContext; + + private ProfileService profileService; + + public void bundleChanged(BundleEvent event) { + switch (event.getType()) { + case BundleEvent.STARTED: + processBundleStartup(event.getBundle().getBundleContext()); + break; + case BundleEvent.STOPPING: + processBundleStop(event.getBundle().getBundleContext()); + break; + } + } + + public void init() { + processBundleStartup(bundleContext); + + // process already started bundles + for (Bundle bundle : bundleContext.getBundles()) { + if (bundle.getBundleContext() != null && bundle.getBundleId() != bundleContext.getBundle().getBundleId()) { + processBundleStartup(bundle.getBundleContext()); + } + } + + bundleContext.addBundleListener(this); + logger.info("Schema registry initialized."); + } + + public void destroy() { + bundleContext.removeBundleListener(this); + logger.info("Schema registry shutdown."); + } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + public void setProfileService(ProfileService profileService) { + this.profileService = profileService; + } + + public boolean isValid(Object object, String schemaId) { + JsonSchema jsonSchema = jsonSchemasById.get(schemaId); + if (jsonSchema != null) { + try { + // this is a workaround to get the JsonNode from the object, maybe there is a better way (more efficient) to do this ? Review comment: Thanks, I've changed to use this method. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
