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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*******************************************************************************
* Copyright (c) 2011 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.orcs.db.mock.internal;
import java.util.Collection;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.junit.Assert;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.ConfigurationAdmin;
/**
* @author Roberto E. Escobar
*/
public final class OsgiUtil {
private OsgiUtil() {
// Utility class
}
public static <T> T getService(Class<T> clazz) throws OseeCoreException {
Bundle bundle = FrameworkUtil.getBundle(OsgiUtil.class);
checkStarted(bundle);
BundleContext context = bundle.getBundleContext();
Assert.assertNotNull(context);
ServiceReference<T> reference = context.getServiceReference(clazz);
Assert.assertNotNull(String.format("Unable to find service [%s]", clazz), reference);
T service = context.getService(reference);
Assert.assertNotNull(String.format("Unable to find service instance for [%s]", clazz), service);
return service;
}
public static <T> T getService(Class<T> clazz, String filter, long waitTimeMillis) throws OseeCoreException {
Bundle bundle = FrameworkUtil.getBundle(OsgiUtil.class);
checkStarted(bundle);
BundleContext context = bundle.getBundleContext();
Assert.assertNotNull(context);
ServiceReference<T> reference = null;
int totalWaitTime = 0;
do {
try {
Collection<ServiceReference<T>> references = context.getServiceReferences(clazz, filter);
if (references != null && !references.isEmpty()) {
reference = references.iterator().next();
}
} catch (Exception ex) {
// do nothing;
} finally {
try {
if (totalWaitTime < waitTimeMillis && reference == null) {
Thread.sleep(100);
totalWaitTime += 100;
}
} catch (InterruptedException ex1) {
// do nothing;
}
}
} while (totalWaitTime < waitTimeMillis && reference == null);
Assert.assertNotNull(String.format("Unable to find service [%s]", clazz), reference);
T service = context.getService(reference);
Assert.assertNotNull(String.format("Unable to find service instance for [%s]", clazz), service);
return service;
}
public static Bundle getBundleByName(String bundleName) {
Bundle bundle = FrameworkUtil.getBundle(OsgiUtil.class);
checkStarted(bundle);
BundleContext context = bundle.getBundleContext();
Bundle toReturn = null;
for (Bundle item : context.getBundles()) {
if (bundleName.equals(item.getSymbolicName())) {
toReturn = item;
break;
}
}
return toReturn;
}
public static ConfigurationAdmin getConfigAdmin() {
Bundle cmBundle = getBundleByName("org.eclipse.equinox.cm");
checkStarted(cmBundle);
try {
return getService(ConfigurationAdmin.class);
} catch (Exception ex) {
throw new OseeCoreException(ex.getCause(), "Error acquiring configuration admin");
}
}
private static void checkStarted(Bundle bundle) {
Assert.assertNotNull("Bundle cannot be null", bundle);
int bundleState = bundle.getState();
if (bundleState != Bundle.STARTING && bundleState != Bundle.ACTIVE) {
try {
bundle.start();
} catch (BundleException ex) {
throw new OseeCoreException(ex.getCause(), "Error starting bundle [%s]", bundle.getSymbolicName());
}
}
}
}
|