blob: 6a9a3416a68607963c40c3276ce0244328f2372b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* 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:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.framework.database.internal;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.data.IDatabaseInfo;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeDatabaseConnectionInfoNotFoundException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.database.core.IDbConnectionInformationContributor;
import org.eclipse.osee.framework.jdk.core.util.OseeProperties;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
/**
* @author Andrew M. Finkbeiner
*/
public class DatabaseInfoProvider {
private IDatabaseInfo selectedDbInfo;
private final BundleContext context;
public DatabaseInfoProvider(BundleContext context) {
this.context = context;
}
public IDatabaseInfo getDatabaseInfo(String serviceId) throws OseeCoreException {
Conditions.checkNotNull(serviceId, "Service Id to find");
return findDatabaseInfo(serviceId);
}
public IDatabaseInfo getSelectedDatabaseInfo() throws OseeDataStoreException {
if (selectedDbInfo == null) {
String dbConnectionId = OseeProperties.getOseeDbConnectionId();
if (Strings.isValid(dbConnectionId)) {
selectedDbInfo = findDatabaseInfo(dbConnectionId);
} else {
throw new IllegalStateException("No DB connection information provided");
}
}
return selectedDbInfo;
}
private IDatabaseInfo findDatabaseInfo(String serverIdToFind) throws OseeDataStoreException {
Set<String> infoKeys = new HashSet<String>();
ServiceTracker tracker = new ServiceTracker(context, IDbConnectionInformationContributor.class.getName(), null);
tracker.open(true);
try {
Object[] services = tracker.getServices();
if (services != null) {
for (Object object : services) {
if (object instanceof IDbConnectionInformationContributor) {
IDbConnectionInformationContributor contributor = (IDbConnectionInformationContributor) object;
try {
for (IDatabaseInfo databaseInfo : contributor.getDbInformation()) {
String key = databaseInfo.getId();
infoKeys.add(key);
if (serverIdToFind.equals(key)) {
return databaseInfo;
}
}
} catch (Exception ex) {
OseeLog.log(Activator.class, Level.SEVERE, ex);
}
}
}
}
} finally {
tracker.close();
}
throw new OseeDatabaseConnectionInfoNotFoundException(String.format(
"DB connection information was not found for: [%s]\n Available connection ids are: [%s]\n", serverIdToFind,
infoKeys));
}
}
|