/** * Returns a service matching the given criteria. * * @param <T> class implemented or extended by the service * @param bc bundle context for accessing the OSGi registry * @param className name of class implemented or extended by the service * @return matching service (not null) * @throws ServiceLookupException when no matching service has been found after the timeout */ public static <T> T getService( BundleContext bc, String className ) { return ServiceLookup.<T> getService( bc, className, DEFAULT_TIMEOUT, "" ); }
/** * Returns a service matching the given criteria. * * @param <T> class implemented or extended by the service * @param bc bundle context for accessing the OSGi registry * @param type class implemented or extended by the service * @return matching service (not null) * @throws ServiceLookupException when no matching service has been found after the timeout */ public static <T> T getService( BundleContext bc, Class<T> type ) { return getService( bc, type, DEFAULT_TIMEOUT ); }
/** * Returns a service matching the given criteria. * * @param <T> class implemented or extended by the service * @param bc bundle context for accessing the OSGi registry * @param type class implemented or extended by the service * @param props properties to be matched by the service * @return matching service (not null) * @throws ServiceLookupException when no matching service has been found after the timeout */ public static <T> T getService( BundleContext bc, Class<T> type, Map<String, String> props ) { return getService( bc, type, DEFAULT_TIMEOUT, props ); }
/** * Returns a service matching the given criteria. * * @param <T> class implemented or extended by the service * @param bc bundle context for accessing the OSGi registry * @param type class implemented or extended by the service * @param timeout maximum wait period in milliseconds * @return matching service (not null) * @throws ServiceLookupException when no matching service has been found after the timeout */ public static <T> T getService( BundleContext bc, Class<T> type, long timeout ) { return ServiceLookup.<T> getService( bc, type.getName(), timeout, "" ); }
/** * Returns a service matching the given criteria. * * @param <T> class implemented or extended by the service * @param bc bundle context for accessing the OSGi registry * @param type class implemented or extended by the service * @param timeout maximum wait period in milliseconds * @param filter LDAP filter to be matched by the service. The class name will be added to the * filter. * @return matching service (not null) * @throws ServiceLookupException when no matching service has been found after the timeout */ public static <T> T getService( BundleContext bc, Class<T> type, long timeout, String filter ) { return ServiceLookup.<T> getService( bc, type.getName(), timeout, filter ); }
/** * Returns a service matching the given criteria. * * @param <T> class implemented or extended by the service * @param bc bundle context for accessing the OSGi registry * @param type class implemented or extended by the service * @param timeout maximum wait period in milliseconds * @param props properties to be matched by the service * @return matching service (not null) * @throws ServiceLookupException when no matching service has been found after the timeout */ public static <T> T getService( BundleContext bc, Class<T> type, long timeout, Map<String, String> props ) { return ServiceLookup.<T> getService( bc, type.getName(), timeout, props ); }
public ProbeInvoker createProbeInvoker(Object context, String expr) { BundleContext ctx = (BundleContext) context; Injector injector = ServiceLookup.getService(ctx, Injector.class); return new JUnitProbeInvoker(expr, ctx, injector); } }
private static void setupBundle( boolean start, int startLevel, BundleContext bundleContext, Bundle bundle ) throws BundleException { StartLevel sl = ServiceLookup.getService( bundleContext, StartLevel.class ); sl.setBundleStartLevel( bundle, startLevel ); if( start ) { bundle.start(); } }
@Override public ProbeInvoker createProbeInvoker(Object context, String expr) { BundleContext ctx = (BundleContext) context; Injector injector = ServiceLookup.getService(ctx, Injector.class); return new TestNGProbeInvoker(ctx, injector); } }
@Override public ProbeInvoker createProbeInvoker(Object context, String expr) { BundleContext ctx = (BundleContext) context; Injector injector = ServiceLookup.getService(ctx, Injector.class); return new JUnitProbeInvoker(ctx, injector); } }
private ProbeInvoker createInvoker(BundleContext ctx) { String invokerType = System.getProperty("pax.exam.invoker"); if (invokerType == null) { throw new TestContainerException("System property pax.exam.invoker must be defined"); } else { Map<String, String> props = new HashMap<String, String>(); props.put("driver", invokerType); ProbeInvokerFactory factory = ServiceLookup.getService(ctx, ProbeInvokerFactory.class, props); return factory.createProbeInvoker(ctx, null); } }
@Override public Object remoteCall(final Class<?> serviceType, final String methodName, final Class<?>[] methodParams, String filter, final RelativeTimeout timeout, final Object... actualParams) throws NoSuchServiceException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { LOG.trace("Remote call of [" + serviceType.getName() + "." + methodName + "]"); Object service = ServiceLookup.getService(bundleContext, serviceType, timeout.getValue(), filter); Object obj = null; try { obj = serviceType.getMethod(methodName, methodParams).invoke(service, actualParams); } catch (InvocationTargetException t) { if (t.getTargetException().getCause() instanceof RerunTestException) { LOG.debug("rerun the test"); service = ServiceLookup.getService(bundleContext, serviceType, timeout.getValue(), filter); obj = serviceType.getMethod(methodName, methodParams).invoke(service, actualParams); } else { throw t; } } return obj; }
public void setBundleStartLevel( long bundleId, int startLevel ) throws RemoteException, BundleException { BundleContext bc = framework.getBundleContext(); StartLevel sl = ServiceLookup.getService( bc, StartLevel.class ); Bundle bundle = bc.getBundle( bundleId ); sl.setBundleStartLevel( bundle, startLevel ); }
protected void fakeModuleStartupEvent() { // We send the startup event, unfortunately we have to use the OSGi ClassLoaders from non-OSGi context, // so there is a bit of magic with ClassLoaders here. // First get the service Object eventAdmin = ServiceLookup.getService(getFramework().getBundleContext(), EventAdmin.class); // Then obtain the right OSGi ClassLoader we will ue ClassLoader bundleCl = eventAdmin.getClass().getClassLoader(); try { // This the event class we will use for constructing the event Class<?> eventClass = bundleCl.loadClass(Event.class.getName()); // We have to use the constructor via reflection Constructor constructor = eventClass.getConstructor(String.class, Map.class); Object event = constructor.newInstance(PlatformConstants.STARTUP_TOPIC, new HashMap<String, Object>()); // Finally we invoke the send method. Method sendEventMethod = eventAdmin.getClass().getMethod("sendEvent", eventClass); sendEventMethod.invoke(eventAdmin, event); startupEventSent = true; } catch (Exception e) { LOG.error("Error while sending the startup event", e); } }
@Override public void runTest(TestDescription description, TestListener listener) { BundleContext bundleContext = framework.getBundleContext(); ProbeInvoker probeInvokerService = ServiceLookup.getService(bundleContext, ProbeInvoker.class, determineExamServiceTimeout()); probeInvokerService.runTest(description, listener); }
/** * Performs field injection on the given object. The injection method is looked up via the Java * SE service loader. * * @param testClassInstance * test class instance */ private void inject(Object testClassInstance) { if (systemType.equals(EXAM_SYSTEM_TEST)) { BundleContext bc = FrameworkUtil.getBundle(getClass()).getBundleContext(); Injector injector = ServiceLookup.getService(bc, Injector.class); injector.injectFields(testClassInstance); } else { InjectorFactory injectorFactory = ServiceProviderFinder .loadUniqueServiceProvider(InjectorFactory.class); Injector injector = injectorFactory.createInjector(); injector.injectFields(testClassInstance); } }
private void injectField(Object target, Class<?> targetClass, Field field) { Class<?> type = field.getType(); String filterString = ""; String timeoutProp = System.getProperty(EXAM_SERVICE_TIMEOUT_KEY, EXAM_SERVICE_TIMEOUT_DEFAULT); long timeout = Integer.parseInt(timeoutProp); Filter filter = field.getAnnotation(Filter.class); if (filter != null) { filterString = filter.value(); timeout = filter.unit().toMillis(filter.timeout()); } // Retrieve bundle Context just before calling getService to avoid that the bundle restarts // in between BundleContext bc = getBundleContext(targetClass, timeout); Object service = (BundleContext.class == type) ? bc : ServiceLookup.getService(bc, type, timeout, filterString); setField(target, field, service); }
final StartLevel sl = ServiceLookup.getService( bc, StartLevel.class ); final CountDownLatch latch = new CountDownLatch( 1 ); bc.addFrameworkListener( new FrameworkListener()
@Override public synchronized void call(TestAddress address) { Map<String, String> props = new HashMap<String, String>(); props.put(PROBE_SIGNATURE_KEY, address.root().identifier()); BundleContext bundleContext = framework.getBundleContext(); ProbeInvoker probeInvokerService; probeInvokerService = ServiceLookup.getService(bundleContext, ProbeInvoker.class, determineExamServiceTimeout(), props); probeInvokerService.call(address.arguments()); }