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: package org.cougaar.mts.rmi;
027:
028: import org.cougaar.core.mts.MessageAddress;
029: import org.cougaar.core.mts.MessageAttributes;
030: import org.cougaar.core.mts.SerializationUtils;
031: import org.cougaar.mts.base.CommFailureException;
032: import org.cougaar.mts.base.CougaarIOException;
033: import org.cougaar.mts.base.LinkProtocol;
034: import org.cougaar.mts.base.MisdeliveredMessageException;
035: import org.cougaar.mts.base.SocketFactory;
036: import org.cougaar.mts.std.AttributedMessage;
037:
038: /**
039: * This {@link LinkProtocol} is a simple extension of {@link
040: * RMILinkProtocol} that uses preserialization to send byte-arrays via
041: * the {@link SerializedMT} interface rather than AttributedMessages via the
042: * {@link MT} interface.
043: */
044: public class SerializedRMILinkProtocol extends RMILinkProtocol {
045:
046: public SerializedRMILinkProtocol() {
047: super ();
048: }
049:
050: protected String getProtocolType() {
051: return "-SerializedRMI";
052: }
053:
054: // Is this different?
055: protected int computeCost(AttributedMessage message) {
056: return super .computeCost(message);
057: }
058:
059: protected MTImpl makeMTImpl(MessageAddress myAddress,
060: SocketFactory socfac) throws java.rmi.RemoteException {
061: return new SerializedMTImpl(myAddress, getServiceBroker(),
062: socfac);
063: }
064:
065: protected MessageAttributes doForwarding(MT remote,
066: AttributedMessage message)
067: throws MisdeliveredMessageException,
068: java.rmi.RemoteException, CommFailureException {
069: if (remote instanceof SerializedMT) {
070: byte[] messageBytes = null;
071: try {
072: messageBytes = SerializationUtils.toByteArray(message);
073: } catch (CougaarIOException mex) {
074: throw new CommFailureException(mex);
075: } catch (java.io.IOException iox) {
076: // What would this mean?
077: }
078:
079: byte[] res = null;
080: try {
081: res = ((SerializedMT) remote)
082: .rerouteMessage(messageBytes);
083: } catch (CougaarIOException mex) {
084: throw new CommFailureException(mex);
085: } catch (java.rmi.RemoteException remote_ex) {
086: Throwable cause = remote_ex.getCause();
087: checkForMisdelivery(cause, message);
088: // Not a misdelivery - rethrow the remote exception
089: throw remote_ex;
090: } catch (IllegalArgumentException illegal_arg) {
091: checkForMisdelivery(illegal_arg, message);
092: // Not a misdelivery - rethrow the exception
093: throw illegal_arg;
094: }
095:
096: MessageAttributes attrs = null;
097: try {
098: attrs = (MessageAttributes) SerializationUtils
099: .fromByteArray(res);
100: } catch (CougaarIOException mex) {
101: throw new CommFailureException(mex);
102: } catch (java.io.IOException iox) {
103: // What would this mean?
104: } catch (ClassNotFoundException cnf) {
105: // What would this mean?
106: }
107: return attrs;
108: } else {
109: return super.doForwarding(remote, message);
110: }
111: }
112:
113: }
|