1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics;
14 import static org.simantics.db.common.utils.Transaction.commit;
15 import static org.simantics.db.common.utils.Transaction.endTransaction;
16 import static org.simantics.db.common.utils.Transaction.readGraph;
17 import static org.simantics.db.common.utils.Transaction.startTransaction;
18 import static org.simantics.db.common.utils.Transaction.writeGraph;
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.time.Instant;
26 import java.time.ZoneId;
27 import java.time.format.DateTimeFormatter;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Properties;
36 import java.util.TreeMap;
37 import java.util.UUID;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipFile;
41 import org.eclipse.core.runtime.ILog;
42 import org.eclipse.core.runtime.IProduct;
43 import org.eclipse.core.runtime.IProgressMonitor;
44 import org.eclipse.core.runtime.IStatus;
45 import org.eclipse.core.runtime.NullProgressMonitor;
46 import org.eclipse.core.runtime.Platform;
47 import org.eclipse.core.runtime.Status;
48 import org.eclipse.core.runtime.SubMonitor;
49 import org.eclipse.osgi.service.resolver.BundleDescription;
51 import org.ini4j.InvalidFileFormatException;
52 import org.simantics.SimanticsPlatform.OntologyRecoveryPolicy;
53 import org.simantics.SimanticsPlatform.RecoveryPolicy;
54 import org.simantics.databoard.Bindings;
55 import org.simantics.databoard.Databoard;
56 import org.simantics.datatypes.literal.Font;
57 import org.simantics.datatypes.literal.RGB;
58 import org.simantics.db.Driver;
59 import org.simantics.db.Driver.Management;
60 import org.simantics.db.Manager;
61 import org.simantics.db.ReadGraph;
62 import org.simantics.db.Resource;
63 import org.simantics.db.Session;
64 import org.simantics.db.SessionModel;
65 import org.simantics.db.UndoContext;
66 import org.simantics.db.VirtualGraph;
67 import org.simantics.db.WriteGraph;
68 import org.simantics.db.common.request.ObjectsWithType;
69 import org.simantics.db.common.request.Queries;
70 import org.simantics.db.common.request.WriteRequest;
71 import org.simantics.db.common.request.WriteResultRequest;
72 import org.simantics.db.common.utils.Transaction;
73 import org.simantics.db.exception.ClusterSetExistException;
74 import org.simantics.db.exception.DatabaseException;
75 import org.simantics.db.exception.ResourceNotFoundException;
76 import org.simantics.db.indexing.DatabaseIndexing;
77 import org.simantics.db.layer0.genericrelation.DependenciesRelation;
78 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
79 import org.simantics.db.layer0.util.SimanticsKeys;
80 import org.simantics.db.layer0.util.TGTransferableGraphSource;
81 import org.simantics.db.layer0.variable.VariableRepository;
82 import org.simantics.db.management.SessionContext;
83 import org.simantics.db.request.Read;
84 import org.simantics.db.service.LifecycleSupport.LifecycleListener;
85 import org.simantics.db.service.LifecycleSupport.LifecycleState;
86 import org.simantics.db.service.QueryControl;
87 import org.simantics.db.service.UndoRedoSupport;
88 import org.simantics.db.service.VirtualGraphSupport;
89 import org.simantics.db.service.XSupport;
90 import org.simantics.graph.db.GraphDependencyAnalyzer;
91 import org.simantics.graph.db.GraphDependencyAnalyzer.IU;
92 import org.simantics.graph.db.GraphDependencyAnalyzer.IdentityNode;
93 import org.simantics.graph.db.IImportAdvisor;
94 import org.simantics.graph.db.ImportResult;
95 import org.simantics.graph.db.TransferableGraphs;
96 import org.simantics.graph.diff.Diff;
97 import org.simantics.graph.diff.TransferableGraphDelta1;
98 import org.simantics.internal.Activator;
99 import org.simantics.internal.startup.StartupExtensions;
100 import org.simantics.layer0.Layer0;
101 import org.simantics.operation.Layer0X;
102 import org.simantics.project.IProject;
103 import org.simantics.project.ProjectFeatures;
104 import org.simantics.project.ProjectKeys;
105 import org.simantics.project.Projects;
106 import org.simantics.project.exception.ProjectException;
107 import org.simantics.project.features.registry.GroupReference;
108 import org.simantics.project.management.DatabaseManagement;
109 import org.simantics.project.management.GraphBundle;
110 import org.simantics.project.management.GraphBundleEx;
111 import org.simantics.project.management.GraphBundleRef;
112 import org.simantics.project.management.PlatformUtil;
113 import org.simantics.project.management.ServerManager;
114 import org.simantics.project.management.ServerManagerFactory;
115 import org.simantics.project.management.WorkspaceUtil;
116 import org.simantics.utils.FileUtils;
117 import org.simantics.utils.datastructures.Pair;
118 import org.simantics.utils.logging.TimeLogger;
119 import org.simantics.utils.strings.EString;
120 import org.slf4j.Logger;
121 import org.slf4j.LoggerFactory;
124 * SimanticsPlatform performs procedures required in order to get simantics
125 * workbench into operational state. This consists of the following steps:
127 * <li> Asserting there is Database
129 * <li> Starting Database process
131 * <li> Opening a session to Database process
133 * <li> Asserting required ontologies or other transferable graphs are installed in the database
135 * <li> Asserting required project is installed in the database
137 * <li> Asserting Simantics Features are installed in the database
139 * <li> Asserting Simantics Features are installed to the project
141 * <li> Shutdown: Save Session, Close session, Kill Database process
145 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
147 public class SimanticsPlatform implements LifecycleListener {
149 private static final Logger LOGGER = LoggerFactory.getLogger(SimanticsPlatform.class);
152 * The policy is relevant when developing Simantics from Eclipse IDE.
153 * It is applied when the ontology in the database of a workspace doesn't match
154 * a newer ontology in the Eclipse workspace.
156 public static enum OntologyRecoveryPolicy { ThrowError, Merge, ReinstallDatabase }
159 * This policy dictates how the Simantics platform startup should react if
160 * the started workspace is not set up properly. The alternatives are to
161 * just throw an error and fail or to attempt all possible measures to fix
162 * the encountered problems.
164 public static enum RecoveryPolicy { ThrowError, FixError }
166 /** Singleton instance, started in SimanticsWorkbenchAdvisor */
167 public static final SimanticsPlatform INSTANCE = new SimanticsPlatform();
169 /** Set to true when the Simantics Platform is in good-and-go condition */
170 public boolean running;
172 /** ID of the database driver that the platform is currently using */
173 private String currentDatabaseDriver;
175 /** Database Session */
176 public Session session;
177 private Management databasebManagement;
179 /** Database session context */
180 public SessionContext sessionContext;
182 /** Project identifier in Database */
183 public String projectURI;
186 public String projectName;
188 /** Project resource */
189 public Resource projectResource;
191 /** Session specific bindings */
192 public SimanticsBindings simanticsBindings;
193 public SimanticsBindings simanticsBindings2;
195 public Thread mainThread;
198 * The {@link IProject} activated by
199 * {@link #startUp(IProgressMonitor, RecoveryPolicy, OntologyRecoveryPolicy, ServerAddress, PlatformUserAgent)}
201 private IProject project;
206 * Create a new simantics plaform manager in uninitialized state and
207 * with default policies. <p>
209 public SimanticsPlatform() {
210 log = Platform.getLog(Activator.getBundleContext().getBundle());
211 mainThread = Thread.currentThread();
214 public String getApplicationClientId() {
215 IProduct product = Platform.getProduct();
216 if(product == null) return "noProduct";//UUID.randomUUID().toString();
217 String application = product.getApplication();
218 return application != null ? application : UUID.randomUUID().toString();
221 private Session setupDatabase(String databaseDriverId, IProgressMonitor progressMonitor, RecoveryPolicy workspacePolicy, PlatformUserAgent userAgent) throws PlatformException {
222 if (progressMonitor == null)
223 progressMonitor = new NullProgressMonitor();
224 Path workspaceLocation = Platform.getLocation().toFile().toPath();
225 Path dbLocation = workspaceLocation.resolve("db");
226 Path dbIniPath = workspaceLocation.resolve("db.ini");
227 // The driver file overrides any command line arguments to prevent
228 // using the wrong driver for an existing database directory.
229 ServerManager serverManager;
231 Ini dbIni = loadOrCreateDatabaseIni(dbIniPath, databaseDriverId);
232 databaseDriverId = dbIni.get("driver", "id");
233 serverManager = ServerManagerFactory.create(databaseDriverId, dbLocation.toAbsolutePath().toString());
234 } catch (DatabaseException | IOException e) {
235 throw new PlatformException("Failed to initialize database ServerManager with driver " + databaseDriverId, e);
237 progressMonitor.beginTask("Setting up Simantics Database", 100);
238 progressMonitor.setTaskName("Asserting Database is installed.");
239 String msg = "Failed to initialize Simantics database.";
242 log.log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "Initializing database at " + dbLocation + " with driver " + databaseDriverId));
243 progressMonitor.setTaskName("Creating database at " + dbLocation);
244 databasebManagement = serverManager.getManagement(dbLocation.toFile());
245 databasebManagement.create();
246 currentDatabaseDriver = databaseDriverId;
248 return serverManager.createDatabase(dbLocation.toFile());
249 } catch (DatabaseException e) {
250 throw new PlatformException(msg, e);
251 } catch (Throwable e) {
252 throw new PlatformException(msg, e);
254 progressMonitor.worked(20);
258 public void synchronizeOntologies(IProgressMonitor progressMonitor, OntologyRecoveryPolicy ontologyPolicy, boolean requireSynchronize) throws PlatformException {
260 if (progressMonitor == null) progressMonitor = new NullProgressMonitor();
262 final DatabaseManagement mgmt = new DatabaseManagement();
264 PlatformUtil.compileAllDynamicOntologies();
266 progressMonitor.setTaskName("Asserting all ontologies are installed");
267 final Map<GraphBundleRef, GraphBundleEx> platformTGs = new HashMap<GraphBundleRef, GraphBundleEx>();
270 // Get a list of bundles installed into the database
271 progressMonitor.subTask("find installed bundles from database");
272 Map<GraphBundleRef, GraphBundleEx> installedTGs = new HashMap<GraphBundleRef, GraphBundleEx>();
273 for (GraphBundle b : session.syncRequest( mgmt.GraphBundleQuery )) {
274 installedTGs.put(GraphBundleRef.of(b), GraphBundleEx.extend(b));
277 if(!requireSynchronize && installedTGs.size() > 1 && !Platform.inDevelopmentMode()) return;
278 // if(installedTGs.size() > 1) return;
280 // Get a list of all bundles in the platform (Bundle Context)
281 List<GraphBundle> tgs = new ArrayList<GraphBundle>();
282 progressMonitor.subTask("load all transferable graphs from platform");
283 PlatformUtil.getAllGraphs(tgs);
284 progressMonitor.subTask("extend bundles to compile versions");
285 for (GraphBundle b : tgs) {
286 GraphBundleEx gbe = GraphBundleEx.extend(b);
288 platformTGs.put(GraphBundleRef.of(b), gbe);
291 // Compile a list of TGs that need to be installed or reinstalled in the database
292 progressMonitor.subTask("check bundle reinstallation demand");
293 List<GraphBundleEx> installTGs = new ArrayList<GraphBundleEx>();
294 // Create list of TGs to update, <newTg, oldTg>
295 Map<GraphBundleEx,GraphBundleEx> reinstallTGs = new TreeMap<GraphBundleEx,GraphBundleEx>();
296 for (Entry<GraphBundleRef, GraphBundleEx> e : platformTGs.entrySet()) {
297 GraphBundleRef key = e.getKey();
298 GraphBundleEx platformBundle = e.getValue();
299 GraphBundleEx existingBundle = installedTGs.get(key);
301 // System.out.println("GraphBundleRef key=" + key.toString());
303 if (existingBundle == null) {
304 // Bundle did not exist in the database, put it into list of bundles to install
305 installTGs.add(platformBundle);
308 // Bundle exists in the database
309 boolean platformBundleIsNewer = existingBundle.getVersion().compareTo(platformBundle.getVersion())<0;
310 if (!platformBundleIsNewer)
312 // Check hash of transferable graph to know whether to update or not.
313 if (platformBundle.getHashcode() == existingBundle.getHashcode())
315 //System.out.println("Ontology hashcodes do not match: platform bundle="
316 // + platformBundle.getVersionedId() + ", hash=" + platformBundle.getHashcode()
317 // + "; existing bundle=" + existingBundle.getVersionedId() + ", hash=" + existingBundle.getHashcode());
318 reinstallTGs.put(platformBundle, existingBundle);
322 // Database is missing graphs
323 if (!installTGs.isEmpty() || !reinstallTGs.isEmpty()) {
324 session.getService(XSupport.class).setServiceMode(true, true);
327 if (ontologyPolicy == OntologyRecoveryPolicy.ThrowError) {
328 StringBuilder sb = new StringBuilder("The following graphs are not installed in the database: ");
329 if (!installTGs.isEmpty()) {
331 for (GraphBundleEx e : installTGs) {
332 if (i>0) sb.append(", ");
334 sb.append(e.toString());
336 sb.append(" is missing from the database.\n");
338 if (!reinstallTGs.isEmpty()) {
340 for (Entry<GraphBundleEx, GraphBundleEx> e : reinstallTGs.entrySet()) {
341 if (i>0) sb.append(", ");
343 sb.append(e.getKey().toString());
345 sb.append(" Database/Platform Bundle version mismatch.\n");
347 sb.append("Hint: Use -fixErrors to install the graphs.");
348 throw new PlatformException(sb.toString());
350 // Reinstall database
351 if (ontologyPolicy == OntologyRecoveryPolicy.ReinstallDatabase) {
352 log.log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "Reinstalling the database."));
361 throw new PlatformException("Reinstalling Database, NOT IMPLEMENTED");
364 if (ontologyPolicy == OntologyRecoveryPolicy.Merge) {
365 progressMonitor.subTask("Merging ontology changes");
366 // Sort missing TGs into install order
367 GraphDependencyAnalyzer<GraphBundle> analyzer = new GraphDependencyAnalyzer<GraphBundle>();
368 for(GraphBundle tg : installTGs) analyzer.addGraph(tg, tg.getGraph());
369 for(GraphBundle tg : reinstallTGs.keySet()) analyzer.addGraph(tg, tg.getGraph());
370 if(!analyzer.analyzeDependency()) {
371 Collection<Pair<GraphBundle, GraphBundle>> problems = analyzer.getConflicts();
372 StringBuilder sb = new StringBuilder();
373 for (Pair<GraphBundle, GraphBundle> problem : problems) {
374 sb.append("Conflict with "+problem.first+" and "+problem.second+".\n");
376 throw new PlatformException(sb.toString());
378 else if(!session.syncRequest( analyzer.queryExternalDependenciesSatisfied )) {
379 Collection<IdentityNode> unsatisfiedDependencies = analyzer.getUnsatisfiedDependencies();
380 StringBuilder sb = new StringBuilder();
381 for (IdentityNode dep: unsatisfiedDependencies) {
382 sb.append("Unsatisfied Dependency "+dep+". Required by\n");
383 for(IU iu : GraphDependencyAnalyzer.toCollection(dep.getRequires())) {
384 sb.append(" " + ((GraphBundle)iu.getId()).getId() + "\n");
387 throw new PlatformException(sb.toString());
390 List<GraphBundle> sortedBundles = analyzer.getSortedGraphs();
391 if(!sortedBundles.isEmpty()) {
393 session.syncRequest(new WriteRequest() {
395 public void perform(WriteGraph graph) throws DatabaseException {
397 graph.newClusterSet(graph.getRootLibrary());
398 } catch (ClusterSetExistException e) {
399 // Cluster set exist already, no problem.
401 graph.setClusterSet4NewResource(graph.getRootLibrary());
402 graph.flushCluster();
406 boolean mergedOntologies = false;
409 for(final GraphBundle tg : sortedBundles) {
411 final IImportAdvisor advisor = new OntologyImportAdvisor(tg, mgmt);
412 final GraphBundle oldTG = reinstallTGs.get(tg);
414 boolean createImmutable = tg.getImmutable();
418 session.getService(XSupport.class).setServiceMode(true, createImmutable);
421 log.log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "Installing "+tg.toString()+" - "+tg.getName()));
422 ImportResult result = TransferableGraphs.importGraph1(session, new TGTransferableGraphSource(tg.getGraph()), advisor, null);
423 if (!result.missingExternals.isEmpty()) {
424 log.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import of " + tg.toString() + " was missing the following external entities:\n" + EString.implode(result.missingExternals)));
431 startTransaction(session, false);
432 TransferableGraphDelta1 delta = new Diff(oldTG.getGraph(), tg.getGraph()).diff();
433 final long[] oldResources = oldTG.getResourceArray();
434 boolean changes = TransferableGraphs.hasChanges(readGraph(), oldResources, delta);
437 //log.log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "Nothing to merge for "+tg.toString()));
441 log.log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "Merging new version of "+tg.toString()));
443 startTransaction(session, true);
449 long[] resourceArray = TransferableGraphs.applyDelta(writeGraph(), oldResources, delta);
450 tg.setResourceArray(resourceArray);
451 mgmt.setGraphBundleEntry(tg);
453 mergedOntologies = true;
454 } catch (Throwable t) {
455 throw new PlatformException(t);
462 session.syncRequest(new WriteRequest() {
464 public void perform(WriteGraph graph) throws DatabaseException {
465 graph.setClusterSet4NewResource(graph.getRootLibrary());
466 graph.flushCluster();
470 if (mergedOntologies)
471 DatabaseIndexing.deleteAllIndexes();
474 TimeLogger.log("Ontologies synchronized.");
477 session.getService(XSupport.class).setServiceMode(false, false);
479 progressMonitor.worked(20);
480 } catch (IOException e) {
481 throw new PlatformException(e);
482 } catch (DatabaseException e) {
483 throw new PlatformException(e);
488 public boolean assertConfiguration(IProgressMonitor progressMonitor, RecoveryPolicy workspacePolicy) throws PlatformException {
490 if (progressMonitor == null) progressMonitor = new NullProgressMonitor();
492 File workspaceLocation = Platform.getLocation().toFile();
494 boolean installProject = false;
495 progressMonitor.setTaskName("Asserting simantics.cfg is installed");
497 File propertyFile = new File(workspaceLocation, "simantics.cfg");
498 Properties properties;
500 properties = WorkspaceUtil.readProperties(propertyFile);
501 } catch (IOException e) {
502 if (workspacePolicy == RecoveryPolicy.ThrowError) throw new PlatformException("Could not load "+propertyFile);
504 // Create a project and write Property file
505 properties = new Properties();
506 properties.setProperty("project_uri", "http://Projects/Development%20Project");
507 properties.setProperty("project_name", "Development Project");
508 WorkspaceUtil.writeProperties(propertyFile, properties);
509 installProject |= true;
511 projectURI = properties.getProperty("project_uri");
512 projectName = properties.getProperty("project_name");
513 progressMonitor.worked(10);
514 } catch (IOException e) {
515 throw new PlatformException(e);
518 return installProject;
522 public boolean assertProject(IProgressMonitor progressMonitor, RecoveryPolicy workspacePolicy, boolean installProject) throws PlatformException {
524 if (progressMonitor == null) progressMonitor = new NullProgressMonitor();
526 final DatabaseManagement mgmt = new DatabaseManagement();
528 progressMonitor.setTaskName("Asserting project resource exists in the database");
530 projectResource = session.syncRequest( Queries.resource( projectURI ) );
531 } catch (ResourceNotFoundException nfe) {
532 // Project was not found
533 if (workspacePolicy == RecoveryPolicy.ThrowError)
534 throw new PlatformException("Project Resource "+projectURI+" is not found in the database.");
535 // Create empty project with no features
537 Transaction.startTransaction(session, true);
539 // The project needs to be created mutable.
540 session.getService(XSupport.class).setServiceMode(true, false);
542 ArrayList<String> empty = new ArrayList<String>();
543 projectResource = mgmt.createProject(projectName, empty);
544 installProject |= true;
546 session.getService(XSupport.class).setServiceMode(false, false);
547 Transaction.commit();
549 Transaction.endTransaction();
551 //session.getService( LifecycleSupport.class ).save();
552 } catch (DatabaseException e) {
553 throw new PlatformException("Failed to create "+projectURI, e);
555 } catch (DatabaseException e) {
556 throw new PlatformException("Failed to create "+projectURI, e);
558 progressMonitor.worked(10);
560 return installProject;
564 public void updateInstalledGroups(IProgressMonitor progressMonitor, boolean installProject) throws PlatformException {
568 // Attach all feature groups available in platform to created project
569 progressMonitor.setTaskName("Install all features");
570 Set<GroupReference> publishedFeatureGroups = ProjectFeatures.getInstallGroupsOfPublishedFeatures();
571 Collection<GroupReference> groupsWithoutVersion = GroupReference.stripVersions(publishedFeatureGroups);
573 // final List<String> Platform_Features = new ArrayList<String>();
575 // // Convert graph instances
576 // Collection<TransferableGraph1> platformGraphs = new ArrayList<TransferableGraph1>();
577 // for (GraphBundleEx e : platformTGs.values()) platformGraphs.add( e.getGraph() );
578 // IGraph graph = Graphs.createGraph(platformGraphs);
580 // Res PublishedProjectFeatures = UriUtils.uriToPath( ProjectResource.URIs.PublishedProjectFeatures );
581 // Path HasFeature = UriUtils.uriToPath( ProjectResource.URIs.HasFeature );
582 // for(Res feature : graph.getObjects(PublishedProjectFeatures, HasFeature)) {
583 // System.out.println("Installing Project Feature: "+feature.toString());
584 // Platform_Features.add( feature.toString() );
588 Transaction.startTransaction(session, true);
590 // for (String feature : Platform_Features) {
592 // getResource(feature);
593 // } catch(ResourceNotFoundException e) {
594 // System.out.println(feature+" not found");
596 // mgmt.installFeature(projectResource, feature);
598 Projects.setProjectInstalledGroups(writeGraph(), projectResource, groupsWithoutVersion);
599 Transaction.commit();
601 Transaction.endTransaction();
603 //session.getService( LifecycleSupport.class ).save();
604 } catch(DatabaseException ae) {
605 throw new PlatformException("Failed to install features", ae);
607 progressMonitor.worked(10);
612 public void assertSessionModel(IProgressMonitor progressMonitor) throws PlatformException {
614 Properties properties = session.getService(Properties.class);
615 final String clientId = properties.getProperty("clientId");
619 // Currently this needs to be done before data becomes available
620 VirtualGraphSupport support = session.getService(VirtualGraphSupport.class);
621 VirtualGraph activations = support.getWorkspacePersistent("activations");
623 Resource sessionModel = session.syncRequest(new Read<Resource>() {
626 public Resource perform(ReadGraph graph) throws DatabaseException {
628 Layer0X L0X = Layer0X.getInstance(graph);
629 for(Resource sessionModel : graph.syncRequest(new ObjectsWithType(graph.getRootLibrary(), L0X.HasSession, L0X.Session))) {
630 String id = graph.getPossibleRelatedValue(sessionModel, L0X.Session_HasClientId);
631 if(id != null && id.equals(clientId)) return sessionModel;
639 if(sessionModel == null) {
641 sessionModel = session.syncRequest(new WriteResultRequest<Resource>(activations) {
644 public Resource perform(WriteGraph graph) throws DatabaseException {
645 Layer0 L0 = Layer0.getInstance(graph);
646 Layer0X L0X = Layer0X.getInstance(graph);
647 Resource session = graph.newResource();
648 graph.claim(session, L0.InstanceOf, null, L0X.Session);
649 graph.claim(session, L0X.Session_HasUser, null, graph.getResource("http://Users/AdminUser"));
650 graph.addLiteral(session, L0X.Session_HasClientId, L0X.Session_HasClientId_Inverse, clientId, Bindings.STRING);
651 graph.claim(graph.getRootLibrary(), L0X.HasSession, session);
658 session.registerService(SessionModel.class, new PlatformSessionModel(sessionModel));
659 } catch (DatabaseException e) {
660 throw new PlatformException(e);
665 static class PlatformSessionModel implements SessionModel {
666 private final Resource sessionModel;
668 public PlatformSessionModel(Resource model) {
669 this.sessionModel = model;
673 public Resource getResource() {
678 public void resetDatabase(IProgressMonitor monitor) throws PlatformException {
679 File dbLocation = Platform.getLocation().append("db").toFile();
680 if(!dbLocation.exists()) return;
682 Driver driver = Manager.getDriver("procore");
683 Management management = driver.getManagement(dbLocation.getAbsolutePath(), null);
685 } catch (DatabaseException e) {
686 throw new PlatformException("Failed to remove database at " + dbLocation.getAbsolutePath(), e);
688 // We have created extra files to database folder which have to be deleted also.
689 // This is an awful idea! Do not create extra files to database folder!
691 for (int i=0; i<10; ++i) {
693 FileUtils.deleteAll(dbLocation);
696 } catch (IOException e) {
697 // Assuming this has been thrown because delete file/folder failed.
702 } catch (InterruptedException e) {
703 // Ignoring interrupted exception.
707 throw new PlatformException("Failed to remove database folder at " + dbLocation.getAbsolutePath(), t);
709 public void resetWorkspace(IProgressMonitor monitor, ArrayList<String> fileFilter) throws PlatformException, IllegalStateException, IOException {
710 File file = Platform.getLocation().toFile();
711 if (null != fileFilter)
712 FileUtils.deleteAllWithFilter(file , fileFilter);
713 resetDatabase(monitor);
716 public boolean handleBaselineDatabase() throws PlatformException {
717 Path workspaceLocation = Platform.getLocation().toFile().toPath();
718 Path baselineIndicatorFile = workspaceLocation.resolve(".baselined");
719 if (Files.isRegularFile(baselineIndicatorFile)) {
720 // This means that the workspace has already been initialized from
721 // a database baseline and further initialization is not necessary.
725 String dbBaselineArchive = System.getProperty("org.simantics.db.baseline", null);
726 if (dbBaselineArchive == null)
729 Path baseline = Paths.get(dbBaselineArchive);
730 if (!Files.isRegularFile(baseline))
731 throw new PlatformException("Specified database baseline archive " + baseline + " does not exist. Cannot initialize workspace database.");
733 validateBaselineFile(baseline);
734 validateWorkspaceForBaselineInitialization(workspaceLocation);
737 Files.createDirectories(workspaceLocation);
738 FileUtils.extractZip(baseline.toFile(), workspaceLocation.toFile());
739 Files.write(baselineIndicatorFile, baselineIndicatorContents(baselineIndicatorFile));
741 } catch (IOException e) {
742 throw new PlatformException(e);
746 private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern("d. MMM yyyy HH:mm:ss");
748 private static byte[] baselineIndicatorContents(Path path) throws IOException {
749 return String.format("%s%n%s%n",
751 Instant.now().atZone(ZoneId.systemDefault()).format(TIMESTAMP_FORMAT))
755 private void validateWorkspaceForBaselineInitialization(Path workspaceLocation) throws PlatformException {
757 Path db = workspaceLocation.resolve("db");
758 if (Files.exists(db))
759 throw new PlatformException("Database location " + db + " already exists. Cannot re-initialize workspace from baseline.");
760 Path index = workspaceLocation.resolve(".metadata/.plugins/org.simantics.db.indexing");
761 if (!Files.exists(index) || !isEmptyDirectory(index))
762 throw new PlatformException("Index location " + index + " already exists. Cannot re-initialize workspace from baseline.");
763 } catch (IOException e) {
764 throw new PlatformException("Failed to validate workspace for baseline initialization", e);
768 private static boolean isEmptyDirectory(Path dir) throws IOException {
769 return Files.walk(dir).count() == 1;
772 private void validateBaselineFile(Path baseline) throws PlatformException {
773 try (ZipFile zip = new ZipFile(baseline.toFile())) {
774 ZipEntry db = zip.getEntry("db");
776 throw new PlatformException("Baseline archive does not contain database directory 'db'");
777 ZipEntry index = zip.getEntry(".metadata/.plugins/org.simantics.db.indexing");
779 throw new PlatformException("Baseline archive does not contain database index directory '.metadata/.plugins/org.simantics.db.indexing'");
780 } catch (IOException e) {
781 throw new PlatformException("Failed to validate baseline archive " + baseline, e);
786 * Start-up the platform. The procedure consists of 8 steps. Once everything
787 * is up and running, all fields are set property.
790 * If workspacePolicy is FixErrors, there is an attempt to fix unexpected
791 * errors. It includes installing database files, installing ontologies, and
792 * installing project features.
795 * In SWB this is handled in SimanticsWorkbenchAdvisor#openWindows().
798 * If remote server is given, simantics plaform takes connection there
799 * instead of local server at "db/".
801 * @param workspacePolicy action to take on workspace/database related
803 * @param ontologyPolicy action to take on ontology mismatch
804 * @param progressMonitor optional progress monitor
805 * @param userAgent interface for resorting to user feedback during platform
806 * startup or <code>null</code> to resort to default measures
807 * @throws PlatformException
809 public SessionContext startUp(String databaseDriverId, IProgressMonitor progressMonitor, RecoveryPolicy workspacePolicy,
810 OntologyRecoveryPolicy ontologyPolicy, boolean requireSynchronize, PlatformUserAgent userAgent)
811 throws PlatformException
815 TimeLogger.log("Beginning of SimanticsPlatform.startUp");
817 LOGGER.info("Beginning of SimanticsPlatform.startUp");
819 if (progressMonitor == null) progressMonitor = new NullProgressMonitor();
821 // For debugging on what kind of platform automatic tests are running in
822 // case there are problems.
823 if ("true".equals(System.getProperty("org.simantics.dumpBundleState")))
824 dumpPlatformBundleState();
826 // 0. Consult all startup extensions before doing anything with the workspace.
827 StartupExtensions.consultStartupExtensions();
828 TimeLogger.log("Consulted platform pre-startup extensions");
830 // 0.1. Clear all temporary files
831 Simantics.clearTemporaryDirectory();
832 TimeLogger.log("Cleared temporary directory");
834 // 0.2 Clear VariableRepository.repository static map which holds references to SessionImplDb
835 VariableRepository.clear();
837 // 0.3 Handle baseline database before opening db
838 boolean usingBaseline = handleBaselineDatabase();
840 // 1. Assert there is a database at <workspace>/db
841 session = setupDatabase(databaseDriverId, progressMonitor, workspacePolicy, userAgent);
842 TimeLogger.log("Database setup complete");
845 XSupport support = session.getService(XSupport.class);
846 if (support.rolledback()) {
848 DatabaseIndexing.deleteAllIndexes();
849 } catch (IOException e) {
850 throw new PlatformException(e);
854 // 2. Assert all graphs, and correct versions, are installed to the database
856 synchronizeOntologies(progressMonitor, ontologyPolicy, requireSynchronize);
857 TimeLogger.log("Synchronized ontologies");
860 // 4. Assert simantics.cfg exists
861 boolean installProject = assertConfiguration(progressMonitor,workspacePolicy);
863 // 5. Assert Project Resource is installed in the database
864 installProject = assertProject(progressMonitor, workspacePolicy, installProject);
866 // 6. Install all features into project, if in debug mode
867 updateInstalledGroups(progressMonitor, installProject);
868 TimeLogger.log("Installed all features into project");
870 // 7. Assert L0.Session in database for this session
871 assertSessionModel(progressMonitor);
873 session.getService(XSupport.class).setServiceMode(false, false);
876 session.sync(new WriteRequest() {
879 public void perform(WriteGraph graph) throws DatabaseException {
880 QueryControl qc = graph.getService(QueryControl.class);
885 TimeLogger.log("Flushed queries");
886 } catch (DatabaseException e) {
887 LOGGER.error("Flushing queries failed.", e);
889 boolean loadProject = true;
892 sessionContext = SimanticsPlatform.INSTANCE.createSessionContext(true);
893 // This must be before setSessionContext since some listeners might query this
894 sessionContext.setHint(SimanticsKeys.KEY_PROJECT, SimanticsPlatform.INSTANCE.projectResource);
896 Simantics.setSessionContext(sessionContext);
898 // 1. Put ResourceBinding that throws an exception to General Bindings
899 simanticsBindings = new SimanticsBindings( null );
900 Bindings.classBindingFactory.addFactory( simanticsBindings );
903 // 2. Create session-specific second Binding context (Databoard) and
904 // put that to Session as a service
905 Session session = sessionContext.getSession();
906 Databoard sessionDataboard = new Databoard();
907 session.registerService(Databoard.class, sessionDataboard);
908 simanticsBindings2 = new SimanticsBindings( session );
909 sessionDataboard.classBindingFactory.addFactory( simanticsBindings2 );
911 // Register datatype bindings
912 Bindings.defaultBindingFactory.getRepository().put(RGB.Integer.BINDING.type(), RGB.Integer.BINDING);
913 Bindings.defaultBindingFactory.getRepository().put(Font.BINDING.type(), Font.BINDING);
917 TimeLogger.log("Load projects");
918 project = Projects.loadProject(sessionContext.getSession(), SimanticsPlatform.INSTANCE.projectResource);
920 sessionContext.setHint(ProjectKeys.KEY_PROJECT, project);
921 TimeLogger.log("Loading projects complete");
924 TimeLogger.log("Project activated");
927 } catch (DatabaseException e) {
928 LOGGER.error("Platform startup failed.", e);
929 throw new PlatformException(e);
930 } catch (ProjectException e) {
931 boolean hasStackTrace = e.getStackTrace().length > 0;
933 throw new PlatformException(e.getMessage(), hasStackTrace);
934 throw new PlatformException(e, hasStackTrace);
939 return sessionContext;
943 public SessionContext createSessionContext(boolean init) throws PlatformException {
945 // Construct and initialize SessionContext from Session.
946 SessionContext sessionContext = SessionContext.create(session, init);
947 TimeLogger.log("Session context created");
949 sessionContext.registerServices();
950 TimeLogger.log("Session services registered");
952 return sessionContext;
953 } catch (DatabaseException e) {
954 throw new PlatformException(e);
958 // private static File getIgnorePrerequisitesFile(URL workspaceUrl) {
959 // if (workspaceUrl == null)
961 // return new File(workspaceUrl.getPath(), ".ignorePrerequisites");
964 // private void ensurePrerequisites(IProgressMonitor progressMonitor, PlatformUserAgent userAgent) throws PlatformException {
965 // Location loc = Platform.getInstanceLocation();
966 // File ignorePrerequisites = getIgnorePrerequisitesFile(loc.getURL());
967 // if (loc.isSet() && ignorePrerequisites != null) {
968 // if (ignorePrerequisites.exists() || ignorePrerequisites.isFile())
973 // ServerEnvironment.ensureServerDependenciesMet();
974 // } catch (ExecutionEnvironmentException e) {
975 // // Not installed properly, ask user whether to try installation.
977 // StringBuilder msg = new StringBuilder();
978 // msg.append("Your system seems to be missing the following prerequisites for running this application:\n\n");
979 // for (Product product : e.requiredProducts)
980 // msg.append("\t" + product.getDescription() + "\n");
981 // msg.append("\nYou can either install the missing components now or ignore and attempt to start the application without them. Ignore Always will ignore this question for this workspace.");
982 // msg.append("\n\nSelecting Cancel will close the application.");
984 // int selection = 0;
985 // if (userAgent != null) {
986 // selection = userAgent.showPrompt("Missing Prerequisites", msg.toString(), new String[] {
987 // "Install Pre-requisites",
993 // boolean tryInstall = false;
994 // switch (selection) {
996 // tryInstall = true;
999 // ignorePrerequisites.createNewFile();
1004 // throw new CancelStartupException();
1007 // if (tryInstall) {
1008 // // Try to install it and check for success afterwards.
1009 // ServerEnvironment.tryInstallDependencies(progressMonitor);
1010 // ServerEnvironment.ensureServerDependenciesMet();
1012 // } catch (InstallException ie) {
1013 // throw new PlatformException(ie);
1014 // } catch (ExecutionEnvironmentException eee) {
1015 // throw new PlatformException(eee);
1016 // } catch (IOException ie) {
1017 // throw new PlatformException(ie);
1023 * Shutdown Simantics Platform.
1025 * In SWB this is handled in SimanticsWorkbenchAdvisor#disconnectFromWorkspace.
1027 * @param progressMonitor optional progress monitor
1028 * @throws PlatformException
1030 public void shutdown(IProgressMonitor progressMonitor) throws PlatformException
1032 SubMonitor progress = SubMonitor.convert(progressMonitor, 100);
1033 PlatformException platformException = null;
1035 progress.subTask("Close Project");
1036 if (project != null) {
1037 project.safeDispose();
1039 progress.worked(10);
1042 progress.subTask("Close Database Session");
1043 Databoard databoard = null;
1044 if (sessionContext != null) {
1045 Session s = sessionContext.peekSession();
1047 databoard = s.peekService(Databoard.class);
1049 progress.subTask("Flushing Index Caches");
1051 Simantics.flushIndexCaches(progress.newChild(20), s);
1052 } catch (Throwable t) {
1053 LOGGER.error("Failed to flush index caches.", t);
1057 progress.subTask("Close Database Session");
1058 sessionContext.safeDispose();
1059 sessionContext = null;
1060 Simantics.setSessionContext(null);
1062 if (simanticsBindings != null) {
1063 Bindings.classBindingFactory.removeFactory( simanticsBindings );
1064 simanticsBindings = null;
1066 if (databoard != null) {
1067 if (simanticsBindings2 != null) {
1068 databoard.classBindingFactory.removeFactory( simanticsBindings2 );
1069 simanticsBindings2 = null;
1074 // Make sure Simantics clipboard doesn't store unwanted session data references.
1075 Simantics.setClipboard(new SimanticsClipboardImpl());
1077 progress.worked(30);
1080 projectResource = null;
1081 currentDatabaseDriver = null;
1083 DependenciesRelation.assertFinishedTracking();
1085 } catch (Exception e) {
1086 platformException = new PlatformException("Failed to shutdown Simantics Platform", e);
1089 progress.worked(10);
1090 progress.subTask("Shutting down database");
1092 if (null != databasebManagement)
1093 databasebManagement.shutdown();
1094 } catch (Throwable t) {
1095 LOGGER.error("Database shutdown failed.", t);
1097 progress.worked(10);
1099 progress.subTask("Clearing Workspace Temporary Directory");
1101 Simantics.clearTemporaryDirectory();
1102 } catch (Throwable t) {
1103 LOGGER.error("Failed to clear the temporary directory.", t);
1105 progress.worked(10);
1106 if (null != platformException)
1107 throw platformException;
1110 // TODO: consider removing this in the future ??
1112 public void stateChanged(LifecycleState newState) {
1113 if(newState == LifecycleState.CLOSED) {
1115 if(Platform.isRunning()) {
1116 mainThread.interrupt();
1123 * @return <code>true</code> if discard was successful, <code>false</code>
1124 * if there was no session, {@link UndoRedoSupport} or
1125 * {@link UndoContext} to discard through
1127 public boolean discardSessionUndoHistory() {
1128 Session s = session;
1130 UndoRedoSupport urs = s.peekService(UndoRedoSupport.class);
1132 UndoContext uc = urs.getUndoContext(s);
1142 public void reconnect(String databaseDriverId) throws Exception {
1143 // Starts database server.
1144 if (currentDatabaseDriver != null)
1145 databaseDriverId = currentDatabaseDriver;
1146 SimanticsPlatform.INSTANCE.startUp(databaseDriverId, null, RecoveryPolicy.ThrowError, OntologyRecoveryPolicy.ThrowError, true, null);
1149 private void dumpPlatformBundleState() {
1150 BundleDescription[] bs = Platform.getPlatformAdmin().getState().getBundles();
1151 System.out.println("Total bundles: " + bs.length);
1152 for (BundleDescription b : bs) {
1153 System.out.format("%-80s @ %s\n", b.toString(), b.getLocation());
1157 private Ini loadOrCreateDatabaseIni(Path path, String databaseDriverId)
1158 throws InvalidFileFormatException, IOException
1160 File f = path.toFile();
1161 Ini dbIni = Files.isRegularFile(path) ? new Ini(f) : new Ini();
1162 String iniId = dbIni != null ? dbIni.get("driver", "id") : null;
1163 if (iniId == null) {
1164 dbIni.put("driver", "id", databaseDriverId);