io.micronaut.context.env.Environment Java Examples

The following examples show how to use io.micronaut.context.env.Environment. 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: GrpcServerConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 * @param environment The environment
 * @param serverHost The server host
 * @param serverPort The server port
 * @param executorService The IO executor service
 */
public GrpcServerConfiguration(
        Environment environment,
        @Property(name = HOST) @Nullable String serverHost,
        @Property(name = PORT) @Nullable Integer serverPort,
        @Named(TaskExecutors.IO) ExecutorService executorService) {
    this.environment = environment;
    this.serverPort = serverPort != null ? serverPort :
            environment.getActiveNames().contains(Environment.TEST) ? SocketUtils.findAvailableTcpPort() : DEFAULT_PORT;
    this.serverHost = serverHost;
    if (serverHost != null) {
        this.serverBuilder = NettyServerBuilder.forAddress(
                new InetSocketAddress(serverHost, this.serverPort)
        );
    } else {
        this.serverBuilder = NettyServerBuilder.forPort(this.serverPort);
    }
    this.serverBuilder.executor(executorService);
}
 
Example #2
Source File: GrpcManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructors a new managed channel configuration.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service to use
 */
public GrpcManagedChannelConfiguration(String name, Environment env, ExecutorService executorService) {
    this.name = name;
    final Optional<SocketAddress> socketAddress = env.getProperty(PREFIX + '.' + name + SETTING_URL, SocketAddress.class);
    if (socketAddress.isPresent()) {
        resolveName = false;
        this.channelBuilder = NettyChannelBuilder.forAddress(socketAddress.get());
    } else {
        final Optional<String> target = env.getProperty(PREFIX + '.' + name + SETTING_TARGET, String.class);
        if (target.isPresent()) {
            this.channelBuilder = NettyChannelBuilder.forTarget(
                    target.get()
            );

        } else {
            final URI uri = name.contains("//") ? URI.create(name) : null;
            if (uri != null && uri.getHost() != null && uri.getPort() > -1) {
                resolveName = false;
                this.channelBuilder = NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort());
            } else {
                this.channelBuilder = NettyChannelBuilder.forTarget(name);
            }
        }
    }
    this.getChannelBuilder().executor(executorService);
}
 
Example #3
Source File: GrpcManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Constructors a new managed channel configuration.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service to use
 */
public GrpcManagedChannelConfiguration(String name, Environment env, ExecutorService executorService) {
    this.name = name;
    final Optional<SocketAddress> socketAddress = env.getProperty(PREFIX + '.' + name + SETTING_URL, SocketAddress.class);
    if (socketAddress.isPresent()) {
        resolveName = false;
        this.channelBuilder = NettyChannelBuilder.forAddress(socketAddress.get());
    } else {
        final Optional<String> target = env.getProperty(PREFIX + '.' + name + SETTING_TARGET, String.class);
        if (target.isPresent()) {
            this.channelBuilder = NettyChannelBuilder.forTarget(
                    target.get()
            );

        } else {
            final URI uri = name.contains("//") ? URI.create(name) : null;
            if (uri != null && uri.getHost() != null && uri.getPort() > -1) {
                resolveName = false;
                this.channelBuilder = NettyChannelBuilder.forAddress(uri.getHost(), uri.getPort());
            } else {
                this.channelBuilder = NettyChannelBuilder.forTarget(name);
            }
        }
    }
    this.getChannelBuilder().executor(executorService);
}
 
Example #4
Source File: Route53AutoNamingRegistrationClient.java    From micronaut-aws with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor for setup.
 * @param environment current environemnts
 * @param route53AutoRegistrationConfiguration  config for auto registration
 * @param clientConfiguration general client configuraiton
 * @param amazonComputeInstanceMetadataResolver resolver for aws compute metdata
 * @param executorService this is for executing the thread to monitor the register operation for completion
 * @param awsServiceDiscoveryResolver this allows is to swap out the bean for a mock version for unit testing
 */
protected Route53AutoNamingRegistrationClient(
        Environment environment,
        Route53AutoRegistrationConfiguration route53AutoRegistrationConfiguration,
        AWSClientConfiguration clientConfiguration,
        AmazonComputeInstanceMetadataResolver amazonComputeInstanceMetadataResolver,
        @Named(TaskExecutors.IO) Executor executorService,
        AWSServiceDiscoveryResolver awsServiceDiscoveryResolver) {
    super(route53AutoRegistrationConfiguration);
    this.environment = environment;
    this.route53AutoRegistrationConfiguration = route53AutoRegistrationConfiguration;
    this.clientConfiguration = clientConfiguration;
    this.awsServiceDiscoveryResolver = awsServiceDiscoveryResolver;
    this.amazonComputeInstanceMetadataResolver = amazonComputeInstanceMetadataResolver;
    this.executorService = executorService;
}
 
