1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common;
14 import java.util.concurrent.TimeUnit;
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;
28 * An internal facade for accessing basic Simantics platform services.
29 * Usable without a graphical UI, i.e. in headless contexts.
31 * Use org.simantics.Simantics instead where ever possible.
33 public class SimanticsInternal {
35 private static ISessionContextProviderSource providerSource = null;
38 * Queue execution of a runnable.
42 public static void async(Runnable runnable) {
43 ThreadUtils.getBlockingWorkExecutor().execute(runnable);
46 public static void async(Runnable runnable, int delay, TimeUnit unit) {
47 ThreadUtils.getTimer().schedule(runnable, delay, unit);
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.
55 * @param runnable a non-blocking runnable
57 public static void asyncNonblocking(Runnable runnable) {
58 ThreadUtils.getNonBlockingWorkExecutor().execute(runnable);
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.
66 * @param runnable a non-blocking runnable
70 public static void asyncNonblocking(Runnable runnable, int initialDelay, int period) {
71 ThreadUtils.getNonBlockingWorkExecutor().scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.MILLISECONDS);
74 public static synchronized ISessionContext setSessionContext(ISessionContext ctx) {
75 return getSessionContextProvider().setSessionContext(ctx);
78 public static void setSessionContextProviderSource(ISessionContextProviderSource source) {
80 throw new IllegalArgumentException("null provider source");
81 providerSource = source;
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;
91 public static ISessionContextProvider getSessionContextProvider() {
92 return getProviderSource().getActive();
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.
100 * @return the session context associated with the currently active context
101 * or <code>null</code> if the context has no session context
103 public static ISessionContext getSessionContext() {
104 ISessionContextProvider provider = getSessionContextProvider();
105 return provider != null ? provider.getSessionContext() : null;
109 * Returns the database Session bound to the currently active context.
112 * The method always returns a non-null Session or produces an
113 * IllegalStateException if a Session was not attainable.
116 * @return the Session bound to the currently active workbench window
117 * @throws IllegalStateException if no Session was available
119 public static Session getSession() {
120 ISessionContext ctx = getSessionContext();
122 throw new IllegalStateException("Session unavailable, no database session open");
123 return ctx.getSession();
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.
132 * @return the Session bound to the currently active context or
135 public static Session peekSession() {
136 ISessionContext ctx = getSessionContext();
137 return ctx == null ? null : ctx.peekSession();
140 public static Layer0 getLayer0() throws DatabaseException {
141 return Layer0.getInstance(getSession());
144 public static Layer0X getLayer0X() throws DatabaseException {
145 return Layer0X.getInstance(getSession());
149 public static <T> T sync(ReadInterface<T> r) throws DatabaseException {
150 return getSession().sync(r);
153 public static <T> T sync(WriteInterface<T> r) throws DatabaseException {
154 return getSession().sync(r);