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

io.github.ensyb.properties.PropertiesLoader Maven / Gradle / Ivy

package io.github.ensyb.properties;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.ensyb.properties.meta.DefaultValue;
import io.github.ensyb.properties.meta.IConstants;
import io.github.ensyb.properties.meta.Key;
import io.github.ensyb.properties.meta.Source;
import io.github.ensyb.properties.meta.Value;

public class PropertiesLoader {
	private static final Logger LOG = LoggerFactory.getLogger(PropertiesLoader.class);

	private static final String DEFAULT_LOADED_VALUE = "";

	@SuppressWarnings("unchecked")
	public static  Type of(Class classOf) {
		return (Type) Proxy.newProxyInstance(classOf.getClassLoader(), new Class[] { classOf },
				(Object proxy, Method method, Object[] args) -> {
					try {
						return loadOrDefault(method);
					} catch (Exception e) {
						throw new PropertieLoaderException(e.getMessage());
					}
				});
	}

	@SuppressWarnings("unchecked")
	public static  Type ofNoSource(Class classOf) {
		return (Type) Proxy.newProxyInstance(classOf.getClassLoader(), new Class[] { classOf },
				(Object proxy, Method method, Object[] args) -> {
					try {
						return loadOfNoSource(method);
					} catch (Exception e) {
						throw new PropertieLoaderException(e.getMessage());
					}
				});
	}

	private static String loadOrDefault(Method method) {
		String sourceValue = getSourceValue(method).orElse(DEFAULT_LOADED_VALUE);

		String keyValue = getKeyValueIfPresent(method).orElse(DEFAULT_LOADED_VALUE);
		if (!sourceValue.isEmpty()) {
			String loadedValue = loadValueFromResourceLoader(keyValue, sourceValue);
			if (loadedValue.isEmpty()) {
				String ifOccuredException = "Can't load propertie with key " + keyValue
						+ " and there is no default value specified @DefaultValue for this field";
				return getDefault(method, ifOccuredException);
			}
			return loadedValue;
		} else {
			String ifOccuredException = "Can't use key @Key if there is no source @Source specified, instead"
					+ " of that use defaultValue @DefaultValue ";
			return getDefault(method, ifOccuredException);
		}
	}

	private static Optional getSourceValue(Method method) {
		String source = null;
		Annotation sourceAnotation = method.getDeclaringClass().getAnnotation(Source.class);
		if (sourceAnotation == null)
			LOG.warn("WARNING : There is no source @Source annotation to load properties file");

		if (sourceAnotation instanceof Source)
			source = ((Source) sourceAnotation).value();
		return Optional.ofNullable(source);
	}

	private static Optional getKeyValueIfPresent(Method method) {
		String annotationValue = null;
		if (checkIfAnnotationIsPresentOnMethod(method, Key.class)) {
			annotationValue = (String) method.getAnnotation(Key.class).value();
		}
		return Optional.ofNullable(annotationValue);
	}

	private static String getDefault(Method method, String exceptionMessage) {
		String defaultValue = getDefaultValueIfPresent(method).orElse(DEFAULT_LOADED_VALUE);
		if (defaultValue.isEmpty())
			throw new PropertieLoaderException(exceptionMessage);
		return defaultValue;
	}

	private static Optional getDefaultValueIfPresent(Method method) {
		String annotationValue = null;
		if (checkIfAnnotationIsPresentOnMethod(method, DefaultValue.class)) {
			annotationValue = (String) method.getAnnotation(DefaultValue.class).value();
		} else if (checkIfAnnotationIsPresentOnMethod(method, Value.class)) {
			annotationValue = (String) method.getAnnotation(Value.class).value();
		}
		return Optional.ofNullable(annotationValue);
	}

	private static String loadValueFromResourceLoader(String key, String source) {
		return PropertieResourceLoader.getPropertie(key, source).orElse(DEFAULT_LOADED_VALUE);
	}

	private static  boolean checkIfAnnotationIsPresentOnMethod(Method method,
			Class annotationClass) {
		Annotation annoatation = method.getAnnotation(annotationClass);
		return annoatation != null;
	}

	private static String loadOfNoSource(Method method) {
		loadOfNoSourceConatinsAnnotations(method);
		String defaultValue = getDefaultValueIfPresent(method).orElse("");
		if (defaultValue.isEmpty()) {
			return getValueFromValueAnnotation(method);
		}
		return defaultValue;
	}

	private static void loadOfNoSourceConatinsAnnotations(Method method) {
		String message = "if you loading properties form file use PropertiesLoader.of method";
		if (checkIfAnnotationIsPresentOnMethod(method, Source.class))
			throw new PropertieLoaderException(message);

		if (checkIfAnnotationIsPresentOnMethod(method, Key.class)) {
			throw new PropertieLoaderException(message);
		}

	}

	private static String getValueFromValueAnnotation(Method method) {
		if (!checkIfAnnotationIsPresentOnMethod(method, Value.class))
			throw new PropertieLoaderException("There is no @Value on this field cannot load anything ");

		String annotationValue = (String) method.getAnnotation(Value.class).value();
		if (null == annotationValue || annotationValue.isEmpty()) {
			throw new PropertieLoaderException("@Value cannot be empty");
		}

		return annotationValue;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy