+ public boolean handleBaselineDatabase() throws PlatformException {
+ Path workspaceLocation = Platform.getLocation().toFile().toPath();
+ Path baselineIndicatorFile = workspaceLocation.resolve(".baselined");
+ if (Files.isRegularFile(baselineIndicatorFile)) {
+ // This means that the workspace has already been initialized from
+ // a database baseline and further initialization is not necessary.
+ return true;
+ }
+
+ String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
+ if (dbBaselineArchive == null)
+ return false;
+
+ Path baseline = Paths.get(dbBaselineArchive);
+ if (!Files.isRegularFile(baseline))
+ throw new PlatformException("Specified database baseline archive " + baseline + " does not exist. Cannot initialize workspace database.");
+
+ validateBaselineFile(baseline);
+ validateWorkspaceForBaselineInitialization(workspaceLocation);
+
+ try {
+ Files.createDirectories(workspaceLocation);
+ FileUtils.extractZip(baseline.toFile(), workspaceLocation.toFile());
+ Files.write(baselineIndicatorFile, baselineIndicatorContents(baselineIndicatorFile));
+ return true;
+ } catch (IOException e) {
+ throw new PlatformException(e);
+ }
+ }
+
+ private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("d. MMM yyyy HH:mm:ss");
+
+ private static byte[] baselineIndicatorContents(Path path) throws IOException {
+ return String.format("%s%n%s%n",
+ path.toString(),
+ Instant.now().atZone(ZoneId.systemDefault()).format(TIMESTAMP_FORMAT))
+ .getBytes("UTF-8");
+ }
+
+ private void validateWorkspaceForBaselineInitialization(Path workspaceLocation) throws PlatformException {
+ try {
+ Path db = workspaceLocation.resolve("db");
+ if (Files.exists(db))
+ throw new PlatformException("Database location " + db + " already exists. Cannot re-initialize workspace from baseline.");
+ Path index = workspaceLocation.resolve(".metadata/.plugins/org.simantics.db.indexing");
+ if (!Files.exists(index) || !isEmptyDirectory(index))
+ throw new PlatformException("Index location " + index + " already exists. Cannot re-initialize workspace from baseline.");
+ } catch (IOException e) {
+ throw new PlatformException("Failed to validate workspace for baseline initialization", e);
+ }
+ }
+
+ private static boolean isEmptyDirectory(Path dir) throws IOException {
+ return Files.walk(dir).count() == 1;
+ }
+
+ private void validateBaselineFile(Path baseline) throws PlatformException {
+ try (ZipFile zip = new ZipFile(baseline.toFile())) {
+ ZipEntry db = zip.getEntry("db");
+ if (db == null)
+ throw new PlatformException("Baseline archive does not contain database directory 'db'");
+ ZipEntry index = zip.getEntry(".metadata/.plugins/org.simantics.db.indexing");
+ if (index == null)
+ throw new PlatformException("Baseline archive does not contain database index directory '.metadata/.plugins/org.simantics.db.indexing'");
+ } catch (IOException e) {
+ throw new PlatformException("Failed to validate baseline archive " + baseline, e);
+ }
+ }
+