rolapschema.java

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

JAVA
1,346
字号
                    String catalogStr = null;
                    String md5Bytes = null;
                    try {
                        catalogStr = readURL(catalogName);
                        md5Bytes = encodeMD5(catalogStr);

                    } catch (Exception ex) {
                        // Note, can not throw an Exception from this method
                        // but just to show that all is not well in Mudville
                        // we print stack trace (for now - better to change
                        // method signature and throw).
                        ex.printStackTrace();
                    }
                    if (md5Bytes != null) {
                        SoftReference ref =
                            (SoftReference) mapUrlToSchema.get(md5Bytes);
                        if (ref != null) {
                            schema = (RolapSchema) ref.get();
                            if (schema == null) {
                                // clear out the reference since schema is null
                                mapUrlToSchema.remove(key);
                                mapUrlToSchema.remove(md5Bytes);
                            }
                        }
                    }

                    if ((schema == null) ||
                        md5Bytes == null ||
                        schema.md5Bytes == null ||
                        ! schema.md5Bytes.equals(md5Bytes)) {

                        schema = new RolapSchema(key,
                                                 md5Bytes,
                                                 catalogName,
                                                 catalogStr,
                                                 connectInfo,
                                                 dataSource);

                        SoftReference ref = new SoftReference(schema);
                        if (md5Bytes != null) {
                            mapUrlToSchema.put(md5Bytes, ref);
                        }
                        mapUrlToSchema.put(key, ref);

                        if (LOGGER.isDebugEnabled()) {
                            String msg = "Pool.get: create schema \"" +
                                catalogName +
                                "\" with MD5";
                            LOGGER.debug(msg);
                        }

                    } else if (LOGGER.isDebugEnabled()) {
                        String msg = "Pool.get: schema \"" +
                            catalogName +
                            "\" exists already with MD5";
                        LOGGER.debug(msg);
                    }

                } else {
                    SoftReference ref = (SoftReference) mapUrlToSchema.get(key);
                    if (ref != null) {
                        schema = (RolapSchema) ref.get();
                        if (schema == null) {
                            // clear out the reference since schema is null
                            mapUrlToSchema.remove(key);
                        }
                    }

                    if (schema == null) {
                        schema = new RolapSchema(key,
                                                 catalogName,
                                                 connectInfo,
                                                 dataSource);

                        mapUrlToSchema.put(key, new SoftReference(schema));

                        if (LOGGER.isDebugEnabled()) {
                            String msg = "Pool.get: create schema \"" +
                                catalogName +
                                "\"";
                            LOGGER.debug(msg);
                        }

                    } else if (LOGGER.isDebugEnabled()) {
                        String msg = "Pool.get: schema \"" +
                            catalogName +
                            "\" exists already ";
                        LOGGER.debug(msg);
                    }

                }
            }
            return schema;
        }

        synchronized void remove(final String catalogName,
                                 final String connectionKey,
                                 final String jdbcUser,
                                 final String dataSourceStr) {
            final String key = makeKey(catalogName,
                                       connectionKey,
                                       jdbcUser,
                                       dataSourceStr);
            if (LOGGER.isDebugEnabled()) {
                String msg = "Pool.remove: schema \"" +
                     catalogName +
                    "\" and datasource string \"" +
                    dataSourceStr +
                    "\"";
                LOGGER.debug(msg);
            }

            remove(key);
        }
        synchronized void remove(final String catalogName,
                                 final DataSource dataSource) {
            final String key = makeKey(catalogName,
                                       dataSource);
            if (LOGGER.isDebugEnabled()) {
                String msg = "Pool.remove: schema \"" +
                    catalogName +
                    "\" and datasource object";
                LOGGER.debug(msg);
            }

            remove(key);
        }
        private void remove(String key) {
            SoftReference ref = (SoftReference) mapUrlToSchema.get(key);
            if (ref != null) {
                RolapSchema schema = (RolapSchema) ref.get();
                if ((schema != null) && (schema.md5Bytes != null)) {
                    mapUrlToSchema.remove(schema.md5Bytes);
                }
            }
            mapUrlToSchema.remove(key);
        }

        synchronized void clear() {
            if (LOGGER.isDebugEnabled()) {
                String msg = "Pool.clear: clearing all RolapSchemas";
                LOGGER.debug(msg);
            }

            mapUrlToSchema.clear();
        }

        /**
         * This returns an iterator over a copy of the RolapSchema's container.
         *
         * @return Iterator over RolapSchemas.
         */
        synchronized Iterator getRolapSchemas() {
            List list = new ArrayList();
            for (Iterator it = mapUrlToSchema.values().iterator();
                    it.hasNext(); ) {
                SoftReference ref = (SoftReference) it.next();
                RolapSchema schema = (RolapSchema) ref.get();
                // Schema is null if already garbage collected
                if (schema != null) {
                    list.add(schema);
                } else {
                    // We will remove the stale reference
                    try {
                        it.remove();
                    } catch (Exception ex) {
                        // Should not happen, so
                        // warn but otherwise ignore
                        LOGGER.warn(ex);
                    }
                }
            }
            return list.iterator();
        }
        synchronized boolean contains(RolapSchema rolapSchema) {
            return mapUrlToSchema.containsKey(rolapSchema.key);
        }


        /**
         * Creates a key with which to identify a schema in the cache.
         */
        private static String makeKey(final String catalogName,
                                      final String connectionKey,
                                      final String jdbcUser,
                                      final String dataSourceStr) {
            final StringBuffer buf = new StringBuffer(100);

            appendIfNotNull(buf, catalogName);
            appendIfNotNull(buf, connectionKey);
            appendIfNotNull(buf, jdbcUser);
            appendIfNotNull(buf, dataSourceStr);

            final String key = buf.toString();
            return key;
        }
        /**
         * Creates a key with which to identify a schema in the cache.
         */
        private static String makeKey(final String catalogName,
                                      final DataSource dataSource) {
            final StringBuffer buf = new StringBuffer(100);

            appendIfNotNull(buf, catalogName);
            buf.append('.');
            buf.append("external#");
            buf.append(System.identityHashCode(dataSource));

            final String key = buf.toString();
            return key;
        }

        private static void appendIfNotNull(StringBuffer buf, String s) {
            if (s != null) {
                if (buf.length() > 0) {
                    buf.append('.');
                }
                buf.append(s);
            }
        }
    }

    public static void flushSchema(final String catalogName,
                                   final String connectionKey,
                                   final String jdbcUser,
                                   String dataSourceStr) {
        Pool.instance().remove(catalogName,
                               connectionKey,
                               jdbcUser,
                               dataSourceStr);
    }

    public static void flushSchema(final String catalogName,
                                   final DataSource dataSource) {
        Pool.instance().remove(catalogName,
                               dataSource);
    }

    public static void clearCache() {
        Pool.instance().clear();
    }
    public static Iterator getRolapSchemas() {
        return Pool.instance().getRolapSchemas();
    }
    public static boolean cacheContains(RolapSchema rolapSchema) {
        return Pool.instance().contains(rolapSchema);
    }

    public Cube lookupCube(final String cube, final boolean failIfNotFound) {
        Cube mdxCube = lookupCube(cube);
        if (mdxCube == null && failIfNotFound) {
            throw MondrianResource.instance().MdxCubeNotFound.ex(cube);
        }
        return mdxCube;
    }

    /**
     * Finds a cube called 'cube' in the current catalog, or return null if no
     * cube exists.
     */
    protected Cube lookupCube(final String cubeName) {
        return (Cube) mapNameToCube.get(
                MondrianProperties.instance().CaseSensitive.get() ?
                cubeName :
                cubeName.toUpperCase());
    }

    public List getCubesWithStar(RolapStar star) {
        List list = new ArrayList();
        for (Iterator it = mapNameToCube.values().iterator(); it.hasNext(); ) {
            RolapCube cube = (RolapCube) it.next();
            if (star == cube.getStar()) {
                list.add(cube);
            }
        }
        return list;
    }

    /**
     * Adds a cube to the cube name map.
     * @see #lookupCube(String)
     */
    protected void addCube(final Cube cube) {
        this.mapNameToCube.put(
                MondrianProperties.instance().CaseSensitive.get() ?
                cube.getName() :
                cube.getName().toUpperCase(),
                cube);
    }

    public Cube[] getCubes() {
        return (Cube[]) mapNameToCube.values().toArray(new RolapCube[0]);
    }

    public Iterator getCubeIterator() {
        return mapNameToCube.values().iterator();
    }

    public Hierarchy[] getSharedHierarchies() {
        return (RolapHierarchy[])
                mapSharedHierarchyNameToHierarchy.values().toArray(
                        new RolapHierarchy[0]);
    }

    RolapHierarchy getSharedHierarchy(final String name) {
        return (RolapHierarchy) mapSharedHierarchyNameToHierarchy.get(name);
    }

    public NamedSet getNamedSet(String name) {
        return (NamedSet) mapNameToSet.get(name);
    }

    public Role lookupRole(final String role) {
        return (Role) mapNameToRole.get(role);
    }

    public FunTable getFunTable() {
        return funTable;
    }

    /**
     * Defines a user-defined function in this table.
     *
     * <p>If the function is not valid, throws an error.
     *
     * @param name Name of the function.
     * @param className Name of the class which implements the function.
     *   The class must implement {@link mondrian.spi.UserDefinedFunction}
     *   (otherwise it is a user-error).
     */
    private void defineFunction(
            Map mapNameToUdf,
            String name,
            String className) {
        // Lookup class.
        final Class klass;
        try {

⌨️ 快捷键说明

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