本文整理汇总了Java中org.osgi.util.tracker.ServiceTracker.waitForService方法的典型用法代码示例。如果您正苦于以下问题:Java ServiceTracker.waitForService方法的具体用法?Java ServiceTracker.waitForService怎么用?Java ServiceTracker.waitForService使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.osgi.util.tracker.ServiceTracker
的用法示例。
在下文中一共展示了ServiceTracker.waitForService方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getService
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
/**
* Looks up an OSGi service. If the service is not yet available, this method will wait for 60 seconds for the service to become available. If the service
* does not appear in this period, a ServiceException is thrown.
*
* @param serviceClass
* The service interface of the service to look up
* @param timeoutInMillis
* The amount of time in milliseconds to wait for the service to become available
* @return an implementation of the given service interface
* @throws a
* ServiceException, if the service couldn't be found in the OSGi service registry
*/
public static <T> T getService(Class<T> serviceClass, long timeoutInMillis) {
BundleContext ctx = FrameworkUtil.getBundle(ServiceUtil.class).getBundleContext();
ServiceTracker<T, T> tracker = new ServiceTracker<>(ctx, serviceClass, null);
tracker.open();
T service = null;
try {
service = tracker.waitForService(timeoutInMillis);
} catch (InterruptedException e) {
throw new ServiceException("Interrupted while waiting for the service " + serviceClass.getName(), e);
}
tracker.close();
if (service != null) {
return service;
} else {
throw new ServiceException("Service " + serviceClass.getName() + " not available");
}
}
示例2: installNexusEdition
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
private static void installNexusEdition(final BundleContext ctx, @Nullable final String editionName)
throws Exception
{
if (editionName != null && editionName.length() > 0) {
final ServiceTracker<?, FeaturesService> tracker = new ServiceTracker<>(ctx, FeaturesService.class, null);
tracker.open();
try {
FeaturesService featuresService = tracker.waitForService(1000);
Feature editionFeature = featuresService.getFeature(editionName);
log.info("Installing: {}", editionFeature);
// edition might already be installed in the cache; if so then skip installation
if (!featuresService.isInstalled(editionFeature)) {
// avoid auto-refreshing bundles as that could trigger unwanted restart/lifecycle events
EnumSet<Option> options = EnumSet.of(NoAutoRefreshBundles, NoAutoRefreshManagedBundles);
featuresService.installFeature(editionFeature.getId(), options);
}
log.info("Installed: {}", editionFeature);
}
finally {
tracker.close();
}
}
}
示例3: waitForService
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
/**
* Wait for an OSGi service to become active.
*
* @param serviceImpl The service implementation class
* @param timeout The length of time to wait for the service
*/
private void waitForService(Class serviceImpl, long timeout) {
Class serviceInterface = serviceImpl.getInterfaces()[0];
BundleContext bundleContext = FrameworkUtil.getBundle(serviceInterface).getBundleContext();
ServiceReference factoryRef = bundleContext.getServiceReference(serviceInterface.getName());
ServiceTracker serviceTracker = new ServiceTracker(bundleContext, factoryRef, null);
serviceTracker.open();
try {
serviceTracker.waitForService(timeout);
} catch (InterruptedException e) {
LOGGER.error("Could not get service", e);
}
serviceTracker.close();
}
示例4: testErrorHandling
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
/**
* While it may appear that this test is doing nothing, what it is doing is
* testing what happens when the OSGi framework is shutdown while the
* Spring/OSGi extender is deadlocked. If all goes well, the test will
* gracefully end. If not, it will hang for quite a while.
*/
public void testErrorHandling() throws Exception {
Resource errorResource = getLocator().locateArtifact("org.eclipse.gemini.blueprint.iandt", "deadlock",
getSpringDMVersion());
assertNotNull("bundle resource exists", errorResource);
Bundle errorBundle = bundleContext.installBundle(errorResource.getURL().toExternalForm());
assertNotNull("bundle exists", errorBundle);
errorBundle.start();
StringBuilder filter = new StringBuilder();
filter.append("(&");
filter.append("(").append(Constants.OBJECTCLASS).append("=").append(
AbstractRefreshableApplicationContext.class.getName()).append(")");
filter.append("(").append(ConfigurableOsgiBundleApplicationContext.APPLICATION_CONTEXT_SERVICE_PROPERTY_NAME);
filter.append("=").append("org.eclipse.gemini.blueprint.iandt.deadlock").append(")");
filter.append(")");
ServiceTracker tracker = new ServiceTracker(bundleContext, bundleContext.createFilter(filter.toString()), null);
try {
tracker.open();
AbstractRefreshableApplicationContext appContext = (AbstractRefreshableApplicationContext) tracker.waitForService(3000);
assertNull("Deadlock context should not be published", appContext);
}
finally {
tracker.close();
}
}
示例5: testEndPoint
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
public void testEndPoint() throws Exception {
ServiceRegistration<?> serviceRegistration = null;
try {
TestAddon testAddon = new TestAddon();
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("osgi.jaxrs.resource.base", "/test-addon");
serviceRegistration = bundleContext.registerService(
Object.class, testAddon, properties);
// TODO this availability should be checked through a jaxrs runtime service
Filter filter = bundleContext.createFilter("(CXF_ENDPOINT_ADDRESS=/test-addon)");
ServiceTracker<?, ?> st = new ServiceTracker<>(bundleContext, filter, null);
st.open();
if (st.waitForService(5000) == null) {
fail();
}
// TODO add http client to connect to the endpoint
}
finally {
if (serviceRegistration != null) {
serviceRegistration.unregister();
}
}
}
示例6: getService
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
static <T> T getService(Class<T> clazz) {
Bundle bundle = FrameworkUtil.getBundle(IntegrationTest.class);
if (bundle != null) {
ServiceTracker<T, T> st = new ServiceTracker<T, T>(bundle.getBundleContext(), clazz, null);
st.open();
if (st != null) {
try {
return st.waitForService(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
示例7: waitForService
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
protected void waitForService(String filter, long timeout) throws InvalidSyntaxException, InterruptedException {
ServiceTracker<Object, Object> st = new ServiceTracker<Object, Object>(bundleContext, bundleContext.createFilter(filter), null);
try {
st.open();
st.waitForService(timeout);
} finally {
st.close();
}
}
示例8: execute
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
public void execute(@Observes(precedence = Integer.MAX_VALUE)
EventContext<BeforeSuite> event) throws InvalidSyntaxException {
Bundle bundle = FrameworkUtil.getBundle(getClass());
BundleContext bundleContext = bundle.getBundleContext();
Filter filter = FrameworkUtil.createFilter(
"(&(objectClass=org.springframework.context.ApplicationContext)" +
"(org.springframework.context.service.name=" +
bundle.getSymbolicName() + "))");
ServiceTracker<ApplicationContext, ApplicationContext> serviceTracker =
new ServiceTracker<>(bundleContext, filter, null);
serviceTracker.open();
try {
serviceTracker.waitForService(30 * 1000L);
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
serviceTracker.close();
event.proceed();
}
示例9: waitForService
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private void waitForService(String filter, long timeout) throws InvalidSyntaxException,
InterruptedException {
ServiceTracker st = new ServiceTracker(bundleContext,
bundleContext.createFilter(filter),
null);
try {
st.open();
st.waitForService(timeout);
} finally {
st.close();
}
}
示例10: startRuleEngine
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
private void startRuleEngine(BundleContext context) throws InterruptedException {
// TODO: This is a workaround as long as we cannot determine the time when all models have been loaded
Thread.sleep(2000);
// we now request the RuleEngine, so that it is activated and starts processing the rules
// TODO: This should probably better be moved in a new bundle, so that the core bundle does
// not have a (optional) dependency on model.rule.runtime anymore.
try {
ServiceTracker<RuleEngine, RuleEngine> tracker = new ServiceTracker<RuleEngine, RuleEngine>(context, RuleEngine.class, null);
tracker.open();
tracker.waitForService(10000);
} catch(NoClassDefFoundError e) {}
}
示例11: waitForService
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
protected Object waitForService(String filter, long timeout) throws InvalidSyntaxException, InterruptedException {
ServiceTracker<Object, Object> st = new ServiceTracker<>(bundleContext, bundleContext.createFilter(filter), null);
try {
st.open();
return st.waitForService(timeout);
} finally {
st.close();
}
}
示例12: getFromDefinition
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
/**
* Get a datasource from a definition found in the unit definition.
*
* @param def The definition found
* @return A data source that matches the definition, or null if it
* is not a data source definition recognised
*/
private DataSource getFromDefinition(final String def) {
final String SERVICEDEF = "osgi:service/";
if (def == null || !def.startsWith(SERVICEDEF)) {
return null;
}
String remainder = def.substring(SERVICEDEF.length());
int index = remainder.indexOf("/");
String filter;
if (index < 0) {
filter = "(" + Constants.OBJECTCLASS + "=" + remainder + ")";
}
else {
String className = remainder.substring(0, index);
String subfilter = remainder.substring(index + 1);
filter = "(&(" + Constants.OBJECTCLASS + "=" + className + ")" + subfilter + ")";
}
try {
final ServiceTracker<DataSource, DataSource> tracker =
new ServiceTracker<>(unitBundle.getBundleContext(), FrameworkUtil.createFilter(filter), null);
tracker.open();
tracker.waitForService(10000L);
return (DataSource) Proxy.newProxyInstance(PersistenceUnitInfoImpl.this.getClassLoader(),
new Class<?>[] {DataSource.class},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
DataSource datasource = tracker.waitForService(2000L);
if (datasource == null) {
throw new RuntimeException("data source: " + def + " is not known as service");
}
return method.invoke(datasource, args);
}
});
} catch (Exception exc) {
exc.printStackTrace();
return null;
}
}
示例13: resolve
import org.osgi.util.tracker.ServiceTracker; //导入方法依赖的package包/类
private Object resolve(
Class<?> componentClass, String filterString, Class<?> testCaseClass) {
Bundle bundle = getBundle(testCaseClass);
BundleContext bundleContext = bundle.getBundleContext();
ServiceTracker<?, ?> serviceTracker = null;
try {
Filter filter;
if (filterString != null) {
filter = bundleContext.createFilter(
String.format(
"(&(objectClass=%s)%s)", componentClass.getName(),
filterString));
}
else {
filter = bundleContext.createFilter(
String.format(
"(objectClass=%s)", componentClass.getName()));
}
serviceTracker = new ServiceTracker<>(bundleContext, filter, null);
serviceTracker.open();
Object service = serviceTracker.waitForService(5 * 1000L);
if (service == null) {
throw new RuntimeException(
"Timeout waiting for : " + filterString);
}
return service;
}
catch (InvalidSyntaxException ise) {
throw new RuntimeException(
"Bad Syntax for the filter: " + filterString, ise);
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
finally {
if (serviceTracker != null) {
serviceTracker.close();
}
}
}