]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/SimanticsInternal.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / 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.common;
13
14 import java.util.concurrent.TimeUnit;
15
16 import org.simantics.db.Session;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.management.ISessionContext;
19 import org.simantics.db.management.ISessionContextProvider;
20 import org.simantics.db.management.ISessionContextProviderSource;
21 import org.simantics.db.request.ReadInterface;
22 import org.simantics.db.request.WriteInterface;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.operation.Layer0X;
25 import org.simantics.utils.threads.ThreadUtils;
26
27 /**
28  * An internal facade for accessing basic Simantics platform services.
29  * Usable without a graphical UI, i.e. in headless contexts.
30  * 
31  * Use org.simantics.Simantics instead where ever possible.
32  */
33 public class SimanticsInternal {
34
35     private static ISessionContextProviderSource providerSource = null;
36
37     /**
38      * Queue execution of a runnable. 
39      * 
40      * @param runnable
41      */
42     public static void async(Runnable runnable) {
43         ThreadUtils.getBlockingWorkExecutor().execute(runnable);
44     }
45
46     public static void async(Runnable runnable, int delay, TimeUnit unit) {
47         ThreadUtils.getTimer().schedule(runnable, delay, unit);
48     }
49     
50     /**
51      * Queue execution of a non-blocking runnable. Use this method with caution. 
52      * A non-blocking runnable nevers locks anything, No Locks, No semaphores,
53      * No Object.wait(), No synchronized() {} blocks.   
54      * 
55      * @param runnable a non-blocking runnable
56      */
57     public static void asyncNonblocking(Runnable runnable) {
58         ThreadUtils.getNonBlockingWorkExecutor().execute(runnable);
59     }
60
61     /**
62      * Schedule execution of a non-blocking runnable. Use this method with caution. 
63      * A non-blocking runnable never locks anything, No Locks, No semaphores,
64      * No Object,wait(), No synchronized() {} blocks.   
65      * 
66      * @param runnable a non-blocking runnable
67      * @param initialDelay
68      * @param period
69      */
70     public static void asyncNonblocking(Runnable runnable, int initialDelay, int period) {
71         ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);
72     }
73     
74     public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {
75         return getSessionContextProvider().setSessionContext(ctx);
76     }
77
78     public static void setSessionContextProviderSource(ISessionContextProviderSource source) {
79         if (source == null)
80             throw new IllegalArgumentException("null provider source");
81         providerSource = source;
82     }
83
84     public static ISessionContextProviderSource getProviderSource() {
85         if (providerSource == null)
86             throw new IllegalStateException(
87             "providerSource must be initialized by the application before using class Simantics");
88         return providerSource;
89     }
90
91     public static ISessionContextProvider getSessionContextProvider() {
92         return getProviderSource().getActive();
93     }
94
95     /**
96      * Returns the database session context associated with the currently active
97      * context. This method should be used to retrieve session contexts only
98      * when the client is sure that the correct context is active.
99      * 
100      * @return the session context associated with the currently active context
101      *         or <code>null</code> if the context has no session context
102      */
103     public static ISessionContext getSessionContext() {
104         ISessionContextProvider provider = getSessionContextProvider();
105         return provider != null ? provider.getSessionContext() : null;
106     }
107
108     /**
109      * Returns the database Session bound to the currently active context.
110      * 
111      * <p>
112      * The method always returns a non-null Session or produces an
113      * IllegalStateException if a Session was not attainable.
114      * </p>
115      * 
116      * @return the Session bound to the currently active workbench window
117      * @throws IllegalStateException if no Session was available
118      */
119     public static Session getSession() {
120         ISessionContext ctx = getSessionContext();
121         if (ctx == null)
122             throw new IllegalStateException("Session unavailable, no database session open");
123         return ctx.getSession();
124     }
125
126     /**
127      * Returns the database Session bound to the currently active context.
128      * Differently from {@link #getSession()}, this method returns
129      * <code>null</code> if there is no current Session available.
130      * 
131      * @see #getSession()
132      * @return the Session bound to the currently active context or
133      *         <code>null</code>
134      */
135     public static Session peekSession() {
136         ISessionContext ctx = getSessionContext();
137         return ctx == null ? null : ctx.peekSession();
138     }
139
140     public static Layer0 getLayer0() throws DatabaseException {
141         return Layer0.getInstance(getSession());
142     }
143
144     public static Layer0X getLayer0X() throws DatabaseException {
145         return Layer0X.getInstance(getSession());
146     }
147
148     
149     public static <T> T sync(ReadInterface<T> r) throws DatabaseException {
150         return getSession().sync(r);
151     }
152     
153     public static <T> T sync(WriteInterface<T> r) throws DatabaseException {
154         return getSession().sync(r);
155     }
156     
157 }