private void configureLocalServices(ServiceRegistry registry) { registry.register( Keyboard.MANAGER.getName(), new ServiceFactory() { @Override public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) { return Keyboard.MANAGER.bind(new KeyboardImpl(view.getContext(), mKeyboardState), pipe); } }); registry.register( RawKeyboardService.MANAGER.getName(), new ServiceFactory() { @Override public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) { return RawKeyboardService.MANAGER.bind( new RawKeyboardServiceImpl(mRawKeyboardState), pipe); } }); registry.register( ApplicationMessages.MANAGER.getName(), new ServiceFactory() { @Override public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) { return ApplicationMessages.MANAGER.bind(new ApplicationMessagesImpl(), pipe); } }); }
public void testGetServiceProviderByClass() { Class[] CATEGORIES = new Class[] {ImageReaderSpi.class}; ServiceRegistry registry = new ServiceRegistry(Arrays.<Class<?>>asList(CATEGORIES).iterator()); ImageReaderSpi reader = new SampleImageReaderSpi(); registry.registerServiceProvider(reader, CATEGORIES[0]); ImageReaderSpi provider = registry.getServiceProviderByClass(SampleImageReaderSpi.class); assertEquals(reader, provider); }
public void testContains() { Class[] CATEGORIES = new Class[] {ImageReaderSpi.class}; ServiceRegistry registry = new ServiceRegistry(Arrays.<Class<?>>asList(CATEGORIES).iterator()); ImageReaderSpi reader1 = new SampleImageReaderSpi(); ImageReaderSpi reader2 = new SampleImageReaderSpi(); registry.registerServiceProvider(reader1, CATEGORIES[0]); assertTrue("Failed to check if reader1 registered", registry.contains(reader1)); assertFalse("Failed to check if reader2 registered", registry.contains(reader2)); }
public void testDeregisterAll() { Class[] CATEGORIES = new Class[] {ImageReaderSpi.class}; ServiceRegistry registry = new ServiceRegistry(Arrays.<Class<?>>asList(CATEGORIES).iterator()); ImageReaderSpi reader1 = new Reader1Spi(); ImageReaderSpi reader2 = new Reader2Spi(); // Register two providers registry.registerServiceProvider(reader1, CATEGORIES[0]); registry.registerServiceProvider(reader2, CATEGORIES[0]); registry.deregisterAll(ImageReaderSpi.class); assertFalse( "Reader1 is still regitered, deregisterAll(Class) failed", registry.contains(reader1)); assertFalse( "Reader2 is still regitered, deregisterAll(Class) failed", registry.contains(reader2)); // Re-register two providers registry.registerServiceProvider(reader1, CATEGORIES[0]); registry.registerServiceProvider(reader2, CATEGORIES[0]); registry.deregisterAll(); assertFalse("Reader1 is still regitered, deregisterAll() failed", registry.contains(reader1)); assertFalse("Reader2 is still regitered, deregisterAll() failed", registry.contains(reader2)); }
@SuppressWarnings("unchecked") public void testDeregisterServiceProvider() throws Exception { Class[] CATEGORIES = new Class[] { javax.imageio.spi.ImageReaderSpi.class, javax.imageio.spi.SampleImageReaderSpi.class }; ServiceRegistry registry = new ServiceRegistry(Arrays.<Class<?>>asList(CATEGORIES).iterator()); SampleImageReaderSpi spi = new SampleImageReaderSpi(); // Test deregisterServiceProvider(Object, Class) registry.registerServiceProvider(spi, CATEGORIES[0]); assertTrue( "deregisterServiceProvider(Object, Class) returns incorrect value for a registered provider ", registry.deregisterServiceProvider(spi, CATEGORIES[0])); assertFalse( "deregisterServiceProvider(Object, Class) returns incorrect value for a unregistered provider", registry.deregisterServiceProvider(spi, CATEGORIES[0])); // Test deregisterServiceProvider(Object) registry.registerServiceProvider(spi, CATEGORIES[0]); registry.registerServiceProvider(spi, CATEGORIES[1]); registry.deregisterServiceProvider(spi); assertFalse( "deregisterServiceProvider(Object) failed to remove all providers", registry.deregisterServiceProvider(spi, CATEGORIES[0])); assertFalse( "deregisterServiceProvider(Object) failed to remove all providers", registry.deregisterServiceProvider(spi, CATEGORIES[1])); }
public void testRegistryServiceProvider() throws Exception { Class[] CATEGORIES = new Class[] {ImageWriterSpi.class, ImageReaderSpi.class}; ServiceRegistry registry = new ServiceRegistry(Arrays.<Class<?>>asList(CATEGORIES).iterator()); SampleImageReaderSpi spiA = new SampleImageReaderSpi(); SampleImageReaderSpi spiB = new SampleImageReaderSpi(); assertTrue( "registerServiceProvider() returns incorrect value for the empty registry", registry.registerServiceProvider(spiA, CATEGORIES[1])); assertFalse( "registerServiceProvider() returns incorrect value if a provider of the same class was previously registered", registry.registerServiceProvider(spiB, CATEGORIES[1])); }
public void testLookupProviders() { // lookup from a correct provider-configuration file Iterator it = ServiceRegistry.lookupProviders(CorrectProviderConfiguration.class); assertEquals( "Failed to find provider and instantiate it", "class javax.imageio.spi.CorrectProviderConfiguration", it.next().getClass().toString()); // lookup from incorrect provider-configuration file try { it = ServiceRegistry.lookupProviders(IncorrectProviderConfiguration.class); fail("ServiceConfigurationError expected"); } catch (Error e) { // Ok } }
/** Destroy the wrapper. This is called when the bundle is stopped. */ protected void close() { valid = false; /* invalidate context */ final ServiceRegistry registry = framework.getServiceRegistry(); registry.removeAllServiceListeners(this); framework.removeAllListeners(this); /* service's registered by the bundle, if any, are unregistered. */ registry.unregisterServices(this); /* service's used by the bundle, if any, are released. */ registry.releaseServicesInUse(this); synchronized (contextLock) { servicesInUse = null; } }
@SuppressWarnings("unchecked") void start() { m_slReg = (ServiceRegistration<StartLevel>) m_registry.registerService( m_felix, new String[] {StartLevel.class.getName()}, new StartLevelImpl(m_felix), null); }
public void unregister() { synchronized (this) { if (!isValid() || m_isUnregistering) { throw new IllegalStateException("Service already unregistered."); } m_isUnregistering = true; } m_registry.unregisterService(m_bundle, this); synchronized (this) { m_svcObj = null; m_factory = null; } }
/** * Matches a service in the registry with the supplied type. If no match is found and a parent * registry has been delclared, the lookup request is passed onto the parent registry, otherwise * null is returned. * * @param type the service type * @return an instance of the type or null if the type could not be resolved */ public <T> T lookup(Class<T> type) { for (Object object : m_args) { if (null != object) { Class c = object.getClass(); if (type.isAssignableFrom(c)) { return type.cast(object); } } } if (null != m_parent) { return m_parent.lookup(type); } return null; }
public void setProperties(Dictionary dict) { Map oldProps; synchronized (this) { // Make sure registration is valid. if (!isValid()) { throw new IllegalStateException("The service registration is no longer valid."); } // Remember old properties. oldProps = m_propMap; // Set the properties. initializeProperties(dict); } // Tell registry about it. m_registry.servicePropertiesModified(this, new MapToDictionary(oldProps)); }
void ungetService(Bundle relBundle, Object svcObj) { // If the service object is a service factory, then // let it release the service object. if (m_factory != null) { try { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new ServiceFactoryPrivileged(relBundle, svcObj)); } else { ungetFactoryUnchecked(relBundle, svcObj); } } catch (Exception ex) { m_registry .getLogger() .log( m_bundle, Logger.LOG_ERROR, "ServiceRegistrationImpl: Error ungetting service.", ex); } } }
private Bundle[] getUsingBundles() { return m_registry.getUsingBundles(m_ref); }
public void testGetServiceProviders() { Class[] CATEGORIES = new Class[] {ImageReaderSpi.class}; ServiceRegistry registry = new ServiceRegistry(Arrays.<Class<?>>asList(CATEGORIES).iterator()); ImageReaderSpi reader = new SampleImageReaderSpi(); ImageReaderSpi reader1 = new Reader1Spi(); ImageReaderSpi reader2 = new Reader2Spi(); // Add 3 different providers to the registry registry.registerServiceProvider(reader, CATEGORIES[0]); registry.registerServiceProvider(reader1, CATEGORIES[0]); registry.registerServiceProvider(reader2, CATEGORIES[0]); // Add a different type of provider to the category ImageWriterSpi writer = new SampleImageWriterSpi(); try { registry.registerServiceProvider(writer, CATEGORIES[0]); fail("ClassCastException expected"); } catch (ClassCastException expected) { // Ok } // Set ordering between these providers // reader2 > reader1 > reader assertTrue( "Failed to set ordering: reader2 > reader1", registry.setOrdering(CATEGORIES[0], reader2, reader1)); assertTrue( "Failed to set ordering: reader1 > reader", registry.setOrdering(CATEGORIES[0], reader1, reader)); // Return false if this ordering has already been set assertFalse( "Failed to check if the ordering reader1 > reader has been set", registry.setOrdering(CATEGORIES[0], reader1, reader)); // If two providers are the same try { registry.setOrdering(CATEGORIES[0], reader, reader); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException expected) { // Ok } // If either provider is null try { registry.setOrdering(CATEGORIES[0], null, reader); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException expected) { // Ok } // Get the iterator of sorted providers Iterator it = registry.getServiceProviders(CATEGORIES[0], true); // Verify the order assertEquals("Failed to return reader2", it.next(), reader2); assertEquals("Failed to return reader1", it.next(), reader1); assertEquals("Failed to return reader", it.next(), reader); // The iterator should be able to run more than once it = registry.getServiceProviders(CATEGORIES[0], true); // Verify the order assertEquals("Failed to return reader2", it.next(), reader2); assertEquals("Failed to return reader1", it.next(), reader1); assertEquals("Failed to return reader", it.next(), reader); // Unset orderings assertTrue( "Failed to unset ordering: reader2 > reader1", registry.unsetOrdering(CATEGORIES[0], reader2, reader1)); assertTrue( "Failed to unset ordering: reader1 > reader", registry.unsetOrdering(CATEGORIES[0], reader1, reader)); // Return false if this ordering is not set assertFalse( "Failed to check if the ordering is not set", registry.unsetOrdering(CATEGORIES[0], reader2, reader)); }