]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.internal;\r
13 \r
14 import java.io.File;\r
15 import java.util.concurrent.TimeUnit;\r
16 \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
31 \r
32 /**\r
33  * An internal facade for accessing basic Simantics platform services.\r
34  * Usable without a graphical UI, i.e. in headless contexts.\r
35  * \r
36  * Use org.simantics.Simantics instead where ever possible.\r
37  */\r
38 public class SimanticsInternal {\r
39 \r
40     private static ISessionContextProviderSource providerSource = null;\r
41 \r
42     /**\r
43      * Queue execution of a runnable. \r
44      * \r
45      * @param runnable\r
46      */\r
47     public static void async(Runnable runnable) {\r
48         ThreadUtils.getBlockingWorkExecutor().execute(runnable);\r
49     }\r
50 \r
51     public static void async(Runnable runnable, int delay, TimeUnit unit) {\r
52         ThreadUtils.getTimer().schedule(runnable, delay, unit);\r
53     }\r
54     \r
55     /**\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
59      * \r
60      * @param runnable a non-blocking runnable\r
61      */\r
62     public static void asyncNonblocking(Runnable runnable) {\r
63         ThreadUtils.getNonBlockingWorkExecutor().execute(runnable);\r
64     }\r
65 \r
66     /**\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
70      * \r
71      * @param runnable a non-blocking runnable\r
72      * @param initialDelay\r
73      * @param period\r
74      */\r
75     public static void asyncNonblocking(Runnable runnable, int initialDelay, int period) {\r
76         ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);\r
77     }\r
78     \r
79     public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {\r
80         return getSessionContextProvider().setSessionContext(ctx);\r
81     }\r
82 \r
83     public static void setSessionContextProviderSource(ISessionContextProviderSource source) {\r
84         if (source == null)\r
85             throw new IllegalArgumentException("null provider source");\r
86         providerSource = source;\r
87     }\r
88 \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
94     }\r
95 \r
96     public static ISessionContextProvider getSessionContextProvider() {\r
97         return getProviderSource().getActive();\r
98     }\r
99 \r
100     /**\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
104      * \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
107      */\r
108     public static ISessionContext getSessionContext() {\r
109         ISessionContextProvider provider = getSessionContextProvider();\r
110         return provider != null ? provider.getSessionContext() : null;\r
111     }\r
112 \r
113     /**\r
114      * Returns the database Session bound to the currently active context.\r
115      * \r
116      * <p>\r
117      * The method always returns a non-null Session or produces an\r
118      * IllegalStateException if a Session was not attainable.\r
119      * </p>\r
120      * \r
121      * @return the Session bound to the currently active workbench window\r
122      * @throws IllegalStateException if no Session was available\r
123      */\r
124     public static Session getSession() {\r
125         ISessionContext ctx = getSessionContext();\r
126         if (ctx == null)\r
127             throw new IllegalStateException("Session unavailable, no database session open");\r
128         return ctx.getSession();\r
129     }\r
130 \r
131     /**\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
135      * \r
136      * @see #getSession()\r
137      * @return the Session bound to the currently active context or\r
138      *         <code>null</code>\r
139      */\r
140     public static Session peekSession() {\r
141         ISessionContext ctx = getSessionContext();\r
142         return ctx == null ? null : ctx.peekSession();\r
143     }\r
144 \r
145     /**\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
149      *         moment\r
150      */\r
151     public static Resource getProject() {\r
152         ISessionContext ctx = getSessionContext();\r
153         if (ctx == null)\r
154             throw new IllegalStateException("No current database session");\r
155         return ctx.getHint(SimanticsKeys.KEY_PROJECT);\r
156     }\r
157 \r
158     /**\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
161      */\r
162     public static Resource peekProject() {\r
163         ISessionContext ctx = getSessionContext();\r
164         return ctx != null ? (Resource) ctx.getHint(SimanticsKeys.KEY_PROJECT) : null;\r
165     }\r
166 \r
167     private static SimanticsClipboard clipboard = SimanticsClipboard.EMPTY;\r
168 \r
169     /**\r
170      * @param content\r
171      */\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
176     }\r
177 \r
178     public static SimanticsClipboard getClipboard() {\r
179         return clipboard;\r
180     }\r
181 \r
182     public static Layer0 getLayer0() throws DatabaseException {\r
183         return Layer0.getInstance(getSession());\r
184     }\r
185 \r
186     public static Layer0X getLayer0X() throws DatabaseException {\r
187         return Layer0X.getInstance(getSession());\r
188     }\r
189 \r
190     \r
191     public static <T> T sync(ReadInterface<T> r) throws DatabaseException {\r
192         return getSession().sync(r);\r
193     }\r
194     \r
195     public static <T> T sync(WriteInterface<T> r) throws DatabaseException {\r
196         return getSession().sync(r);\r
197     }\r
198     \r
199     public static File getTemporaryDirectory() {\r
200         File workspace = Platform.getLocation().toFile();\r
201         File temp = new File(workspace, "tempFiles");\r
202         temp.mkdirs();\r
203         return temp;\r
204     }\r
205     \r
206 }\r