3434import java .io .FileInputStream ;
3535import java .io .FileNotFoundException ;
3636import java .io .IOException ;
37- import java .net .URISyntaxException ;
38- import java .net .URL ;
37+ import java .io .InputStream ;
3938import java .util .ArrayList ;
4039import java .util .HashMap ;
4140import java .util .List ;
@@ -105,63 +104,76 @@ public NodeSchemaLoadResult loadSchemaFromFile(String schemaFilePath)
105104 throws IllegalArgumentException , FileNotFoundException {
106105 try {
107106 File schemaFile = new File (schemaFilePath );
108- if (!schemaFile .exists ()) {
107+
108+ if (schemaFile .exists ()) {
109+ LOG .info ("Load network topology schema file " +
110+ schemaFile .getAbsolutePath ());
111+ try (FileInputStream inputStream = new FileInputStream (schemaFile )) {
112+ return loadSchemaFromStream (schemaFilePath , inputStream );
113+ }
114+ } else {
109115 // try to load with classloader
110116 ClassLoader classloader =
111117 Thread .currentThread ().getContextClassLoader ();
112118 if (classloader == null ) {
113119 classloader = NodeSchemaLoader .class .getClassLoader ();
114120 }
115121 if (classloader != null ) {
116- URL url = classloader .getResource (schemaFilePath );
117- if (url != null ) {
118- schemaFile = new File (url .toURI ());
122+ try (InputStream stream = classloader
123+ .getResourceAsStream (schemaFilePath )) {
124+ if (stream != null ) {
125+ LOG .info ("Loading file from " + classloader
126+ .getResources (schemaFilePath ));
127+ return loadSchemaFromStream (schemaFilePath , stream );
128+ }
119129 }
120130 }
121- }
122131
123- if (!schemaFile .exists ()) {
124- String msg = "Network topology layer schema file " +
125- schemaFilePath + "[" + schemaFile .getAbsolutePath () +
126- "] is not found." ;
127- LOG .warn (msg );
128- throw new FileNotFoundException (msg );
129132 }
130133
131- LOG .info ("Load network topology schema file " +
132- schemaFile .getCanonicalPath ());
133- if (FilenameUtils .getExtension (schemaFilePath ).toLowerCase ()
134- .compareTo ("yaml" ) == 0 ) {
135- return loadSchemaFromYaml (schemaFile );
136- } else {
137- return loadSchema (schemaFile );
138- }
134+ String msg = "Network topology layer schema file " +
135+ schemaFilePath + "[" + schemaFile .getAbsolutePath () +
136+ "] is not found." ;
137+ LOG .warn (msg );
138+ throw new FileNotFoundException (msg );
139+
139140 } catch (FileNotFoundException e ) {
140141 throw e ;
141- } catch (ParserConfigurationException | IOException | SAXException |
142- URISyntaxException e ) {
142+ } catch (ParserConfigurationException | IOException | SAXException e ) {
143143 throw new IllegalArgumentException ("Failed to load network topology node"
144- + " schema file: " + schemaFilePath + " , error:" + e .getMessage ());
144+ + " schema file: " + schemaFilePath + " , error:" + e .getMessage (),
145+ e );
146+ }
147+ }
148+
149+ private NodeSchemaLoadResult loadSchemaFromStream (String schemaFilePath ,
150+ InputStream stream )
151+ throws ParserConfigurationException , SAXException , IOException {
152+ if (FilenameUtils .getExtension (schemaFilePath ).toLowerCase ()
153+ .compareTo ("yaml" ) == 0 ) {
154+ return loadSchemaFromYaml (stream );
155+ } else {
156+ return loadSchema (stream );
145157 }
146158 }
147159
148160 /**
149161 * Load network topology layer schemas from a XML configuration file.
150- * @param schemaFile schema file
162+ * @param inputStream schema file as an inputStream
151163 * @return all valid node schemas defined in schema file
152164 * @throws ParserConfigurationException ParserConfigurationException happen
153165 * @throws IOException no such schema file
154166 * @throws SAXException xml file has some invalid elements
155167 * @throws IllegalArgumentException xml file content is logically invalid
156168 */
157- private NodeSchemaLoadResult loadSchema (File schemaFile ) throws
169+ private NodeSchemaLoadResult loadSchema (InputStream inputStream ) throws
158170 ParserConfigurationException , SAXException , IOException {
159- LOG .info ("Loading network topology layer schema file " + schemaFile );
171+ LOG .info ("Loading network topology layer schema file" );
160172 // Read and parse the schema file.
161173 DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
162174 dbf .setIgnoringComments (true );
163175 DocumentBuilder builder = dbf .newDocumentBuilder ();
164- Document doc = builder .parse (schemaFile );
176+ Document doc = builder .parse (inputStream );
165177 Element root = doc .getDocumentElement ();
166178
167179 if (!CONFIGURATION_TAG .equals (root .getTagName ())) {
@@ -200,28 +212,27 @@ private NodeSchemaLoadResult loadSchema(File schemaFile) throws
200212
201213 /**
202214 * Load network topology layer schemas from a YAML configuration file.
203- * @param schemaFile schema file
215+ * @param schemaFile as inputStream
204216 * @return all valid node schemas defined in schema file
205217 * @throws ParserConfigurationException ParserConfigurationException happen
206218 * @throws IOException no such schema file
207219 * @throws SAXException xml file has some invalid elements
208220 * @throws IllegalArgumentException xml file content is logically invalid
209221 */
210- private NodeSchemaLoadResult loadSchemaFromYaml (File schemaFile ) {
222+ private NodeSchemaLoadResult loadSchemaFromYaml (InputStream schemaFile ) {
211223 LOG .info ("Loading network topology layer schema file {}" , schemaFile );
212224 NodeSchemaLoadResult finalSchema ;
213225
214226 try {
215227 Yaml yaml = new Yaml ();
216228 NodeSchema nodeTree ;
217229
218- try (FileInputStream fileInputStream = new FileInputStream (schemaFile )) {
219- nodeTree = yaml .loadAs (fileInputStream , NodeSchema .class );
220- }
230+ nodeTree = yaml .loadAs (schemaFile , NodeSchema .class );
231+
221232 List <NodeSchema > schemaList = new ArrayList <>();
222233 if (nodeTree .getType () != LayerType .ROOT ) {
223234 throw new IllegalArgumentException ("First layer is not a ROOT node."
224- + " schema file: " + schemaFile . getAbsolutePath () );
235+ + " schema file." );
225236 }
226237 schemaList .add (nodeTree );
227238 if (nodeTree .getSublayer () != null ) {
@@ -232,11 +243,11 @@ private NodeSchemaLoadResult loadSchemaFromYaml(File schemaFile) {
232243 if (nodeTree .getType () == LayerType .LEAF_NODE
233244 && nodeTree .getSublayer () != null ) {
234245 throw new IllegalArgumentException ("Leaf node in the middle of path."
235- + " schema file: " + schemaFile . getAbsolutePath () );
246+ + " schema file." );
236247 }
237248 if (nodeTree .getType () == LayerType .ROOT ) {
238249 throw new IllegalArgumentException ("Multiple root nodes are defined."
239- + " schema file: " + schemaFile . getAbsolutePath () );
250+ + " schema file." );
240251 }
241252 schemaList .add (nodeTree );
242253 if (nodeTree .getSublayer () != null ) {
@@ -246,12 +257,10 @@ private NodeSchemaLoadResult loadSchemaFromYaml(File schemaFile) {
246257 }
247258 }
248259 finalSchema = new NodeSchemaLoadResult (schemaList , true );
249- } catch (RuntimeException e ) {
250- throw e ;
251260 } catch (Exception e ) {
252261 throw new IllegalArgumentException ("Fail to load network topology node"
253- + " schema file: " + schemaFile . getAbsolutePath () + " , error:"
254- + e .getMessage ());
262+ + " schema file: " + schemaFile + " , error:"
263+ + e .getMessage (), e );
255264 }
256265
257266 return finalSchema ;
0 commit comments