Use NIO file API instead of File 61/3861/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 11 Feb 2020 11:11:56 +0000 (13:11 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 11 Feb 2020 11:11:56 +0000 (13:11 +0200)
gitlab #462

Change-Id: I1472003a00b42cecb8fe9423707aa8ec8b4717e6

bundles/org.simantics/src/org/simantics/DatabaseBaselines.java

index c58cb508623026fbf5f054cc67645e6d87d1db1a..f781372ad374df903fe4803cdaa6f873beffaef4 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics;
 
-import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -28,119 +27,118 @@ public class DatabaseBaselines {
        private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseBaselines.class);
 
        private static final boolean REQUIRE_INDEX_IN_BASELINE = false;
-       
+
        private static final String DB_DIRECTORY = "db"; //$NON-NLS-1$
        private static final String INDEX_DIRECTORY = ".metadata/.plugins/org.simantics.db.indexing"; //$NON-NLS-1$
 
        private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("d. MMM yyyy HH:mm:ss");
 
-    public static boolean handleBaselineDatabase(Path installLocation, boolean databaseExists) 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;
-        }
-
-        Path baseline = resolveBaselineFile(installLocation);
-        if (baseline == null) {
-            baseline = getAutomaticBaselinePath();
-            if(baseline == null)
-                return false;
-            if(databaseExists)
-                return false;
-            if(!existsAutomaticBaseline())
-                return false;
-        }
-        
-        DatabaseBaselines.validateBaselineFile(baseline);
-        DatabaseBaselines.validateWorkspaceForBaselineInitialization(workspaceLocation);
-        DatabaseBaselines.initializeWorkspaceWithBaseline(baseline, workspaceLocation, baselineIndicatorFile);
-        return true;
-        
-    }
-
-    private static Path resolveBaselineFile(Path installLocation) throws PlatformException {
-        String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
-        if (dbBaselineArchive == null)
-            return null;
-
-        Path baseline = Paths.get(dbBaselineArchive);
-        if (baseline.isAbsolute()) {
-            if (!Files.isRegularFile(baseline))
-                throw new PlatformException("Specified database baseline archive " + baseline
-                        + " does not exist. Cannot initialize workspace database from baseline.");
-            return baseline;
-        }
-
-        // Relative path resolution order:
-        // 1. from the platform "install location"
-        // 2. from working directory
-        //Path installLocation = tryGetInstallLocation();
-        if (installLocation != null) {
-            Path installedBaseline = installLocation.resolve(dbBaselineArchive);
-            if (Files.isRegularFile(installedBaseline))
-                return installedBaseline;
-        }
-        if (!Files.isRegularFile(baseline))
-            throw new PlatformException("Specified database baseline archive " + baseline
-                    + " does not exist in either the install location (" + installLocation
-                    + ") or the working directory (" + Paths.get(".").toAbsolutePath()
-                    + "). Cannot initialize workspace database.");
-        return null;
-    }
-
-    private static boolean useAutomaticBaseline() {
-        return getAutomaticBaselinePath() != null;
-    }
-
-    private static Path getAutomaticBaselinePath() {
-        if("true".equals(System.getProperty("org.simantics.db.baseline.automatic")))
-            return Paths.get("automatic_baseline", "baseline.zip");
-        else
-            return null;
-    }
-    
-    private static boolean existsAutomaticBaseline() {
-        Path baselineFile = getAutomaticBaselinePath();
-        if(baselineFile == null)
-            return false;
-        return Files.exists(baselineFile);
-    }
-
-    public static boolean shouldCreateAutomaticBaseline(boolean existsDatabase) throws PlatformException {
-        if(!useAutomaticBaseline()) {
-            // Are we using this feature? 
-            return false;
-        }
-        if(existsDatabase) {
-            // Baseline can only be created after db initialization
-            return false;
-        }
-        if(existsAutomaticBaseline()) {
-            // Existing baselines should not be automatically overridden
-            return false;
-        }
-        return true;
-    }
-
-    public static void createAutomaticBaseline(Path dbLocation) throws PlatformException {
-
-        if(existsAutomaticBaseline())
-            return;
-
-        try {
-            DatabaseBaselines.packageBaseline(dbLocation.getParent(), getAutomaticBaselinePath());
-        } catch (IOException e) {
-            LOGGER.error("Error while creating automatic baseline", e);
-        }
-
-    }
-
-    public static Path packageBaseline(Path fromWorkspace, Path packageFilePath) throws IOException {
-           File packageFile = packageFilePath.toFile();
-           packageFile.getParentFile().mkdirs();
+       public static boolean handleBaselineDatabase(Path installLocation, boolean databaseExists) 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;
+               }
+
+               Path baseline = resolveBaselineFile(installLocation);
+               if (baseline == null) {
+                       baseline = getAutomaticBaselinePath();
+                       if(baseline == null)
+                               return false;
+                       if(databaseExists)
+                               return false;
+                       if(!existsAutomaticBaseline())
+                               return false;
+               }
+
+               DatabaseBaselines.validateBaselineFile(baseline);
+               DatabaseBaselines.validateWorkspaceForBaselineInitialization(workspaceLocation);
+               DatabaseBaselines.initializeWorkspaceWithBaseline(baseline, workspaceLocation, baselineIndicatorFile);
+               return true;
+
+       }
+
+       private static Path resolveBaselineFile(Path installLocation) throws PlatformException {
+               String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
+               if (dbBaselineArchive == null)
+                       return null;
+
+               Path baseline = Paths.get(dbBaselineArchive);
+               if (baseline.isAbsolute()) {
+                       if (!Files.isRegularFile(baseline))
+                               throw new PlatformException("Specified database baseline archive " + baseline
+                                               + " does not exist. Cannot initialize workspace database from baseline.");
+                       return baseline;
+               }
+
+               // Relative path resolution order:
+               // 1. from the platform "install location"
+               // 2. from working directory
+               //Path installLocation = tryGetInstallLocation();
+               if (installLocation != null) {
+                       Path installedBaseline = installLocation.resolve(dbBaselineArchive);
+                       if (Files.isRegularFile(installedBaseline))
+                               return installedBaseline;
+               }
+               if (!Files.isRegularFile(baseline))
+                       throw new PlatformException("Specified database baseline archive " + baseline
+                                       + " does not exist in either the install location (" + installLocation
+                                       + ") or the working directory (" + Paths.get(".").toAbsolutePath()
+                                       + "). Cannot initialize workspace database.");
+               return null;
+       }
+
+       private static boolean useAutomaticBaseline() {
+               return getAutomaticBaselinePath() != null;
+       }
+
+       private static Path getAutomaticBaselinePath() {
+               if("true".equals(System.getProperty("org.simantics.db.baseline.automatic")))
+                       return Paths.get("automatic_baseline", "baseline.zip");
+               else
+                       return null;
+       }
+
+       private static boolean existsAutomaticBaseline() {
+               Path baselineFile = getAutomaticBaselinePath();
+               if(baselineFile == null)
+                       return false;
+               return Files.exists(baselineFile);
+       }
+
+       public static boolean shouldCreateAutomaticBaseline(boolean existsDatabase) throws PlatformException {
+               if(!useAutomaticBaseline()) {
+                       // Are we using this feature? 
+                       return false;
+               }
+               if(existsDatabase) {
+                       // Baseline can only be created after db initialization
+                       return false;
+               }
+               if(existsAutomaticBaseline()) {
+                       // Existing baselines should not be automatically overridden
+                       return false;
+               }
+               return true;
+       }
+
+       public static void createAutomaticBaseline(Path dbLocation) throws PlatformException {
+
+               if(existsAutomaticBaseline())
+                       return;
+
+               try {
+                       DatabaseBaselines.packageBaseline(dbLocation.getParent(), getAutomaticBaselinePath());
+               } catch (IOException e) {
+                       LOGGER.error("Error while creating automatic baseline", e);
+               }
+
+       }
+
+       public static Path packageBaseline(Path fromWorkspace, Path packageFilePath) throws IOException {
+               Files.createDirectories(packageFilePath.getParent());
                return compressZip(fromWorkspace, collectBaselinePaths(fromWorkspace), packageFilePath);
        }