rolaputil.java

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

JAVA
443
字号
/*
// $Id: //open/mondrian/src/main/mondrian/rolap/RolapUtil.java#27 $
// 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.
// (C) Copyright 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, 2001
*/

package mondrian.rolap;
import mondrian.olap.MondrianProperties;
import mondrian.olap.Util;
import mondrian.resource.MondrianResource;

import org.apache.log4j.Logger;
import java.io.*;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

/**
 * Utility methods for classes in the <code>mondrian.rolap</code> package.
 *
 * @author jhyde
 * @since 22 December, 2001
 * @version $Id: //open/mondrian/src/main/mondrian/rolap/RolapUtil.java#27 $
 **/
public class RolapUtil {

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


    static final RolapMember[] emptyMemberArray = new RolapMember[0];
    public static PrintWriter debugOut = null;
    public static Boolean produceDebugOut = null;
    private static Semaphore querySemaphore;

    /**
     * Special cell value indicates that the value is not in cache yet.
     */
    public static final RuntimeException valueNotReadyException =
            new RuntimeException("value not ready");

    /**
     * Special value represents a null key.
     */
    public static final Object sqlNullValue = new Object() {
        public boolean equals(Object o) {
            return o == this;
        }
        public int hashCode() {
            return super.hashCode();
        }
        public String toString() {
            return "null";
        }
    };

    /**
     * Names of classes of drivers we've loaded (or have tried to load).
     * @synchronization Lock the {@link RolapConnection} class.
     */
    private static final HashSet loadedDrivers = new HashSet();

    static final void add(List list, Object[] array) {
        for (int i = 0; i < array.length; i++) {
            list.add(array[i]);
        }
    }

    static final RolapMember[] toArray(List v) {
        return v.isEmpty()
            ? emptyMemberArray
            : (RolapMember[]) v.toArray(new RolapMember[v.size()]);
    }

    static RolapMember lookupMember(
            MemberReader reader,
            String[] uniqueNameParts,
            boolean failIfNotFound) {
        RolapMember member = null;
        for (int i = 0; i < uniqueNameParts.length; i++) {
            String name = uniqueNameParts[i];
            List children;
            if (member == null) {
                children = reader.getRootMembers();
            } else {
                children = new ArrayList();
                reader.getMemberChildren(member, children);
                member = null;
            }
            for (int j = 0, n = children.size(); j < n; j++) {
                RolapMember child = (RolapMember) children.get(j);
                if (child.getName().equals(name)) {
                    member = child;
                    break;
                }
            }
            if (member == null) {
                break;
            }
        }
        if (member == null && failIfNotFound) {
            throw MondrianResource.instance().MdxCantFindMember.ex(Util.implode(uniqueNameParts));
        }
        return member;
    }

    /**
     * Adds an object to the end of an array.  The resulting array is of the
     * same type (e.g. <code>String[]</code>) as the input array.
     **/
    static Object[] addElement(Object[] a, Object o) {
        Class clazz = a.getClass().getComponentType();
        Object[] a2 = (Object[]) Array.newInstance(clazz, a.length + 1);
        System.arraycopy(a, 0, a2, 0, a.length);
        a2[a.length] = o;
        return a2;
    }

    /**
     * Adds an array to the end of an array.  The resulting array is of the
     * same type (e.g. <code>String[]</code>) as the input array.
     */
    static Object[] addElements(Object[] a, Object[] b) {
        Class clazz = a.getClass().getComponentType();
        Object[] c = (Object[]) Array.newInstance(clazz, a.length + b.length);
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    /**
     * Enables tracing if the {@link MondrianProperties#TraceLevel} property
     * is greater than 0 and a debug output file
     * ({@link MondrianProperties#DebugOutFile}) is configured.
     */
    public static void checkTracing() {
        if (produceDebugOut == null) {
            int trace = MondrianProperties.instance().TraceLevel.get();
            if (trace > 0) {
                String debugOutFile =
                        MondrianProperties.instance().DebugOutFile.get();
                if (debugOutFile != null) {
                    File f;
                    try {
                        f = new File(debugOutFile);
                        setDebugOut(new PrintWriter(new FileOutputStream(f), true));
                    } catch (Exception e) {
                        setDebugOut(new PrintWriter(System.out, true));
                    }
                } else {
                    setDebugOut(new PrintWriter(System.out, true));
                }
                produceDebugOut = Boolean.TRUE;
            } else {
                produceDebugOut = Boolean.FALSE;
            }
        }
    }

    /**
     * redirect debug output to another PrintWriter
     * @param pw
     */
    static public void setDebugOut( PrintWriter pw) {
        debugOut = pw;
    }

    /**
     * Executes a query, printing to the trace log if tracing is enabled.
     * If the query fails, it throws the same {@link SQLException}, and closes
     * the result set. If it succeeds, the caller must close the returned
     * {@link ResultSet}.
     */
    public static ResultSet executeQuery(
            Connection jdbcConnection,
            String sql,
            String component)
            throws SQLException {
        checkTracing();
        getQuerySemaphore().enter();
        Statement statement = null;
        ResultSet resultSet = null;
        String status = "failed";
        if (produceDebugOut == Boolean.TRUE) {
            RolapUtil.debugOut.print(
                component + ": executing sql [" + sql + "]");
            RolapUtil.debugOut.flush();
        }
        try {
            final long start = System.currentTimeMillis();
            statement = jdbcConnection.createStatement();
            resultSet = statement.executeQuery(sql);
            final long end = System.currentTimeMillis();
            final long elapsed = end - start;
            Util.addDatabaseTime(elapsed);
            status = ", " + elapsed + " ms";
            return resultSet;
        } catch (SQLException e) {
            status = ", failed (" + e + ")";
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e2) {
                // ignore
            }
            throw (SQLException) e.fillInStackTrace();
        } finally {
            if (produceDebugOut == Boolean.TRUE) {
                RolapUtil.debugOut.println(status);
            }
            if (LOGGER.isDebugEnabled()) {
            	LOGGER.debug(component + ": executing sql [" + sql + "]" + status);
            }

⌨️ 快捷键说明

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