⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jahiausermanagerroutingservice.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            t.printStackTrace();            return userList;        }        Vector resultVector = (Vector) result;        Enumeration resultEnum = resultVector.elements();        while (resultEnum.hasMoreElements()) {            Set curResult = (Set) resultEnum.nextElement();            userList.addAll(curResult);        }        return userList;    }    /**     * Find users according to a table of name=value properties. If the left     * side value is "*" for a property then it will be tested against all the     * properties. ie *=test* will match every property that starts with "test"     *     * @param providerKey key of the provider in which to search, may be     * obtained by calling getProviderList()     * @param siteID site identifier     * @param searchCriterias a Properties object that contains search criterias     * in the format name,value (for example "*"="*" or "username"="*test*") or     * null to search without criterias     *     * @return Set a set of JahiaUser elements that correspond to those     * search criterias     */    public Set searchUsers(String providerKey, int siteID,                              Properties searchCriterias) {        Class[] parameterTypes = new Class[2];        parameterTypes[0] = Integer.TYPE;        parameterTypes[1] = propertiesClass;        Object[] args = new Object[2];        args[0] = new Integer(siteID);        args[1] = searchCriterias;        Object result = null;        Vector providerList = new Vector();        providerList.add(providerKey);        try {            result = routeCall("searchUsers",                               parameterTypes,                               null,                               ROUTE_CALL_ONE,                               providerList,                               false,                               null,                               args);        } catch (Throwable t) {            t.printStackTrace();            return null;        }        return (Set) result;    }    /**     * Routes the calls to a set of providers. This method is the center of this     * routing service. It allows for a very flexible way of calling back-end     * providers by leaving the routing decision until runtime and making it     * therefore very configurable.     *     * @param methodName           the name of the method to invoke     * @param parameterTypes       an array of Class objects that specify the     *                             type of the parameters for the method. This     *                             is necessary since we cannot determine this     *                             at runtime because of primitive type     *                             encapsulation. Therefore for primitive types     *                             these must be specified here such as     *                             Integer.TYPE, Boolean.TYPE, etc in order to     *                             look up the correct method     * @param userProperties       the properties that will be used to evaluate     *                             the provider to call. This is defined by a     *                             set of rules.     * @param typeOfCall           the type of call, values are ROUTE_CALL_ONE     *                             which means only one provider will be called     *                             based on rule evaluation, ROUTE_CALL_ALL     *                             means the method will be invoked for all the     *                             providers, ROUTE_CALL_ALL_UNTIL_SUCCESS means     *                             all the providers methods will be called     *                             until the first one returns a success. A     *                             success depends on the return type. For int     *                             it means all values exception -1, for     *                             booleans it means a "true" value, and for all     *                             other object it means a non null value     * @param providersToCall      a vector of String objects that specify a     *                             list of providers to call in order. There may     *                             be zero, one or multiple providers. This     *                             parameter bypasses the criteria evaluation     *                             so be careful how you use it. It also uses     *                             the typeOfCall parameter to define the type     *                             of call, so make sure you use combinations     *                             that correspond, such as ROUTE_CALL_ONE and     *                             a single entry, ROUTE_CALL_ALL and at least     *                             one or more entries, or     *                             ROUTE_CALL_ALL_UNTIL_SUCCESS with one or     *                             more. Passing an empty vector or null will     *                             disable the forcing of the list of providers.     * @param useDifferentInstance set to true if you want to use the specified     *                             instance passed in parameter. If false it     *                             will use the provider's singleton instance.     *                             This is mostly useful if you want to be able     *                             to call static instances, or do something     *                             very special.     * @param instance             the instance of the object we're using for     *                             this call. May be null if we are calling a     *                             static method.     * @param args                 an array of Objects which contain the     *                             parameters. The primitive types must be     *                             encapsulated into objects.     *     * @return the object returned. In the case of the ROUTE_CALL_ALL this     * returns a Vector containing all the results of the calls. If there were     * any errors this is very likely to return null.     *     * @throws Throwable this exception is actually an exception that has been     * generated within the called method, and that's just being passed upwards.     */    private Object routeCall(String methodName,                             Class[] parameterTypes,                             Properties userProperties,                             int typeOfCall,                             Vector providersToCall,                             boolean useDifferentInstance,                             Object instance,                             Object[] args)    throws Throwable {        switch (typeOfCall) {            case ROUTE_CALL_ONE:                {                // we're calling only one of the provider, we must determine                // which one by using the user properties matched against the                // criteria , or by using the providersToCall parameter                JahiaUserManagerProvider providerInstance = null;                if (providersToCall != null) {                    if (providersToCall.size() >= 1) {                        Object providerItem = providersToCall.elementAt(0);                        if (providerItem instanceof String) {                            String providerKey = (String) providerItem;                                UserManagerProviderBean aProvider = (UserManagerProviderBean) providersTable.get (                                        providerKey);                            providerInstance = aProvider.getInstance();                        }                    }                }                if (providerInstance == null) {                    Enumeration criteriaEnum = criteriaList.elements();                    while (criteriaEnum.hasMoreElements()) {                        RoutingCriteria curCriteria = (RoutingCriteria) criteriaEnum.nextElement();                        if (curCriteria.matchesValues(userProperties)) {                            String providerKey = curCriteria.getDestination();                                UserManagerProviderBean curProvider = (UserManagerProviderBean) providersTable.get (                                        providerKey);                            providerInstance = curProvider.getInstance();                            break;                        }                    }                }                if (providerInstance == null) {                    // fallback, we must find at least one provider to call.                    providerInstance = defaultProviderInstance;                    if (providerInstance == null) {                        // what no default provider ?? exit immediately.                        return null;                    }                }                Object result = null;                try {                    Method methodToInvoke =                                providerInstance.getClass ().getMethod (methodName,                                        parameterTypes);                    if (useDifferentInstance) {                        result = methodToInvoke.invoke(instance, args);                    } else {                        result = methodToInvoke.invoke(providerInstance, args);                    }                } catch (NoSuchMethodException nsme) {                    nsme.printStackTrace();                    result = null;                } catch (InvocationTargetException ite) {                    ite.printStackTrace();                    result = null;                    throw ite.getTargetException();                } catch (IllegalAccessException iae) {                    iae.printStackTrace();                    result = null;                }                return result;            }            case ROUTE_CALL_ALL:                {                // we're calling all the providers                Vector results = new Vector();                Enumeration providerEnum = getProvidersToCall(providersToCall).elements();                while (providerEnum.hasMoreElements()) {                    JahiaUserManagerProvider curProvider =                        (JahiaUserManagerProvider) providerEnum.nextElement();                    Object result = null;                    try {                        Method methodToInvoke =                                    curProvider.getClass ().getMethod (methodName,                                            parameterTypes);                        if (useDifferentInstance) {                            result = methodToInvoke.invoke(instance, args);                        } else {                            result = methodToInvoke.invoke(curProvider, args);                        }                        results.add(result);                    } catch (NoSuchMethodException nsme) {                        nsme.printStackTrace();                        results = null;                        break;                    } catch (InvocationTargetException ite) {                        ite.printStackTrace();                        results = null;                        throw ite.getTargetException();                    } catch (IllegalAccessException iae) {                        iae.printStackTrace();                        results = null;                        break;                    }                }                return results;            }            case ROUTE_CALL_ALL_UNTIL_SUCCESS:                {                // we're calling the providers in order, until one returns                // a success condition (to be defined)                Enumeration providerEnum = getProvidersToCall(providersToCall).elements();                Object result = null;                while (providerEnum.hasMoreElements()) {                    JahiaUserManagerProvider curProvider =                        (JahiaUserManagerProvider) providerEnum.nextElement();                    try {                        Method methodToInvoke =                                    curProvider.getClass ().getMethod (methodName,                                            parameterTypes);                        if (useDifferentInstance) {                            result = methodToInvoke.invoke(instance, args);                        } else {                            result = methodToInvoke.invoke(curProvider, args);                        }                        Class returnType = methodToInvoke.getReturnType();                        // conditions for success are :                        // for int, all values except -1                        // for booleans, true value                        // for all other object, a non null value                        if (returnType.isPrimitive()) {                            if ("int".equals(returnType.getName())) {                                Integer resultInt = (Integer) result;                                if (resultInt.intValue() != -1) {                                    return result;                                }                            } else if ("boolean".equals(returnType.getName())) {                                Boolean resultBool = (Boolean) result;                                if (resultBool.booleanValue()) {                                    return result;                                }                            } else {                                if (result != null) {                                    return result;                                }                            }                        } else {                            if (result != null) {                                return result;                            }                        }                    } catch (NoSuchMethodException nsme) {                        nsme.printStackTrace();                        result = null;                    } catch (InvocationTargetException ite) {                        ite.printStackTrace();                        result = null;                        throw ite.getTargetException();                    } catch (IllegalAccessException iae) {                        iae.printStackTrace();                        result = null;                    }                }                return result;            }        }        return null;    }    /**     * Returns a vector of provider instances to call depending on the specified     * list of provider names.     *     * @param providersToCall a vector containing String that contain names     * of the providers to call. If no list is specified, the result list is     * composed of the internal list of providers     *     * @return a Vector of provider instances, may be empty if none was found,     * but never null. If no name list is specified, returns the internal list     * of providers     */    private Vector getProvidersToCall(Vector providersToCall) {        // we use this temporary vector in order to avoid having twice        // the same code for the dispatching...        Vector tmpProviderInstances = new Vector();        if (providersToCall != null) {            if (providersToCall.size() >= 1) {                Enumeration providerEnum = providersToCall.elements();                while (providerEnum.hasMoreElements()) {                    Object curProviderEntry = providerEnum.nextElement();

⌨️ 快捷键说明

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