001    /*
002    // This software is subject to the terms of the Common Public License
003    // Agreement, available at the following URL:
004    // http://www.opensource.org/licenses/cpl.html.
005    // Copyright (C) 2004-2005 TONBELLER AG
006    // All Rights Reserved.
007    // You must accept the terms of that agreement to use this software.
008    */
009    package mondrian.rolap;
010    
011    import java.util.*;
012    
013    import mondrian.olap.Exp;
014    import mondrian.olap.FunDef;
015    import mondrian.olap.NativeEvaluator;
016    
017    /**
018     * Composite of {@link RolapNative}s. Uses chain of responsibility
019     * to select the appropriate {@link RolapNative} evaluator.
020     */
021    public class RolapNativeRegistry extends RolapNative {
022    
023        private Map<String, RolapNative> nativeEvaluatorMap =
024            new HashMap<String, RolapNative>();
025    
026        public RolapNativeRegistry() {
027            super.setEnabled(true);
028    
029            /*
030             * Mondrian functions which might be evaluated natively.
031             */
032            register("NonEmptyCrossJoin".toUpperCase(), new RolapNativeCrossJoin());
033            register("CrossJoin".toUpperCase(), new RolapNativeCrossJoin());
034            register("TopCount".toUpperCase(), new RolapNativeTopCount());
035            register("Filter".toUpperCase(), new RolapNativeFilter());
036        }
037    
038        /**
039         * Returns the matching NativeEvaluator or null if <code>fun</code> can not
040         * be executed in SQL for the given context and arguments.
041         */
042        public NativeEvaluator createEvaluator(
043            RolapEvaluator evaluator, FunDef fun, Exp[] args)
044        {
045            if (!isEnabled()) {
046                return null;
047            }
048    
049            RolapNative rn = nativeEvaluatorMap.get(fun.getName().toUpperCase());
050    
051            if (rn == null) {
052                return null;
053            }
054    
055            NativeEvaluator ne = rn.createEvaluator(evaluator, fun, args);
056    
057            if (ne != null) {
058                if (listener != null) {
059                    NativeEvent e = new NativeEvent(this, ne);
060                    listener.foundEvaluator(e);
061                }
062            }
063            return ne;
064        }
065    
066        public void register(String funName, RolapNative rn) {
067            nativeEvaluatorMap.put(funName, rn);
068        }
069    
070        /** for testing */
071        void setListener(Listener listener) {
072            super.setListener(listener);
073            for (RolapNative rn : nativeEvaluatorMap.values()) {
074                rn.setListener(listener);
075            }
076        }
077    
078        /** for testing */
079        void useHardCache(boolean hard) {
080            for (RolapNative rn : nativeEvaluatorMap.values()) {
081                rn.useHardCache(hard);
082            }
083        }
084    }
085    
086    // End RolapNativeRegistry.java