001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.mts.std;
028:
029: import org.cougaar.core.mts.MessageAttributes;
030: import org.cougaar.core.mts.SerializationUtils;
031:
032: import org.cougaar.mts.base.MisdeliveredMessageException;
033: import org.cougaar.mts.base.CommFailureException;
034: import org.cougaar.mts.base.UnregisteredNameException;
035: import org.cougaar.mts.base.NameLookupException;
036: import org.cougaar.mts.base.DestinationLink;
037: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
038: import org.cougaar.mts.base.LoopbackLinkProtocol;
039: import org.cougaar.mts.base.CougaarIOException;
040: import org.cougaar.mts.base.StandardAspect;
041:
042: /**
043: * This is debugging Aspect forces serialization on the {@link
044: * LoopbackLinkProtocol}. This can help check for issues related to
045: * serialization that wouldn't arise otherwise.
046: */
047: public class SerializationAspect extends StandardAspect {
048:
049: public SerializationAspect() {
050: }
051:
052: public Object getDelegate(Object object, Class type) {
053: if (type == DestinationLink.class) {
054: DestinationLink link = (DestinationLink) object;
055: if (link.getProtocolClass() == LoopbackLinkProtocol.class)
056: return new SerializingDestinationLink(link);
057: else
058: return null;
059: } else {
060: return null;
061: }
062: }
063:
064: private class SerializingDestinationLink extends
065: DestinationLinkDelegateImplBase
066:
067: {
068: SerializingDestinationLink(DestinationLink link) {
069: super (link);
070: }
071:
072: public synchronized MessageAttributes forwardMessage(
073: AttributedMessage message)
074: throws UnregisteredNameException, NameLookupException,
075: CommFailureException, MisdeliveredMessageException
076:
077: {
078: byte[] data = null;
079: AttributedMessage clone = null;
080: try {
081: if (loggingService.isInfoEnabled())
082: loggingService.info("Serializing " + message);
083: data = SerializationUtils.toByteArray(message);
084: if (loggingService.isInfoEnabled())
085: loggingService.info("Serialized " + message);
086: if (loggingService.isInfoEnabled())
087: loggingService.info("Deserializing");
088: clone = (AttributedMessage) SerializationUtils
089: .fromByteArray(data);
090: if (loggingService.isInfoEnabled())
091: loggingService.info("Deserialized as " + clone);
092: } catch (CougaarIOException cougaar_iox) {
093: loggingService
094: .error("SerializationAspect", cougaar_iox);
095: throw new CommFailureException(cougaar_iox);
096: } catch (java.io.IOException iox) {
097: loggingService.error("SerializationAspect", iox);
098: return null;
099: } catch (ClassNotFoundException cnf) {
100: loggingService.error("SerializationAspect", cnf);
101: return null;
102: }
103:
104: return super.forwardMessage(clone);
105:
106: }
107:
108: }
109:
110: }
|