0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: */
0019:
0020: package org.apache.axis2.engine;
0021:
0022: import junit.framework.TestCase;
0023: import org.apache.axiom.om.OMAbstractFactory;
0024: import org.apache.axiom.om.util.UUIDGenerator;
0025: import org.apache.axiom.soap.SOAPFactory;
0026: import org.apache.axis2.AxisFault;
0027: import org.apache.axis2.addressing.EndpointReference;
0028: import org.apache.axis2.context.ConfigurationContext;
0029: import org.apache.axis2.context.MessageContext;
0030: import org.apache.axis2.context.OperationContext;
0031: import org.apache.axis2.context.ServiceContext;
0032: import org.apache.axis2.context.ServiceGroupContext;
0033: import org.apache.axis2.description.AxisMessage;
0034: import org.apache.axis2.description.AxisOperation;
0035: import org.apache.axis2.description.AxisService;
0036: import org.apache.axis2.description.AxisServiceGroup;
0037: import org.apache.axis2.description.HandlerDescription;
0038: import org.apache.axis2.description.InOutAxisOperation;
0039: import org.apache.axis2.description.TransportInDescription;
0040: import org.apache.axis2.description.TransportOutDescription;
0041: import org.apache.axis2.dispatchers.AddressingBasedDispatcher;
0042: import org.apache.axis2.dispatchers.RequestURIBasedDispatcher;
0043: import org.apache.axis2.dispatchers.SOAPActionBasedDispatcher;
0044: import org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher;
0045: import org.apache.axis2.handlers.AbstractHandler;
0046: import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
0047: import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
0048: import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
0049: import org.apache.axis2.transport.http.SimpleHTTPServer;
0050: import org.apache.axis2.util.ObjectStateUtils;
0051: import org.apache.axis2.wsdl.WSDLConstants;
0052: import org.apache.commons.logging.Log;
0053: import org.apache.commons.logging.LogFactory;
0054:
0055: import javax.xml.namespace.QName;
0056: import java.io.File;
0057: import java.io.FileInputStream;
0058: import java.io.FileOutputStream;
0059: import java.io.ObjectInputStream;
0060: import java.io.ObjectOutputStream;
0061: import java.util.ArrayList;
0062: import java.util.HashMap;
0063: import java.util.Iterator;
0064:
0065: public class MessageContextSaveATest extends TestCase {
0066: protected static final Log log = LogFactory
0067: .getLog(MessageContextSaveATest.class);
0068:
0069: private QName serviceName = new QName("TestService");
0070: private QName operationName = new QName("Operation_1");
0071:
0072: private ConfigurationContext configurationContext = null;
0073: private ServiceGroupContext serviceGroupContext = null;
0074: private OperationContext operationContext = null;
0075:
0076: private AxisServiceGroup axisServiceGroup = null;
0077: private AxisService axisService = null;
0078: private AxisOperation axisOperation = null;
0079: private AxisMessage axisMessage = null;
0080:
0081: private Phase phase1 = null;
0082: private Phase phase2 = null;
0083: private Phase phase3 = null;
0084: private Phase phase4 = null;
0085: private Phase phase5 = null;
0086: private Phase phase6 = null;
0087: private Phase phase7 = null;
0088:
0089: private MessageContext mc = null;
0090: private MessageContext mc2 = null;
0091:
0092: private ArrayList executedHandlers = null;
0093:
0094: public MessageContextSaveATest(String arg0) {
0095: super (arg0);
0096:
0097: try {
0098: prepare();
0099: } catch (Exception e) {
0100: log
0101: .debug("MessageContextSaveATest:constructor: error in setting up object graph ["
0102: + e.getClass().getName()
0103: + " : "
0104: + e.getMessage() + "]");
0105: }
0106: }
0107:
0108: //
0109: // prepare the object hierarchy for testing
0110: //
0111: private void prepare() throws Exception {
0112: //-----------------------------------------------------------------
0113:
0114: AxisConfiguration axisConfiguration = new AxisConfiguration();
0115:
0116: configurationContext = new ConfigurationContext(
0117: axisConfiguration);
0118:
0119: configurationContext.getAxisConfiguration().addMessageReceiver(
0120: "http://www.w3.org/2004/08/wsdl/in-only",
0121: new RawXMLINOnlyMessageReceiver());
0122: configurationContext.getAxisConfiguration().addMessageReceiver(
0123: "http://www.w3.org/2004/08/wsdl/in-out",
0124: new RawXMLINOutMessageReceiver());
0125:
0126: DispatchPhase dispatchPhase = new DispatchPhase();
0127: dispatchPhase.setName("Dispatch");
0128:
0129: AddressingBasedDispatcher abd = new AddressingBasedDispatcher();
0130: abd.initDispatcher();
0131:
0132: RequestURIBasedDispatcher rud = new RequestURIBasedDispatcher();
0133: rud.initDispatcher();
0134:
0135: SOAPActionBasedDispatcher sabd = new SOAPActionBasedDispatcher();
0136: sabd.initDispatcher();
0137:
0138: SOAPMessageBodyBasedDispatcher smbd = new SOAPMessageBodyBasedDispatcher();
0139: smbd.initDispatcher();
0140:
0141: dispatchPhase.addHandler(abd);
0142: dispatchPhase.addHandler(rud);
0143: dispatchPhase.addHandler(sabd);
0144: dispatchPhase.addHandler(smbd);
0145:
0146: configurationContext.getAxisConfiguration().getInFlowPhases()
0147: .add(dispatchPhase);
0148:
0149: //-----------------------------------------------------------------
0150:
0151: axisServiceGroup = new AxisServiceGroup(axisConfiguration);
0152: axisServiceGroup.setServiceGroupName("ServiceGroupTest");
0153:
0154: axisService = new AxisService(serviceName.getLocalPart());
0155: axisServiceGroup.addService(axisService);
0156:
0157: axisOperation = new InOutAxisOperation(operationName);
0158: axisOperation.setMessageReceiver(new MessageReceiver() {
0159: public void receive(MessageContext messageCtx) {
0160:
0161: }
0162: });
0163:
0164: axisService.addOperation(axisOperation);
0165: axisService.mapActionToOperation(operationName.getLocalPart(),
0166: axisOperation);
0167:
0168: configurationContext.getAxisConfiguration().addService(
0169: axisService);
0170:
0171: //-----------------------------------------------------------------
0172:
0173: serviceGroupContext = configurationContext
0174: .createServiceGroupContext(axisService
0175: .getAxisServiceGroup());
0176: serviceGroupContext.setId("ServiceGroupContextTest");
0177:
0178: ServiceContext serviceContext = serviceGroupContext
0179: .getServiceContext(axisService);
0180:
0181: operationContext = serviceContext
0182: .createOperationContext(operationName);
0183:
0184: //-----------------------------------------------------------------
0185:
0186: TransportOutDescription transportOut = new TransportOutDescription(
0187: "null");
0188: TransportOutDescription transportOut2 = new TransportOutDescription(
0189: "happy");
0190: TransportOutDescription transportOut3 = new TransportOutDescription(
0191: "golucky");
0192: transportOut.setSender(new CommonsHTTPTransportSender());
0193: transportOut2.setSender(new CommonsHTTPTransportSender());
0194: transportOut3.setSender(new CommonsHTTPTransportSender());
0195: axisConfiguration.addTransportOut(transportOut3);
0196: axisConfiguration.addTransportOut(transportOut2);
0197: axisConfiguration.addTransportOut(transportOut);
0198:
0199: TransportInDescription transportIn = new TransportInDescription(
0200: "null");
0201: TransportInDescription transportIn2 = new TransportInDescription(
0202: "always");
0203: TransportInDescription transportIn3 = new TransportInDescription(
0204: "thebest");
0205: transportIn.setReceiver(new SimpleHTTPServer());
0206: transportIn2.setReceiver(new SimpleHTTPServer());
0207: transportIn3.setReceiver(new SimpleHTTPServer());
0208: axisConfiguration.addTransportIn(transportIn2);
0209: axisConfiguration.addTransportIn(transportIn);
0210: axisConfiguration.addTransportIn(transportIn3);
0211:
0212: //-----------------------------------------------------------------
0213:
0214: mc = configurationContext.createMessageContext();
0215: mc.setTransportIn(transportIn);
0216: mc.setTransportOut(transportOut);
0217:
0218: mc.setServerSide(true);
0219: // mc.setProperty(MessageContext.TRANSPORT_OUT, System.out);
0220:
0221: SOAPFactory omFac = OMAbstractFactory.getSOAP11Factory();
0222: mc.setEnvelope(omFac.getDefaultEnvelope());
0223:
0224: phase1 = new Phase("beginPhase1");
0225: phase1.addHandler(new TempHandler(1));
0226: phase1.addHandler(new TempHandler(2));
0227: phase1.addHandler(new TempHandler(3));
0228: phase1.addHandler(new TempHandler(4));
0229: phase1.addHandler(new TempHandler(5));
0230: phase1.addHandler(new TempHandler(6));
0231: phase1.addHandler(new TempHandler(7));
0232: phase1.addHandler(new TempHandler(8));
0233: phase1.addHandler(new TempHandler(9));
0234:
0235: phase2 = new Phase("middlePhase2");
0236: phase2.addHandler(new TempHandler(10));
0237: phase2.addHandler(new TempHandler(11));
0238: phase2.addHandler(new TempHandler(12));
0239: phase2.addHandler(new TempHandler(13));
0240: phase2.addHandler(new TempHandler(14));
0241: phase2.addHandler(new TempHandler(15, true));
0242: phase2.addHandler(new TempHandler(16));
0243: phase2.addHandler(new TempHandler(17));
0244: phase2.addHandler(new TempHandler(18));
0245:
0246: phase3 = new Phase("lastPhase3");
0247: phase3.addHandler(new TempHandler(19));
0248: phase3.addHandler(new TempHandler(20));
0249: phase3.addHandler(new TempHandler(21));
0250: phase3.addHandler(new TempHandler(22));
0251: phase3.addHandler(new TempHandler(23));
0252: phase3.addHandler(new TempHandler(24));
0253: phase3.addHandler(new TempHandler(25));
0254: phase3.addHandler(new TempHandler(26));
0255: phase3.addHandler(new TempHandler(27));
0256:
0257: phase4 = new Phase("extraPhase1");
0258: phase4.addHandler(new TempHandler(28));
0259: phase4.addHandler(new TempHandler(29));
0260:
0261: phase5 = new Phase("extraPhase2");
0262: phase5.addHandler(new TempHandler(30));
0263:
0264: phase6 = new Phase("extraPhase3");
0265: phase6.addHandler(new TempHandler(31, true));
0266: phase6.addHandler(new TempHandler(32));
0267:
0268: phase7 = new Phase("extraPhase4");
0269: phase7.addHandler(new TempHandler(33));
0270: phase7.addHandler(new TempHandler(34));
0271: phase7.addHandler(new TempHandler(35));
0272:
0273: axisOperation.getRemainingPhasesInFlow().add(phase1);
0274: axisOperation.getRemainingPhasesInFlow().add(phase2);
0275: axisOperation.getRemainingPhasesInFlow().add(phase3);
0276: axisOperation.getRemainingPhasesInFlow().add(phase4);
0277: axisOperation.getRemainingPhasesInFlow().add(phase5);
0278: axisOperation.getRemainingPhasesInFlow().add(phase6);
0279: axisOperation.getRemainingPhasesInFlow().add(phase7);
0280:
0281: ArrayList phases = new ArrayList();
0282: phases.add(phase1);
0283: phases.add(phase2);
0284: phases.add(phase3);
0285: phases.add(phase4);
0286: phases.add(phase5);
0287: phases.add(phase6);
0288: phases.add(phase7);
0289: axisConfiguration
0290: .setInPhasesUptoAndIncludingPostDispatch(phases);
0291:
0292: mc.setWSAAction(operationName.getLocalPart());
0293: mc.setSoapAction(operationName.getLocalPart());
0294: // System.out.flush();
0295:
0296: mc.setMessageID(UUIDGenerator.getUUID());
0297:
0298: //operationContext.addMessageContext(mc); gets done via the register
0299: axisOperation.registerOperationContext(mc, operationContext);
0300: mc.setOperationContext(operationContext);
0301: mc.setServiceContext(serviceContext);
0302:
0303: mc.setTo(new EndpointReference("axis2/services/NullService"));
0304: mc.setWSAAction("DummyOp");
0305:
0306: axisMessage = axisOperation
0307: .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
0308: mc.setAxisMessage(axisMessage);
0309:
0310: //-----------------------------------------------------------------
0311:
0312: executedHandlers = new ArrayList();
0313: }
0314:
0315: protected void setUp() throws Exception {
0316: //org.apache.log4j.BasicConfigurator.configure();
0317: }
0318:
0319: public void testReceive() throws Exception {
0320: AxisEngine engine = new AxisEngine(configurationContext);
0321:
0322: log
0323: .debug("MessageContextSaveATest:testReceive(): start - - engine.receive(mc) - - - - - - - - - - - - - - - -");
0324: engine.receive(mc);
0325:
0326: log
0327: .debug("MessageContextSaveATest:testReceive(): resume - - engine.resume(mc) - - - - - - - - - - - - - - - -");
0328: engine.resume(mc);
0329:
0330: assertEquals(30, executedHandlers.size());
0331: for (int i = 15; i < 30; i++) {
0332: assertEquals(
0333: ((Integer) executedHandlers.get(i)).intValue(),
0334: i + 1);
0335: }
0336:
0337: // get the phase lists and see if they match up
0338: ArrayList restoredPhases = mc2.getExecutionChain();
0339: int it_count = 0;
0340:
0341: Iterator it = restoredPhases.iterator();
0342: while (it.hasNext()) {
0343: // we know everything at this level is a Phase.
0344: // if you change it, you might get a ClassCastException
0345: Phase restored_phase = (Phase) it.next();
0346:
0347: Phase original_phase = null;
0348:
0349: it_count++;
0350:
0351: if (it_count == 1) {
0352: original_phase = phase1;
0353: } else if (it_count == 2) {
0354: original_phase = phase2;
0355: } else if (it_count == 3) {
0356: original_phase = phase3;
0357: } else if (it_count == 4) {
0358: original_phase = phase4;
0359: } else if (it_count == 5) {
0360: original_phase = phase5;
0361: } else if (it_count == 6) {
0362: original_phase = phase6;
0363: } else if (it_count == 7) {
0364: original_phase = phase7;
0365: } else {
0366: // unexpected
0367: assertTrue(false);
0368: }
0369:
0370: boolean isOk = comparePhases(restored_phase, original_phase);
0371: assertTrue(isOk);
0372: }
0373:
0374: // -------------------------------------------------------------------
0375: // second resume to start the second pause
0376: // -------------------------------------------------------------------
0377: log
0378: .debug("MessageContextSaveATest:testReceive(): resume - - engine.resume(mc) - - - - - - - - - - - - - - - -");
0379: engine.resume(mc);
0380:
0381: assertEquals(35, executedHandlers.size());
0382: for (int i = 31; i < 35; i++) {
0383: assertEquals(
0384: ((Integer) executedHandlers.get(i)).intValue(),
0385: i + 1);
0386: }
0387:
0388: // get the phase lists and see if they match up
0389: restoredPhases = mc2.getExecutionChain();
0390: it_count = 0;
0391:
0392: it = restoredPhases.iterator();
0393: while (it.hasNext()) {
0394: // we know everything at this level is a Phase.
0395: // if you change it, you might get a ClassCastException
0396: Phase restored_phase = (Phase) it.next();
0397:
0398: Phase original_phase = null;
0399:
0400: it_count++;
0401:
0402: if (it_count == 1) {
0403: original_phase = phase1;
0404: } else if (it_count == 2) {
0405: original_phase = phase2;
0406: } else if (it_count == 3) {
0407: original_phase = phase3;
0408: } else if (it_count == 4) {
0409: original_phase = phase4;
0410: } else if (it_count == 5) {
0411: original_phase = phase5;
0412: } else if (it_count == 6) {
0413: original_phase = phase6;
0414: } else if (it_count == 7) {
0415: original_phase = phase7;
0416: } else {
0417: // unexpected
0418: assertTrue(false);
0419: }
0420:
0421: boolean isOk = comparePhases(restored_phase, original_phase);
0422: assertTrue(isOk);
0423: }
0424: }
0425:
0426: /**
0427: * Gets the ID associated with the handler object.
0428: *
0429: * @param o The handler object
0430: * @return The ID associated with the handler,
0431: * -1 otherwise
0432: */
0433: private int getHandlerID(Object o) {
0434: int id = -1;
0435:
0436: if (o instanceof TempHandler) {
0437: id = ((TempHandler) o).getHandlerID();
0438: }
0439:
0440: return id;
0441: }
0442:
0443: /**
0444: * Check the handler objects to see if they are equivalent.
0445: *
0446: * @param o1 The first handler
0447: * @param o2 The second handler
0448: * @return TRUE if the handler objects are equivalent,
0449: * FALSE otherwise
0450: */
0451: private boolean compareHandlers(Object o1, Object o2) {
0452: if ((o1 == null) && (o2 == null)) {
0453: return true;
0454: }
0455:
0456: if ((o1 != null) && (o2 != null)) {
0457: String c1 = o1.getClass().getName();
0458: String c2 = o2.getClass().getName();
0459:
0460: if (c1.equals(c2)) {
0461: log
0462: .debug("MessageContextSaveATest::compareHandlers: class ["
0463: + c1 + "] match ");
0464:
0465: int id1 = getHandlerID(o1);
0466: int id2 = getHandlerID(o2);
0467:
0468: if (id1 == id2) {
0469: log
0470: .debug("MessageContextSaveATest::compareHandlers: id ["
0471: + id1 + "] match");
0472: return true;
0473: } else {
0474: log
0475: .debug("MessageContextSaveATest::compareHandlers: id1 ["
0476: + id1 + "] != id2 [" + id2 + "] ");
0477: return false;
0478: }
0479: } else {
0480: log
0481: .debug("MessageContextSaveATest::compareHandlers: class1 ["
0482: + c1 + "] != class2 [" + c2 + "] ");
0483: return false;
0484: }
0485: }
0486:
0487: return false;
0488: }
0489:
0490: /**
0491: * Compare two phases.
0492: *
0493: * @param o1 The first phase object
0494: * @param o2 The second phase object
0495: * @return TRUE if the phases are equivalent,
0496: * FALSE otherwise
0497: */
0498: private boolean comparePhases(Object o1, Object o2) {
0499: if ((o1 == null) && (o2 == null)) {
0500: log
0501: .debug("MessageContextSaveATest: comparePhases: Phase1[] == Phase2[] - both null objects");
0502: return true;
0503: }
0504:
0505: if (((o1 != null) && (o2 != null))
0506: && ((o1 instanceof Phase) && (o2 instanceof Phase))) {
0507:
0508: try {
0509: Phase p1 = (Phase) o1;
0510: Phase p2 = (Phase) o2;
0511:
0512: String name1 = p1.getName();
0513: String name2 = p2.getName();
0514:
0515: ArrayList list1 = p1.getHandlers();
0516: ArrayList list2 = p2.getHandlers();
0517:
0518: if ((list1 == null) && (list2 == null)) {
0519: log
0520: .debug("MessageContextSaveATest: comparePhases: Phase1["
0521: + name1
0522: + "] == Phase2["
0523: + name2
0524: + "]");
0525: return true;
0526: }
0527:
0528: if ((list1 != null) && (list2 != null)) {
0529: int size1 = list1.size();
0530: int size2 = list2.size();
0531:
0532: if (size1 != size2) {
0533: log
0534: .debug("MessageContextSaveATest: comparePhases: Phase1["
0535: + name1
0536: + "] != Phase2["
0537: + name2
0538: + "] - mismatched size of handler lists");
0539: return false;
0540: }
0541:
0542: for (int j = 0; j < size1; j++) {
0543: Object obj1 = list1.get(j);
0544: Object obj2 = list2.get(j);
0545:
0546: if ((obj1 == null) && (obj2 == null)) {
0547: // ok
0548: } else if ((obj1 != null) && (obj2 != null)) {
0549: boolean check;
0550:
0551: if (obj1 instanceof Phase) {
0552: check = comparePhases(obj1, obj2);
0553: } else {
0554: // must be a handler
0555: check = compareHandlers(obj1, obj2);
0556: }
0557:
0558: if (!check) {
0559: log
0560: .debug("MessageContextSaveATest: comparePhases: Phase1["
0561: + name1
0562: + "] != Phase2["
0563: + name2
0564: + "] - mismatched handler lists");
0565: return false;
0566: }
0567: } else {
0568: // mismatch
0569: log
0570: .debug("MessageContextSaveATest: comparePhases: Phase1["
0571: + name1
0572: + "] != Phase2["
0573: + name2
0574: + "] - mismatched handler lists");
0575: return false;
0576: }
0577: }
0578:
0579: // if we got here, the comparison completed ok
0580: // with a match
0581:
0582: log
0583: .debug("MessageContextSaveATest: comparePhases: Phase1["
0584: + name1
0585: + "] == Phase2["
0586: + name2
0587: + "] - matched handler lists");
0588: return true;
0589: }
0590:
0591: } catch (Exception e) {
0592: // some error
0593: e.printStackTrace();
0594: }
0595: }
0596:
0597: log
0598: .debug("MessageContextSaveATest: comparePhases: Phase1[] != Phase2[]");
0599: return false;
0600: }
0601:
0602: private void showMcMap(HashMap map) {
0603: if ((map != null) && (!map.isEmpty())) {
0604: Iterator itList = map.keySet().iterator();
0605:
0606: while (itList.hasNext()) {
0607: String key = (String) itList.next();
0608:
0609: MessageContext value = (MessageContext) map.get(key);
0610: String valueID = null;
0611:
0612: if (value != null) {
0613: valueID = value.getMessageID();
0614:
0615: log
0616: .debug("MessageContextSaveATest: showMcMap: Message context ID["
0617: + valueID
0618: + "] Key Label ["
0619: + key
0620: + "]");
0621:
0622: }
0623: }
0624: } else {
0625: log
0626: .debug("MessageContextSaveATest: showMcMap: No entries to display for message contexts table.");
0627: }
0628: }
0629:
0630: // this checks the save/restore of a message context that hasn't been
0631: // through the engine to simulate what some WS-RM implementations
0632: // need to do - make a simple message context for a RM ack or other
0633: // simple message
0634: public void testSimpleMC() throws Exception {
0635: String title = "MessageContextSaveATest:testSimpleMC(): ";
0636: log.debug(title + "start - - - - - - - - - - - - - - - -");
0637:
0638: MessageContext simpleMsg = new MessageContext();
0639: MessageContext restoredSimpleMsg = null;
0640:
0641: File theFile = null;
0642: String theFilename = null;
0643:
0644: boolean savedMessageContext = false;
0645: boolean restoredMessageContext = false;
0646: boolean comparesOk = false;
0647:
0648: try {
0649: theFile = File.createTempFile("Simple", null);
0650: theFilename = theFile.getName();
0651: log.debug(title + "temp file = [" + theFilename + "]");
0652: } catch (Exception ex) {
0653: log.debug(title + "error creating temp file = ["
0654: + ex.getMessage() + "]");
0655: theFile = null;
0656: }
0657:
0658: if (theFile != null) {
0659: // ---------------------------------------------------------
0660: // save to the temporary file
0661: // ---------------------------------------------------------
0662: try {
0663: // setup an output stream to a physical file
0664: FileOutputStream outStream = new FileOutputStream(
0665: theFile);
0666:
0667: // attach a stream capable of writing objects to the
0668: // stream connected to the file
0669: ObjectOutputStream outObjStream = new ObjectOutputStream(
0670: outStream);
0671:
0672: // try to save the message context
0673: log.debug(title + "saving message context.....");
0674: savedMessageContext = false;
0675: outObjStream.writeObject(simpleMsg);
0676:
0677: // close out the streams
0678: outObjStream.flush();
0679: outObjStream.close();
0680: outStream.flush();
0681: outStream.close();
0682:
0683: savedMessageContext = true;
0684: log.debug(title + "....saved message context.....");
0685:
0686: long filesize = theFile.length();
0687: log.debug(title + "file size after save [" + filesize
0688: + "] temp file = [" + theFilename + "]");
0689:
0690: } catch (Exception ex2) {
0691: log.debug(title
0692: + "error with saving message context = ["
0693: + ex2.getClass().getName() + " : "
0694: + ex2.getMessage() + "]");
0695: ex2.printStackTrace();
0696: }
0697:
0698: assertTrue(savedMessageContext);
0699:
0700: // ---------------------------------------------------------
0701: // restore from the temporary file
0702: // ---------------------------------------------------------
0703: try {
0704: // setup an input stream to the file
0705: FileInputStream inStream = new FileInputStream(theFile);
0706:
0707: // attach a stream capable of reading objects from the
0708: // stream connected to the file
0709: ObjectInputStream inObjStream = new ObjectInputStream(
0710: inStream);
0711:
0712: // try to restore the message context
0713: log.debug(title + "restoring a message context.....");
0714: restoredMessageContext = false;
0715:
0716: restoredSimpleMsg = (MessageContext) inObjStream
0717: .readObject();
0718: inObjStream.close();
0719: inStream.close();
0720:
0721: restoredSimpleMsg.activate(configurationContext);
0722:
0723: restoredMessageContext = true;
0724: log.debug(title + "....restored message context.....");
0725:
0726: // compare to original execution chain
0727: ArrayList restored_execChain = restoredSimpleMsg
0728: .getExecutionChain();
0729: ArrayList orig_execChain = simpleMsg
0730: .getExecutionChain();
0731:
0732: comparesOk = ObjectStateUtils.isEquivalent(
0733: restored_execChain, orig_execChain, false);
0734: log.debug(title + "execution chain equivalency ["
0735: + comparesOk + "]");
0736: assertTrue(comparesOk);
0737:
0738: // check executed list
0739: Iterator restored_executed_it = restoredSimpleMsg
0740: .getExecutedPhases();
0741: Iterator orig_executed_it = simpleMsg
0742: .getExecutedPhases();
0743: if ((restored_executed_it != null)
0744: && (orig_executed_it != null)) {
0745: while (restored_executed_it.hasNext()
0746: && orig_executed_it.hasNext()) {
0747: Object p1 = restored_executed_it.next();
0748: Object p2 = orig_executed_it.next();
0749:
0750: comparesOk = comparePhases(p1, p2);
0751: log
0752: .debug(title
0753: + "executed phase list: compare phases ["
0754: + comparesOk + "]");
0755: assertTrue(comparesOk);
0756: }
0757: } else {
0758: // problem with the executed lists
0759: assertTrue(false);
0760: }
0761:
0762: } catch (Exception ex2) {
0763: log.debug(title
0764: + "error with saving message context = ["
0765: + ex2.getClass().getName() + " : "
0766: + ex2.getMessage() + "]");
0767: ex2.printStackTrace();
0768: }
0769:
0770: assertTrue(restoredMessageContext);
0771:
0772: // if the save/restore of the message context succeeded,
0773: // then don't keep the temporary file around
0774: boolean removeTmpFile = savedMessageContext
0775: && restoredMessageContext && comparesOk;
0776: if (removeTmpFile) {
0777: try {
0778: theFile.delete();
0779: } catch (Exception e) {
0780: // just absorb it
0781: }
0782: }
0783: }
0784:
0785: log.debug(title + "end - - - - - - - - - - - - - - - -");
0786: }
0787:
0788: // this checks the save/restore of a message context that has
0789: // some properties set
0790: public void testMcProperties() throws Exception {
0791: String title = "MessageContextSaveATest:testMcProperties(): ";
0792: log.debug(title + "start - - - - - - - - - - - - - - - -");
0793:
0794: MessageContext simpleMsg = new MessageContext();
0795: MessageContext restoredSimpleMsg = null;
0796:
0797: simpleMsg.setProperty("key1", "value1");
0798: simpleMsg.setProperty("key2", null);
0799: simpleMsg.setProperty("key3", new Integer(3));
0800: simpleMsg.setProperty("key4", new Long(4L));
0801:
0802: File theFile = null;
0803: String theFilename = null;
0804:
0805: boolean pause = false;
0806: boolean savedMessageContext = false;
0807: boolean restoredMessageContext = false;
0808: boolean comparesOk = false;
0809:
0810: try {
0811: theFile = File.createTempFile("McProps", null);
0812: theFilename = theFile.getName();
0813: log.debug(title + "temp file = [" + theFilename + "]");
0814: } catch (Exception ex) {
0815: log.debug(title + "error creating temp file = ["
0816: + ex.getMessage() + "]");
0817: theFile = null;
0818: }
0819:
0820: if (theFile != null) {
0821: // ---------------------------------------------------------
0822: // save to the temporary file
0823: // ---------------------------------------------------------
0824: try {
0825: // setup an output stream to a physical file
0826: FileOutputStream outStream = new FileOutputStream(
0827: theFile);
0828:
0829: // attach a stream capable of writing objects to the
0830: // stream connected to the file
0831: ObjectOutputStream outObjStream = new ObjectOutputStream(
0832: outStream);
0833:
0834: // try to save the message context
0835: log.debug(title + "saving message context.....");
0836: savedMessageContext = false;
0837: outObjStream.writeObject(simpleMsg);
0838:
0839: // close out the streams
0840: outObjStream.flush();
0841: outObjStream.close();
0842: outStream.flush();
0843: outStream.close();
0844:
0845: savedMessageContext = true;
0846: log.debug(title + "....saved message context.....");
0847:
0848: long filesize = theFile.length();
0849: log.debug(title + "file size after save [" + filesize
0850: + "] temp file = [" + theFilename + "]");
0851:
0852: } catch (Exception ex2) {
0853: log.debug(title
0854: + "error with saving message context = ["
0855: + ex2.getClass().getName() + " : "
0856: + ex2.getMessage() + "]");
0857: ex2.printStackTrace();
0858: }
0859:
0860: assertTrue(savedMessageContext);
0861:
0862: // ---------------------------------------------------------
0863: // restore from the temporary file
0864: // ---------------------------------------------------------
0865: try {
0866: // setup an input stream to the file
0867: FileInputStream inStream = new FileInputStream(theFile);
0868:
0869: // attach a stream capable of reading objects from the
0870: // stream connected to the file
0871: ObjectInputStream inObjStream = new ObjectInputStream(
0872: inStream);
0873:
0874: // try to restore the message context
0875: log.debug(title + "restoring a message context.....");
0876: restoredMessageContext = false;
0877:
0878: restoredSimpleMsg = (MessageContext) inObjStream
0879: .readObject();
0880: inObjStream.close();
0881: inStream.close();
0882:
0883: restoredSimpleMsg.activate(configurationContext);
0884:
0885: restoredMessageContext = true;
0886: log.debug(title + "....restored message context.....");
0887:
0888: // compare to original execution chain
0889: ArrayList restored_execChain = restoredSimpleMsg
0890: .getExecutionChain();
0891: ArrayList orig_execChain = simpleMsg
0892: .getExecutionChain();
0893:
0894: comparesOk = ObjectStateUtils.isEquivalent(
0895: restored_execChain, orig_execChain, false);
0896: log.debug(title + "execution chain equivalency ["
0897: + comparesOk + "]");
0898: assertTrue(comparesOk);
0899:
0900: // check executed list
0901: Iterator restored_executed_it = restoredSimpleMsg
0902: .getExecutedPhases();
0903: Iterator orig_executed_it = simpleMsg
0904: .getExecutedPhases();
0905: if ((restored_executed_it != null)
0906: && (orig_executed_it != null)) {
0907: while (restored_executed_it.hasNext()
0908: && orig_executed_it.hasNext()) {
0909: Object p1 = restored_executed_it.next();
0910: Object p2 = orig_executed_it.next();
0911:
0912: comparesOk = comparePhases(p1, p2);
0913: log
0914: .debug(title
0915: + "executed phase list: compare phases ["
0916: + comparesOk + "]");
0917: assertTrue(comparesOk);
0918: }
0919: } else {
0920: // problem with the executed lists
0921: assertTrue(false);
0922: }
0923:
0924: // check the properties
0925: String value1 = (String) restoredSimpleMsg
0926: .getProperty("key1");
0927: Object value2 = restoredSimpleMsg.getProperty("key2");
0928: Integer value3 = (Integer) restoredSimpleMsg
0929: .getProperty("key3");
0930: Long value4 = (Long) restoredSimpleMsg
0931: .getProperty("key4");
0932:
0933: assertEquals("value1", value1);
0934: assertNull(value2);
0935:
0936: boolean isOk = false;
0937: if ((value3 != null) && value3.equals(new Integer(3))) {
0938: isOk = true;
0939: }
0940: assertTrue(isOk);
0941:
0942: if ((value4 != null) && value4.equals(new Long(4L))) {
0943: isOk = true;
0944: }
0945: assertTrue(isOk);
0946:
0947: } catch (Exception ex2) {
0948: log.debug(title
0949: + "error with restoring message context = ["
0950: + ex2.getClass().getName() + " : "
0951: + ex2.getMessage() + "]");
0952: ex2.printStackTrace();
0953: }
0954:
0955: assertTrue(restoredMessageContext);
0956:
0957: // if the save/restore of the message context succeeded,
0958: // then don't keep the temporary file around
0959: boolean removeTmpFile = savedMessageContext
0960: && restoredMessageContext && comparesOk;
0961: if (removeTmpFile) {
0962: try {
0963: theFile.delete();
0964: } catch (Exception e) {
0965: // just absorb it
0966: }
0967: }
0968: }
0969:
0970: log.debug(title + "end - - - - - - - - - - - - - - - -");
0971: }
0972:
0973: public void testMapping() throws Exception {
0974:
0975: String title = "MessageContextSaveATest:testMapping(): ";
0976: log.debug(title + "start - - - - - - - - - - - - - - - -");
0977:
0978: MessageContext restoredMC = null;
0979:
0980: //---------------------------------------------------------------------
0981: // make sure that the operation context messageContexts table
0982: // has an entry for the message context that we working with
0983: //---------------------------------------------------------------------
0984: // look at the OperationContext messageContexts table
0985: HashMap mcMap1 = mc.getOperationContext().getMessageContexts();
0986:
0987: if ((mcMap1 == null) || (mcMap1.isEmpty())) {
0988: mc.getAxisOperation().addMessageContext(mc,
0989: mc.getOperationContext());
0990: }
0991: // update the table
0992: mcMap1 = mc.getOperationContext().getMessageContexts();
0993:
0994: log
0995: .debug(title
0996: + "- - - - - original message contexts table- - - - - - - - - - -");
0997: showMcMap(mcMap1);
0998:
0999: //---------------------------------------------------------------------
1000: // save and restore the message context
1001: //---------------------------------------------------------------------
1002:
1003: File theFile;
1004: String theFilename = null;
1005:
1006: boolean pause = false;
1007: boolean savedMessageContext = false;
1008: boolean restoredMessageContext = false;
1009: boolean comparesOk = false;
1010:
1011: try {
1012: theFile = File.createTempFile("McMappings", null);
1013: theFilename = theFile.getName();
1014: log.debug(title + "temp file = [" + theFilename + "]");
1015: } catch (Exception ex) {
1016: log.debug(title + "error creating temp file = ["
1017: + ex.getMessage() + "]");
1018: theFile = null;
1019: }
1020:
1021: if (theFile != null) {
1022: // ---------------------------------------------------------
1023: // save to the temporary file
1024: // ---------------------------------------------------------
1025: try {
1026: // setup an output stream to a physical file
1027: FileOutputStream outStream = new FileOutputStream(
1028: theFile);
1029:
1030: // attach a stream capable of writing objects to the
1031: // stream connected to the file
1032: ObjectOutputStream outObjStream = new ObjectOutputStream(
1033: outStream);
1034:
1035: // try to save the message context
1036: log.debug(title + "saving message context.....");
1037: savedMessageContext = false;
1038: outObjStream.writeObject(mc);
1039:
1040: // close out the streams
1041: outObjStream.flush();
1042: outObjStream.close();
1043: outStream.flush();
1044: outStream.close();
1045:
1046: savedMessageContext = true;
1047: log.debug(title + "....saved message context.....");
1048:
1049: long filesize = theFile.length();
1050: log.debug(title + "file size after save [" + filesize
1051: + "] temp file = [" + theFilename + "]");
1052:
1053: } catch (Exception ex2) {
1054: log.debug(title
1055: + "error with saving message context = ["
1056: + ex2.getClass().getName() + " : "
1057: + ex2.getMessage() + "]");
1058: ex2.printStackTrace();
1059: }
1060:
1061: assertTrue(savedMessageContext);
1062:
1063: // ---------------------------------------------------------
1064: // restore from the temporary file
1065: // ---------------------------------------------------------
1066: try {
1067: // setup an input stream to the file
1068: FileInputStream inStream = new FileInputStream(theFile);
1069:
1070: // attach a stream capable of reading objects from the
1071: // stream connected to the file
1072: ObjectInputStream inObjStream = new ObjectInputStream(
1073: inStream);
1074:
1075: // try to restore the message context
1076: log.debug(title + "restoring a message context.....");
1077: restoredMessageContext = false;
1078:
1079: restoredMC = (MessageContext) inObjStream.readObject();
1080: inObjStream.close();
1081: inStream.close();
1082:
1083: restoredMC.activate(configurationContext);
1084:
1085: restoredMessageContext = true;
1086: log.debug(title + "....restored message context.....");
1087:
1088: // get the table after the restore
1089: HashMap mcMap2 = restoredMC.getOperationContext()
1090: .getMessageContexts();
1091:
1092: log
1093: .debug("MessageContextSaveATest:testMapping(): - - - - - restored message contexts table- - - - - - - - - - -");
1094: showMcMap(mcMap2);
1095:
1096: boolean okMap = compareMCMaps(mcMap1, mcMap2);
1097: assertTrue(okMap);
1098:
1099: } catch (Exception ex2) {
1100: log.debug(title
1101: + "error with restoring message context = ["
1102: + ex2.getClass().getName() + " : "
1103: + ex2.getMessage() + "]");
1104: ex2.printStackTrace();
1105: }
1106:
1107: assertTrue(restoredMessageContext);
1108:
1109: // if the save/restore of the message context succeeded,
1110: // then don't keep the temporary file around
1111: boolean removeTmpFile = savedMessageContext
1112: && restoredMessageContext && comparesOk;
1113: if (removeTmpFile) {
1114: try {
1115: theFile.delete();
1116: } catch (Exception e) {
1117: // just absorb it
1118: }
1119: }
1120: }
1121:
1122: log.debug(title + "end - - - - - - - - - - - - - - - -");
1123:
1124: }
1125:
1126: private boolean compareMCMaps(HashMap m1, HashMap m2) {
1127: String title = "MessageContextSaveATest:compareMCMaps(): ";
1128:
1129: if ((m1 != null) && (m2 != null)) {
1130: int size1 = m1.size();
1131: int size2 = m2.size();
1132:
1133: if (size1 != size2) {
1134: log.debug(title + "MISMATCH: map1 size [" + size1
1135: + "] != map2 size [" + size2 + "]");
1136: return false;
1137: }
1138:
1139: String id1 = null;
1140: String id2 = null;
1141:
1142: // check the keys, ordering is not important between the two maps
1143: Iterator it1 = m1.keySet().iterator();
1144:
1145: while (it1.hasNext()) {
1146: String key1 = (String) it1.next();
1147: MessageContext value1 = (MessageContext) m1.get(key1);
1148:
1149: if (value1 != null) {
1150: id1 = value1.getMessageID();
1151:
1152: MessageContext value2 = (MessageContext) m2
1153: .get(key1);
1154:
1155: if (value2 != null) {
1156: id2 = value2.getMessageID();
1157: } else {
1158: // mismatch
1159: log
1160: .debug(title
1161: + "MISMATCH: no message context in one of the tables for key ["
1162: + key1 + "]");
1163: return false;
1164: }
1165:
1166: if ((id1 != null) && (id2 != null)) {
1167: if (!id1.equals(id2)) {
1168: // mismatch
1169: log.debug(title
1170: + "MISMATCH: messageID_1 [" + id1
1171: + "] != messageID_2 [" + id2
1172: + "]");
1173: return false;
1174: }
1175: } else {
1176: // null values, can't tell
1177: log
1178: .debug(title
1179: + "MISMATCH: one or more null message IDs");
1180: return false;
1181: }
1182: }
1183: }
1184: return true;
1185: } else if ((m1 == null) && (m2 == null)) {
1186: return true;
1187: } else {
1188: // mismatch
1189: log.debug(title + "MISMATCH: one of the tables is null");
1190: return false;
1191: }
1192: }
1193:
1194: public class TempHandler extends AbstractHandler {
1195: private Integer handlerID = null;
1196:
1197: private File theFile = null;
1198: private String theFilename = null;
1199:
1200: private boolean pause = false;
1201: private boolean savedMessageContext = false;
1202: private boolean restoredMessageContext = false;
1203: private boolean comparesOk = false;
1204:
1205: //-----------------------------------------------------------------
1206: // constructors
1207: //-----------------------------------------------------------------
1208:
1209: public TempHandler() {
1210: this .handlerID = new Integer(-5);
1211: }
1212:
1213: public TempHandler(int index, boolean pause) {
1214: this .handlerID = new Integer(index);
1215: this .pause = pause;
1216: init(new HandlerDescription("handler" + index));
1217: }
1218:
1219: public TempHandler(int index) {
1220: this .handlerID = new Integer(index);
1221: init(new HandlerDescription("handler" + index));
1222: }
1223:
1224: //-----------------------------------------------------------------
1225: // methods
1226: //-----------------------------------------------------------------
1227:
1228: public int getHandlerID() {
1229: if (handlerID != null) {
1230: return handlerID.intValue();
1231: }
1232:
1233: return -5;
1234: }
1235:
1236: public InvocationResponse invoke(MessageContext msgContext)
1237: throws AxisFault {
1238: String title = "TempHandler[" + getHandlerID()
1239: + "]:invoke(): ";
1240: log.debug(title + "pause = [" + pause + "]");
1241: savedMessageContext = false;
1242: restoredMessageContext = false;
1243:
1244: if (pause) {
1245: log.debug(title + "msgContext.pause()");
1246: msgContext.pause();
1247: pause = false;
1248:
1249: try {
1250: theFile = File.createTempFile("mcSave", null);
1251: theFilename = theFile.getName();
1252: log.debug(title + "temp file = [" + theFilename
1253: + "]");
1254: } catch (Exception ex) {
1255: log.debug(title + "error creating temp file = ["
1256: + ex.getMessage() + "]");
1257: theFile = null;
1258: }
1259:
1260: if (theFile != null) {
1261: // ---------------------------------------------------------
1262: // save to the temporary file
1263: // ---------------------------------------------------------
1264: try {
1265: // setup an output stream to a physical file
1266: FileOutputStream outStream = new FileOutputStream(
1267: theFile);
1268:
1269: // attach a stream capable of writing objects to the
1270: // stream connected to the file
1271: ObjectOutputStream outObjStream = new ObjectOutputStream(
1272: outStream);
1273:
1274: // try to save the message context
1275: log
1276: .debug(title
1277: + "saving message context.....");
1278: savedMessageContext = false;
1279: outObjStream.writeObject(msgContext);
1280:
1281: // close out the streams
1282: outObjStream.flush();
1283: outObjStream.close();
1284: outStream.flush();
1285: outStream.close();
1286:
1287: savedMessageContext = true;
1288: log.debug(title
1289: + "....saved message context.....");
1290:
1291: long filesize = theFile.length();
1292: log.debug(title + "file size after save ["
1293: + filesize + "] temp file = ["
1294: + theFilename + "]");
1295:
1296: } catch (Exception ex2) {
1297: log
1298: .debug(title
1299: + "error with saving message context = ["
1300: + ex2.getClass().getName()
1301: + " : " + ex2.getMessage()
1302: + "]");
1303: ex2.printStackTrace();
1304: }
1305:
1306: assertTrue(savedMessageContext);
1307:
1308: // ---------------------------------------------------------
1309: // restore from the temporary file
1310: // ---------------------------------------------------------
1311: try {
1312: // setup an input stream to the file
1313: FileInputStream inStream = new FileInputStream(
1314: theFile);
1315:
1316: // attach a stream capable of reading objects from the
1317: // stream connected to the file
1318: ObjectInputStream inObjStream = new ObjectInputStream(
1319: inStream);
1320:
1321: // try to restore the message context
1322: log.debug(title
1323: + "restoring a message context.....");
1324: restoredMessageContext = false;
1325:
1326: MessageContext msgContext2 = (MessageContext) inObjStream
1327: .readObject();
1328: inObjStream.close();
1329: inStream.close();
1330:
1331: msgContext2.activate(configurationContext);
1332:
1333: restoredMessageContext = true;
1334: log.debug(title
1335: + "....restored message context.....");
1336:
1337: // compare to original execution chain
1338: ArrayList restored_execChain = msgContext2
1339: .getExecutionChain();
1340: ArrayList orig_execChain = msgContext
1341: .getExecutionChain();
1342:
1343: comparesOk = ObjectStateUtils.isEquivalent(
1344: restored_execChain, orig_execChain,
1345: false);
1346: log.debug(title
1347: + "execution chain equivalency ["
1348: + comparesOk + "]");
1349: assertTrue(comparesOk);
1350:
1351: // check executed list
1352: Iterator restored_executed_it = msgContext2
1353: .getExecutedPhases();
1354: Iterator orig_executed_it = msgContext
1355: .getExecutedPhases();
1356: if ((restored_executed_it != null)
1357: && (orig_executed_it != null)) {
1358: while (restored_executed_it.hasNext()
1359: && orig_executed_it.hasNext()) {
1360: Object p1 = restored_executed_it.next();
1361: Object p2 = orig_executed_it.next();
1362:
1363: comparesOk = comparePhases(p1, p2);
1364: log
1365: .debug(title
1366: + "executed phase list: compare phases ["
1367: + comparesOk + "]");
1368: assertTrue(comparesOk);
1369: }
1370: } else {
1371: // problem with the executed lists
1372: assertTrue(false);
1373: }
1374:
1375: // now put the restored message context in the global
1376: // variable for the test
1377: mc2 = msgContext2;
1378: } catch (Exception ex2) {
1379: log
1380: .debug(title
1381: + "error with saving message context = ["
1382: + ex2.getClass().getName()
1383: + " : " + ex2.getMessage()
1384: + "]");
1385: ex2.printStackTrace();
1386: }
1387:
1388: assertTrue(restoredMessageContext);
1389:
1390: // if the save/restore of the message context succeeded,
1391: // then don't keep the temporary file around
1392: boolean removeTmpFile = savedMessageContext
1393: && restoredMessageContext && comparesOk;
1394: if (removeTmpFile) {
1395: try {
1396: theFile.delete();
1397: } catch (Exception e) {
1398: // just absorb it
1399: }
1400: }
1401: }
1402:
1403: return InvocationResponse.SUSPEND;
1404:
1405: } else {
1406: log.debug(title + "executedHandlers.add(" + handlerID
1407: + ")");
1408: executedHandlers.add(handlerID);
1409: }
1410:
1411: return InvocationResponse.CONTINUE;
1412: }
1413:
1414: }
1415: }
|