messagecontextsavectest.java

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

JAVA
1,277
字号
            HashMap mcTable2 = oc2.getMessageContexts();

            if ((mcTable1 != null) && (mcTable2 != null)) {
                if ((!mcTable1.isEmpty()) && (!mcTable2.isEmpty())) {
                    int size1 = mcTable1.size();
                    int size2 = mcTable2.size();

                    if (size1 != size2) {
                        log.debug(title +
                                " Return FALSE:  table sizes don't match   size1[" + size1 +
                                "] != size2 [" + size2 + "] ");
                        return false;
                    }

                    Iterator it1 = mcTable1.keySet().iterator();

                    while (it1.hasNext()) {
                        String key1 = (String) it1.next();
                        MessageContext mc1 = (MessageContext) mcTable1.get(key1);
                        MessageContext mc2 = (MessageContext) mcTable2.get(key1);

                        if ((mc1 != null) && (mc2 != null)) {
                            // check the IDs
                            String id1 = mc1.getMessageID();
                            String id2 = mc2.getMessageID();

                            if ((id1 != null) && (id2 != null)) {
                                if (!id1.equals(id2)) {
                                    log.debug(title +
                                            " Return FALSE:  message IDs don't match   id1[" + id1 +
                                            "] != id2 [" + id2 + "] ");
                                    return false;
                                }
                            } else if ((id1 == null) && (id2 == null)) {
                                // can't tell, keep going
                            } else {
                                // mismatch
                                log.debug(title +
                                        " Return FALSE:  message IDs don't match   id1[" + id1 +
                                        "] != id2 [" + id2 + "] ");
                                return false;
                            }

                        } else if ((mc1 == null) && (mc2 == null)) {
                            // entries match
                        } else {
                            // mismatch
                            log.debug(
                                    title + " Return FALSE:  message context objects don't match ");
                            return false;
                        }
                    }

                    log.debug(title + " Return TRUE:  message context tables match");
                    return true;

                } else if (mcTable1.isEmpty() && mcTable2.isEmpty()) {
                    log.debug(title + " Return TRUE:  message context tables are both empty ");
                    return true;
                } else {
                    log.debug(title + " Return FALSE:  message context tables mismatch");
                    return false;
                }
            } else if ((mcTable1 == null) && (mcTable2 == null)) {
                log.debug(title + " Return TRUE:  message context tables are null");
                return true;
            } else {
                log.debug(title + " Return FALSE:  message context tables don't match");
                return false;
            }
        } else if ((oc1 == null) && (oc2 == null)) {
            log.debug(title + " Return TRUE:  operation context objects are null ");
            return true;
        } else {
            log.debug(title + " Return FALSE:  operation context objects don't match ");
            return false;
        }


    }


    private void showMCTable(OperationContext oc) {
        if (oc == null) {
            return;
        }

        HashMap mcTable = oc.getMessageContexts();

        if ((mcTable == null) || (mcTable.isEmpty())) {
            return;
        }

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

        while (it.hasNext()) {
            String key = (String) it.next();
            MessageContext mc = (MessageContext) mcTable.get(key);

            if (mc != null) {
                String id = mc.getMessageID();
                log.debug("message context table entry:   label [" + key +
                        "]    message ID [" + id + "]    ");
            }
        }

    }


    /**
     * Gets the ID associated with the handler object.
     *
     * @param o The handler object
     * @return The ID associated with the handler,
     *         -1 otherwise
     */
    private int getHandlerID(Object o) {
        int id = -1;

        if (o instanceof TempHandler) {
            id = ((TempHandler) o).getHandlerID();
        }

        return id;
    }

    //=========================================================================
    // Handler classes 
    //=========================================================================

    /**
     * Pauses and saves the message context the message context
     */
    public class SaveHandler extends AbstractHandler {
        private Integer handlerID = null;

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

        private boolean performSave = true;

        private boolean savedOk = false;

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

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

        public SaveHandler(int index, File saveFile, boolean doIt) {
            this.handlerID = new Integer(index);
            init(new HandlerDescription(new String("handler" + index)));
            theFile = saveFile;
            performSave = doIt;
        }

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

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

            return -5;
        }

        public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
            String title = "SaveHandler[" + getHandlerID() + "]:invoke(): ";
            savedOk = false;

            if (performSave == false) {
                log.debug(title + "Configured for no action to be performed.");
                return InvocationResponse.CONTINUE;
            }


            log.debug(title + "msgContext.pause()");
            msgContext.pause();

            if (theFile != null) {
                try {
                    log.debug(title + "Resetting the file to use.");
                    theFile.delete();
                    theFile.createNewFile();
                    theFilename = theFile.getName();
                    log.debug(title + "temp file = [" + theFilename + "]");
                }
                catch (Exception ex) {
                    log.debug(title + "error creating new file = [" + ex.getMessage() + "]");
                }

                if (theFile.exists() == true) {
                    // ---------------------------------------------------------
                    // 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.....");
                        savedOk = false;
                        outObjStream.writeObject(msgContext);

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

                        savedOk = 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(savedOk);

                }
            }

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

            return InvocationResponse.SUSPEND;

        }

    }


    public class TempHandler extends AbstractHandler {
        private Integer handlerID = null;
        private int count = 0;
        private int numberProperties = 3;
        private String propertyKey = "Property";
        private String propertyValue = "ServiceLevelSetting";

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

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

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

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

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

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

            return -5;
        }


        public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
            String title = "TempHandler[" + getHandlerID() + "]:invoke(): ";

            // get the service context from the message context
            ServiceContext serviceContext = msgContext.getServiceContext();

            if (serviceContext == null) {
                // get the service context from the operation context
                OperationContext operationContext = msgContext.getOperationContext();
                serviceContext = operationContext.getServiceContext();
            }

            if (serviceContext != null) {
                for (int j = 0; j < numberProperties; j++) {
                    count++;
                    String key = new String(propertyKey + ".ID[" + getHandlerID() + "]." + count);
                    String value = new String(propertyValue + "[" + count + "]");
                    serviceContext.setProperty(key, value);
                }
            }

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

            return InvocationResponse.CONTINUE;
        }

    }

}

⌨️ 快捷键说明

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