]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/Manager.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / Manager.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.db;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.InvalidSyntaxException;
21 import org.osgi.framework.ServiceReference;
22 import org.simantics.db.internal.Activator;
23
24 /**
25  * @author Antti Villberg
26  */
27 public final class Manager {
28
29     private Manager() {}
30
31     /**
32      * Search for registered database driver by driver id.
33      *
34      * @param id for database driver.
35      * @return database driver or null.
36      */
37     public static Driver getDriver(String id) {
38         Map<String, Driver> drivers = collectDrivers();
39         return drivers.get(id);
40     }
41
42     private static Map<String, Driver> collectDrivers() {
43         BundleContext context = Activator.getContext();
44         Collection<ServiceReference<Driver>> serviceReferences;
45         try {
46             serviceReferences = context.getServiceReferences(Driver.class, null);
47         } catch (InvalidSyntaxException e) {
48             e.printStackTrace();
49             serviceReferences = Collections.emptyList();
50         }
51         Map<String, Driver> drivers = new HashMap<>(serviceReferences.size());
52         for (ServiceReference<Driver> reference : serviceReferences) {
53             Driver driver = context.getService(reference);
54             String driverName = driver.getName();
55             if (driverName != null && !driverName.isEmpty()) {
56                 drivers.put(driverName, driver);
57             }
58         }
59         return drivers;
60     }
61     
62     public static DatabaseUserAgent getUserAgent(String id) {
63         Map<String, DatabaseUserAgent> agents = collectUserAgents();
64         return agents.get(id);
65     }
66     
67     private static Map<String, DatabaseUserAgent> collectUserAgents() {
68         BundleContext context = Activator.getContext();
69         Collection<ServiceReference<DatabaseUserAgent>> serviceReferences;
70         try {
71             serviceReferences = context.getServiceReferences(DatabaseUserAgent.class, null);
72         } catch (InvalidSyntaxException e) {
73             e.printStackTrace();
74             serviceReferences = Collections.emptyList();
75         }
76         Map<String, DatabaseUserAgent> drivers = new HashMap<>(serviceReferences.size());
77         for (ServiceReference<DatabaseUserAgent> reference : serviceReferences) {
78             DatabaseUserAgent dbua = context.getService(reference);
79             String driverName = dbua.getId();
80             if (driverName != null && !driverName.isEmpty()) {
81                 drivers.put(driverName, dbua);
82             }
83         }
84         return drivers;
85     }
86
87 }