defaultrules.java

来自「数据仓库展示程序」· Java 代码 · 共 433 行 · 第 1/2 页

JAVA
433
字号
/*
// $Id: //open/mondrian/src/main/mondrian/rolap/aggmatcher/DefaultRules.java#7 $
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2005-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/

package mondrian.rolap.aggmatcher;


import mondrian.olap.*;
import mondrian.rolap.RolapStar;
import mondrian.recorder.*;
import mondrian.resource.MondrianResource;

import org.apache.log4j.Logger;
import org.eigenbase.xom.*;
import org.eigenbase.xom.Parser;
import org.eigenbase.util.property.*;
import org.eigenbase.util.property.Property;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * This class is a container for the default aggregate recognition rules.
 * It is generated by parsing the default rule xml information found
 * in the {@link MondrianProperties#AggregateRules} value which normally is
 * a resource in the jar file (but can be a url).
 *
 * <p>It is a singleton since it is used to recognize tables independent of
 * database connection (each {@link mondrian.rolap.RolapSchema} uses the same
 * instance).
 *
 * @author <a>Richard M. Emberson</a>
 * @version $Id: //open/mondrian/src/main/mondrian/rolap/aggmatcher/DefaultRules.java#7 $
 */
public class DefaultRules {

    private static final Logger LOGGER = Logger.getLogger(DefaultRules.class);

    private static final MondrianResource mres = MondrianResource.instance();
    /**
     * There is a single instance of the {@link DefaultRecognizer} and the
     * {@link DefaultRules} class is a container of that instance.
     */
    public static synchronized DefaultRules getInstance() {
        if (instance == null) {
            InputStream inStream = getAggRuleInputStream();
            if (inStream  == null) {
                return null;
            }

            DefaultDef.AggRules defs = makeAggRules(inStream);

            // validate the DefaultDef.AggRules object
            ListRecorder reclists = new ListRecorder();
            try {
                defs.validate(reclists);
            } catch (RecorderException e) {
                // ignore
            }

            reclists.logWarningMessage(LOGGER);
            reclists.logErrorMessage(LOGGER);

            if (reclists.hasErrors()) {
                reclists.throwRTException();
            }


            // make sure the tag name exists
            String tag = MondrianProperties.instance().AggregateRuleTag.get();
            DefaultDef.AggRule aggrule = defs.getAggRule(tag);
            if (aggrule == null) {
                throw mres.MissingDefaultAggRule.ex(tag);
            }

            DefaultRules rules = new DefaultRules(defs);
            rules.setTag(tag);
            instance = rules;
        }
        return instance;
    }
    private static InputStream getAggRuleInputStream() {
        String aggRules = MondrianProperties.instance().AggregateRules.get();

        InputStream inStream = DefaultRules.class.getResourceAsStream(aggRules);
        if (inStream == null) {
            try {
                URL url = new URL(aggRules);
                inStream = url.openStream();
            } catch (MalformedURLException e) {
                // ignore
            } catch (IOException e) {
                // ignore
            }
        }
        if (inStream == null) {
            String msg = mres.CouldNotLoadDefaultAggregateRules.str(aggRules);
            LOGGER.warn(msg);
        }
        return inStream;
    }
    private static DefaultRules instance = null;

    static {
        // When the value of the AggregateRules property is changed, force
        // system to reload the DefaultRules.
        // There is no need to provide equals/hashCode methods for this
        // Trigger since it is a singleton and is never removed.
        Trigger trigger =
            new Trigger() {
                public boolean isPersistent() {
                    return true;
                }
                public int phase() {
                    return Trigger.PRIMARY_PHASE;
                }
                public void execute(Property property, String value) {
                    synchronized (DefaultRules.class) {
                        DefaultRules oldInstance = DefaultRules.instance;
                        DefaultRules.instance = null;

                        DefaultRules newInstance = null;
                        Exception ex = null;
                        try {
                            newInstance = DefaultRules.getInstance();
                        } catch (Exception e) {
                            ex = e;
                        }
                        if (ex != null) {
                            DefaultRules.instance = oldInstance;

                            throw new Trigger.VetoRT(ex);

                        } else if (newInstance == null) {
                            DefaultRules.instance = oldInstance;

                            String msg =
                                mres.FailedCreateNewDefaultAggregateRules.str(
                                        property.getPath(), value);
                            throw new Trigger.VetoRT(msg);

                        } else {
                            instance = newInstance;
                        }
                    }
                }
            };

        final MondrianProperties properties = MondrianProperties.instance();
        properties.AggregateRules.addTrigger(trigger);
        properties.AggregateRuleTag.addTrigger(trigger);
    }

    protected static DefaultDef.AggRules makeAggRules(final File file) {
        DOMWrapper def = makeDOMWrapper(file);
        try {
            DefaultDef.AggRules rules = new DefaultDef.AggRules(def);
            return rules;
        } catch (XOMException e) {
            throw mres.AggRuleParse.ex(file.getName(), e);
        }
    }
    protected static DefaultDef.AggRules makeAggRules(final URL url) {
        DOMWrapper def = makeDOMWrapper(url);
        try {
            DefaultDef.AggRules rules = new DefaultDef.AggRules(def);
            return rules;
        } catch (XOMException e) {
            throw mres.AggRuleParse.ex(url.toString(),e);
        }
    }
    protected static DefaultDef.AggRules makeAggRules(
                                            final InputStream inStream) {
        DOMWrapper def = makeDOMWrapper(inStream);
        try {
            DefaultDef.AggRules rules = new DefaultDef.AggRules(def);
            return rules;
        } catch (XOMException e) {
            throw mres.AggRuleParse.ex("InputStream",e);
        }
    }
    protected static DefaultDef.AggRules makeAggRules(final String text,
                                                      final String name) {
        DOMWrapper def = makeDOMWrapper(text, name);
        try {
            DefaultDef.AggRules rules = new DefaultDef.AggRules(def);
            return rules;
        } catch (XOMException e) {
            throw mres.AggRuleParse.ex(name,e);
        }
    }
    protected static DOMWrapper makeDOMWrapper(final File file) {
        try {
            return makeDOMWrapper(file.toURL());
        } catch (MalformedURLException e) {
            throw mres.AggRuleParse.ex(file.getName(),e);
        }
    }
    protected static DOMWrapper makeDOMWrapper(final URL url) {
        try {
            final Parser xmlParser = XOMUtil.createDefaultParser();
            DOMWrapper def = xmlParser.parse(url);
            return def;
        } catch (XOMException e) {
            throw mres.AggRuleParse.ex(url.toString(),e);
        }
    }
    protected static DOMWrapper makeDOMWrapper(final InputStream inStream) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?