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