當前位置: 首頁>>代碼示例>>Java>>正文


Java ServiceTracker.waitForService方法代碼示例

本文整理匯總了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");
    }
}
 
開發者ID:VisuFlow,項目名稱:visuflow-plugin,代碼行數:31,代碼來源:ServiceUtil.java

示例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();
    }
  }
}
 
開發者ID:sonatype,項目名稱:nexus-public,代碼行數:27,代碼來源:BootstrapListener.java

示例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();
}
 
開發者ID:nateyolles,項目名稱:publick-sling-blog,代碼行數:23,代碼來源:OsgiConfigurationServiceImpl.java

示例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();
	}
}
 
開發者ID:eclipse,項目名稱:gemini.blueprint,代碼行數:34,代碼來源:DeadlockHandlingTest.java

示例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();
		}
	}
}
 
開發者ID:csierra,項目名稱:osgi-jaxrs-extracted,代碼行數:33,代碼來源:JaxrsTest.java

示例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;
}
 
開發者ID:fipro78,項目名稱:osgi-ds-getting-started-pde,代碼行數:16,代碼來源:IntegrationTest.java

示例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();
    }
}
 
開發者ID:Jahia,項目名稱:jahia-loganalyzer,代碼行數:10,代碼來源:BaseTest.java

示例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();
}
 
開發者ID:liferay-labs,項目名稱:arquillian-liferay,代碼行數:29,代碼來源:LiferayWaitForServiceBuilderObserver.java

示例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();
    }
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:15,代碼來源:CamelKarafTestSupport.java

示例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) {}
}
 
開發者ID:Neulinet,項目名稱:Zoo,代碼行數:14,代碼來源:CoreActivator.java

示例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();
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:10,代碼來源:ArtemisFeatureTest.java

示例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;
    }
}
 
開發者ID:arievanwi,項目名稱:osgi.ee,代碼行數:47,代碼來源:PersistenceUnitInfoImpl.java

示例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();
		}
	}
}
 
開發者ID:liferay-labs,項目名稱:arquillian-liferay,代碼行數:51,代碼來源:LiferayTestEnricher.java


注:本文中的org.osgi.util.tracker.ServiceTracker.waitForService方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。