@Override
    public synchronized S register(P provider) {

        log.debug("registering provider {} :{}", provider, provider.id());
        checkNotNull(provider, "Provider cannot be null");

        // If the provider is a primary one, check for a conflict.
        ProviderId pid = provider.id();
        checkState(pid.isAncillary() || !providersByScheme.containsKey(pid.scheme()),
            "A primary provider with id %s is already registered",
            providersByScheme.get(pid.scheme()));

        checkState(pid.isAncillary() || !services.containsKey(provider.id()),
            "Provider %s already registered", provider.id());

        // Register the provider by URI scheme only if it is not ancillary.
        S service = null;

        if (!pid.isAncillary()) {
            service = createProviderService(provider);
            services.put(provider.id(), service);
            providers.put(provider.id(), provider);
            providersByScheme.put(pid.scheme(), provider);
        } else {
            ProviderId servicePid = new ProviderId(provider.id().scheme(), provider.id().id(), false);
            service = services.get(servicePid);
            checkState(service != null,
                "Primary provider with id %s not registered yet", pid);
        }

        return service;
    }
@Test(expected = IllegalStateException.class)
    public void duplicateRegistration() {
        TestProviderRegistry registry = new TestProviderRegistry();
        TestProvider pFoo = new TestProvider(new ProviderId("of", "foo"));
        registry.register(pFoo);
        registry.register(pFoo);
    }