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