| 1 | package com.hammurapi.extract; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.ServiceLoader; |
| 5 | |
| 6 | import com.hammurapi.extract.scripting.ScriptingExtractorFactory; |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Extractor factory service. Creates scripted extractors and predicates. |
| 11 | * The service is discoverable using the Java Service Loader framework. |
| 12 | * @author Pavel Vlasov. |
| 13 | * |
| 14 | */ |
| 15 | public interface ExtractorFactory { |
| 16 | |
| 17 | /** |
| 18 | * Binding for context object. |
| 19 | */ |
| 20 | String CONTEXT_BINDING = "eCtx"; |
| 21 | |
| 22 | /** |
| 23 | * Binding for arguments arrray if parameter names are not provided. |
| 24 | */ |
| 25 | String ARGS_BINDING = "args"; |
| 26 | |
| 27 | /** |
| 28 | * This instance uses the service loading framework to discover |
| 29 | * extractor factory providers. It iterates over the providers and |
| 30 | * invokes provider's createExtractor() methods until it gets not null |
| 31 | * result which is returned from the method. |
| 32 | * If there are no providers supporting given language, then null is returned. |
| 33 | */ |
| 34 | ExtractorFactory INSTANCE = new ExtractorFactory() { |
| 35 | |
| 36 | private ScriptingExtractorFactory sef = new ScriptingExtractorFactory(); |
| 37 | |
| 38 | public <T, V, C> Extractor<T, V, C> createExtractor(String language, String code, String[] parameterNames, Class<T>[] parameterTypes, Class<V> valueType, Class<C> contextType, ClassLoader classLoader) { |
| 39 | ServiceLoader<ExtractorFactory> sl = classLoader==null ? ServiceLoader.load(ExtractorFactory.class) : ServiceLoader.load(ExtractorFactory.class, classLoader); |
| 40 | Iterator<ExtractorFactory> efi = sl.iterator(); |
| 41 | while (efi.hasNext()) { |
| 42 | Extractor<T, V, C> ret = efi.next().createExtractor(language, code, parameterNames, parameterTypes, valueType, contextType, classLoader); |
| 43 | if (ret!=null) { |
| 44 | return ret; |
| 45 | } |
| 46 | } |
| 47 | return sef.createExtractor(language, code, parameterNames, parameterTypes, valueType, contextType, classLoader); |
| 48 | } |
| 49 | |
| 50 | public <T, C> Predicate<T, C> createPredicate(String language, String code, String[] parameterNames, Class<T>[] parameterTypes, Class<C> contextType, ClassLoader classLoader) { |
| 51 | ServiceLoader<ExtractorFactory> sl = classLoader==null ? ServiceLoader.load(ExtractorFactory.class) : ServiceLoader.load(ExtractorFactory.class, classLoader); |
| 52 | Iterator<ExtractorFactory> efi = sl.iterator(); |
| 53 | while (efi.hasNext()) { |
| 54 | Predicate<T, C> ret = efi.next().createPredicate(language, code, parameterNames, parameterTypes, contextType, classLoader); |
| 55 | if (ret!=null) { |
| 56 | return ret; |
| 57 | } |
| 58 | } |
| 59 | return sef.createPredicate(language, code, parameterNames, parameterTypes, contextType, classLoader); |
| 60 | } |
| 61 | |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Creates extractor. |
| 66 | * @param <T> Source objects type. |
| 67 | * @param <V> Value type. |
| 68 | * @param language Extractor language. |
| 69 | * @param code Extractor code. |
| 70 | * @param parameterTypes Extractor parameter types. |
| 71 | * @param valueType Extractor value type. If this type is Boolean then the factory |
| 72 | * shall return Predicate. |
| 73 | * @param classLoader Class loader to use for script evaluation. |
| 74 | * @return Extractor or null if this factory doesn't support given language. |
| 75 | */ |
| 76 | <T, V, C> Extractor<T, V, C> createExtractor(String language, String code, String[] parameterNames, Class<T>[] parameterTypes, Class<V> valueType, Class<C> contextType, ClassLoader classLoader); |
| 77 | |
| 78 | /** |
| 79 | * Creates extractor. |
| 80 | * @param <T> Source objects type. |
| 81 | * @param language Extractor language. |
| 82 | * @param code Extractor code. |
| 83 | * @param parameterTypes Extractor parameter types. |
| 84 | * @param valueType Extractor value type. If this type is Boolean then the factory |
| 85 | * shall return Predicate. |
| 86 | * @param classLoader Class loader to use for script evaluation. |
| 87 | * @return Extractor or null if this factory doesn't support given language. |
| 88 | */ |
| 89 | <T, C> Predicate<T, C> createPredicate(String language, String code, String[] parameterNames, Class<T>[] parameterTypes, Class<C> contextType, ClassLoader classLoader); |
| 90 | } |