rolapschema.java

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

JAVA
1,346
字号
            klass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw MondrianResource.instance().UdfClassNotFound.ex(name,
                    className);
        }
        // Find a constructor.
        Constructor constructor;
        Object[] args = {};
        // 1. Look for a constructor "public Udf(String name)".
        try {
            constructor = klass.getConstructor(new Class[] {String.class});
            if (Modifier.isPublic(constructor.getModifiers())) {
                args = new Object[] {name};
            } else {
                constructor = null;
            }
        } catch (NoSuchMethodException e) {
            constructor = null;
        }
        // 2. Otherwise, look for a constructor "public Udf()".
        if (constructor == null) {
            try {
                constructor = klass.getConstructor(new Class[] {});
                if (Modifier.isPublic(constructor.getModifiers())) {
                    args = new Object[] {};
                } else {
                    constructor = null;
                }
            } catch (NoSuchMethodException e) {
                constructor = null;
            }
        }
        // 3. Else, no constructor suitable.
        if (constructor == null) {
            throw MondrianResource.instance().UdfClassWrongIface.ex(name,
                    className, UserDefinedFunction.class.getName());
        }
        // Instantiate class.
        final UserDefinedFunction udf;
        try {
            udf = (UserDefinedFunction) constructor.newInstance(args);
        } catch (InstantiationException e) {
            throw MondrianResource.instance().UdfClassWrongIface.ex(name,
                    className, UserDefinedFunction.class.getName());
        } catch (IllegalAccessException e) {
            throw MondrianResource.instance().UdfClassWrongIface.ex(name,
                    className, UserDefinedFunction.class.getName());
        } catch (ClassCastException e) {
            throw MondrianResource.instance().UdfClassWrongIface.ex(name,
                    className, UserDefinedFunction.class.getName());
        } catch (InvocationTargetException e) {
            throw MondrianResource.instance().UdfClassWrongIface.ex(name,
                    className, UserDefinedFunction.class.getName());
        }
        // Validate function.
        validateFunction(udf);
        // Check for duplicate.
        UserDefinedFunction existingUdf =
                (UserDefinedFunction) mapNameToUdf.get(name);
        if (existingUdf != null) {
            throw MondrianResource.instance().UdfDuplicateName.ex(name);
        }
        mapNameToUdf.put(name, udf);
    }

    /**
     * Throws an error if a user-defined function does not adhere to the
     * API.
     */
    private void validateFunction(final UserDefinedFunction udf) {
        // Check that the name is not null or empty.
        final String udfName = udf.getName();
        if (udfName == null || udfName.equals("")) {
            throw Util.newInternal("User-defined function defined by class '" +
                    udf.getClass() + "' has empty name");
        }
        // It's OK for the description to be null.
        final String description = udf.getDescription();
        Util.discard(description);
        final Type[] parameterTypes = udf.getParameterTypes();
        for (int i = 0; i < parameterTypes.length; i++) {
            Type parameterType = parameterTypes[i];
            if (parameterType == null) {
                throw Util.newInternal("Invalid user-defined function '" +
                        udfName + "': parameter type #" + i +
                        " is null");
            }
        }
        // It's OK for the reserved words to be null or empty.
        final String[] reservedWords = udf.getReservedWords();
        Util.discard(reservedWords);
        // Test that the function returns a sensible type when given the FORMAL
        // types. It may still fail when we give it the ACTUAL types, but it's
        // impossible to check that now.
        final Type returnType = udf.getReturnType(parameterTypes);
        if (returnType == null) {
            throw Util.newInternal("Invalid user-defined function '" +
                    udfName + "': return type is null");
        }
        final Syntax syntax = udf.getSyntax();
        if (syntax == null) {
            throw Util.newInternal("Invalid user-defined function '" +
                    udfName + "': syntax is null");
        }
    }

    /**
     * Gets a {@link MemberReader} with which to read a hierarchy. If the
     * hierarchy is shared (<code>sharedName</code> is not null), looks up
     * a reader from a cache, or creates one if necessary.
     *
     * @synchronization thread safe
     */
    synchronized MemberReader createMemberReader(
        final String sharedName,
        final RolapHierarchy hierarchy,
        final String memberReaderClass) {

        MemberReader reader;
        if (sharedName != null) {
            reader = (MemberReader) mapSharedHierarchyToReader.get(sharedName);
            if (reader == null) {
                reader = createMemberReader(hierarchy, memberReaderClass);
                // share, for other uses of the same shared hierarchy
                if (false) mapSharedHierarchyToReader.put(sharedName, reader);
                mapSharedHierarchyNameToHierarchy.put(sharedName, hierarchy);
            } else {
//                final RolapHierarchy sharedHierarchy = (RolapHierarchy)
//                        mapSharedHierarchyNameToHierarchy.get(sharedName);
//                final RolapDimension sharedDimension = (RolapDimension)
//                        sharedHierarchy.getDimension();
//                final RolapDimension dimension =
//                    (RolapDimension) hierarchy.getDimension();
//                Util.assertTrue(
//                        dimension.getGlobalOrdinal() ==
//                        sharedDimension.getGlobalOrdinal());
            }
        } else {
            reader = createMemberReader(hierarchy, memberReaderClass);
        }
        return reader;
    }

    /**
     * Creates a {@link MemberReader} with which to read a hierarchy.
     */
    private MemberReader createMemberReader(
            final RolapHierarchy hierarchy,
            final String memberReaderClass) {

        if (memberReaderClass != null) {
            Exception e2 = null;
            try {
                Properties properties = null;
                Class clazz = Class.forName(memberReaderClass);
                Constructor constructor = clazz.getConstructor(new Class[] {
                    RolapHierarchy.class, Properties.class});
                Object o = constructor.newInstance(
                    new Object[] {hierarchy, properties});
                if (o instanceof MemberReader) {
                    return (MemberReader) o;
                } else if (o instanceof MemberSource) {
                    return new CacheMemberReader((MemberSource) o);
                } else {
                    throw Util.newInternal("member reader class " + clazz +
                                                    " does not implement " + MemberSource.class);
                }
            } catch (ClassNotFoundException e) {
                e2 = e;
            } catch (NoSuchMethodException e) {
                e2 = e;
            } catch (InstantiationException e) {
                e2 = e;
            } catch (IllegalAccessException e) {
                e2 = e;
            } catch (InvocationTargetException e) {
                e2 = e;
            }
            throw Util.newInternal(e2,
                    "while instantiating member reader '" + memberReaderClass);
        } else {
            SqlMemberSource source = new SqlMemberSource(hierarchy);

            // The following code is disabled bcause
            // counting members is too slow. The test suite
            // runs faster without this. So the optimization here
            // is not to be too clever!

            // Also, the CacheMemberReader is buggy.

            int memberCount;
            if (false) {
                memberCount = source.getMemberCount();
            } else {
                memberCount = Integer.MAX_VALUE;
            }
            int largeDimensionThreshold =
                    MondrianProperties.instance().LargeDimensionThreshold.get();

            return (memberCount > largeDimensionThreshold)
                ? new SmartMemberReader(source)
                : (MemberReader) new CacheMemberReader(source);
        }
    }

    public SchemaReader getSchemaReader() {
        return new RolapSchemaReader(defaultRole, this) {
            public Cube getCube() {
                throw new UnsupportedOperationException();
            }
        };
    }

    /**
     * Connection for purposes of parsing and validation. Careful! It won't
     * have the correct locale or access-control profile.
     */
    public RolapConnection getInternalConnection() {
        return internalConnection;
    }

    private Role createDefaultRole() {
        Role role = new Role();
        role.grant(this, Access.ALL);
        role.makeImmutable();
        return role;
    }

    private RolapStar makeRolapStar(final MondrianDef.Relation fact) {
        DataSource dataSource = getInternalConnection().getDataSource();
        RolapStar star = new RolapStar(this, dataSource, fact);
        return star;
    }

    /**
     * <code>RolapStarRegistry</code> is a registry for {@link RolapStar}s.
     */
    class RolapStarRegistry {
        private final Map stars = new HashMap();

        RolapStarRegistry() {
        }

        /**
         * Looks up a {@link RolapStar}, creating it if it does not exist.
         *
         * <p> {@link RolapStar.Table#addJoin} works in a similar way.
         */
        synchronized RolapStar getOrCreateStar(final MondrianDef.Relation fact) {
            String factTableName = fact.toString();
            RolapStar star = (RolapStar) stars.get(factTableName);
            if (star == null) {
                star = makeRolapStar(fact);
                stars.put(factTableName, star);
            }
            return star;
        }
        synchronized RolapStar getStar(final String factTableName) {
            return (RolapStar) stars.get(factTableName);
        }
        synchronized Iterator getStars() {
            return stars.values().iterator();
        }
    }

    private RolapStarRegistry rolapStarRegistry = new RolapStarRegistry();

    public RolapStarRegistry getRolapStarRegistry() {
        return rolapStarRegistry;
    }

    /**
     * Function table which contains all of the user-defined functions in this
     * schema, plus all of the standard functions.
     */
    class RolapSchemaFunctionTable extends FunTableImpl {
        private final List udfList;

        RolapSchemaFunctionTable(Collection udfs) {
            udfList = new ArrayList(udfs);
        }

        protected void defineFunctions() {
            final FunTable builtinFunTable = BuiltinFunTable.instance();
            final List reservedWords = builtinFunTable.getReservedWords();
            for (int i = 0; i < reservedWords.size(); i++) {
                String reservedWord = (String) reservedWords.get(i);
                defineReserved(reservedWord);
            }
            final List resolvers = builtinFunTable.getResolvers();
            for (int i = 0; i < resolvers.size(); i++) {
                Resolver resolver = (Resolver) resolvers.get(i);
                define(resolver);
            }
            for (int i = 0; i < udfList.size(); i++) {
                UserDefinedFunction udf = (UserDefinedFunction) udfList.get(i);
                define(new UdfResolver(udf));
            }
        }


        public List getFunInfoList() {
            return Collections.unmodifiableList(this.funInfoList);
        }
    }

    public RolapStar getStar(final String factTableName) {
      return getRolapStarRegistry().getStar(factTableName);
    }

    public Iterator getStars() {
      return getRolapStarRegistry().getStars();
    }
    public void flushRolapStarCaches() {
        for (Iterator itStars = getStars(); itStars.hasNext(); ) {
            RolapStar star = (RolapStar) itStars.next();
            // this will only flush the star's aggregate cache if
            // 1) DisableCaching is true or the star's cube has
            // cacheAggregations set to false in the schema.
            star.clearCache();
        }
    }
    public static void flushAllRolapStarCaches() {
        for (Iterator itSchemas = RolapSchema.getRolapSchemas();
                itSchemas.hasNext(); ) {

            RolapSchema schema = (RolapSchema) itSchemas.next();
            schema.flushRolapStarCaches();
        }
    }

}

// End RolapSchema.java

⌨️ 快捷键说明

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