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