]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/BaselineCreatorApplication.java
Platform startup performance improvements
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / BaselineCreatorApplication.java
1 package org.simantics;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.time.LocalDateTime;
9 import java.time.format.DateTimeFormatter;
10
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IProgressMonitor;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.equinox.app.IApplication;
18 import org.eclipse.equinox.app.IApplicationContext;
19 import org.eclipse.osgi.service.datalocation.Location;
20 import org.simantics.application.arguments.Arguments;
21 import org.simantics.application.arguments.IArgumentFactory;
22 import org.simantics.application.arguments.IArgumentFactory.StringArgumentFactory;
23 import org.simantics.application.arguments.IArgumentFactory.NoValueArgumentFactory;
24 import org.simantics.application.arguments.IArguments;
25 import org.simantics.application.arguments.SimanticsArguments;
26 import org.simantics.internal.Activator;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Tuukka Lehtonen
32  * @since 1.34.0
33  */
34 public class BaselineCreatorApplication implements IApplication {
35
36         private static final Logger LOGGER = LoggerFactory.getLogger(BaselineCreatorApplication.class);
37
38         private static final IArgumentFactory<String> OUTPUT = new StringArgumentFactory("-o");
39         private static final IArgumentFactory<Boolean> VERBOSE = new NoValueArgumentFactory("-v");
40
41         IArgumentFactory<?>[] accepted = {
42                         SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS,
43                         SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL,
44                         SimanticsArguments.DISABLE_INDEX,
45                         SimanticsArguments.DATABASE_ID,
46                         OUTPUT,
47                         VERBOSE,
48         };
49
50         private static String currentLocalDateTimeStamp() {
51                 return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
52         }
53
54         private static Path constructOutputPath(Path workspace, IArguments parsedArgs) {
55                 if (parsedArgs.contains(OUTPUT)) {
56                         return workspace.resolve(parsedArgs.get(OUTPUT));
57                 } else {
58                         return workspace.resolve(workspace.getFileName().toString() + "-" + currentLocalDateTimeStamp() + ".zip");
59                 }
60         }
61
62         private static Path getInstanceLocation() throws CoreException, IOException {
63                 Location l = Platform.getInstanceLocation();
64                 if (l == null)
65                         throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
66                                         "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
67
68                 Location instanceLoc = Platform.getInstanceLocation();
69                 if (instanceLoc == null || instanceLoc.isReadOnly())
70                         throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
71                                         "Workspace not defined. Use -data <path> argument to define where to place the baselining workspace."));
72
73                 URL workspaceUrl = l.getURL();
74                 Path workspacePath = new File(workspaceUrl.getPath()).toPath();
75                 Files.createDirectories(workspacePath);
76                 return workspacePath;
77         }
78
79         @Override
80         public Object start(IApplicationContext context) throws Exception {
81                 try {
82                         Path workspace = getInstanceLocation();
83
84                         String[] args = (String[]) context.getArguments().get("application.args");
85                         IArguments parsedArgs = Arguments.parse(args, accepted);
86
87                         Path output = constructOutputPath(workspace, parsedArgs);
88
89                         // Create database and indexes
90                         IProgressMonitor progress = parsedArgs.contains(VERBOSE)
91                                         ? new TimingProgressMonitor()
92                                         : new NullProgressMonitor();
93                         Simantics.startUpHeadless(parsedArgs, progress);
94                         Simantics.shutdown(progress);
95
96                         // Create the baseline package file
97                         Path actualOutput = DatabaseBaselines.packageBaseline(workspace, output);
98                         System.out.println("OK " + actualOutput.toAbsolutePath());
99
100                         return IApplication.EXIT_OK;
101                 } catch (Exception e) {
102                         LOGGER.error("Baseline creation failed.", e);
103                         throw (Exception) e;
104                 } finally {
105                         System.exit(0);
106                 }
107         }
108
109         @Override
110         public void stop() {
111         }
112
113 }