Example #5
Source File: KafkaProducerConfiguration.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs the default producer configuration.
 *
 * @param producerName The name of the producer
 * @param defaultConfiguration The default Kafka configuration
 * @param environment The environment
 */
public KafkaProducerConfiguration(
        @Parameter String producerName,
        KafkaDefaultConfiguration defaultConfiguration,
        Environment environment) {
    super(new Properties());
    Properties config = getConfig();
    config.putAll(defaultConfiguration.getConfig());
    String propertyKey = PREFIX + '.' + NameUtils.hyphenate(producerName, true);
    if (environment.containsProperties(propertyKey)) {
        config.putAll(
                environment.getProperties(propertyKey, StringConvention.RAW)
        );
    }

}
 
Example #6
Source File: KafkaDefaultConfiguration.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
private static Properties resolveDefaultConfiguration(Environment environment) {
    Map<String, Object> values = environment.containsProperties(PREFIX) ? environment.getProperties(PREFIX, StringConvention.RAW) : Collections.emptyMap();
    Properties properties = new Properties();
    values.entrySet().stream().filter(entry -> {
        String key = entry.getKey();
        return Stream.of("embedded", "consumers", "producers", "streams").noneMatch(key::startsWith);
    }).forEach(entry -> {
        Object value = entry.getValue();
        if (ConversionService.SHARED.canConvert(entry.getValue().getClass(), String.class)) {
            Optional<?> converted = ConversionService.SHARED.convert(entry.getValue(), String.class);
            if (converted.isPresent()) {
                value = converted.get();
            }
        }
        properties.setProperty(entry.getKey(), value.toString());

    });
    return properties;
}
 
Example #7
Source File: GrpcServerConfiguration.java    From micronaut-grpc with Apache License 2.0 6 votes vote down vote up
/**
 * Default constructor.
 * @param environment The environment
 * @param serverHost The server host
 * @param serverPort The server port
 * @param executorService The IO executor service
 */
public GrpcServerConfiguration(
        Environment environment,
        @Property(name = HOST) @Nullable String serverHost,
        @Property(name = PORT) @Nullable Integer serverPort,
        @Named(TaskExecutors.IO) ExecutorService executorService) {
    this.environment = environment;
    this.serverPort = serverPort != null ? serverPort :
            environment.getActiveNames().contains(Environment.TEST) ? SocketUtils.findAvailableTcpPort() : DEFAULT_PORT;
    this.serverHost = serverHost;
    if (serverHost != null) {
        this.serverBuilder = NettyServerBuilder.forAddress(
                new InetSocketAddress(serverHost, this.serverPort)
        );
    } else {
        this.serverBuilder = NettyServerBuilder.forPort(this.serverPort);
    }
    this.serverBuilder.executor(executorService);
}
 
Example #8
Source File: HttpFunction.java    From micronaut-gcp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
protected ApplicationContextBuilder newApplicationContextBuilder() {
    final ApplicationContextBuilder builder = super.newApplicationContextBuilder();
    builder.deduceEnvironment(false);
    builder.environments(Environment.FUNCTION, Environment.GOOGLE_COMPUTE);
    return builder;
}
 
Example #9
Source File: GrpcDefaultManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service
 */
public GrpcDefaultManagedChannelConfiguration(
        String name,
        Environment env,
        ExecutorService executorService) {
    super(name, env, executorService);
}
 
Example #10
Source File: GrpcNamedManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service
 */
public GrpcNamedManagedChannelConfiguration(
        @Parameter String name,
        Environment env,
        ExecutorService executorService) {
    super(name, env, executorService);
}
 
Example #11
Source File: EntityManagerFactoryBean.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
/**
 * @param jpaConfiguration The JPA configuration
 * @param environment      The environment
 * @param beanLocator      The bean locator
 */
public EntityManagerFactoryBean(
        JpaConfiguration jpaConfiguration,
        Environment environment,
        BeanLocator beanLocator) {

    this.jpaConfiguration = jpaConfiguration;
    this.environment = environment;
    this.beanLocator = beanLocator;
}
 
Example #12
Source File: AbstractKafkaStreamsConfiguration.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Shared initialization.
 *
 * @param applicationConfiguration The application config
 * @param environment The env
 * @param config The config to be initialized
 */
protected void init(ApplicationConfiguration applicationConfiguration, Environment environment, Properties config) {
    // set the default application id
    String applicationName = applicationConfiguration.getName().orElse(Environment.DEFAULT_NAME);
    config.putIfAbsent(StreamsConfig.APPLICATION_ID_CONFIG, applicationName);

    if (environment.getActiveNames().contains(Environment.TEST)) {
        String tmpDir = System.getProperty("java.io.tmpdir");
        if (StringUtils.isNotEmpty(tmpDir)) {
            if (new File(tmpDir, applicationName).mkdirs()) {
                config.putIfAbsent(StreamsConfig.STATE_DIR_CONFIG, tmpDir);
            }
        }
    }
}
 
