1 /*******************************************************************************
2 * Copyright (c) 2013 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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.internal;
15 import java.util.Properties;
16 import java.util.UUID;
18 import org.eclipse.core.runtime.IProduct;
19 import org.eclipse.core.runtime.Platform;
20 import org.simantics.db.ServerEx;
21 import org.simantics.db.Session;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.service.LifecycleSupport;
24 import org.simantics.db.service.XSupport;
25 import org.simantics.project.management.ServerManager;
26 import org.simantics.project.management.ServerManagerFactory;
29 * An as-simple-as-possible utility class for starting a Simantics database
30 * instance and opening a connection ({@link Session}) to it.
33 * To get up and running, simply invoke:
35 * SessionUtil util = new SessionUtil("my-client-id");
37 * File workspaceLocation = ...;
38 * Session session = util.openSession(workspaceLocation);
39 * // do something with the database session
46 * This class is a provisional utility, pending for public inclusion.
48 * @author Tuukka Lehtonen
51 public class SessionUtil {
53 private String clientId;
54 @SuppressWarnings("unused")
55 private File workspace;
57 private ServerManager serverManager;
58 private ServerEx server;
59 private Session session;
61 public static String getApplicationClientId() {
62 IProduct product = Platform.getProduct();
63 if(product == null) return "noProduct";
64 String application = product.getApplication();
65 return application != null ? application : UUID.randomUUID().toString();
68 public SessionUtil() {
69 this(UUID.randomUUID().toString());
72 public SessionUtil(String clientId) {
73 serverManager = ServerManagerFactory.getServerManager();
74 this.clientId = clientId;
77 public void close() throws DatabaseException {
78 if (session != null) {
79 session.getService(LifecycleSupport.class).close();
81 serverManager.close();
84 // public Session open(File workspace) throws IOException, DatabaseException {
85 // File dbDir = new File(workspace, "db");
86 // if (!dbDir.exists() || !dbDir.isDirectory())
87 // throw new FileNotFoundException("database directory " + dbDir + " not found");
88 // server = serverManager.getServer(dbDir);
89 // server.start(null);
90 // session = openSession(server, dbDir);
91 // this.workspace = workspace;
95 private Session openSession(ServerEx server, File dbDir) throws DatabaseException {
96 Properties info = new Properties(ServerManager.DEFAULT);
97 session = server.createSession(info);
98 session.getService(XSupport.class).setServiceMode(true, true);