]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/internal/SimanticsInternal.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / internal / SimanticsInternal.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/internal/SimanticsInternal.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/internal/SimanticsInternal.java
new file mode 100644 (file)
index 0000000..ce4fd23
--- /dev/null
@@ -0,0 +1,206 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.internal;\r
+\r
+import java.io.File;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.eclipse.core.runtime.Platform;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.SimanticsClipboard;\r
+import org.simantics.db.layer0.util.SimanticsKeys;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.management.ISessionContextProvider;\r
+import org.simantics.db.management.ISessionContextProviderSource;\r
+import org.simantics.db.request.ReadInterface;\r
+import org.simantics.db.request.WriteInterface;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+\r
+/**\r
+ * An internal facade for accessing basic Simantics platform services.\r
+ * Usable without a graphical UI, i.e. in headless contexts.\r
+ * \r
+ * Use org.simantics.Simantics instead where ever possible.\r
+ */\r
+public class SimanticsInternal {\r
+\r
+    private static ISessionContextProviderSource providerSource = null;\r
+\r
+    /**\r
+     * Queue execution of a runnable. \r
+     * \r
+     * @param runnable\r
+     */\r
+    public static void async(Runnable runnable) {\r
+        ThreadUtils.getBlockingWorkExecutor().execute(runnable);\r
+    }\r
+\r
+    public static void async(Runnable runnable, int delay, TimeUnit unit) {\r
+        ThreadUtils.getTimer().schedule(runnable, delay, unit);\r
+    }\r
+    \r
+    /**\r
+     * Queue execution of a non-blocking runnable. Use this method with caution. \r
+     * A non-blocking runnable nevers locks anything, No Locks, No semaphores,\r
+     * No Object.wait(), No synchronized() {} blocks.   \r
+     * \r
+     * @param runnable a non-blocking runnable\r
+     */\r
+    public static void asyncNonblocking(Runnable runnable) {\r
+        ThreadUtils.getNonBlockingWorkExecutor().execute(runnable);\r
+    }\r
+\r
+    /**\r
+     * Schedule execution of a non-blocking runnable. Use this method with caution. \r
+     * A non-blocking runnable never locks anything, No Locks, No semaphores,\r
+     * No Object,wait(), No synchronized() {} blocks.   \r
+     * \r
+     * @param runnable a non-blocking runnable\r
+     * @param initialDelay\r
+     * @param period\r
+     */\r
+    public static void asyncNonblocking(Runnable runnable, int initialDelay, int period) {\r
+        ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);\r
+    }\r
+    \r
+    public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {\r
+        return getSessionContextProvider().setSessionContext(ctx);\r
+    }\r
+\r
+    public static void setSessionContextProviderSource(ISessionContextProviderSource source) {\r
+        if (source == null)\r
+            throw new IllegalArgumentException("null provider source");\r
+        providerSource = source;\r
+    }\r
+\r
+    public static ISessionContextProviderSource getProviderSource() {\r
+        if (providerSource == null)\r
+            throw new IllegalStateException(\r
+            "providerSource must be initialized by the application before using class Simantics");\r
+        return providerSource;\r
+    }\r
+\r
+    public static ISessionContextProvider getSessionContextProvider() {\r
+        return getProviderSource().getActive();\r
+    }\r
+\r
+    /**\r
+     * Returns the database session context associated with the currently active\r
+     * context. This method should be used to retrieve session contexts only\r
+     * when the client is sure that the correct context is active.\r
+     * \r
+     * @return the session context associated with the currently active context\r
+     *         or <code>null</code> if the context has no session context\r
+     */\r
+    public static ISessionContext getSessionContext() {\r
+        ISessionContextProvider provider = getSessionContextProvider();\r
+        return provider != null ? provider.getSessionContext() : null;\r
+    }\r
+\r
+    /**\r
+     * Returns the database Session bound to the currently active context.\r
+     * \r
+     * <p>\r
+     * The method always returns a non-null Session or produces an\r
+     * IllegalStateException if a Session was not attainable.\r
+     * </p>\r
+     * \r
+     * @return the Session bound to the currently active workbench window\r
+     * @throws IllegalStateException if no Session was available\r
+     */\r
+    public static Session getSession() {\r
+        ISessionContext ctx = getSessionContext();\r
+        if (ctx == null)\r
+            throw new IllegalStateException("Session unavailable, no database session open");\r
+        return ctx.getSession();\r
+    }\r
+\r
+    /**\r
+     * Returns the database Session bound to the currently active context.\r
+     * Differently from {@link #getSession()}, this method returns\r
+     * <code>null</code> if there is no current Session available.\r
+     * \r
+     * @see #getSession()\r
+     * @return the Session bound to the currently active context or\r
+     *         <code>null</code>\r
+     */\r
+    public static Session peekSession() {\r
+        ISessionContext ctx = getSessionContext();\r
+        return ctx == null ? null : ctx.peekSession();\r
+    }\r
+\r
+    /**\r
+     * @return the currently open and active project as an IProject\r
+     * @throws IllegalStateException if there is no currently active database\r
+     *         session, which also means there is no active project at the\r
+     *         moment\r
+     */\r
+    public static Resource getProject() {\r
+        ISessionContext ctx = getSessionContext();\r
+        if (ctx == null)\r
+            throw new IllegalStateException("No current database session");\r
+        return ctx.getHint(SimanticsKeys.KEY_PROJECT);\r
+    }\r
+\r
+    /**\r
+     * @return the currently open and active project as an IProject or null if\r
+     *         there is either no project or no active database session\r
+     */\r
+    public static Resource peekProject() {\r
+        ISessionContext ctx = getSessionContext();\r
+        return ctx != null ? (Resource) ctx.getHint(SimanticsKeys.KEY_PROJECT) : null;\r
+    }\r
+\r
+    private static SimanticsClipboard clipboard = SimanticsClipboard.EMPTY;\r
+\r
+    /**\r
+     * @param content\r
+     */\r
+    public static void setClipboard(SimanticsClipboard content) {\r
+        if (content == null)\r
+            throw new NullPointerException("null clipboard content");\r
+        clipboard = content;\r
+    }\r
+\r
+    public static SimanticsClipboard getClipboard() {\r
+        return clipboard;\r
+    }\r
+\r
+    public static Layer0 getLayer0() throws DatabaseException {\r
+        return Layer0.getInstance(getSession());\r
+    }\r
+\r
+    public static Layer0X getLayer0X() throws DatabaseException {\r
+        return Layer0X.getInstance(getSession());\r
+    }\r
+\r
+    \r
+    public static <T> T sync(ReadInterface<T> r) throws DatabaseException {\r
+       return getSession().sync(r);\r
+    }\r
+    \r
+    public static <T> T sync(WriteInterface<T> r) throws DatabaseException {\r
+       return getSession().sync(r);\r
+    }\r
+    \r
+    public static File getTemporaryDirectory() {\r
+        File workspace = Platform.getLocation().toFile();\r
+        File temp = new File(workspace, "tempFiles");\r
+        temp.mkdirs();\r
+        return temp;\r
+    }\r
+    \r
+}\r