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.ui.internal;
14 import java.util.concurrent.TimeUnit;
16 import org.simantics.db.management.ISessionContext;
17 import org.simantics.internal.TimedSessionCache;
18 import org.simantics.ui.SimanticsUI;
19 import org.simantics.utils.datastructures.cache.SoftTimedCache;
22 * @author Tuukka Lehtonen
24 * @deprecated do not use, no direct replacement
27 public final class SessionUtils {
30 * Puts the specified session on the list of to-be-closed sessions. If no
31 * one needs the session after 10 seconds, it will be closed automatically.
32 * It will also be closed automatically if there is memory pressure to do
36 * @see TimedSessionCache
38 public static void releaseUnusedSessionAfterHoldTime(ISessionContext ctx) {
39 releaseUnusedSessionAfterHoldTime(ctx, 10000);
43 * Puts the specified session on the list of to-be-closed sessions. If no
44 * one needs the session after the specified amount of time, it will be
45 * closed automatically. It will also be closed automatically if there is
46 * memory pressure to do so.
49 * @param holdTimeMs hold time for released session in milliseconds
50 * @see TimedSessionCache
52 public static void releaseUnusedSessionAfterHoldTime(ISessionContext ctx, long holdTime) {
54 throw new IllegalArgumentException("null session context");
56 // See if the session is used by the UI.
57 // If it is, don't put the session on the release queue.
58 if (SimanticsUI.isInUse(ctx))
61 synchronized (SessionUtils.class) {
62 // Ensure that all previously cached sessions are scheduled for disposal.
63 TimedSessionCache cache = TimedSessionCache.getCache();
64 for (SoftTimedCache<Object, ISessionContext>.CacheEntry e : cache.getEntries()) {
65 e.schedule(holdTime, TimeUnit.MILLISECONDS);
68 // Cache this session for an infinite amount of time.
69 TimedSessionCache.getCache().put(ctx.getSession(), ctx, 5, TimeUnit.MINUTES);
73 // private static ISessionContext releaseFromCache(Object address) {
74 // synchronized (SessionUtils.class) {
75 // ISessionContext cachedCtx = TimedSessionCache.getCache().release(address);
84 // * @throws Layer0MissingException
85 // * @throws IOException
87 // static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, UserAuthenticator auth)
88 // throws DatabaseException, IOException
90 // return getSessionContext(monitor, server, auth, true);
97 // * @throws Layer0MissingException
98 // * @throws IOException
100 // static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server)
101 // throws DatabaseException, IOException
103 // return getSessionContext(monitor, server, null, true);
109 // * @param registerServices
111 // * @throws Layer0MissingException
112 // * @throws IOException
114 // static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, UserAuthenticator auth, boolean registerServices)
115 // throws DatabaseException, IOException
117 // return getSessionContext(monitor, server.getName(), auth, registerServices);
123 // * @param registerServices
125 // * @throws Layer0MissingException
126 // * @throws IOException
128 // static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, boolean registerServices)
129 // throws DatabaseException, IOException
131 // return getSessionContext(monitor, server.getAddress(), null, registerServices);
137 // * @param registerServices
139 // * @throws Layer0MissingException
140 // * @throws IOException
142 // static ISessionContext getSessionContext(IProgressMonitor monitor, ServerAddress address, boolean registerServices)
143 // throws DatabaseException, IOException
145 // return getSessionContext(monitor, address, null, registerServices);
151 // * @param registerServices
152 // * @return a database session context
153 // * @throws Layer0MissingException if the layer0 ontology in the database is
154 // * not compatible with this program or it does not exist
155 // * @throws IOException
157 // static ISessionContext getSessionContext(IProgressMonitor monitor, final ServerAddress address, UserAuthenticator auth, boolean registerServices)
158 // throws DatabaseException, IOException
160 // return getSessionContext(monitor, address, auth, registerServices, false);
167 // * @param registerServices
168 // * @param forceReload
169 // * @return a database session context
170 // * @throws Layer0MissingException if the layer0 ontology in the database is
171 // * not compatible with this program or it does not exist
172 // * @throws IOException
174 // static synchronized ISessionContext getSessionContext(IProgressMonitor monitor, final ServerAddress address, UserAuthenticator auth, boolean registerServices, boolean forceReload)
175 // throws DatabaseException, IOException
177 // if(!forceReload) {
178 // ISessionContext newCtx = Simantics.getSessionContext();
179 // if (newCtx != null && address.equals(newCtx.getAddress()))
182 // // for (ISessionContextProvider provider : SimanticsUI.getProviderSource().getAll()) {
183 // // ISessionContext ctx = provider.getSessionContext();
184 // // if (ctx != null && address.equals(ctx.getAddress()))
188 // ISessionContext cachedCtx = releaseFromCache(address);
189 // if (cachedCtx != null) {
190 // if (registerServices && cachedCtx instanceof SessionContext) {
191 // safeRegisterServices((SessionContext) cachedCtx);
197 // if (monitor != null)
198 // monitor.subTask("Connecting to database at " + address);
200 // boolean success = false;
201 // boolean lastFailed = false;
203 // SessionContext ctx = null;
204 // UserAuthenticationAgent agent = null;
206 // agent = UserAuthenticationAgents.staticAgent(auth);
208 // while (!success) {
209 // final DataContainer<LoginModel> loginModel = new DataContainer<LoginModel>();
210 // final DataContainer<ServerInfo> serverInfo = new DataContainer<ServerInfo>();
212 // if (agent == null) {
213 // final boolean forceQuery = lastFailed;
214 // agent = new UserAuthenticationAgent() {
216 // public UserAuthenticator getAuthenticator(final ServerInformation information) throws IOException {
217 // final DataContainer<UserAuthenticator> authenticator = new DataContainer<UserAuthenticator>();
218 // serverInfo.set(new ServerInfo("", address, information));
220 // ThreadUtils.syncExec(SWTThread.getThreadAccess(Display.getDefault()), new Runnable() {
222 // public void run() {
223 // LoginModel model = AuthenticationUtils.queryAuthenticationInfo(Display.getDefault().getActiveShell(), serverInfo.get(), forceQuery);
224 // if (model != null) {
225 // loginModel.set(model);
226 // authenticator.set(UserAuthenticators.byNameAndPassword(model.getName(), model.getPassword()));
231 // if (authenticator.get() == null)
232 // throw new LoginCancelledException("User authentication was not provided.");
233 // return authenticator.get();
239 // ctx = SessionContext.openAndInitializeSession(address, agent);
242 // LoginModel lm = loginModel.get();
243 // if (lm != null && lm.isRemember() && !lm.isLoadedFromStore()) {
244 // // Only remember credentials if they were actually given and the login was successful.
245 // AuthenticationUtils.storeAuthenticationInfo(serverInfo.get(), lm);
248 // } catch (IOException e) {
249 // lastFailed = true;
251 // Throwable cause = e.getCause();
252 // if(cause instanceof InvalidAuthenticationException) {
254 // } else if(cause instanceof InvalidUserException) {
259 // } catch (DatabaseException e) {
260 // e.printStackTrace();
265 // // [Tuukka] Removed to remove the last reference to UndoCoreManager.
266 // //ctx.setServerActivation(UndoCoreManager.getManager().peekActivation(address));
268 // if (registerServices) {
269 // if (monitor != null)
270 // monitor.subTask("Registering session services");
271 // safeRegisterServices(ctx);
273 // ISessionContext newCtx = ctx;
278 // private static void safeRegisterServices(final SessionContext ctx) {
279 // SafeRunner.run(new ISafeRunnable() {
281 // public void handleException(Throwable exception) {
282 // Activator.getDefault().getLog().log(
283 // new Status(IStatus.ERROR, Activator.PLUGIN_ID,
284 // "Database session service registration produced unexpected error", exception));
287 // public void run() {
288 // ctx.registerServices();
294 // * Replace the existing ISessionContext in SimanticsUI with the specified
295 // * <code>newContext</code> and release the previous session with the
296 // * default hold time.
298 // * @param newContext the new UI database context
300 // static synchronized void replaceUISession(ISessionContext newContext) {
301 // ISessionContext oldContext = SimanticsUI.setSessionContext(newContext);
302 // if (oldContext != null) {
303 // releaseUnusedSessionAfterHoldTime(oldContext);