mondrianproperties.java

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

JAVA
534
字号
/*
// $Id: //open/mondrian/src/main/mondrian/olap/MondrianProperties.java#34 $
// 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) 2001-2005 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jhyde, 22 December, 2002
*/
package mondrian.olap;

import org.apache.log4j.Logger;
import org.eigenbase.util.property.*;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

/**
 * <code>MondrianProperties</code> contains the properties which determine the
 * behavior of a mondrian instance.
 *
 * <p>There is a method for property valid in a
 * <code>mondrian.properties</code> file. Although it is possible to retrieve
 * properties using the inherited {@link Properties#getProperty(String)}
 * method, we recommend that you use methods in this class.
 *
 * @author jhyde
 * @since 22 December, 2002
 * @version $Id: //open/mondrian/src/main/mondrian/olap/MondrianProperties.java#34 $
 */
public class MondrianProperties extends TriggerableProperties {
    private static final Logger LOGGER =
            Logger.getLogger(MondrianProperties.class);

    /**
     * Properties, drawn from {@link System#getProperties}, plus the contents
     * of "mondrian.properties" if it exists. A singleton.
     */
    private static MondrianProperties instance;
    private static final String mondrianDotProperties = "mondrian.properties";
    private static final String buildDotProperties = "build.properties";


    public static synchronized MondrianProperties instance() {
        if (instance == null) {
            instance = new MondrianProperties();
            instance.populate(null);
        }
        return instance;
    }

    private int populateCount;

    public MondrianProperties() {
    }

    public boolean triggersAreEnabled() {
        return EnableTriggers.get();
    }

    /**
     * Loads this property set from: the file "mondrian.properties" (if it
     * exists); the "mondrian.properties" in the JAR (if we're in a servlet);
     * and from the system properties.
     *
     * @param servletContext May be null
     */
    public void populate(ServletContext servletContext) {
        // Read properties file "mondrian.properties", if it exists.
        File file = new File(mondrianDotProperties);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("populate: file=" +
                file.getAbsolutePath() +
                " exists=" +
                file.exists()
                );
        }
        if (file.exists()) {
            try {
                URL url = Util.toURL(file);
                load(url);

                // For file-based installations (i.e. testing), load any overrides from
                // build.properties.
                file = new File(buildDotProperties);

                if (file.exists()) {
                    url = Util.toURL(file);
                    load(url);
                }
            } catch (MalformedURLException e) {
                LOGGER.error("Mondrian: file '"
                    + file.getAbsolutePath()
                    + "' could not be loaded ("
                    + e
                    + ")");
            }
        } else if (populateCount == 0 && false) {
            LOGGER.warn("Mondrian: Warning: file '"
                + file.getAbsolutePath()
                + "' not found");
        }

        // If we're in a servlet, read "mondrian.properties" from WEB-INF
        // directory.
        if (servletContext != null) {
            try {
                final URL resourceUrl = servletContext.getResource(
                        "/WEB-INF/" + mondrianDotProperties);
                if (resourceUrl != null) {
                    load(resourceUrl);
                } else if (populateCount == 0 && false) {
                    LOGGER.warn("Mondrian: Warning: servlet resource '"
                        + mondrianDotProperties
                        + "' not found");
                }
            } catch (MalformedURLException e) {
                LOGGER.error("Mondrian: '" + mondrianDotProperties
                    + "' could not be loaded from servlet context ("
                    + e
                    + ")");
            }
        }
        // copy in all system properties which start with "mondrian."
        int count = 0;
        for (Enumeration keys = System.getProperties().keys();
                keys.hasMoreElements(); ) {
            String key = (String) keys.nextElement();
            String value = System.getProperty(key);
            if (key.startsWith("mondrian.")) {
                // NOTE: the super allows us to bybase calling triggers
                // Is this the correct behavior?
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("populate: key=" + key + ", value=" + value);
                }
                super.setProperty(key, value);
                count++;
            }
        }
        if (populateCount++ == 0) {
            LOGGER.info("Mondrian: loaded "
                + count
                + " system properties");
        }
    }

    /**
     * Tries to load properties from a URL. Does not fail, just prints success
     * or failure to log.
     */
    private void load(final URL url) {
        try {
            load(url.openStream());
            if (populateCount == 0) {
                LOGGER.info("Mondrian: properties loaded from '"
                    + url
                    + "'");
            }
        } catch (IOException e) {
            LOGGER.error("Mondrian: error while loading properties "
                + "from '"
                + url
                + "' ("
                + e
                + ")");
        }
    }

    /**
     * Maximum number of simultaneous queries the system will allow.
     */
    public final IntegerProperty QueryLimit = new IntegerProperty(
            this, "mondrian.query.limit", 40);

    /**
     * Property which controls the amount of tracing displayed.
     */
    public final IntegerProperty TraceLevel = new IntegerProperty(
            this, "mondrian.trace.level");

    /**
     * Property containing the name of the file to which tracing is to be
     * written.
     */
    public final StringProperty DebugOutFile = new StringProperty(
            this, "mondrian.debug.out.file", null);

    /**
     * Property containing a list of JDBC drivers to load automatically.
     * Must be a comma-separated list of class names, and the classes must be
     * on the class path.
     */
    public final StringProperty JdbcDrivers = new StringProperty(
            this,
            "mondrian.jdbcDrivers",
            "sun.jdbc.odbc.JdbcOdbcDriver," +
            "org.hsqldb.jdbcDriver," +
            "oracle.jdbc.OracleDriver," +
            "com.mysql.jdbc.Driver");

    /**
     * Property which, if set to a value greatert than zero, limits the maximum
     * size of a result set.
     */
    public final IntegerProperty ResultLimit = new IntegerProperty(
            this, "mondrian.result.limit", 0);

    /** @deprecated obsolete */
    public final IntegerProperty CachePoolCostLimit = new IntegerProperty(
            this, "mondrian.rolap.CachePool.costLimit", 10000);

    /** @deprecated obsolete */
    public final BooleanProperty PrintCacheablesAfterQuery =
            new BooleanProperty(
                    this, "mondrian.rolap.RolapResult.printCacheables");

    /** @deprecated obsolete */
    public final BooleanProperty getFlushAfterQuery = new BooleanProperty(
            this, "mondrian.rolap.RolapResult.flushAfterEachQuery");

    // mondrian.test properties

    /**
     * Property which determines which tests are run.
     * This is a regular expression as defined by
     * {@link java.util.regex.Pattern}.
     * If this property is specified, only tests whose names match the pattern
     * in its entirety will be run.
     *
     * @see #TestClass
     */
    public final StringProperty TestName = new StringProperty(
            this, "mondrian.test.Name", null);

    /**
     * Property which determines which test class to run.
     * This is the name of the class which either implements
     * <code>junit.framework.Test</code> or has a method
     * <code>public [static] junit.framework.Test suite()</code>.
     *
     * @see #TestName
     */
    public final StringProperty TestClass = new StringProperty(
            this, "mondrian.test.Class", null);

    /**
     * Property containing the connect string which regresssion tests should
     * use to connect to the database.
     * Format is specified in {@link Util#parseConnectString(String)}.
     */
    public final StringProperty TestConnectString = new StringProperty(
            this, "mondrian.test.connectString", null);


    // miscellaneous

    /**
     * Property containing the JDBC URL of the FoodMart database.
     * The default value is to connect to an ODBC data source called
     * "MondrianFoodMart".

⌨️ 快捷键说明

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