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

com.visionarts.powerjambda.utils.EnvironmentVariableUtils Maven / Gradle / Ivy

/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.visionarts.powerjambda.utils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * Helper class for getting environment variables on AWS Lambda runtime.
*
* */ public final class EnvironmentVariableUtils { /** * An operation that accepts three input arguments and returns no result.
*
* * @param The first input argument * @param The second input argument * @param The third input argument */ @FunctionalInterface public interface TriConsumer { /** * Performs the operation given the specified arguments.
*
* * @param t The first input argument * @param r The second input argument * @param s The third input argument */ void accept(T t, R r, S s); } private static final Map, TriConsumer> FIELD_SET_CONSUMER_MAP; static { Map, TriConsumer> map = new HashMap<>(8); map.put(Boolean.class, (f, o, v) -> ReflectionUtils.fieldSetAsBoolean(f, o, v)); map.put(boolean.class, (f, o, v) -> ReflectionUtils.fieldSetAsBoolean(f, o, v)); map.put(String.class, (f, o, v) -> ReflectionUtils.fieldSetAsString(f, o, v)); map.put(Integer.class, (f, o, v) -> ReflectionUtils.fieldSetAsInteger(f, o, v)); map.put(int.class, (f, o, v) -> ReflectionUtils.fieldSetAsInteger(f, o, v)); FIELD_SET_CONSUMER_MAP = Collections.unmodifiableMap(map); } // Suppresses default constructor, ensuring non-instantiability. private EnvironmentVariableUtils() { } /** * Instantiates the given configuration class from Environment variables on AWS Lambda.
*
* Note: The configuration class must have the following rules.
* - Need a constructor with no arguments
* - The field type is public and available in [Boolean/boolean/String/Integer/int]
* - The modifier of the field must not be final
* - Search field's name from Environment variables when getting an Environment value
* * @param The configuration class * @param configurationClazz The configuration class to instantiate * @return The new configuration instance */ public static C newConfigurationInstance(Class configurationClazz) { Objects.requireNonNull(configurationClazz); Utils.require(configurationClazz, FunctionalUtils.not(Class::isInterface), () -> new IllegalArgumentException("Specified class is an interface : " + configurationClazz.toString())); try { C configuration = configurationClazz.getConstructor().newInstance(); Arrays.stream(configurationClazz.getFields()) .forEach(field -> fieldSetFromEnvVal(field, configuration)); return configuration; } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } private static void fieldSetFromEnvVal(Field field, Object targetObject) { String value = System.getenv(field.getName()); if (value == null) { // skip if not exist on environment return; } Optional.ofNullable(FIELD_SET_CONSUMER_MAP.get(field.getType())) .orElseThrow(() -> new UnsupportedOperationException( "Not supported type : " + field.getType().getSimpleName() + ", available in [Boolean/boolean/String/Integer/int]")) .accept(field, targetObject, value); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy