X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db%2Fsrc%2Forg%2Fsimantics%2Fdb%2FManager.java;fp=bundles%2Forg.simantics.db%2Fsrc%2Forg%2Fsimantics%2Fdb%2FManager.java;h=69b75e33759f43eb99a736e79ab85c36b5411b54;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db/src/org/simantics/db/Manager.java b/bundles/org.simantics.db/src/org/simantics/db/Manager.java new file mode 100644 index 000000000..69b75e337 --- /dev/null +++ b/bundles/org.simantics.db/src/org/simantics/db/Manager.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.simantics.db.internal.Activator; + +/** + * @author Antti Villberg + */ +public final class Manager { + + private Manager() {} + + /** + * Search for registered database driver by driver id. + * + * @param id for database driver. + * @return database driver or null. + */ + public static Driver getDriver(String id) { + Map drivers = collectDrivers(); + return drivers.get(id); + } + + private static Map collectDrivers() { + BundleContext context = Activator.getContext(); + Collection> serviceReferences; + try { + serviceReferences = context.getServiceReferences(Driver.class, null); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + serviceReferences = Collections.emptyList(); + } + Map drivers = new HashMap<>(serviceReferences.size()); + for (ServiceReference reference : serviceReferences) { + Driver driver = context.getService(reference); + String driverName = driver.getName(); + if (driverName != null && !driverName.isEmpty()) { + drivers.put(driverName, driver); + } + } + return drivers; + } + + public static DatabaseUserAgent getUserAgent(String id) { + Map agents = collectUserAgents(); + return agents.get(id); + } + + private static Map collectUserAgents() { + BundleContext context = Activator.getContext(); + Collection> serviceReferences; + try { + serviceReferences = context.getServiceReferences(DatabaseUserAgent.class, null); + } catch (InvalidSyntaxException e) { + e.printStackTrace(); + serviceReferences = Collections.emptyList(); + } + Map drivers = new HashMap<>(serviceReferences.size()); + for (ServiceReference reference : serviceReferences) { + DatabaseUserAgent dbua = context.getService(reference); + String driverName = dbua.getId(); + if (driverName != null && !driverName.isEmpty()) { + drivers.put(driverName, dbua); + } + } + return drivers; + } + +}