messagecontextsaveatest.java

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

JAVA
1,306
字号
            }

            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;

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

                restoredSimpleMsg.activate(configurationContext);

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

                // compare to original execution chain
                ArrayList restored_execChain = restoredSimpleMsg.getExecutionChain();
                ArrayList orig_execChain = simpleMsg.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 = restoredSimpleMsg.getExecutedPhases();
                Iterator orig_executed_it = simpleMsg.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);
                }

            }
            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
                }
            }
        }

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


    // this checks the save/restore of a message context that has
    // some properties set
    public void testMcProperties() throws Exception {
        String title = "MessageContextSaveATest:testMcProperties(): ";
        log.debug(title + "start - - - - - - - - - - - - - - - -");

        MessageContext simpleMsg = new MessageContext();
        MessageContext restoredSimpleMsg = null;

        simpleMsg.setProperty("key1", "value1");
        simpleMsg.setProperty("key2", null);
        simpleMsg.setProperty("key3", new Integer(3));
        simpleMsg.setProperty("key4", new Long(4L));


        File theFile = null;
        String theFilename = null;

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

        try {
            theFile = File.createTempFile("McProps", 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(simpleMsg);

                // 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;

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

                restoredSimpleMsg.activate(configurationContext);

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

                // compare to original execution chain
                ArrayList restored_execChain = restoredSimpleMsg.getExecutionChain();
                ArrayList orig_execChain = simpleMsg.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 = restoredSimpleMsg.getExecutedPhases();
                Iterator orig_executed_it = simpleMsg.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);
                }

                // check the properties
                String value1 = (String) restoredSimpleMsg.getProperty("key1");
                Object value2 = restoredSimpleMsg.getProperty("key2");
                Integer value3 = (Integer) restoredSimpleMsg.getProperty("key3");
                Long value4 = (Long) restoredSimpleMsg.getProperty("key4");

                assertEquals("value1", value1);
                assertNull(value2);

                boolean isOk = false;
                if ((value3 != null) && value3.equals(new Integer(3))) {
                    isOk = true;
                }
                assertTrue(isOk);

                if ((value4 != null) && value4.equals(new Long(4L))) {
                    isOk = true;
                }
                assertTrue(isOk);

            }
            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 - - - - - - - - - - - - - - - -");
    }


    public void testMapping() throws Exception {

        String title = "MessageContextSaveATest:testMapping(): ";
        log.debug(title + "start - - - - - - - - - - - - - - - -");

        MessageContext restoredMC = null;

        //---------------------------------------------------------------------
        // make sure that the operation context messageContexts table 
        // has an entry for the message context that we working with
        //---------------------------------------------------------------------
        // look at the OperationContext messageContexts table
        HashMap mcMap1 = mc.getOperationContext().getMessageContexts();

        if ((mcMap1 == null) || (mcMap1.isEmpty())) {
            mc.getAxisOperation().addMessageContext(mc, mc.getOperationContext());
        }
        // update the table
        mcMap1 = mc.getOperationContext().getMessageContexts();

        log.debug(title + "- - - - - original message contexts table- - - - - - - - - - -");
        showMcMap(mcMap1);

        //---------------------------------------------------------------------
        // save and restore the message context
        //---------------------------------------------------------------------

        File theFile;
        String theFilename = null;

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

        try {
            theFile = File.createTempFile("McMappings", 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(mc);

                // 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) {

⌨️ 快捷键说明

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