From: Tuukka Lehtonen Date: Tue, 11 Feb 2020 10:48:29 +0000 (+0000) Subject: Merge "Easier baselines" X-Git-Tag: v1.43.0~105 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=9474abeac493ca545a58a571ab28e73729a7da3e;hp=1347cf21200f706081392f24a1f9dae735ecec84 Merge "Easier baselines" --- diff --git a/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchAdvisor.java b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchAdvisor.java index f5bda18b0..8a8b49375 100644 --- a/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchAdvisor.java +++ b/bundles/org.simantics.workbench/src/org/simantics/workbench/internal/SimanticsWorkbenchAdvisor.java @@ -88,6 +88,7 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; import org.simantics.CancelStartupException; +import org.simantics.DatabaseBaselines; import org.simantics.PlatformException; import org.simantics.Simantics; import org.simantics.SimanticsPlatform; @@ -105,7 +106,6 @@ import org.simantics.ui.SimanticsUI; import org.simantics.ui.jobs.SessionGarbageCollectorJob; import org.simantics.ui.workbench.PerspectiveBarsActivator; import org.simantics.ui.workbench.PerspectiveContextActivator; -import org.simantics.ui.workbench.WorkbenchShutdownService; import org.simantics.utils.logging.TimeLogger; import org.simantics.utils.ui.dialogs.ShowError; import org.simantics.utils.ui.dialogs.ShowMessage; @@ -432,6 +432,11 @@ public class SimanticsWorkbenchAdvisor extends WorkbenchAdvisor { if (PROFILE_PLATFORM_STARTUP) mon = new TimingProgressMonitor(); SimanticsPlatform.INSTANCE.startUp(databaseDriverId, mon, workspacePolicy, ontologyPolicy, requireSynchronize, new JFaceUserAgent()); + if(DatabaseBaselines.shouldCreateAutomaticBaseline(SimanticsPlatform.INSTANCE.databaseExists() != null)) { + SimanticsPlatform.INSTANCE.shutdown(null); + DatabaseBaselines.createAutomaticBaseline(SimanticsPlatform.INSTANCE.dbLocation()); + SimanticsPlatform.INSTANCE.reconnect(Simantics.getDefaultDatabaseDriver()); + } // Make sure that the default perspective comes from the project if // the project has set ProjectKeys#DEFAULT_PERSPECTIVE. diff --git a/bundles/org.simantics/src/org/simantics/DatabaseBaselines.java b/bundles/org.simantics/src/org/simantics/DatabaseBaselines.java index fe3393a2c..c58cb5086 100644 --- a/bundles/org.simantics/src/org/simantics/DatabaseBaselines.java +++ b/bundles/org.simantics/src/org/simantics/DatabaseBaselines.java @@ -1,5 +1,6 @@ package org.simantics; +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -13,6 +14,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; +import org.eclipse.core.runtime.Platform; import org.simantics.utils.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,8 +34,114 @@ public class DatabaseBaselines { private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("d. MMM yyyy HH:mm:ss"); - public static Path packageBaseline(Path fromWorkspace, Path packageFile) throws IOException { - return compressZip(fromWorkspace, collectBaselinePaths(fromWorkspace), packageFile); + 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(); + return compressZip(fromWorkspace, collectBaselinePaths(fromWorkspace), packageFilePath); } private static List collectBaselinePaths(Path workspace) throws IOException { diff --git a/bundles/org.simantics/src/org/simantics/SimanticsPlatform.java b/bundles/org.simantics/src/org/simantics/SimanticsPlatform.java index c0a85cd1a..730e4e887 100644 --- a/bundles/org.simantics/src/org/simantics/SimanticsPlatform.java +++ b/bundles/org.simantics/src/org/simantics/SimanticsPlatform.java @@ -239,7 +239,7 @@ public class SimanticsPlatform implements LifecycleListener { if (progressMonitor == null) progressMonitor = new NullProgressMonitor(); Path workspaceLocation = Platform.getLocation().toFile().toPath(); - Path dbLocation = workspaceLocation.resolve("db"); + Path dbLocation = dbLocation(); Path dbIniPath = workspaceLocation.resolve("db.ini"); // The driver file overrides any command line arguments to prevent // using the wrong driver for an existing database directory. @@ -679,8 +679,9 @@ public class SimanticsPlatform implements LifecycleListener { public void resetDatabase(IProgressMonitor monitor) throws PlatformException { // TODO: fix this to use Path APIs - File dbLocation = Platform.getLocation().append("db").toFile(); - if(!dbLocation.exists()) return; + File dbLocation = dbLocation().toFile(); + if(!dbLocation.exists()) + return; try { // Load driver Driver driver = Manager.getDriver("acorn"); Management management = driver.getManagement(dbLocation.getAbsolutePath(), null); @@ -721,53 +722,16 @@ public class SimanticsPlatform implements LifecycleListener { return l == null ? null : new File(l.getURL().getPath()).toPath(); } - private Path resolveBaselineFile() 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."); + public Path databaseExists() { + Path dbLocation = dbLocation(); + if(Files.exists(dbLocation)) + return dbLocation; return null; } - - private boolean handleBaselineDatabase() throws PlatformException { + + public Path dbLocation() { 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(); - if (baseline == null) - return false; - - DatabaseBaselines.validateBaselineFile(baseline); - DatabaseBaselines.validateWorkspaceForBaselineInitialization(workspaceLocation); - DatabaseBaselines.initializeWorkspaceWithBaseline(baseline, workspaceLocation, baselineIndicatorFile); - return true; + return workspaceLocation.resolve("db"); } /** @@ -827,7 +791,7 @@ public class SimanticsPlatform implements LifecycleListener { // 0.3 Handle baseline database before opening db @SuppressWarnings("unused") - boolean usingBaseline = handleBaselineDatabase(); + boolean usingBaseline = DatabaseBaselines.handleBaselineDatabase(tryGetInstallLocation(), databaseExists() != null); // 1. Assert there is a database at /db SessionDescriptor sessionDescriptor = setupDatabase(databaseDriverId, monitor.newChild(200, SubMonitor.SUPPRESS_NONE), workspacePolicy, userAgent);