1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
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;
25 * @author Antti Villberg
27 public final class Manager {
32 * Search for registered database driver by driver id.
34 * @param id for database driver.
35 * @return database driver or null.
37 public static Driver getDriver(String id) {
38 Map<String, Driver> drivers = collectDrivers();
39 return drivers.get(id);
42 private static Map<String, Driver> collectDrivers() {
43 BundleContext context = Activator.getContext();
44 Collection<ServiceReference<Driver>> serviceReferences;
46 serviceReferences = context.getServiceReferences(Driver.class, null);
47 } catch (InvalidSyntaxException e) {
49 serviceReferences = Collections.emptyList();
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);
62 public static DatabaseUserAgent getUserAgent(String id) {
63 Map<String, DatabaseUserAgent> agents = collectUserAgents();
64 return agents.get(id);
67 private static Map<String, DatabaseUserAgent> collectUserAgents() {
68 BundleContext context = Activator.getContext();
69 Collection<ServiceReference<DatabaseUserAgent>> serviceReferences;
71 serviceReferences = context.getServiceReferences(DatabaseUserAgent.class, null);
72 } catch (InvalidSyntaxException e) {
74 serviceReferences = Collections.emptyList();
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);