]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/internal/SessionUtil.java
Merge "Move debugging options under DevelopmentKeys"
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / internal / SessionUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.internal;
13
14 import java.io.File;
15 import java.util.Properties;
16 import java.util.UUID;
17
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;
27
28 /**
29  * An as-simple-as-possible utility class for starting a Simantics database
30  * instance and opening a connection ({@link Session}) to it.
31  *
32  * <p>
33  * To get up and running, simply invoke:
34  * <pre>
35  *     SessionUtil util = new SessionUtil("my-client-id");
36  *     try {
37  *         File workspaceLocation = ...;
38  *         Session session = util.openSession(workspaceLocation);
39  *         // do something with the database session
40  *     } finally {
41  *         util.close();
42  *     }
43  * </pre>
44  *
45  * <p>
46  * This class is a provisional utility, pending for public inclusion.
47  *
48  * @author Tuukka Lehtonen
49  * @see Session
50  */
51 public class SessionUtil {
52
53         private String clientId;
54         @SuppressWarnings("unused")
55         private File workspace;
56
57         private ServerManager serverManager;
58         private ServerEx server;
59         private Session session;
60
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();
66         }
67
68         public SessionUtil() {
69                 this(UUID.randomUUID().toString());
70         }
71
72         public SessionUtil(String clientId) {
73                 serverManager = ServerManagerFactory.getServerManager();
74                 this.clientId = clientId;
75         }
76
77         public void close() throws DatabaseException {
78                 if (session != null) {
79                         session.getService(LifecycleSupport.class).close();
80                 }
81                 serverManager.close();
82         }
83
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;
92 //              return session;
93 //      }
94
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);
99                 return session;
100         }
101
102 }