|
| 1 | +package com.evolvedbinary.bblValidator.service; |
| 2 | + |
| 3 | +import com.evolvedbinary.bblValidator.dto.SchemaInfo; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import jakarta.annotation.PostConstruct; |
| 6 | +import jakarta.inject.Singleton; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +@Singleton |
| 21 | +public class SchemaService { |
| 22 | + |
| 23 | + private static final Logger LOG = LoggerFactory.getLogger(SchemaService.class); |
| 24 | + private static final String SCHEMA_DIRECTORY = "schemas"; |
| 25 | + |
| 26 | + private final List<SchemaInfo> schemas = new ArrayList<>(); |
| 27 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 28 | + |
| 29 | + @PostConstruct |
| 30 | + public void loadSchemas() { |
| 31 | + try { |
| 32 | + // Load schemas from classpath |
| 33 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 34 | + |
| 35 | + // Get all .json files from the schemas directory |
| 36 | + try (InputStream is = classLoader.getResourceAsStream(SCHEMA_DIRECTORY)) { |
| 37 | + if (is == null) { |
| 38 | + LOG.warn("Schemas directory not found in classpath"); |
| 39 | + return; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Scan for schema metadata files |
| 44 | + loadSchemasFromClasspath(); |
| 45 | + |
| 46 | + LOG.info("Loaded {} schemas from disk", schemas.size()); |
| 47 | + } catch (Exception e) { |
| 48 | + LOG.error("Error loading schemas from disk", e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void loadSchemasFromClasspath() { |
| 53 | + try { |
| 54 | + // Get resource URL and list files |
| 55 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 56 | + var resource = classLoader.getResource(SCHEMA_DIRECTORY); |
| 57 | + |
| 58 | + if (resource != null) { |
| 59 | + Path schemaPath = Paths.get(resource.toURI()); |
| 60 | + |
| 61 | + try (Stream<Path> paths = Files.walk(schemaPath, 1)) { |
| 62 | + paths.filter(path -> path.toString().endsWith(".json")) |
| 63 | + .forEach(this::loadSchemaMetadata); |
| 64 | + } |
| 65 | + } |
| 66 | + } catch (Exception e) { |
| 67 | + LOG.error("Error scanning schema directory", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void loadSchemaMetadata(Path metadataPath) { |
| 72 | + try { |
| 73 | + String content = Files.readString(metadataPath, StandardCharsets.UTF_8); |
| 74 | + SchemaInfo schemaInfo = objectMapper.readValue(content, SchemaInfo.class); |
| 75 | + |
| 76 | + // Load corresponding schema file |
| 77 | + String schemaFileName = metadataPath.getFileName().toString().replace(".json", ".csvs"); |
| 78 | + Path schemaFilePath = metadataPath.getParent().resolve(schemaFileName); |
| 79 | + |
| 80 | + if (Files.exists(schemaFilePath)) { |
| 81 | + schemas.add(schemaInfo); |
| 82 | + LOG.debug("Loaded schema: {}", schemaInfo.getId()); |
| 83 | + } else { |
| 84 | + LOG.warn("Schema file not found for metadata: {}", schemaFileName); |
| 85 | + } |
| 86 | + } catch (IOException e) { |
| 87 | + LOG.error("Error loading schema metadata from: {}", metadataPath, e); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public List<SchemaInfo> listSchemas() { |
| 92 | + return new ArrayList<>(schemas); |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments