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