messagecontextsavectest.java

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

JAVA
1,277
字号
        // restored message context and the message context on the separate engine
        LinkedHashMap separate_object_graph = getObjectGraphInfo(msgCtx_A1_1_equiv);

        // compare the restored object graph with the existing object graph
        boolean expectStrict =
                compareObjectGraphInfo(restored_object_graph, separate_object_graph, true);
        assertTrue(expectStrict);

        // resume the restored paused message context on an engine that has the 
        // same setup as the engine where the save occurred
        // and save has the Service-level Context objects
        log.debug(title +
                "- - - Resume the restored message context - - - - - - - - - - - - - - - -");
        engineEquiv.resume(mc2);

        LinkedHashMap resumed_object_graph = getObjectGraphInfo(mc2);
        log.debug(title + "*** Post Resumed object graph ****");
        showObjectGraphInfo(resumed_object_graph);

        // there should be no changes in the object graph in our case after the resume
        hasEquivalence = compareObjectGraphInfo(restored_object_graph, resumed_object_graph, true);
        assertTrue(hasEquivalence);


    }

    /**
     * Restores a previously saved message context
     */
    public MessageContext restoreMessageContext(File restoreFile, ConfigurationContext cc) {
        String title = "restoreMessageContext(): ";

        MessageContext restoredMC = null;

        File theFile = restoreFile;
        String theFilename = null;

        // the configuration context to use for message context activation
        ConfigurationContext cfgCtx = cc;

        boolean restoredOk = false;

        if ((theFile != null) && (theFile.exists())) {
            theFilename = theFile.getName();
            log.debug(title + "temp file = [" + theFilename + "]");

            // ---------------------------------------------------------
            // restore from the temporary file
            // ---------------------------------------------------------
            try {
                // setup an input stream to the file
                FileInputStream inStream = new FileInputStream(theFile);

                // attach a stream capable of reading objects from the 
                // stream connected to the file
                ObjectInputStream inObjStream = new ObjectInputStream(inStream);

                // try to restore the message context
                log.debug(title + "restoring a message context.....");
                restoredOk = false;

                MessageContext msgContext2 = (MessageContext) inObjStream.readObject();
                inObjStream.close();
                inStream.close();

                msgContext2.activate(cfgCtx);

                restoredOk = true;
                log.debug(title + "....restored message context.....");

                // now put the restored message context in the global
                // variable for the test 
                restoredMC = msgContext2;
            }
            catch (Exception ex2) {
                log.debug(title + "error with restoring message context = [" +
                        ex2.getClass().getName() + " : " + ex2.getMessage() + "]");
                ex2.printStackTrace();
                restoredMessageContext = null;
            }

            assertTrue(restoredOk);

            // if the restore of the message context succeeded,
            // then don't keep the temporary file around
            boolean removeTmpFile = restoredOk;
            if (removeTmpFile) {
                try {
                    theFile.delete();
                }
                catch (Exception e) {
                    // just absorb it
                }
            }
        }

        return restoredMC;
    }


    private LinkedHashMap getObjectGraphInfo(MessageContext msgCtx) {
        if (msgCtx == null) {
            return null;
        }

        MetaDataEntry metaMC = null;
        MetaDataEntry metaOC = null;
        MetaDataEntry metaSC = null;
        MetaDataEntry metaSGC = null;
        MetaDataEntry metaCC = null;
        MetaDataEntry metaAO = null;
        MetaDataEntry metaAS = null;
        MetaDataEntry metaASG = null;
        MetaDataEntry metaAC = null;

        String keyMC = null;
        String keyOC = null;
        String keySC = null;
        String keySGC = null;
        String keyCC = null;
        String keyAO = null;
        String keyAS = null;
        String keyASG = null;
        String keyAC = null;

        LinkedHashMap objInfo = new LinkedHashMap();

        // get the identification info about the primary objects in the object graph
        //     class name
        //     name string
        //     hashcode string

        // message context
        keyMC = msgCtx.getClass().getName();
        metaMC = new MetaDataEntry(keyMC, msgCtx.getMessageID(), "[" + msgCtx.hashCode() + "]");
        objInfo.put(keyMC, metaMC);

        // operation context
        OperationContext oc = msgCtx.getOperationContext();
        keyOC = oc.getClass().getName();
        metaOC = new MetaDataEntry(keyOC, oc.getOperationName(), "[" + oc.hashCode() + "]");
        objInfo.put(keyOC, metaOC);

        // service context
        ServiceContext sc = msgCtx.getServiceContext();
        keySC = sc.getClass().getName();
        metaSC = new MetaDataEntry(keySC, sc.getName(), "[" + sc.hashCode() + "]");
        objInfo.put(keySC, metaSC);

        // service group context
        ServiceGroupContext sgc = msgCtx.getServiceGroupContext();
        keySGC = sgc.getClass().getName();
        metaSGC = new MetaDataEntry(keySGC, sgc.getId(), "[" + sgc.hashCode() + "]");
        objInfo.put(keySGC, metaSGC);

        // configuration context
        ConfigurationContext cc = msgCtx.getConfigurationContext();
        keyCC = cc.getClass().getName();
        metaCC = new MetaDataEntry(keyCC, null, "[" + cc.hashCode() + "]");
        objInfo.put(keyCC, metaCC);

        // axis operation
        AxisOperation ao = msgCtx.getAxisOperation();
        keyAO = ao.getClass().getName();
        metaAO = new MetaDataEntry(keyAO, ao.getName().toString(), "[" + ao.hashCode() + "]");
        objInfo.put(keyAO, metaAO);

        // axis service
        AxisService as = msgCtx.getAxisService();
        keyAS = as.getClass().getName();
        metaAS = new MetaDataEntry(keyAS, as.getName(), "[" + as.hashCode() + "]");
        objInfo.put(keyAS, metaAS);

        // axis service group
        AxisServiceGroup asg = msgCtx.getAxisServiceGroup();
        keyASG = asg.getClass().getName();
        metaASG = new MetaDataEntry(keyASG, asg.getServiceGroupName(), "[" + asg.hashCode() + "]");
        objInfo.put(keyASG, metaASG);

        // axis configuration
        AxisConfiguration ac = cc.getAxisConfiguration();
        keyAC = ac.getClass().getName();
        metaAC = new MetaDataEntry(keyAC, null, "[" + ac.hashCode() + "]");
        objInfo.put(keyAC, metaAC);

        return objInfo;
    }

    /**
     * Compare two mappings containing object graph info.
     * This uses the class name and object ID.
     * <p/>
     * Strict comparison includes the object hash codes. If
     * you expect the same object to be represented in
     * both maps, you may want to use Strict checking.
     * <p/>
     *
     * @param map1   The first object graph info map
     * @param map2   The second object graph info map
     * @param strict TRUE if strict comparison
     * @return Outcome of the comparison: TRUE if equivalent, FALSE otherwise
     */
    private boolean compareObjectGraphInfo(LinkedHashMap map1, LinkedHashMap map2, boolean strict) {
        String title = "MessageContextSaveCTest: compareObjectGraphInfo(): ";

        if ((map1 != null) && (map2 != null)) {
            if (map1.size() != map2.size()) {
                log.debug(title + "Object graph info mappings are different sizes.");
                return false;
            }

            Iterator it = map1.keySet().iterator();

            while (it.hasNext()) {
                // the key is the class name associated with the object
                String key = (String) it.next();

                // skip certain objects, those will always be unique
                if ((key.indexOf("MessageContext") == -1) &&
                        (key.indexOf("OperationContext") == -1) &&
                        (key.indexOf("ConfigurationContext") == -1) &&
                        (key.indexOf("AxisConfiguration") == -1)
                        ) {
                    // the class names listed above were not found
                    // so we're dealing with the other objects
                    MetaDataEntry value1 = (MetaDataEntry) map1.get(key);
                    MetaDataEntry value2 = (MetaDataEntry) map2.get(key);

                    if ((value1 != null) && (value2 != null)) {
                        // check the object identification
                        String name1 = value1.getName();
                        String name2 = value2.getName();

                        if ((name1 != null) && (name2 != null)) {
                            if (name1.equals(name2) == false) {
                                log.debug(title + "name1 [" + name1 + "]  !=   name2 [" +
                                        name2 + "]");
                                return false;
                            }
                        } else if ((name1 == null) && (name2 == null)) {
                            // ok
                        } else {
                            // mismatch
                            log.debug(title + "name1 [" + name1 + "]  !=   name2 [" + name2 + "]");
                            return false;
                        }

                        // Strict testing means checking the object hashcodes.
                        // Use this option when you expect the same
                        // objects in the map.
                        if (strict) {
                            String code1 = value1.getExtraName();
                            String code2 = value2.getExtraName();

                            if ((code1 != null) && (code2 != null)) {
                                if (code1.equals(code2) == false) {
                                    log.debug(title + "name [" + name1 + "]  code1 [" +
                                            code1 + "]  !=   code2 [" + code2 + "]");
                                    return false;
                                }
                            } else if ((code1 == null) && (code2 == null)) {
                                // ok
                            } else {
                                // mismatch
                                log.debug(title + "name [" + name1 + "]code1 [" + code1 +
                                        "]  !=   code2 [" + code2 + "]");
                                return false;
                            }
                        }
                    } else if ((value1 == null) && (value2 == null)) {
                        // ok
                    } else {
                        // mismatch
                        log.debug(title + "value1 [" + value1 + "]  !=   value2 [" + value2 + "]");
                        return false;
                    }
                }
            }

            return true;

        } else if ((map1 == null) && (map2 == null)) {
            return true;
        } else {
            log.debug(title + "mismatch: one or more of the maps are null.  ");
            return false;
        }

    }


    private void showObjectGraphInfo(LinkedHashMap map) {
        if (map == null) {
            return;
        }

        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            String metaClassName = (String) it.next();
            MetaDataEntry meta = (MetaDataEntry) map.get(metaClassName);

            if (meta != null) {
                String classname = meta.getClassName();
                String name = meta.getName();
                String hashcode = meta.getExtraName();

                log.debug("class[" + classname + "]  id[" + name + "]  hashcode" + hashcode + " ");
            }

        }

    }


    private boolean compareMCTable(OperationContext oc1, OperationContext oc2) {
        String title = "compareMCTable: ";

        if ((oc1 != null) && (oc2 != null)) {
            HashMap mcTable1 = oc1.getMessageContexts();

⌨️ 快捷键说明

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