]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/internal/SessionUtils.java
86ef494522a6c8fc406cb7e1ccea344659cc3e14
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / internal / SessionUtils.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.ui.internal;
13
14 import java.util.concurrent.TimeUnit;
15
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;
20
21 /**
22  * @author Tuukka Lehtonen
23  * @since 2007-08-28
24  * @deprecated do not use, no direct replacement
25  */
26 @Deprecated
27 public final class SessionUtils {
28
29     /**
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
33      * so.
34      * 
35      * @param ctx
36      * @see TimedSessionCache
37      */
38     public static void releaseUnusedSessionAfterHoldTime(ISessionContext ctx) {
39         releaseUnusedSessionAfterHoldTime(ctx, 10000);
40     }
41
42     /**
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.
47      * 
48      * @param ctx
49      * @param holdTimeMs hold time for released session in milliseconds
50      * @see TimedSessionCache
51      */
52     public static void releaseUnusedSessionAfterHoldTime(ISessionContext ctx, long holdTime) {
53         if (ctx == null)
54             throw new IllegalArgumentException("null session context");
55
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))
59             return;
60
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);
66             }
67
68             // Cache this session for an infinite amount of time.
69             TimedSessionCache.getCache().put(ctx.getSession(), ctx, 5, TimeUnit.MINUTES);
70         }
71     }
72
73 //    private static ISessionContext releaseFromCache(Object address) {
74 //        synchronized (SessionUtils.class) {
75 //            ISessionContext cachedCtx = TimedSessionCache.getCache().release(address);
76 //            return cachedCtx;
77 //        }
78 //    }
79 //
80 //    /**
81 //     * @param monitor
82 //     * @param server
83 //     * @return
84 //     * @throws Layer0MissingException
85 //     * @throws IOException
86 //     */
87 //    static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, UserAuthenticator auth)
88 //    throws DatabaseException, IOException
89 //    {
90 //        return getSessionContext(monitor, server, auth, true);
91 //    }
92 //
93 //    /**
94 //     * @param monitor
95 //     * @param server
96 //     * @return
97 //     * @throws Layer0MissingException
98 //     * @throws IOException
99 //     */
100 //    static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server)
101 //    throws DatabaseException, IOException
102 //    {
103 //        return getSessionContext(monitor, server, null, true);
104 //    }
105 //
106 //    /**
107 //     * @param monitor
108 //     * @param server
109 //     * @param registerServices
110 //     * @return
111 //     * @throws Layer0MissingException
112 //     * @throws IOException
113 //     */
114 //    static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, UserAuthenticator auth, boolean registerServices)
115 //    throws DatabaseException, IOException
116 //    {
117 //        return getSessionContext(monitor, server.getName(), auth, registerServices);
118 //    }
119 //
120 //    /**
121 //     * @param monitor
122 //     * @param server
123 //     * @param registerServices
124 //     * @return
125 //     * @throws Layer0MissingException
126 //     * @throws IOException
127 //     */
128 //    static ISessionContext getSessionContext(IProgressMonitor monitor, ServerInfo server, boolean registerServices)
129 //    throws DatabaseException, IOException
130 //    {
131 //        return getSessionContext(monitor, server.getAddress(), null, registerServices);
132 //    }
133 //
134 //    /**
135 //     * @param monitor
136 //     * @param server
137 //     * @param registerServices
138 //     * @return
139 //     * @throws Layer0MissingException
140 //     * @throws IOException
141 //     */
142 //    static ISessionContext getSessionContext(IProgressMonitor monitor, ServerAddress address, boolean registerServices)
143 //    throws DatabaseException, IOException
144 //    {
145 //        return getSessionContext(monitor, address, null, registerServices);
146 //    }
147 //
148 //    /**
149 //     * @param monitor
150 //     * @param info
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
156 //     */
157 //    static ISessionContext getSessionContext(IProgressMonitor monitor, final ServerAddress address, UserAuthenticator auth, boolean registerServices)
158 //    throws DatabaseException, IOException
159 //    {
160 //        return getSessionContext(monitor, address, auth, registerServices, false);
161 //    }
162 //
163 //    /**
164 //     * @param monitor
165 //     * @param address
166 //     * @param auth
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
173 //     */
174 //    static synchronized ISessionContext getSessionContext(IProgressMonitor monitor, final ServerAddress address, UserAuthenticator auth, boolean registerServices, boolean forceReload)
175 //    throws DatabaseException, IOException
176 //    {
177 //        if(!forceReload) {
178 //            ISessionContext newCtx = SimanticsUI.getSessionContext();
179 //            if (newCtx != null && address.equals(newCtx.getAddress()))
180 //                return newCtx;
181 //
182 //            //        for (ISessionContextProvider provider : SimanticsUI.getProviderSource().getAll()) {
183 //            //            ISessionContext ctx = provider.getSessionContext();
184 //            //            if (ctx != null && address.equals(ctx.getAddress()))
185 //            //                return ctx;s
186 //            //        }
187 //
188 //            ISessionContext cachedCtx = releaseFromCache(address);
189 //            if (cachedCtx != null) {
190 //                if (registerServices && cachedCtx instanceof SessionContext) {
191 //                    safeRegisterServices((SessionContext) cachedCtx);
192 //                }
193 //                return cachedCtx;
194 //            }
195 //        }
196 //
197 //        if (monitor != null)
198 //            monitor.subTask("Connecting to database at " + address);
199 //
200 //        boolean success = false;
201 //        boolean lastFailed = false;
202 //
203 //        SessionContext ctx = null;
204 //        UserAuthenticationAgent agent = null;
205 //        if (auth != null)
206 //            agent = UserAuthenticationAgents.staticAgent(auth);
207 //
208 //        while (!success) {
209 //            final DataContainer<LoginModel> loginModel = new DataContainer<LoginModel>();
210 //            final DataContainer<ServerInfo> serverInfo = new DataContainer<ServerInfo>();
211 //
212 //            if (agent == null) {
213 //                final boolean forceQuery = lastFailed;
214 //                agent = new UserAuthenticationAgent() {
215 //                    @Override
216 //                    public UserAuthenticator getAuthenticator(final ServerInformation information) throws IOException {
217 //                        final DataContainer<UserAuthenticator> authenticator = new DataContainer<UserAuthenticator>();
218 //                        serverInfo.set(new ServerInfo("", address, information));
219 //
220 //                        ThreadUtils.syncExec(SWTThread.getThreadAccess(Display.getDefault()), new Runnable() {
221 //                            @Override
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()));
227 //                                }
228 //                            }
229 //                        });
230 //
231 //                        if (authenticator.get() == null)
232 //                            throw new LoginCancelledException("User authentication was not provided.");
233 //                        return authenticator.get();
234 //                    }
235 //                };
236 //            }
237 //
238 //            try {
239 //                ctx = SessionContext.openAndInitializeSession(address, agent);
240 //                success = true;
241 //
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);
246 //                }
247 //
248 //            } catch (IOException e) {
249 //                lastFailed = true;
250 //
251 //                Throwable cause = e.getCause();
252 //                if(cause instanceof InvalidAuthenticationException) {
253 //                    agent = null;
254 //                } else if(cause instanceof InvalidUserException) {
255 //                    agent = null;
256 //                } else {
257 //                    throw e;
258 //                }
259 //            } catch (DatabaseException e) {
260 //                e.printStackTrace();
261 //                throw e;
262 //            }
263 //        }
264 //
265 //        // [Tuukka] Removed to remove the last reference to UndoCoreManager.
266 //        //ctx.setServerActivation(UndoCoreManager.getManager().peekActivation(address));
267 //
268 //        if (registerServices) {
269 //            if (monitor != null)
270 //                monitor.subTask("Registering session services");
271 //            safeRegisterServices(ctx);
272 //        }
273 //        ISessionContext newCtx = ctx;
274 //
275 //        return newCtx;
276 //    }
277 //
278 //    private static void safeRegisterServices(final SessionContext ctx) {
279 //        SafeRunner.run(new ISafeRunnable() {
280 //            @Override
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));
285 //            }
286 //            @Override
287 //            public void run() {
288 //                ctx.registerServices();
289 //            }
290 //        });
291 //    }
292 //
293 //    /**
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.
297 //     * 
298 //     * @param newContext the new UI database context
299 //     */
300 //    static synchronized void replaceUISession(ISessionContext newContext) {
301 //        ISessionContext oldContext = SimanticsUI.setSessionContext(newContext);
302 //        if (oldContext != null) {
303 //            releaseUnusedSessionAfterHoldTime(oldContext);
304 //        }
305 //    }
306
307 }