@@ -163,31 +163,42 @@ private static boolean isEntityType(JSONObject jsonSchema) {
163163 private static Schema loadMainSchema (
164164 String schemaPath , String entityType , String schemaUri , SchemaClient schemaClient )
165165 throws SchemaProcessingException {
166- InputStream schemaInputStream =
167- SchemaFieldExtractor .class .getClassLoader ().getResourceAsStream (schemaPath );
168- if (schemaInputStream == null ) {
169- LOG .error ("Schema file not found at path: {}" , schemaPath );
170- throw new SchemaProcessingException (
171- "Schema file not found for entity type: " + entityType ,
172- SchemaProcessingException .ErrorType .RESOURCE_NOT_FOUND );
173- }
174-
175- JSONObject rawSchema = new JSONObject (new JSONTokener (schemaInputStream ));
176- SchemaLoader schemaLoader =
177- SchemaLoader .builder ()
178- .schemaJson (rawSchema )
179- .resolutionScope (schemaUri )
180- .schemaClient (schemaClient )
181- .build ();
166+ try (InputStream schemaInputStream =
167+ SchemaFieldExtractor .class .getClassLoader ().getResourceAsStream (schemaPath )) {
168+ if (schemaInputStream == null ) {
169+ LOG .error ("Schema file not found at path: {}" , schemaPath );
170+ throw new SchemaProcessingException (
171+ "Schema file not found for entity type: " + entityType ,
172+ SchemaProcessingException .ErrorType .RESOURCE_NOT_FOUND );
173+ }
182174
183- try {
184- Schema schema = schemaLoader .load ().build ();
185- LOG .debug ("Schema '{}' loaded successfully." , schemaPath );
186- return schema ;
187- } catch (Exception e ) {
188- LOG .error ("Error loading schema '{}': {}" , schemaPath , e .getMessage ());
175+ JSONObject rawSchema = new JSONObject (new JSONTokener (schemaInputStream ));
176+ SchemaLoader schemaLoader =
177+ SchemaLoader .builder ()
178+ .schemaJson (rawSchema )
179+ .resolutionScope (schemaUri )
180+ .schemaClient (schemaClient )
181+ .build ();
182+
183+ try {
184+ Schema schema = schemaLoader .load ().build ();
185+ LOG .debug ("Schema '{}' loaded successfully." , schemaPath );
186+ return schema ;
187+ } catch (Exception e ) {
188+ LOG .error ("Error loading schema '{}': {}" , schemaPath , e .getMessage ());
189+ throw new SchemaProcessingException (
190+ "Error loading schema '" + schemaPath + "': " + e .getMessage (),
191+ SchemaProcessingException .ErrorType .INVALID_SCHEMA ,
192+ e );
193+ }
194+ } catch (IOException e ) {
195+ LOG .error ("Error closing schema input stream '{}': {}" , schemaPath , e .getMessage ());
189196 throw new SchemaProcessingException (
190- "Error loading schema '" + schemaPath + "': " + e .getMessage (),
197+ "Error closing schema input stream: " + e .getMessage (),
198+ SchemaProcessingException .ErrorType .INVALID_SCHEMA ,
199+ e );
200+ }
201+ }
191202 SchemaProcessingException .ErrorType .OTHER );
192203 }
193204 }
@@ -414,30 +425,36 @@ private Schema resolveSchemaByType(String typeName, String schemaUri, SchemaClie
414425
415426 private Schema loadSchema (String schemaPath , String schemaUri , SchemaClient schemaClient )
416427 throws SchemaProcessingException {
417- InputStream schemaInputStream = getClass ().getClassLoader ().getResourceAsStream (schemaPath );
418- if (schemaInputStream == null ) {
419- LOG .error ("Schema file not found at path: {}" , schemaPath );
420- throw new SchemaProcessingException (
421- "Schema file not found for path: " + schemaPath ,
422- SchemaProcessingException .ErrorType .RESOURCE_NOT_FOUND );
423- }
424-
425- JSONObject rawSchema = new JSONObject (new JSONTokener (schemaInputStream ));
426- SchemaLoader schemaLoader =
427- SchemaLoader .builder ()
428- .schemaJson (rawSchema )
429- .resolutionScope (schemaUri ) // Base URI for resolving $ref
430- .schemaClient (schemaClient )
431- .build ();
428+ try (InputStream schemaInputStream = getClass ().getClassLoader ().getResourceAsStream (schemaPath )) {
429+ if (schemaInputStream == null ) {
430+ LOG .error ("Schema file not found at path: {}" , schemaPath );
431+ throw new SchemaProcessingException (
432+ "Schema file not found for path: " + schemaPath ,
433+ SchemaProcessingException .ErrorType .RESOURCE_NOT_FOUND );
434+ }
432435
433- try {
434- Schema schema = schemaLoader .load ().build ();
435- LOG .debug ("Schema '{}' loaded successfully." , schemaPath );
436- return schema ;
437- } catch (Exception e ) {
438- LOG .error ("Error loading schema '{}': {}" , schemaPath , e .getMessage ());
436+ JSONObject rawSchema = new JSONObject (new JSONTokener (schemaInputStream ));
437+ SchemaLoader schemaLoader =
438+ SchemaLoader .builder ()
439+ .schemaJson (rawSchema )
440+ .resolutionScope (schemaUri ) // Base URI for resolving $ref
441+ .schemaClient (schemaClient )
442+ .build ();
443+
444+ try {
445+ Schema schema = schemaLoader .load ().build ();
446+ LOG .debug ("Schema '{}' loaded successfully." , schemaPath );
447+ return schema ;
448+ } catch (Exception e ) {
449+ LOG .error ("Error loading schema '{}': {}" , schemaPath , e .getMessage ());
450+ throw new SchemaProcessingException (
451+ "Error loading schema '" + schemaPath + "': " + e .getMessage (),
452+ SchemaProcessingException .ErrorType .OTHER );
453+ }
454+ } catch (IOException e ) {
455+ LOG .error ("Error closing schema input stream '{}': {}" , schemaPath , e .getMessage ());
439456 throw new SchemaProcessingException (
440- "Error loading schema '" + schemaPath + "' : " + e .getMessage (),
457+ "Error closing schema input stream : " + e .getMessage (),
441458 SchemaProcessingException .ErrorType .OTHER );
442459 }
443460 }
0 commit comments