Example #13
Source File: KafkaStreamsConfiguration.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new {@link KafkaStreamsConfiguration} for the given defaults.
 *
 * @param streamName The stream name
 * @param defaultConfiguration The default configuration
 * @param applicationConfiguration The application configuration
 * @param environment The environment
 */
public KafkaStreamsConfiguration(
        @Parameter String streamName,
        KafkaDefaultConfiguration defaultConfiguration,
        ApplicationConfiguration applicationConfiguration,
        Environment environment) {
    super(defaultConfiguration);
    Properties config = getConfig();
    String propertyKey = PREFIX + '.' + NameUtils.hyphenate(streamName, true);
    config.putAll(environment.getProperty(propertyKey, Properties.class).orElseGet(Properties::new));
    init(applicationConfiguration, environment, config);
}
 
Example #14
Source File: DefaultKafkaStreamsConfiguration.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new {@link KafkaStreamsConfiguration} for the given defaults.
 *
 * @param defaultConfiguration The default configuration
 * @param applicationConfiguration The application configuration
 * @param environment The environment
 */
public DefaultKafkaStreamsConfiguration(KafkaDefaultConfiguration defaultConfiguration,
                                        ApplicationConfiguration applicationConfiguration,
                                        Environment environment) {
    super(defaultConfiguration);
    Properties config = getConfig();
    config.putAll(defaultConfiguration.getConfig());
    init(applicationConfiguration, environment, config);
}
 
Example #15
Source File: KafkaDefaultConfiguration.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs the default Kafka configuration.
 *
 * @param environment The environment
 */
public KafkaDefaultConfiguration(Environment environment) {
    super(resolveDefaultConfiguration(environment));
    getConfig().putIfAbsent(
            ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
            AbstractKafkaConfiguration.DEFAULT_BOOTSTRAP_SERVERS
    );
}
 
Example #16
Source File: KafkaConsumerConfiguration.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new {@link KafkaConsumerConfiguration} for the given defaults.
 *
 * @param consumerName The name of the consumer
 * @param defaultConfiguration The default configuration
 * @param environment The environment
 */
public KafkaConsumerConfiguration(
        @Parameter String consumerName,
        KafkaDefaultConfiguration defaultConfiguration,
        Environment environment) {
    super(new Properties());
    Properties config = getConfig();
    config.putAll(defaultConfiguration.getConfig());
    String propertyKey = PREFIX + '.' + NameUtils.hyphenate(consumerName, true);
    if (environment.containsProperties(propertyKey)) {
        config.putAll(
                environment.getProperties(propertyKey, StringConvention.RAW)
        );
    }
}
 
Example #17
Source File: EmbeddedMain.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
public static void main(String...args) {
    ApplicationContext applicationContext = ApplicationContext.run(
            Collections.singletonMap(
                    AbstractKafkaConfiguration.EMBEDDED, true
            )
    , Environment.TEST);
    KafkaEmbedded embedded = applicationContext.getBean(KafkaEmbedded.class);
}
 
Example #18
Source File: GrpcDefaultManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service
 */
public GrpcDefaultManagedChannelConfiguration(
        String name,
        Environment env,
        ExecutorService executorService) {
    super(name, env, executorService);
}
 
Example #19
Source File: GrpcNamedManagedChannelConfiguration.java    From micronaut-grpc with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 * @param name The name
 * @param env The environment
 * @param executorService The executor service
 */
public GrpcNamedManagedChannelConfiguration(
        @Parameter String name,
        Environment env,
        ExecutorService executorService) {
    super(name, env, executorService);
}
 
Example #20
Source File: Route53AutoNamingClient.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 *
 * @param route53ClientDiscoveryConfiguration The route 53 configuration
 * @param awsServiceDiscoveryResolver The AWS service discovery resolver
 * @param environment The environment
 */
public Route53AutoNamingClient(Route53ClientDiscoveryConfiguration route53ClientDiscoveryConfiguration,
                               AWSServiceDiscoveryResolver awsServiceDiscoveryResolver,
                               Environment environment) {
    this.route53ClientDiscoveryConfiguration = route53ClientDiscoveryConfiguration;
    this.awsServiceDiscoveryResolver = awsServiceDiscoveryResolver;
    this.environment = environment;

}
 
