axisdescription.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 553 行 · 第 1/2 页

JAVA
553
字号

    /**
     * Applies the policies on the Description Hierarchy recursively.
     *
     * @throws AxisFault an error occurred applying the policy
     */
    public void applyPolicy() throws AxisFault {

        AxisConfiguration configuration = getAxisConfiguration();
        if (configuration == null) {
            return; // CHECKME: May be we need to throw an Exception ??
        }

        Policy effPolicy = getApplicablePolicy(this);

        if (effPolicy != null) {

            /*
             * for the moment we consider policies with only one alternative. If
             * the policy contains multiple alternatives only the first
             * alternative will be considered.
             */
            Iterator iterator = effPolicy.getAlternatives();
            if (!iterator.hasNext()) {
                throw new AxisFault(
                        "Policy doesn't contain any policy alternatives");
            }

            List assertionList = (List) iterator.next();

            Assertion assertion;
            String namespaceURI;

            List moduleList;

            List namespaceList = new ArrayList();
            List modulesToEngage = new ArrayList();

            for (Iterator assertions = assertionList.iterator(); assertions
                    .hasNext();) {
                assertion = (Assertion) assertions.next();
                namespaceURI = assertion.getName().getNamespaceURI();

                moduleList = configuration
                        .getModulesForPolicyNamesapce(namespaceURI);

                if (moduleList == null) {
                    log.debug("can't find any module to process "
                            + assertion.getName() + " type assertions");
                    continue;
                }

                if (!canSupportAssertion(assertion, moduleList)) {
                    throw new AxisFault("atleast one module can't support "
                            + assertion.getName());
                }

                if (!namespaceList.contains(namespaceURI)) {
                    namespaceList.add(namespaceURI);
                    modulesToEngage.addAll(moduleList);
                }
            }

            /*
             * FIXME We need to disengage any modules that are already engaged
             * *but* has nothing to do with the policy to apply
             */

            engageModulesToAxisDescription(modulesToEngage, this);

        }

        AxisDescription child;

        for (Iterator children = getChildren(); children.hasNext();) {
            child = (AxisDescription) children.next();
            child.applyPolicy();
        }
    }

    private boolean canSupportAssertion(Assertion assertion, List moduleList) {

        AxisModule axisModule;
        Module module;

        for (Iterator iterator = moduleList.iterator(); iterator.hasNext();) {
            axisModule = (AxisModule) iterator.next();
            // FIXME is this step really needed ??
            // Shouldn't axisMoudle.getModule always return not-null value ??
            module = axisModule.getModule();

            if (!(module == null || module.canSupportAssertion(assertion))) {
                log.debug(axisModule.getName() + " says it can't support " + assertion.getName());
                return false;
            }
        }

        return true;
    }

    private void engageModulesToAxisDescription(List moduleList,
                                                AxisDescription description) throws AxisFault {

        AxisModule axisModule;
        Module module;

        for (Iterator iterator = moduleList.iterator(); iterator.hasNext();) {
            axisModule = (AxisModule) iterator.next();
            // FIXME is this step really needed ??
            // Shouldn't axisMoudle.getModule always return not-null value ??
            module = axisModule.getModule();

            if (!(module == null || description.isEngaged(axisModule.getName()))) {
                // engages the module to AxisDescription
                description.engageModule(axisModule);
                // notifies the module about the engagement
                axisModule.getModule().engageNotify(description);
            }
        }
    }

    public AxisConfiguration getAxisConfiguration() {

        if (this instanceof AxisConfiguration) {
            return (AxisConfiguration) this;
        }

        if (this.parent != null) {
            return this.parent.getAxisConfiguration();
        }

        return null;
    }

    public abstract Object getKey();

    /**
     * Engage a Module at this level
     *
     * @param axisModule the Module to engage
     * @throws AxisFault if there's a problem engaging
     */
    public void engageModule(AxisModule axisModule) throws AxisFault {
        engageModule(axisModule, this);
    }

    /**
     * Engage a Module at this level, keeping track of which level the engage was originally
     * called from.  This is meant for internal use only.
     *
     * @param axisModule module to engage
     * @param source the AxisDescription which originally called engageModule()
     * @throws AxisFault if there's a problem engaging
     */
    public void engageModule(AxisModule axisModule, AxisDescription source) throws AxisFault {
        if (engagedModules == null) engagedModules = new HashMap();
        String moduleName = axisModule.getName();
        for (Iterator iterator = engagedModules.values().iterator(); iterator.hasNext();) {
            AxisModule tempAxisModule = ((AxisModule) iterator.next());
            String tempModuleName = tempAxisModule.getName();

            if (moduleName.equals(tempModuleName)) {
                String existing = tempAxisModule.getVersion();
                if (!Utils.checkVersion(axisModule.getVersion(), existing)) {
                    throw new AxisFault(Messages.getMessage("mismatchedModuleVersions",
                            getClass().getName(),
                            moduleName,
                            existing));
                }
            }
            
        }

        // Let the Module know it's being engaged.  If it's not happy about it, it can throw.
        Module module = axisModule.getModule();
        if (module != null) {
            module.engageNotify(this);
        }

        // If we have anything specific to do, let that happen
        onEngage(axisModule, source);

        engagedModules.put(Utils.getModuleName(axisModule.getName(), axisModule.getVersion()),
                           axisModule);
    }

    protected void onEngage(AxisModule module, AxisDescription engager) throws AxisFault {
        // Default version does nothing, feel free to override
    }

    static Collection NULL_MODULES = new ArrayList(0);
    
    public Collection getEngagedModules() {
        return engagedModules == null ? NULL_MODULES : engagedModules.values();
    }

    /**
     * Check if a given module is engaged at this level.
     *
     * @param moduleName module to investigate.
     * @return true if engaged, false if not.
     * TODO: Handle versions?  isEngaged("addressing") should be true even for versioned modulename...
     */
    public boolean isEngaged(String moduleName) {
        return engagedModules != null && engagedModules.keySet().contains(moduleName);
    }

    public boolean isEngaged(AxisModule axisModule) {
        String id = Utils.getModuleName(axisModule.getName(), axisModule.getVersion());
        return engagedModules != null && engagedModules.keySet().contains(id);
    }

    public void disengageModule(AxisModule module) throws AxisFault {
        if (module == null || engagedModules == null) return;
//        String id = Utils.getModuleName(module.getName(), module.getVersion());
        if (isEngaged(module)) {
            onDisengage(module);
            engagedModules.remove(Utils.getModuleName(module.getName(), module.getVersion()));
        }
    }

    protected void onDisengage(AxisModule module) throws AxisFault {
        // Base version does nothing
    }

    private Policy getApplicablePolicy(AxisDescription axisDescription) {

        if (axisDescription instanceof AxisOperation) {
            AxisOperation operation = (AxisOperation) axisDescription;
            AxisService service = operation.getAxisService();

            if (service != null) {

                AxisEndpoint axisEndpoint = service.getEndpoint(service.getEndpointName());

                AxisBinding axisBinding = null;

                if (axisEndpoint != null) {
                    axisBinding = axisEndpoint.getBinding();
                }

                AxisBindingOperation axisBindingOperation = null;

                if (axisBinding != null) {
                    axisBindingOperation = (AxisBindingOperation) axisBinding.getChild(operation.getName());
                }

                if (axisBindingOperation != null) {
                    return axisBindingOperation.getEffectivePolicy();
                }
            }

            return operation.getPolicyInclude().getEffectivePolicy();

        } else if (axisDescription instanceof AxisService) {
            AxisService service = (AxisService) axisDescription;

            AxisEndpoint axisEndpoint = service.getEndpoint(service.getEndpointName());
            AxisBinding axisBinding = null;

            if (axisEndpoint != null) {
                axisBinding = axisEndpoint.getBinding();
            }

            if (axisBinding != null) {
                return axisBinding.getEffectivePolicy();
            }

            return service.getPolicyInclude().getEffectivePolicy();

        } else {
            return axisDescription.getPolicyInclude().getEffectivePolicy();
        }
    }
}

⌨️ 快捷键说明

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