/*
 *  Copyright 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 therian;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.el.CompositeELResolver;
import javax.el.ELContext;
import javax.el.ELContextEvent;
import javax.el.ELContextListener;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;

import org.apache.commons.functor.UnaryProcedure;
import org.apache.commons.functor.core.collection.FilteredIterable;
import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;

import therian.uelbox.ELContextWrapper;
import therian.uelbox.SimpleELContext;

/**
 * Therian entry point.
 */
public class Therian {
    private static final TherianModule DEFAULT_MODULE = TherianModule.create().withOperators(Operators.standard());

    private static final Therian STANDARD = Therian.usingModules(DEFAULT_MODULE);

    private final TherianModule[] modules;
    private final List<Operator<?>> operators = new ArrayList<Operator<?>>();
    private final List<ELResolver> elResolvers = new ArrayList<ELResolver>();

    private Therian(TherianModule... modules) {
        this.modules = modules == null ? new TherianModule[0] : modules;

        for (TherianModule module : this.modules) {
            Collections.addAll(operators, module.getOperators());
            Collections.addAll(elResolvers, module.getElResolvers());
        }
        Collections.sort(operators, Operators.comparator());
        IteratorToGeneratorAdapter.adapt(operators.iterator()).run(new UnaryProcedure<Operator<?>>() {

            public void run(Operator<?> obj) {
                Operators.validateImplementation(obj);
            }
        });
    }

    Iterable<Operator<?>> getOperators() {
        return operators;
    }

    public TherianContext context() {
        return contextFor(new SimpleELContext());
    }

    public TherianContext contextFor(ELContext wrapped) {
        TherianContext result = new TherianContext(new ELContextWrapper(wrapped) {

            @Override
            protected ELResolver wrap(ELResolver elResolver) {
                final CompositeELResolver compositeResolver = new CompositeELResolver();
                for (TherianModule module : modules) {
                    for (ELResolver configuredELResolver : module.getElResolvers()) {
                        compositeResolver.add(configuredELResolver);
                    }
                }
                compositeResolver.add(elResolver);
                return compositeResolver;
            }
        });
        result.putContext(Therian.class, this);
        ExpressionFactory expressionFactory = result.getTypedContext(ExpressionFactory.class);
        if (expressionFactory == null) {
            result.putContext(ExpressionFactory.class, ExpressionFactory.newInstance());
        }
        ELContextEvent event = new ELContextEvent(result);
        for (TherianModule module : modules) {
            for (ELContextListener listener : module.getElContextListeners()) {
                listener.contextCreated(event);
            }
        }
        return result;
    }

    public boolean supports(Operation<?> operation) {
        return FilteredIterable.of(getOperators()).retain(Operators.supporting(operation)).iterator().hasNext();
    }

    public static Therian usingModules(TherianModule... modules) {
        return new Therian(modules);
    }

    public static Therian standard() {
        return STANDARD;
    }

}

@@ -711,16 +711,48 @@
 il.List;
+%0Aimport java.util.ServiceLoader;
 %0A%0Aimport
@@ -1167,24 +1167,66 @@
 extWrapper;%0A
+import therian.uelbox.IterableELResolver;%0A
 import theri
@@ -1422,16 +1422,67 @@
 ndard())
+%0A        .withELResolvers(new IterableELResolver())
 ;%0A%0A    p
@@ -1557,16 +1557,67 @@
 MODULE);
+%0A    private static Therian usingDiscoveredModules;
 %0A%0A    pr
@@ -4023,32 +4023,762 @@
 sNext();%0A    %7D%0A%0A
+    /**%0A     * Return an instance configured as %7B@link Therian#STANDARD%7D +%0A     * %7B@link TherianModule%7Ds discovered using the %7B@link ServiceLoader%7D%0A     * mechanism.%0A     * %0A     * @return Therian%0A     */%0A    public static synchronized Therian usingDiscoveredModules() %7B%0A        if (usingDiscoveredModules == null) %7B%0A            final List%3CTherianModule%3E modules = new ArrayList%3CTherianModule%3E();%0A            modules.add(DEFAULT_MODULE);%0A            for (TherianModule module : ServiceLoader.load(TherianModule.class)) %7B%0A                modules.add(module);%0A            %7D%0A            usingDiscoveredModules = new Therian(modules.toArray(new TherianModule%5Bmodules.size()%5D));%0A        %7D%0A        return usingDiscoveredModules;%0A    %7D%0A%0A
     public stati

use IterableELResolver; add option for Therian with default module + modules discovered by java.util.ServiceLoader