OSGIRuntimeObjectFactory extends RuntimeObjectFactory { @Override public boolean objectDefined( Class<?> clazz ) { if ( this.bundleContext == null || !osgiInitialized.get() ) { return super.objectDefined( clazz ); } try { Collection<ServiceReference<IPentahoObjectReference>> serviceReferences = this.bundleContext .getServiceReferences( IPentahoObjectReference.class, ( "(" + REFERENCE_CLASS + "=" + clazz.getName() + ")" ) ); if ( serviceReferences != null && serviceReferences.size() > 0 ) { return true; } } catch ( IllegalStateException ise ) { return false; } catch ( InvalidSyntaxException e ) { throw new IllegalStateException( "Error finding reference in OSGI" ); } return this.bundleContext.getServiceReference( clazz ) != null; } OSGIRuntimeObjectFactory(); void setBundleContext( BundleContext bundleContext ); IPentahoObjectRegistration registerReference( final IPentahoObjectReference<?> reference,
                                                           OSGIPentahoObjectRegistration existingRegistration,
                                                           Class<?>... classes ); @Override IPentahoObjectRegistration registerReference( IPentahoObjectReference<T> reference, Class<?>... classes ); @Override boolean objectDefined( Class<?> clazz ); static final String REFERENCE_CLASS; }
@Test public void testObjectDefined() throws Exception { assertFalse( objectFactory.objectDefined( String.class ) ); SingletonPentahoObjectReference<String> ref = new SingletonPentahoObjectReference<String>( String.class, "Testing", Collections.<String, Object>singletonMap( "foo", "bar" ), 10 ); IPentahoObjectRegistration iPentahoObjectRegistration = objectFactory.registerReference( ref, String.class ); String s = objectFactory.get( String.class, null ); assertEquals( "Testing", s ); assertTrue( objectFactory.objectDefined( String.class ) ); ServiceRegistration registration = mock( ServiceRegistration.class ); ServiceRegistration registration2 = mock( ServiceRegistration.class ); ServiceReference mockRef = mock( ServiceReference.class ); when(bundleContext.registerService( eq(String.class.getName()), anyObject(), any( Dictionary.class ))).thenReturn( registration ); when(bundleContext.registerService( eq(IPentahoObjectReference.class.getName()), anyObject(), any( Dictionary.class ))).thenReturn( registration2 ); objectFactory.setBundleContext( bundleContext ); when( bundleContext.getServiceReference( String.class )).thenReturn( mockRef ); assertTrue( objectFactory.objectDefined( String.class ) ); iPentahoObjectRegistration.remove(); verify( registration, times(1) ).unregister(); }