22
33import com .evolvedbinary .bblValidator .dto .SchemaInfo ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import io .micronaut .context .annotation .Value ;
56import jakarta .annotation .PostConstruct ;
67import jakarta .inject .Singleton ;
78import org .slf4j .Logger ;
89import org .slf4j .LoggerFactory ;
910
1011import java .io .IOException ;
11- import java .io .InputStream ;
1212import java .nio .charset .StandardCharsets ;
1313import java .nio .file .Files ;
1414import java .nio .file .Path ;
2424public class SchemaService {
2525
2626 private static final Logger LOG = LoggerFactory .getLogger (SchemaService .class );
27- private static final String SCHEMA_DIRECTORY = "schemas" ;
27+
28+ @ Value ("${schema.directory}" )
29+ private String schemaDirectory ;
2830
2931 private final List <SchemaInfo > schemas = new ArrayList <>();
3032 private final Map <String , String > schemaContents = new HashMap <>();
@@ -34,71 +36,75 @@ public class SchemaService {
3436 @ PostConstruct
3537 public void loadSchemas () {
3638 try {
37- // Load schemas from classpath
38- ClassLoader classLoader = getClass ().getClassLoader ();
39-
40- // Get all .json files from the schemas directory
41- try (InputStream is = classLoader .getResourceAsStream (SCHEMA_DIRECTORY )) {
42- if (is == null ) {
43- LOG .warn ("Schemas directory not found in classpath" );
44- return ;
45- }
39+ // Resolve schema directory path
40+ Path schemaPath = resolveSchemaPath ();
41+
42+ if (!Files .exists (schemaPath )) {
43+ LOG .warn ("Schemas directory not found: {}" , schemaPath );
44+ return ;
45+ }
46+
47+ if (!Files .isDirectory (schemaPath )) {
48+ LOG .warn ("Schema path is not a directory: {}" , schemaPath );
49+ return ;
4650 }
4751
4852 // Scan for schema metadata files
49- loadSchemasFromClasspath ( );
53+ loadSchemasFromFileSystem ( schemaPath );
5054
51- LOG .info ("Loaded {} schemas from disk " , schemas .size ());
55+ LOG .info ("Loaded {} schemas from: {} " , schemas .size (), schemaPath );
5256 } catch (Exception e ) {
53- LOG .error ("Error loading schemas from disk " , e );
57+ LOG .error ("Error loading schemas from file system " , e );
5458 }
5559 }
5660
57- private void loadSchemasFromClasspath () {
58- // close the file input stream
59- // try with resoucres
60- // avoid class path
61- // in application yaml add a property for schema folder
62- // make it relative to the start up location
63- // if it starts with a slash then resolve it as absolut path
64- try {
65- // Get resource URL and list files
66- ClassLoader classLoader = getClass ().getClassLoader ();
67- var resource = classLoader .getResource (SCHEMA_DIRECTORY );
68-
69- if (resource != null ) {
70- Path schemaPath = Paths .get (resource .toURI ());
71-
72- try (Stream <Path > paths = Files .walk (schemaPath , 1 )) {
73- paths .filter (path -> path .toString ().endsWith (".json" ))
74- .forEach (this ::loadSchemaMetadata );
75- }
76- }
77- } catch (Exception e ) {
78- LOG .error ("Error scanning schema directory" , e );
61+ /**
62+ * Resolves the schema directory path.
63+ * If the path starts with a slash, it's treated as an absolute path.
64+ * Otherwise, it's resolved relative to the current working directory (startup location).
65+ */
66+ private Path resolveSchemaPath () {
67+ if (schemaDirectory .startsWith ("/" )) {
68+ // Absolute path
69+ return Paths .get (schemaDirectory );
70+ } else {
71+ // Relative to current working directory
72+ return Paths .get (System .getProperty ("user.dir" ), schemaDirectory );
7973 }
8074 }
8175
82- private void loadSchemaMetadata (Path metadataPath ) {
83- try {
84- String content = Files .readString (metadataPath , StandardCharsets .UTF_8 );
85- SchemaInfo schemaInfo = objectMapper .readValue (content , SchemaInfo .class );
86-
87- // Load corresponding schema file
88- String schemaFileName = metadataPath .getFileName ().toString ().replace (".json" , ".csvs" );
89- Path schemaFilePath = metadataPath .getParent ().resolve (schemaFileName );
90-
91- if (Files .exists (schemaFilePath )) {
92- String schemaContent = Files .readString (schemaFilePath , StandardCharsets .UTF_8 );
93- schemaContents .put (schemaInfo .getId (), schemaContent );
94- schemaFilePaths .put (schemaInfo .getId (), schemaFilePath );
95- schemas .add (schemaInfo );
96- LOG .debug ("Loaded schema: {}" , schemaInfo .getId ());
97- } else {
98- LOG .warn ("Schema file not found for metadata: {}" , schemaFileName );
99- }
76+ private void loadSchemasFromFileSystem (Path schemaPath ) {
77+ try (Stream <Path > paths = Files .walk (schemaPath , 1 )) {
78+ paths .filter (path -> path .toString ().endsWith (".json" ))
79+ .forEach (path -> {
80+ try {
81+ loadSchemaMetadata (path );
82+ } catch (Exception e ) {
83+ LOG .error ("Error loading schema metadata from: {}" , path , e );
84+ }
85+ });
10086 } catch (IOException e ) {
101- LOG .error ("Error loading schema metadata from: {}" , metadataPath , e );
87+ LOG .error ("Error scanning schema directory: {}" , schemaPath , e );
88+ }
89+ }
90+
91+ private void loadSchemaMetadata (Path metadataPath ) throws IOException {
92+ String content = Files .readString (metadataPath , StandardCharsets .UTF_8 );
93+ SchemaInfo schemaInfo = objectMapper .readValue (content , SchemaInfo .class );
94+
95+ // Load corresponding schema file
96+ String schemaFileName = metadataPath .getFileName ().toString ().replace (".json" , ".csvs" );
97+ Path schemaFilePath = metadataPath .getParent ().resolve (schemaFileName );
98+
99+ if (Files .exists (schemaFilePath )) {
100+ String schemaContent = Files .readString (schemaFilePath , StandardCharsets .UTF_8 );
101+ // TODO: Only load the schema content when needed
102+ schemaContents .put (schemaInfo .getId (), schemaContent );
103+ schemaFilePaths .put (schemaInfo .getId (), schemaFilePath );
104+ schemas .add (schemaInfo );
105+ LOG .debug ("Loaded schema: {}" , schemaInfo .getId ());
106+ } else {
107+ LOG .warn ("Schema file not found for metadata: {}" , schemaFileName );
102108 }
103109 }
104110
0 commit comments