4 import java.io.IOException;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.time.LocalDateTime;
9 import java.time.format.DateTimeFormatter;
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;
31 * @author Tuukka Lehtonen
34 public class BaselineCreatorApplication implements IApplication {
36 private static final Logger LOGGER = LoggerFactory.getLogger(BaselineCreatorApplication.class);
38 private static final IArgumentFactory<String> OUTPUT = new StringArgumentFactory("-o");
39 private static final IArgumentFactory<Boolean> VERBOSE = new NoValueArgumentFactory("-v");
41 IArgumentFactory<?>[] accepted = {
42 SimanticsArguments.RECOVERY_POLICY_FIX_ERRORS,
43 SimanticsArguments.ONTOLOGY_RECOVERY_POLICY_REINSTALL,
44 SimanticsArguments.DISABLE_INDEX,
45 SimanticsArguments.DATABASE_ID,
50 private static String currentLocalDateTimeStamp() {
51 return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
54 private static Path constructOutputPath(Path workspace, IArguments parsedArgs) {
55 if (parsedArgs.contains(OUTPUT)) {
56 return workspace.resolve(parsedArgs.get(OUTPUT));
58 return workspace.resolve(workspace.getFileName().toString() + "-" + currentLocalDateTimeStamp() + ".zip");
62 private static Path getInstanceLocation() throws CoreException, IOException {
63 Location l = Platform.getInstanceLocation();
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."));
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."));
73 URL workspaceUrl = l.getURL();
74 Path workspacePath = new File(workspaceUrl.getPath()).toPath();
75 Files.createDirectories(workspacePath);
80 public Object start(IApplicationContext context) throws Exception {
82 Path workspace = getInstanceLocation();
84 String[] args = (String[]) context.getArguments().get("application.args");
85 IArguments parsedArgs = Arguments.parse(args, accepted);
87 Path output = constructOutputPath(workspace, parsedArgs);
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);
96 // Create the baseline package file
97 Path actualOutput = DatabaseBaselines.packageBaseline(workspace, output);
98 System.out.println("OK " + actualOutput.toAbsolutePath());
100 return IApplication.EXIT_OK;
101 } catch (Exception e) {
102 LOGGER.error("Baseline creation failed.", e);