Example #21
Source File: GoogleFunctionInitializer.java    From micronaut-gcp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
protected ApplicationContextBuilder newApplicationContextBuilder() {
    ApplicationContextBuilder builder = super.newApplicationContextBuilder();
    builder.deduceEnvironment(false);
    builder.environments(Environment.GOOGLE_COMPUTE);
    return builder;
}
 
Example #22
Source File: CredentialsAndRegionFactory.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * @param environment The {@link Environment}
 * @return An {@link AwsRegionProviderChain} that attempts to read the values from the Micronaut environment
 * first, then delegates to {@link DefaultAwsRegionProviderChain}.
 */
@Singleton
public AwsRegionProviderChain awsRegionProvider(Environment environment) {
    return new AwsRegionProviderChain(
            new EnvironmentAwsRegionProvider(environment),
            new DefaultAwsRegionProviderChain()
    );
}
 
Example #23
Source File: CredentialsAndRegionFactory.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * @param environment The {@link Environment}
 * @return An {@link AwsCredentialsProviderChain} that attempts to read the values from the Micronaut environment
 * first, then delegates to {@link DefaultCredentialsProvider}.
 */
@Bean(preDestroy = "close")
@Singleton
public AwsCredentialsProviderChain awsCredentialsProvider(Environment environment) {
    return AwsCredentialsProviderChain.of(
            EnvironmentAwsCredentialsProvider.create(environment),
            DefaultCredentialsProvider.create()
    );
}
 
Example #24
Source File: MicronautRequestStreamHandler.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@Override
protected String resolveFunctionName(Environment env) {
    if (this.functionName != null) {
        return functionName;
    } else {
        return super.resolveFunctionName(env);
    }
}
 
Example #25
Source File: AWSLambdaConfiguration.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 * @param clientConfiguration clientConfiguration
 * @param environment environment
 */
public AWSLambdaConfiguration(AWSClientConfiguration clientConfiguration, Environment environment) {
    this.clientConfiguration = clientConfiguration;

    this.builder.setCredentials(new AWSCredentialsProviderChain(
        new EnvironmentAWSCredentialsProvider(environment),
        new EnvironmentVariableCredentialsProvider(),
        new SystemPropertiesCredentialsProvider(),
        new ProfileCredentialsProvider(),
        new EC2ContainerCredentialsProviderWrapper()
    ));
}
 
Example #26
Source File: AlexaFunction.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a new builder.
 *
 * @return The {@link ApplicationContextBuilder}
 */
@SuppressWarnings("unchecked")
@NonNull
protected ApplicationContextBuilder newApplicationContextBuilder() {
    return ApplicationContext.build(Environment.FUNCTION, MicronautLambdaContext.ENVIRONMENT_LAMBDA, AlexaEnvironment.ENV_ALEXA)
            .eagerInitSingletons(true)
            .eagerInitConfiguration(true);
}
 
Example #27
Source File: MicronautLambdaContainerHandler.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize() throws ContainerInitializationException {
    Timer.start(TIMER_INIT);
    try {
        this.applicationContext = applicationContextBuilder.environments(
                Environment.FUNCTION, MicronautLambdaContext.ENVIRONMENT_LAMBDA)
                .build()
                .start();
        this.lambdaContainerEnvironment.setApplicationContext(applicationContext);
        this.lambdaContainerEnvironment.setJsonCodec(applicationContext.getBean(JsonMediaTypeCodec.class));
        this.lambdaContainerEnvironment.setRouter(applicationContext.getBean(Router.class));

        Optional<ObjectMapper> objectMapper = applicationContext.findBean(ObjectMapper.class, Qualifiers.byName("aws"));
        if (objectMapper.isPresent()) {
            lambdaContainerEnvironment.setObjectMapper(objectMapper.get());
        } else {
            lambdaContainerEnvironment.setObjectMapper(applicationContext.getBean(ObjectMapper.class));
        }

        this.requestArgumentSatisfier = new RequestArgumentSatisfier(
                applicationContext.getBean(RequestBinderRegistry.class)
        );
        this.resourceResolver = applicationContext.getBean(StaticResourceResolver.class);
    } catch (Exception e) {
        throw new ContainerInitializationException(
                "Error starting Micronaut container: " + e.getMessage(),
                e
        );
    }
    Timer.stop(TIMER_INIT);
}
 
Example #28
Source File: Application.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Micronaut.build(args)
             .deduceEnvironment(false)
             .environments(Environment.GOOGLE_COMPUTE)
             .start();
}
 
Example #29
Source File: Application.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Micronaut.build(args)
             .deduceEnvironment(false)
             .environments(Environment.GOOGLE_COMPUTE)
             .start();
}
 
Example #30
Source File: Application.java    From micronaut-gcp with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Micronaut.build(args)
             .deduceEnvironment(false)
             .environments(Environment.GOOGLE_COMPUTE)
             .start();
}