]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.management/src/org/simantics/db/management/internal/SessionManagerProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.management / src / org / simantics / db / management / internal / SessionManagerProvider.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.db.management.internal;\r
13 \r
14 import java.io.IOException;\r
15 import java.io.InputStream;\r
16 import java.net.URL;\r
17 import java.util.Properties;\r
18 \r
19 import org.eclipse.core.runtime.Platform;\r
20 import org.osgi.framework.Bundle;\r
21 import org.simantics.db.SessionManager;\r
22 import org.simantics.utils.FileUtils;\r
23 \r
24 import fi.vtt.simantics.procore.SessionManagerSource;\r
25 \r
26 /**\r
27  * Complete hack for the time being. Simply provides the SessionManager behind\r
28  * procore's SessionManagerSource with proper initialization.\r
29  */\r
30 public final class SessionManagerProvider {\r
31 \r
32     // TODO: move this into BundleContext as a service ?\r
33     private static SessionManagerProvider provider;\r
34 \r
35     private SessionManager sessionManager;\r
36 \r
37     public static SessionManagerProvider getInstance() {\r
38         if (provider == null)\r
39             provider = new SessionManagerProvider();\r
40         return provider;\r
41     }\r
42 \r
43     public SessionManager getSessionManager() throws IOException {\r
44         if (sessionManager == null) {\r
45             sessionManager = SessionManagerSource.getSessionManager(loadProperties());\r
46         }\r
47         return sessionManager;\r
48     }\r
49 \r
50     private Properties loadProperties() {\r
51         Bundle procore = Platform.getBundle("org.simantics.db.procore");\r
52         URL url = procore.getResource("log4j.properties");\r
53         if (url != null) {\r
54             InputStream in = null;\r
55             try {\r
56                 in = url.openStream();\r
57                 Properties props = new Properties();\r
58                 props.load(in);\r
59                 return props;\r
60             } catch (Exception e) {\r
61             } finally {\r
62                 FileUtils.uncheckedClose(in);\r
63             }\r
64         }\r
65         return null;\r
66     }\r
67 \r
68 }\r