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