--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.internal;\r
+\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.internal.TimedSessionCache;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.cache.SoftTimedCache;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @since 2007-08-28\r
+ * @deprecated do not use, no direct replacement\r
+ */\r
+@Deprecated\r
+public final class SessionUtils {\r
+\r
+ /**\r
+ * Puts the specified session on the list of to-be-closed sessions. If no\r
+ * one needs the session after 10 seconds, it will be closed automatically.\r
+ * It will also be closed automatically if there is memory pressure to do\r
+ * so.\r
+ * \r
+ * @param ctx\r
+ * @see TimedSessionCache\r
+ */\r
+ public static void releaseUnusedSessionAfterHoldTime(ISessionContext ctx) {\r
+ releaseUnusedSessionAfterHoldTime(ctx, 10000);\r
+ }\r
+\r
+ /**\r
+ * Puts the specified session on the list of to-be-closed sessions. If no\r
+ * one needs the session after the specified amount of time, it will be\r
+ * closed automatically. It will also be closed automatically if there is\r
+ * memory pressure to do so.\r
+ * \r
+ * @param ctx\r
+ * @param holdTimeMs hold time for released session in milliseconds\r
+ * @see TimedSessionCache\r
+ */\r
+ public static void releaseUnusedSessionAfterHoldTime(ISessionContext ctx, long holdTime) {\r
+ if (ctx == null)\r
+ throw new IllegalArgumentException("null session context");\r
+\r
+ // See if the session is used by the UI.\r
+ // If it is, don't put the session on the release queue.\r
+ if (SimanticsUI.isInUse(ctx))\r
+ return;\r
+\r
+ synchronized (SessionUtils.class) {\r
+ // Ensure that all previously cached sessions are scheduled for disposal.\r
+ TimedSessionCache cache = TimedSessionCache.getCache();\r
+ for (SoftTimedCache<Object, ISessionContext>.CacheEntry e : cache.getEntries()) {\r
+ e.schedule(holdTime, TimeUnit.MILLISECONDS);\r
+ }\r
+\r
+ // Cache this session for an infinite amount of time.\r
+ TimedSessionCache.getCache().put(ctx.getSession(), ctx, 5, TimeUnit.MINUTES);\r
+ }\r
+ }\r
+\r
+// private static ISessionContext releaseFromCache(Object address) {\r
+// synchronized (SessionUtils.class) {\r
+// ISessionContext cachedCtx = TimedSessionCache.getCache().release(address);\r
+// return cachedCtx;\r
+// }\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param server\r
+// * @return\r
+// * @throws Layer0MissingException\r
+// * @throws IOException\r
+// */\r
+// static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, UserAuthenticator auth)\r
+// throws DatabaseException, IOException\r
+// {\r
+// return getSessionContext(monitor, server, auth, true);\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param server\r
+// * @return\r
+// * @throws Layer0MissingException\r
+// * @throws IOException\r
+// */\r
+// static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server)\r
+// throws DatabaseException, IOException\r
+// {\r
+// return getSessionContext(monitor, server, null, true);\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param server\r
+// * @param registerServices\r
+// * @return\r
+// * @throws Layer0MissingException\r
+// * @throws IOException\r
+// */\r
+// static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, UserAuthenticator auth, boolean registerServices)\r
+// throws DatabaseException, IOException\r
+// {\r
+// return getSessionContext(monitor, server.getName(), auth, registerServices);\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param server\r
+// * @param registerServices\r
+// * @return\r
+// * @throws Layer0MissingException\r
+// * @throws IOException\r
+// */\r
+// static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, boolean registerServices)\r
+// throws DatabaseException, IOException\r
+// {\r
+// return getSessionContext(monitor, server.getAddress(), null, registerServices);\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param server\r
+// * @param registerServices\r
+// * @return\r
+// * @throws Layer0MissingException\r
+// * @throws IOException\r
+// */\r
+// static ISessionContext getSessionContext(IProgressMonitor monitor, ServerAddress address, boolean registerServices)\r
+// throws DatabaseException, IOException\r
+// {\r
+// return getSessionContext(monitor, address, null, registerServices);\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param info\r
+// * @param registerServices\r
+// * @return a database session context\r
+// * @throws Layer0MissingException if the layer0 ontology in the database is\r
+// * not compatible with this program or it does not exist\r
+// * @throws IOException\r
+// */\r
+// static ISessionContext getSessionContext(IProgressMonitor monitor, final ServerAddress address, UserAuthenticator auth, boolean registerServices)\r
+// throws DatabaseException, IOException\r
+// {\r
+// return getSessionContext(monitor, address, auth, registerServices, false);\r
+// }\r
+//\r
+// /**\r
+// * @param monitor\r
+// * @param address\r
+// * @param auth\r
+// * @param registerServices\r
+// * @param forceReload\r
+// * @return a database session context\r
+// * @throws Layer0MissingException if the layer0 ontology in the database is\r
+// * not compatible with this program or it does not exist\r
+// * @throws IOException\r
+// */\r
+// static synchronized ISessionContext getSessionContext(IProgressMonitor monitor, final ServerAddress address, UserAuthenticator auth, boolean registerServices, boolean forceReload)\r
+// throws DatabaseException, IOException\r
+// {\r
+// if(!forceReload) {\r
+// ISessionContext newCtx = SimanticsUI.getSessionContext();\r
+// if (newCtx != null && address.equals(newCtx.getAddress()))\r
+// return newCtx;\r
+//\r
+// // for (ISessionContextProvider provider : SimanticsUI.getProviderSource().getAll()) {\r
+// // ISessionContext ctx = provider.getSessionContext();\r
+// // if (ctx != null && address.equals(ctx.getAddress()))\r
+// // return ctx;s\r
+// // }\r
+//\r
+// ISessionContext cachedCtx = releaseFromCache(address);\r
+// if (cachedCtx != null) {\r
+// if (registerServices && cachedCtx instanceof SessionContext) {\r
+// safeRegisterServices((SessionContext) cachedCtx);\r
+// }\r
+// return cachedCtx;\r
+// }\r
+// }\r
+//\r
+// if (monitor != null)\r
+// monitor.subTask("Connecting to database at " + address);\r
+//\r
+// boolean success = false;\r
+// boolean lastFailed = false;\r
+//\r
+// SessionContext ctx = null;\r
+// UserAuthenticationAgent agent = null;\r
+// if (auth != null)\r
+// agent = UserAuthenticationAgents.staticAgent(auth);\r
+//\r
+// while (!success) {\r
+// final DataContainer<LoginModel> loginModel = new DataContainer<LoginModel>();\r
+// final DataContainer<ServerInfo> serverInfo = new DataContainer<ServerInfo>();\r
+//\r
+// if (agent == null) {\r
+// final boolean forceQuery = lastFailed;\r
+// agent = new UserAuthenticationAgent() {\r
+// @Override\r
+// public UserAuthenticator getAuthenticator(final ServerInformation information) throws IOException {\r
+// final DataContainer<UserAuthenticator> authenticator = new DataContainer<UserAuthenticator>();\r
+// serverInfo.set(new ServerInfo("", address, information));\r
+//\r
+// ThreadUtils.syncExec(SWTThread.getThreadAccess(Display.getDefault()), new Runnable() {\r
+// @Override\r
+// public void run() {\r
+// LoginModel model = AuthenticationUtils.queryAuthenticationInfo(Display.getDefault().getActiveShell(), serverInfo.get(), forceQuery);\r
+// if (model != null) {\r
+// loginModel.set(model);\r
+// authenticator.set(UserAuthenticators.byNameAndPassword(model.getName(), model.getPassword()));\r
+// }\r
+// }\r
+// });\r
+//\r
+// if (authenticator.get() == null)\r
+// throw new LoginCancelledException("User authentication was not provided.");\r
+// return authenticator.get();\r
+// }\r
+// };\r
+// }\r
+//\r
+// try {\r
+// ctx = SessionContext.openAndInitializeSession(address, agent);\r
+// success = true;\r
+//\r
+// LoginModel lm = loginModel.get();\r
+// if (lm != null && lm.isRemember() && !lm.isLoadedFromStore()) {\r
+// // Only remember credentials if they were actually given and the login was successful.\r
+// AuthenticationUtils.storeAuthenticationInfo(serverInfo.get(), lm);\r
+// }\r
+//\r
+// } catch (IOException e) {\r
+// lastFailed = true;\r
+//\r
+// Throwable cause = e.getCause();\r
+// if(cause instanceof InvalidAuthenticationException) {\r
+// agent = null;\r
+// } else if(cause instanceof InvalidUserException) {\r
+// agent = null;\r
+// } else {\r
+// throw e;\r
+// }\r
+// } catch (DatabaseException e) {\r
+// e.printStackTrace();\r
+// throw e;\r
+// }\r
+// }\r
+//\r
+// // [Tuukka] Removed to remove the last reference to UndoCoreManager.\r
+// //ctx.setServerActivation(UndoCoreManager.getManager().peekActivation(address));\r
+//\r
+// if (registerServices) {\r
+// if (monitor != null)\r
+// monitor.subTask("Registering session services");\r
+// safeRegisterServices(ctx);\r
+// }\r
+// ISessionContext newCtx = ctx;\r
+//\r
+// return newCtx;\r
+// }\r
+//\r
+// private static void safeRegisterServices(final SessionContext ctx) {\r
+// SafeRunner.run(new ISafeRunnable() {\r
+// @Override\r
+// public void handleException(Throwable exception) {\r
+// Activator.getDefault().getLog().log(\r
+// new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+// "Database session service registration produced unexpected error", exception));\r
+// }\r
+// @Override\r
+// public void run() {\r
+// ctx.registerServices();\r
+// }\r
+// });\r
+// }\r
+//\r
+// /**\r
+// * Replace the existing ISessionContext in SimanticsUI with the specified\r
+// * <code>newContext</code> and release the previous session with the\r
+// * default hold time.\r
+// * \r
+// * @param newContext the new UI database context\r
+// */\r
+// static synchronized void replaceUISession(ISessionContext newContext) {\r
+// ISessionContext oldContext = SimanticsUI.setSessionContext(newContext);\r
+// if (oldContext != null) {\r
+// releaseUnusedSessionAfterHoldTime(oldContext);\r
+// }\r
+// }\r
+\r
+}\r