io.github.mmm.orm.connection.impl.DbConnectionPoolProviderManagerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mmm-orm-spi Show documentation
Show all versions of mmm-orm-spi Show documentation
Service Provider API (SPI) for mmm-orm.
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.orm.connection.impl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import io.github.mmm.base.collection.ReadOnlyIterator;
import io.github.mmm.base.exception.DuplicateObjectException;
import io.github.mmm.base.exception.ObjectNotFoundException;
import io.github.mmm.orm.connection.DbConnection;
import io.github.mmm.orm.connection.DbConnectionPoolProvider;
import io.github.mmm.orm.connection.DbConnectionPoolProviderManager;
/**
* Implementation of {@link DbConnectionPoolProviderManager}.
*/
public class DbConnectionPoolProviderManagerImpl implements DbConnectionPoolProviderManager {
/** The singleton instance. */
public static final DbConnectionPoolProviderManagerImpl INSTANCE = new DbConnectionPoolProviderManagerImpl();
private final Map> providers;
/**
* The constructor.
*/
@SuppressWarnings("rawtypes")
public DbConnectionPoolProviderManagerImpl() {
super();
this.providers = new HashMap<>();
ServiceLoader loader = ServiceLoader.load(DbConnectionPoolProvider.class);
for (DbConnectionPoolProvider> provider : loader) {
register(provider);
}
}
private void register(DbConnectionPoolProvider> provider) {
DbConnectionPoolProvider> duplicate = this.providers.put(provider.getId(), provider);
if (duplicate != null) {
throw new DuplicateObjectException(duplicate, provider.getId(), provider);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DbConnectionPoolProvider get(String id) {
DbConnectionPoolProvider provider = this.providers.get(id);
if (provider == null) {
throw new ObjectNotFoundException(DbConnectionPoolProvider.class, id);
}
return provider;
}
@Override
public Iterator> iterator() {
return new ReadOnlyIterator<>(this.providers.values().iterator());
}
}