11/*
2- Copyright 2016-2024 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
2+ Copyright 2016-2026 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
33Under the terms of Contract DE-NA0003525 with NTESS,
44the U.S. Government retains certain rights in this software.
55*/
66
77package gov .sandia .n2a .db ;
88
9- import java .io .BufferedReader ;
109import java .io .IOException ;
11- import java .io .InputStreamReader ;
1210import java .lang .ref .Cleaner ;
11+ import java .net .URI ;
12+ import java .net .URL ;
1313import java .nio .file .DirectoryStream ;
14+ import java .nio .file .FileSystem ;
15+ import java .nio .file .FileSystems ;
16+ import java .nio .file .FileVisitResult ;
1417import java .nio .file .Files ;
1518import java .nio .file .Path ;
1619import java .nio .file .Paths ;
20+ import java .nio .file .SimpleFileVisitor ;
21+ import java .nio .file .attribute .BasicFileAttributes ;
1722import java .util .ArrayList ;
23+ import java .util .Collections ;
1824import java .util .HashMap ;
1925import java .util .List ;
2026import java .util .Map ;
21- import java .util .zip .ZipEntry ;
22- import java .util .zip .ZipInputStream ;
23-
2427import gov .sandia .n2a .ui .settings .SettingsRepo ;
2528
2629/**
@@ -215,6 +218,47 @@ public static void checkInitialDB ()
215218 {
216219 if (repos .size () > 0 ) return ;
217220
221+ Path root = Paths .get (properties .get ("resourceDir" )).toAbsolutePath ();
222+ Path reposDir = root .resolve ("repos" );
223+
224+ // Extract initial repos.
225+ FileSystem fs = null ; // Would be nice to use try-with-resources, but we don't know if fs will even be opened.
226+ try
227+ {
228+ URL source = AppData .class .getProtectionDomain ().getCodeSource ().getLocation ();
229+ Path sourcePath = Paths .get (source .toURI ());
230+ if (Files .isRegularFile (sourcePath )) // Assume regular file is JAR.
231+ {
232+ fs = FileSystems .newFileSystem (URI .create ("jar:" + source ), Collections .emptyMap ());
233+ sourcePath = fs .getPath ("/" ); // Get root of JAR, as a Zip filesystem.
234+ }
235+ Path initial = sourcePath .resolve ("gov/sandia/n2a/db/initial" );
236+
237+ Files .walkFileTree (initial , new SimpleFileVisitor <Path > ()
238+ {
239+ public FileVisitResult visitFile (final Path source , final BasicFileAttributes attrs ) throws IOException
240+ {
241+ Path relative = initial .relativize (source );
242+ if (relative .getNameCount () == 3 )
243+ {
244+ Path destination = reposDir .resolve (relative .toString ()); // toString() is necessary to avoid mismatch when source is from Zip filesystem.
245+ Files .createDirectories (destination .getParent ()); // TODO: find a way to not repeat this for every file
246+ Files .copy (source , destination );
247+ }
248+ return FileVisitResult .CONTINUE ;
249+ }
250+ });
251+ }
252+ catch (Exception e )
253+ {
254+ System .err .println ("Unable to load some or all of initial DB" );
255+ e .printStackTrace ();
256+ }
257+ try {if (fs != null ) fs .close ();}
258+ catch (IOException e ) {}
259+
260+ // Configure repos
261+
218262 repos .set (1 , "local" , "visible" );
219263 repos .set (1 , "local" , "editable" );
220264 repos .set (1 , "base" , "visible" );
@@ -223,8 +267,6 @@ public static void checkInitialDB ()
223267 state .set ("local" , "Repos" , "primary" );
224268 state .set ("" , "Repos" , "needUpstream" );
225269
226- Path root = Paths .get (properties .get ("resourceDir" )).toAbsolutePath ();
227- Path reposDir = root .resolve ("repos" );
228270 Path baseDir = reposDir .resolve ("base" );
229271 Path localDir = reposDir .resolve ("local" );
230272
@@ -250,37 +292,6 @@ public static void checkInitialDB ()
250292 referenceContainers .add (baseReferences );
251293 docs .link (new MFolder ("models" , modelContainers ));
252294 docs .link (new MFolder ("references" , referenceContainers ));
253-
254- try (ZipInputStream zip = new ZipInputStream (AppData .class .getResource ("initialDB.zip" ).openStream ()))
255- {
256- ZipEntry entry ;
257- while ((entry = zip .getNextEntry ()) != null )
258- {
259- if (entry .isDirectory ()) continue ;
260- String name = entry .getName ();
261- String [] pieces = name .split ("/" );
262- if (pieces .length != 3 ) continue ;
263- MDir dir = null ;
264- if (pieces [0 ].equals ("local" ))
265- {
266- if (pieces [1 ].equals ("references" )) dir = localReferences ;
267- else dir = localModels ;
268- }
269- else
270- {
271- if (pieces [1 ].equals ("references" )) dir = baseReferences ;
272- else dir = baseModels ;
273- }
274- MDoc doc = (MDoc ) dir .childOrCreate (pieces [2 ]);
275- BufferedReader reader = new BufferedReader (new InputStreamReader (zip , "UTF-8" ));
276- Schema .readAll (doc , reader );
277- }
278- }
279- catch (Exception e )
280- {
281- System .err .println ("Unable to load some or all of initial DB" );
282- e .printStackTrace ();
283- }
284295 }
285296
286297 // Utility for converting documents to latest schema.
0 commit comments