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.db.layer0.internal;
15 import java.util.concurrent.TimeUnit;
17 import org.eclipse.core.runtime.Platform;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.util.SimanticsClipboard;
22 import org.simantics.db.layer0.util.SimanticsKeys;
23 import org.simantics.db.management.ISessionContext;
24 import org.simantics.db.management.ISessionContextProvider;
25 import org.simantics.db.management.ISessionContextProviderSource;
26 import org.simantics.db.request.ReadInterface;
27 import org.simantics.db.request.WriteInterface;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.operation.Layer0X;
30 import org.simantics.utils.threads.ThreadUtils;
33 * An internal facade for accessing basic Simantics platform services.
34 * Usable without a graphical UI, i.e. in headless contexts.
36 * Use org.simantics.Simantics instead where ever possible.
38 public class SimanticsInternal {
40 private static ISessionContextProviderSource providerSource = null;
43 * Queue execution of a runnable.
47 public static void async(Runnable runnable) {
48 ThreadUtils.getBlockingWorkExecutor().execute(runnable);
51 public static void async(Runnable runnable, int delay, TimeUnit unit) {
52 ThreadUtils.getTimer().schedule(runnable, delay, unit);
56 * Queue execution of a non-blocking runnable. Use this method with caution.
57 * A non-blocking runnable nevers locks anything, No Locks, No semaphores,
58 * No Object.wait(), No synchronized() {} blocks.
60 * @param runnable a non-blocking runnable
62 public static void asyncNonblocking(Runnable runnable) {
63 ThreadUtils.getNonBlockingWorkExecutor().execute(runnable);
67 * Schedule execution of a non-blocking runnable. Use this method with caution.
68 * A non-blocking runnable never locks anything, No Locks, No semaphores,
69 * No Object,wait(), No synchronized() {} blocks.
71 * @param runnable a non-blocking runnable
75 public static void asyncNonblocking(Runnable runnable, int initialDelay, int period) {
76 ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);
79 public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {
80 return getSessionContextProvider().setSessionContext(ctx);
83 public static void setSessionContextProviderSource(ISessionContextProviderSource source) {
85 throw new IllegalArgumentException("null provider source");
86 providerSource = source;
89 public static ISessionContextProviderSource getProviderSource() {
90 if (providerSource == null)
91 throw new IllegalStateException(
92 "providerSource must be initialized by the application before using class Simantics");
93 return providerSource;
96 public static ISessionContextProvider getSessionContextProvider() {
97 return getProviderSource().getActive();
101 * Returns the database session context associated with the currently active
102 * context. This method should be used to retrieve session contexts only
103 * when the client is sure that the correct context is active.
105 * @return the session context associated with the currently active context
106 * or <code>null</code> if the context has no session context
108 public static ISessionContext getSessionContext() {
109 ISessionContextProvider provider = getSessionContextProvider();
110 return provider != null ? provider.getSessionContext() : null;
114 * Returns the database Session bound to the currently active context.
117 * The method always returns a non-null Session or produces an
118 * IllegalStateException if a Session was not attainable.
121 * @return the Session bound to the currently active workbench window
122 * @throws IllegalStateException if no Session was available
124 public static Session getSession() {
125 ISessionContext ctx = getSessionContext();
127 throw new IllegalStateException("Session unavailable, no database session open");
128 return ctx.getSession();
132 * Returns the database Session bound to the currently active context.
133 * Differently from {@link #getSession()}, this method returns
134 * <code>null</code> if there is no current Session available.
137 * @return the Session bound to the currently active context or
140 public static Session peekSession() {
141 ISessionContext ctx = getSessionContext();
142 return ctx == null ? null : ctx.peekSession();
146 * @return the currently open and active project as an IProject
147 * @throws IllegalStateException if there is no currently active database
148 * session, which also means there is no active project at the
151 public static Resource getProject() {
152 ISessionContext ctx = getSessionContext();
154 throw new IllegalStateException("No current database session");
155 return ctx.getHint(SimanticsKeys.KEY_PROJECT);
159 * @return the currently open and active project as an IProject or null if
160 * there is either no project or no active database session
162 public static Resource peekProject() {
163 ISessionContext ctx = getSessionContext();
164 return ctx != null ? (Resource) ctx.getHint(SimanticsKeys.KEY_PROJECT) : null;
167 private static SimanticsClipboard clipboard = SimanticsClipboard.EMPTY;
172 public static void setClipboard(SimanticsClipboard content) {
174 throw new NullPointerException("null clipboard content");
178 public static SimanticsClipboard getClipboard() {
182 public static Layer0 getLayer0() throws DatabaseException {
183 return Layer0.getInstance(getSession());
186 public static Layer0X getLayer0X() throws DatabaseException {
187 return Layer0X.getInstance(getSession());
191 public static <T> T sync(ReadInterface<T> r) throws DatabaseException {
192 return getSession().sync(r);
195 public static <T> T sync(WriteInterface<T> r) throws DatabaseException {
196 return getSession().sync(r);
199 public static File getTemporaryDirectory() {
200 File workspace = Platform.getLocation().toFile();
201 File temp = new File(workspace, "tempFiles");