io.micronaut.context.annotation.Primary Java Examples

The following examples show how to use io.micronaut.context.annotation.Primary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleCredentialsFactory.java    From micronaut-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * Method used to return the default {@link GoogleCredentials} and provide it as a bean.
 *
 * It will determine which credential in the following way:
 * <ol>
 *     <li>If <pre>gcp.credentials.location</pre> is specified, use its location</li>
 *     <li>Otherwise, if <pre>gcp.credentials.encodedKey</pre> is specified, decode it and use its content</li>
 *     <li>None of the 2 properties were specified, use Application Default credential resolution. See
 *     <a href="https://github.com/googleapis/google-cloud-java#authentication">Google Cloud Java authentication</a>.
 *     This will resolve credential in the following order:
 *       <ol>
 *           <li>The credentials file pointed to by the <pre>GOOGLE_APPLICATION_CREDENTIALS</pre> environment variable</li>
 *           <li>Credentials provided by the Google Cloud SDK <pre>gcloud auth application-default login</pre> command</li>
 *           <li>Google App Engine built-in credentials when running inside of Google App Engine</li>
 *           <li>Google Cloud Shell built-in credentials when running inside of Google Cloud Shell</li>
 *           <li>Google Compute Engine built-in credentials when running inside of Google Compute Engine or Kubernetes Engine</li>
 *       </ol>
 *     </li>
 * </ol>
 *
 * @return The {@link GoogleCredentials}
 * @throws IOException An exception if an error occurs
 */
@Requires(missingBeans = GoogleCredentials.class)
@Requires(classes = com.google.auth.oauth2.GoogleCredentials.class)
@Primary
@Singleton
protected GoogleCredentials defaultGoogleCredentials() throws IOException {
    final List<String> scopes = configuration.getScopes().stream()
            .map(URI::toString).collect(Collectors.toList());

    GoogleCredentials credentials;
    if (configuration.getLocation().isPresent() && configuration.getEncodedKey().isPresent()) {
        throw new ConfigurationException("Please specify only one of gcp.credentials.location or gcp.credentials.encodedKey");
    } else if (configuration.getLocation().isPresent()) {
        LOG.info("Google Credentials from gcp.credentials.location = " + configuration.getLocation());
        FileInputStream fis = new FileInputStream(configuration.getLocation().get());
        credentials = GoogleCredentials.fromStream(fis);
        fis.close();
    } else if (configuration.getEncodedKey().isPresent()) {
        LOG.info("Google Credentials from gcp.credentials.encodedKey");
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(configuration.getEncodedKey().get());
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        credentials = GoogleCredentials.fromStream(is);
        is.close();
    } else {
        LOG.info("Google Credentials from Application Default Credentials");
        credentials = GoogleCredentials.getApplicationDefault();
    }

    return credentials.createScoped(scopes);
}
 
Example #2
Source File: GrpcManagedChannelFactory.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a managed channel for the given target.
 * @param target The target
 * @return The channel
 */
@Bean
@Primary
protected ManagedChannel managedChannel(@Parameter String target) {
    final NettyChannelBuilder nettyChannelBuilder = beanContext.createBean(NettyChannelBuilder.class, target);
    return nettyChannelBuilder.build();
}
 
Example #3
Source File: GrpcManagedChannelFactory.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a managed channel for the given target.
 * @param target The target
 * @return The channel
 */
@Bean
@Primary
protected ManagedChannel managedChannel(@Parameter String target) {
    final NettyChannelBuilder nettyChannelBuilder = beanContext.createBean(NettyChannelBuilder.class, target);
    return nettyChannelBuilder.build();
}
 
Example #4
Source File: CommandsTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Primary
@MockBean(AppConfig.class)
AppConfig mockSlackAppConfig() throws IOException, SlackApiException {
    AppConfig config = AppConfig.builder().signingSecret(signingSecret).singleTeamBotToken(botToken).build();
    config.setSlack(mockSlack());
    return config;
}
 
Example #5
Source File: PrimaryAnnotationMapper.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@Override
protected List<AnnotationValue<?>> mapInternal(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
    return Collections.singletonList(
            AnnotationValue.builder(Primary.class).build()
    );
}