messagecontextsaveatest.java

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

JAVA
1,306
字号
                log.debug(title + "error with saving message context = [" +
                        ex2.getClass().getName() + " : " + ex2.getMessage() + "]");
                ex2.printStackTrace();
            }

            assertTrue(savedMessageContext);

            // ---------------------------------------------------------
            // 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.....");
                restoredMessageContext = false;

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

                restoredMC.activate(configurationContext);

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

                // get the table after the restore
                HashMap mcMap2 = restoredMC.getOperationContext().getMessageContexts();

                log.debug(
                        "MessageContextSaveATest:testMapping(): - - - - - restored message contexts table- - - - - - - - - - -");
                showMcMap(mcMap2);

                boolean okMap = compareMCMaps(mcMap1, mcMap2);
                assertTrue(okMap);

            }
            catch (Exception ex2) {
                log.debug(title + "error with restoring message context = [" +
                        ex2.getClass().getName() + " : " + ex2.getMessage() + "]");
                ex2.printStackTrace();
            }

            assertTrue(restoredMessageContext);

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

        log.debug(title + "end - - - - - - - - - - - - - - - -");


    }

    private boolean compareMCMaps(HashMap m1, HashMap m2) {
        String title = "MessageContextSaveATest:compareMCMaps(): ";

        if ((m1 != null) && (m2 != null)) {
            int size1 = m1.size();
            int size2 = m2.size();

            if (size1 != size2) {
                log.debug(title + "MISMATCH:  map1 size [" + size1 +
                        "]  !=   map2 size [" + size2 + "]");
                return false;
            }

            String id1 = null;
            String id2 = null;

            // check the keys, ordering is not important between the two maps
            Iterator it1 = m1.keySet().iterator();

            while (it1.hasNext()) {
                String key1 = (String) it1.next();
                MessageContext value1 = (MessageContext) m1.get(key1);

                if (value1 != null) {
                    id1 = value1.getMessageID();

                    MessageContext value2 = (MessageContext) m2.get(key1);

                    if (value2 != null) {
                        id2 = value2.getMessageID();
                    } else {
                        // mismatch
                        log.debug(title +
                                "MISMATCH:  no message context in one of the tables for key [" +
                                key1 + "]");
                        return false;
                    }

                    if ((id1 != null) && (id2 != null)) {
                        if (!id1.equals(id2)) {
                            // mismatch
                            log.debug(title + "MISMATCH:  messageID_1 [" + id1 +
                                    "]   !=    messageID_2 [" + id2 + "]");
                            return false;
                        }
                    } else {
                        // null values, can't tell
                        log.debug(title + "MISMATCH:  one or more null message IDs");
                        return false;
                    }
                }
            }
            return true;
        } else if ((m1 == null) && (m2 == null)) {
            return true;
        } else {
            // mismatch
            log.debug(title + "MISMATCH:  one of the tables is null");
            return false;
        }
    }


    public class TempHandler extends AbstractHandler {
        private Integer handlerID = null;

        private File theFile = null;
        private String theFilename = null;

        private boolean pause = false;
        private boolean savedMessageContext = false;
        private boolean restoredMessageContext = false;
        private boolean comparesOk = false;

        //-----------------------------------------------------------------
        // constructors
        //-----------------------------------------------------------------

        public TempHandler() {
            this.handlerID = new Integer(-5);
        }

        public TempHandler(int index, boolean pause) {
            this.handlerID = new Integer(index);
            this.pause = pause;
            init(new HandlerDescription("handler" + index));
        }

        public TempHandler(int index) {
            this.handlerID = new Integer(index);
            init(new HandlerDescription("handler" + index));
        }

        //-----------------------------------------------------------------
        // methods
        //-----------------------------------------------------------------

        public int getHandlerID() {
            if (handlerID != null) {
                return handlerID.intValue();
            }

            return -5;
        }


        public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
            String title = "TempHandler[" + getHandlerID() + "]:invoke(): ";
            log.debug(title + "pause = [" + pause + "]");
            savedMessageContext = false;
            restoredMessageContext = false;

            if (pause) {
                log.debug(title + "msgContext.pause()");
                msgContext.pause();
                pause = false;

                try {
                    theFile = File.createTempFile("mcSave", null);
                    theFilename = theFile.getName();
                    log.debug(title + "temp file = [" + theFilename + "]");
                }
                catch (Exception ex) {
                    log.debug(title + "error creating temp file = [" + ex.getMessage() + "]");
                    theFile = null;
                }

                if (theFile != null) {
                    // ---------------------------------------------------------
                    // save to the temporary file
                    // ---------------------------------------------------------
                    try {
                        // setup an output stream to a physical file
                        FileOutputStream outStream = new FileOutputStream(theFile);

                        // attach a stream capable of writing objects to the 
                        // stream connected to the file
                        ObjectOutputStream outObjStream = new ObjectOutputStream(outStream);

                        // try to save the message context
                        log.debug(title + "saving message context.....");
                        savedMessageContext = false;
                        outObjStream.writeObject(msgContext);

                        // close out the streams
                        outObjStream.flush();
                        outObjStream.close();
                        outStream.flush();
                        outStream.close();

                        savedMessageContext = true;
                        log.debug(title + "....saved message context.....");

                        long filesize = theFile.length();
                        log.debug(title + "file size after save [" + filesize +
                                "]   temp file = [" + theFilename + "]");

                    }
                    catch (Exception ex2) {
                        log.debug(title + "error with saving message context = [" +
                                ex2.getClass().getName() + " : " + ex2.getMessage() + "]");
                        ex2.printStackTrace();
                    }

                    assertTrue(savedMessageContext);

                    // ---------------------------------------------------------
                    // 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.....");
                        restoredMessageContext = false;

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

                        msgContext2.activate(configurationContext);

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

                        // compare to original execution chain
                        ArrayList restored_execChain = msgContext2.getExecutionChain();
                        ArrayList orig_execChain = msgContext.getExecutionChain();

                        comparesOk = ObjectStateUtils
                                .isEquivalent(restored_execChain, orig_execChain, false);
                        log.debug(title + "execution chain equivalency [" + comparesOk + "]");
                        assertTrue(comparesOk);

                        // check executed list
                        Iterator restored_executed_it = msgContext2.getExecutedPhases();
                        Iterator orig_executed_it = msgContext.getExecutedPhases();
                        if ((restored_executed_it != null) && (orig_executed_it != null)) {
                            while (restored_executed_it.hasNext() && orig_executed_it.hasNext()) {
                                Object p1 = restored_executed_it.next();
                                Object p2 = orig_executed_it.next();

                                comparesOk = comparePhases(p1, p2);
                                log.debug(title +
                                        "executed phase list:  compare phases [" + comparesOk +
                                        "]");
                                assertTrue(comparesOk);
                            }
                        } else {
                            // problem with the executed lists
                            assertTrue(false);
                        }

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

                    assertTrue(restoredMessageContext);

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

                return InvocationResponse.SUSPEND;

            } else {
                log.debug(title + "executedHandlers.add(" + handlerID + ")");
                executedHandlers.add(handlerID);
            }

            return InvocationResponse.CONTINUE;
        }

    }
}

⌨️ 快捷键说明

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