/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.service.cdi.util;

import java.lang.reflect.Type;

import javax.enterprise.inject.Instance;
import javax.enterprise.inject.literal.NamedLiteral;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;

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

/**
 * @author Yuriy Movchan
 * @version 1.0, 05/05/2017
 */
public final class CdiUtil {

    private static final Logger LOG = LoggerFactory.getLogger(CdiUtil.class);

    private CdiUtil() {
    }

    public static <T> T getContextBean(BeanManager beanManager, Type type, String beanName) {
        Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(type, NamedLiteral.of(beanName)));
        if (bean == null) {
            return null;
        }

        T existingInstance = beanManager.getContext(bean.getScope()).get(bean, beanManager.createCreationalContext(bean));

        return existingInstance;
    }

    /*
     * @Deprecated public static <T> T getContextualReference(BeanManager bm,
     * Set<Bean<?>> beans, Class<?> type) { if (beans == null || beans.size() == 0)
     * { return null; }
     *
     * // If we would resolve to multiple beans then BeanManager#resolve would throw
     * an AmbiguousResolutionException Bean<?> bean = bm.resolve(beans); if (bean ==
     * null) { return null; } else { CreationalContext<?> creationalContext =
     * bm.createCreationalContext(bean); return (T) bm.getReference(bean, type,
     * creationalContext); } }
     */
    public static <T> Instance<T> instance(Class<T> clazz) {
        return CDI.current().select(clazz);
    }

    public static <T> Instance<T> instance(Class<T> clazz, String name) {
        return CDI.current().select(clazz, NamedLiteral.of(name));
    }

    public static <T> T bean(Class<T> clazz) {
        return instance(clazz).get();
    }

    public static <T> T bean(Class<T> clazz, String name) {
        return instance(clazz, name).get();
    }

    public static <T> void destroy(Class<T> clazz) {
        Instance<T> instance = instance(clazz);
        if (instance.isResolvable()) {
            instance.destroy(instance.get());
        }
    }

    public static <T> T destroy(Class<T> clazz, String name) {
        Instance<T> instance = instance(clazz, name);
        if (instance.isResolvable()) {
            T obj = instance.get();
            instance.destroy(obj);

            return obj;
        }

        return null;
    }

}

@@ -174,16 +174,56 @@
 .util;%0A%0A
+import java.lang.annotation.Annotation;%0A
 import j
@@ -245,16 +245,16 @@
 t.Type;%0A
-
 %0Aimport 
@@ -470,16 +470,59 @@
 i.CDI;%0A%0A
+import org.apache.commons.lang.ArrayUtils;%0A
 import o
@@ -862,19 +862,382 @@
 beanName
-) %7B
+, Annotation... qualifiers) %7B%0A        NamedLiteral namedLiteral = NamedLiteral.of(beanName);%0A%0A        Annotation%5B%5D allQualifiers = null;%0A        if (qualifiers == null) %7B%0A            allQualifiers = new Annotation%5B%5D %7B namedLiteral %7D;%0A        %7D else %7B%0A            allQualifiers = (Annotation%5B%5D) ArrayUtils.add(new Annotation%5B%5D %7B namedLiteral%7D, qualifiers);%0A        %7D%0A
 %0A       
@@ -1313,45 +1313,27 @@
 pe, 
-NamedLiteral.of(beanName)));%0A        
+allQualifiers));%0A%09%09
 if (
@@ -1348,28 +1348,19 @@
 null) %7B%0A
-            
+%09%09%09
 return n
@@ -1364,24 +1364,18 @@
 n null;%0A
-        
+%09%09
 %7D%0A%0A     

Allow to get bean by additional qualifiers