package hu.inbuss.thymeleaf.cdi;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.AmbiguousResolutionException;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.enterprise.util.AnnotationLiteral;
import org.thymeleaf.context.IContext;
import org.thymeleaf.context.LazyContextVariable;

/**
 * @author PÁLFALVI Tamás &lt;tamas.palfalvi@inbuss.hu&gt;
 */
public class CDIContext implements IContext {
    private final Locale locale;
    private final BeanManager beanManager;
    private final Set<String> names;

    protected CDIContext(final Locale locale, final BeanManager beanManager) {
        this.locale = locale;
        this.beanManager = beanManager;
        names = new HashSet<String>();
        final Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() {});
        for (final Bean<?> b : beans) {
            final String name = b.getName();
            if (name != null)
                names.add(name);
        }
    }

    @Override
    public Locale getLocale() {
        return locale;
    }

    @Override
    public boolean containsVariable(final String name) {
        return names.contains(name);
    }

    @Override
    public Set<String> getVariableNames() {
        return names;
    }

    @Override
    public Object getVariable(String name) {
        final Set<Bean<?>> beans = beanManager.getBeans(name);
        if (beans.isEmpty())
            return null;

        return new LazyContextVariable<Object>() {
            @Override
            protected Object loadValue() {
                try {
                    final Bean<?> bean = beanManager.resolve(beans);
                    final CreationalContext<?> cctx = beanManager.createCreationalContext(bean);
                    return beanManager.getReference(bean, Object.class, cctx);
                } catch (final AmbiguousResolutionException are) {
                    return null;
                }
            }
        };
    }
}

@@ -345,48 +345,8 @@
 er;%0A
-import javax.enterprise.inject.spi.CDI;%0A
 impo

remove unused import