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

io.vertx.up.unity.Uimmit Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.unity;

import io.vertx.core.json.JsonObject;
import io.vertx.up.eon.Plugins;
import io.vertx.up.exception.zero.InjectionLimeKeyException;
import io.vertx.up.fn.Fn;
import io.vertx.up.log.Annal;
import io.vertx.up.plugin.Infix;
import io.vertx.up.runtime.ZeroAmbient;
import io.vertx.up.uca.yaml.Node;
import io.vertx.up.uca.yaml.ZeroUniform;
import io.vertx.up.util.Ut;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Set;

public class Uimmit {

    private transient final Class clazz;
    private transient final Annal logger;

    private Uimmit(final Class clazz) {
        this.clazz = clazz;
        logger = Annal.get(clazz);
    }

    public static Uimmit create(final Class clazz) {
        return Fn.pool(Pool.INFIXES, clazz, () -> new Uimmit(clazz));
    }

    private Class search(
            final Field field
    ) {
        final Annotation[] annotations = field.getDeclaredAnnotations();
        final Set> annotationCls = Plugins.INFIX_MAP.keySet();
        Class hitted = null;
        for (final Annotation annotation : annotations) {
            if (annotationCls.contains(annotation.annotationType())) {
                hitted = annotation.annotationType();
                break;
            }
        }
        return hitted;
    }

    public Object initialize(final Field field) {
        final Class key = search(field);
        final String pluginKey = Plugins.INFIX_MAP.get(key);
        final Class infixCls = ZeroAmbient.getPlugin(pluginKey);
        Object ret = null;
        if (null != infixCls) {
            if (Ut.isImplement(infixCls, Infix.class)) {
                // Config checking
                final Node node = Ut.instance(ZeroUniform.class);
                final JsonObject options = node.read();

                Fn.outUp(!options.containsKey(pluginKey), logger,
                        InjectionLimeKeyException.class,
                        clazz, infixCls, pluginKey);

                final Infix reference = Ut.singleton(infixCls);

                ret = Ut.invoke(reference, "get");
            } else {
                logger.warn(Info.INFIX_IMPL, infixCls.getName(), Infix.class.getName());
            }
        } else {
            logger.warn(Info.INFIX_NULL, pluginKey, field.getName(), field.getDeclaringClass().getName());
        }
        return ret;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy