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

io.jooby.guice.GuiceRegistry Maven / Gradle / Ivy

There is a newer version: 3.5.3
Show newest version
/*
 * Jooby https://jooby.io
 * Apache License Version 2.0 https://jooby.io/LICENSE.txt
 * Copyright 2014 Edgar Espina
 */
package io.jooby.guice;

import com.google.inject.ConfigurationException;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.ProvisionException;
import com.google.inject.name.Names;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.Registry;
import io.jooby.ServiceKey;
import io.jooby.exception.RegistryException;

class GuiceRegistry implements Registry {
  private final Injector injector;

  GuiceRegistry(Injector injector) {
    this.injector = injector;
  }

  @NonNull @Override
  public  T require(@NonNull Class type) {
    return require(Key.get(type));
  }

  @NonNull @Override
  public  T require(@NonNull Class type, @NonNull String name) {
    return require(Key.get(type, Names.named(name)));
  }

  @NonNull @Override
  public  T require(@NonNull ServiceKey key) throws RegistryException {
    String name = key.getName();
    return name == null ? require(key.getType()) : require(key.getType(), name);
  }

  @NonNull private  T require(@NonNull Key key) {
    try {
      return injector.getInstance(key);
    } catch (ProvisionException | ConfigurationException x) {
      throw new RegistryException("Provisioning of `" + key + "` resulted in exception", x);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy