    001package com.hammurapi.extract.scripting;
    002
    003import javax.script.ScriptEngineFactory;
    004import javax.script.ScriptEngineManager;
    005
    006import com.hammurapi.extract.Extractor;
    007import com.hammurapi.extract.ExtractorFactory;
    008import com.hammurapi.extract.Predicate;
    009
    010public class ScriptingExtractorFactory implements ExtractorFactory {
    011
    012        public <T, V, C> Extractor<T, V, C> createExtractor(
    013                        String language,
    014                        String code,
    015                        String[] parameterNames,
    016                        Class<T>[] parameterTypes,
    017                        Class<V> valueType,
    018                        Class<C> contextType,
    019                        ClassLoader classLoader) {
    020
    021                ScriptEngineManager sem = new ScriptEngineManager(classLoader==null ? contextType.getClassLoader() : classLoader);
    022                for (ScriptEngineFactory sef: sem.getEngineFactories()) {
    023                        if (sef.getLanguageName().equals(language)) {
    024                                ScriptingExtractor<T, V, C> scriptingExtractor = new ScriptingExtractor<T, V, C>(code, sef, parameterNames);
    025                                return scriptingExtractor;
    026                        }
    027                }
    028
    029                // Trying engine names
    030                for (ScriptEngineFactory sef: sem.getEngineFactories()) {
    031                        for (String en: sef.getNames()) {
    032                                if (en.equals(language)) {
    033                                        ScriptingExtractor<T, V, C> scriptingExtractor = new ScriptingExtractor<T, V, C>(code, sef, parameterNames);
    034                                        return scriptingExtractor;
    035                                }
    036                        }
    037                }
    038
    039                return null;
    040        }
    041
    042        public <T, C> Predicate<T, C> createPredicate(
    043                        String language,
    044                        String code,
    045                        String[] parameterNames,
    046                        Class<T>[] parameterTypes,
    047                        Class<C> contextType,
    048                        ClassLoader classLoader) {
    049
    050                ScriptEngineManager sem = new ScriptEngineManager(classLoader==null ? contextType.getClassLoader() : classLoader);
    051                for (ScriptEngineFactory sef: sem.getEngineFactories()) {
    052                        if (sef.getLanguageName().equals(language)) {
    053                                ScriptingPredicate<T, C> scriptingPredicate = new ScriptingPredicate<T, C>(code, sef, parameterNames);
    054                                return scriptingPredicate;
    055                        }
    056                }
    057
    058                // Trying engine names
    059                for (ScriptEngineFactory sef: sem.getEngineFactories()) {
    060                        for (String en: sef.getNames()) {
    061                                if (en.equals(language)) {
    062                                        ScriptingPredicate<T, C> scriptingPredicate = new ScriptingPredicate<T, C>(code, sef, parameterNames);
    063                                        return scriptingPredicate;
    064                                }
    065                        }
    066                }
    067
    068                return null;
    069        }
    070
    071
    072}