All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mmm.orm.connection.impl.DbConnectionPoolProviderManagerImpl Maven / Gradle / Ivy

There is a newer version: 0.9.8
Show newest version
/* 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());
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy