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 java.io.InputStream;
030: import java.io.ObjectInput;
031: import java.io.ObjectOutput;
032: import java.io.OutputStream;
033:
034: import org.cougaar.core.mts.MessageAttributes;
035: import org.cougaar.core.mts.ProtectedInputStream;
036: import org.cougaar.core.mts.ProtectedOutputStream;
037: import org.cougaar.core.service.MessageProtectionService;
038: import org.cougaar.mts.base.CommFailureException;
039: import org.cougaar.mts.base.DestinationLink;
040: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
041: import org.cougaar.mts.base.MessageDeliverer;
042: import org.cougaar.mts.base.MessageDelivererDelegateImplBase;
043: import org.cougaar.mts.base.MessageProtectionServiceImpl;
044: import org.cougaar.mts.base.MessageReader;
045: import org.cougaar.mts.base.MessageReaderDelegateImplBase;
046: import org.cougaar.mts.base.MessageWriter;
047: import org.cougaar.mts.base.MessageWriterDelegateImplBase;
048: import org.cougaar.mts.base.MisdeliveredMessageException;
049: import org.cougaar.mts.base.NameLookupException;
050: import org.cougaar.mts.base.StandardAspect;
051: import org.cougaar.mts.base.UnregisteredNameException;
052:
053: /**
054: This Aspect puts the {@link MessageProtectionService} streams in
055: place. It also instantiates a default implementation for that
056: service if no other one is loaded.
057: */
058: public class MessageProtectionAspect extends StandardAspect {
059:
060: private static MessageProtectionService svc;
061:
062: static MessageProtectionService getMessageProtectionService() {
063: return svc;
064: }
065:
066: public void load() {
067: super .load();
068: svc = (MessageProtectionService) getServiceBroker().getService(
069: this , MessageProtectionService.class, null);
070: // System.err.println("Got " +svc+ " from service broker");
071: // Temporary, until NAI's service is available
072: if (svc == null)
073: svc = new MessageProtectionServiceImpl();
074: }
075:
076: public Object getDelegate(Object delegatee, Class type) {
077: if (type == MessageWriter.class) {
078: MessageWriter wtr = (MessageWriter) delegatee;
079: return new ProtectedMessageWriter(wtr);
080: } else if (type == MessageReader.class) {
081: MessageReader rdr = (MessageReader) delegatee;
082: return new ProtectedMessageReader(rdr);
083: } else if (type == DestinationLink.class) {
084: DestinationLink link = (DestinationLink) delegatee;
085: return new ProtectedDestinationLink(link);
086: } else if (type == MessageDeliverer.class) {
087: MessageDeliverer deliverer = (MessageDeliverer) delegatee;
088: return new ProtectedDeliverer(deliverer);
089: } else {
090: return null;
091: }
092: }
093:
094: private class ProtectedMessageWriter extends
095: MessageWriterDelegateImplBase {
096: AttributedMessage msg;
097: ProtectedOutputStream stream;
098:
099: ProtectedMessageWriter(MessageWriter delegatee) {
100: super (delegatee);
101: }
102:
103: public void finalizeAttributes(AttributedMessage msg) {
104: this .msg = msg;
105: super .finalizeAttributes(msg);
106: }
107:
108: public OutputStream getObjectOutputStream(ObjectOutput oo)
109: throws java.io.IOException {
110: OutputStream os = super .getObjectOutputStream(oo);
111: stream = svc.getOutputStream(os, msg.getOriginator(), msg
112: .getTarget(), msg);
113: // System.err.println("Got " +stream+ " from " +svc);
114: return stream;
115: }
116:
117: public void finishOutput() throws java.io.IOException {
118: stream.finishOutput(msg);
119: super .finishOutput();
120: }
121: }
122:
123: private class ProtectedMessageReader extends
124: MessageReaderDelegateImplBase {
125: AttributedMessage msg;
126: ProtectedInputStream stream;
127:
128: ProtectedMessageReader(MessageReader delegatee) {
129: super (delegatee);
130: }
131:
132: public void finalizeAttributes(AttributedMessage msg) {
133: this .msg = msg;
134: super .finalizeAttributes(msg);
135: }
136:
137: public InputStream getObjectInputStream(ObjectInput oi)
138: throws java.io.IOException, ClassNotFoundException {
139: InputStream is = super .getObjectInputStream(oi);
140: stream = svc.getInputStream(is, msg.getOriginator(), msg
141: .getTarget(), msg);
142: // System.err.println("Got " +stream+ " from " +svc);
143: return stream;
144: }
145:
146: public void finishInput() throws java.io.IOException {
147: stream.finishInput(msg);
148: super .finishInput();
149: }
150:
151: }
152:
153: private class ProtectedDeliverer extends
154: MessageDelivererDelegateImplBase {
155: ProtectedDeliverer(MessageDeliverer delegatee) {
156: super (delegatee);
157: }
158:
159: // Only for debugging
160: // public MessageAttributes deliverMessage(AttributedMessage message,
161: // MessageAddress dest)
162: // throws MisdeliveredMessageException
163: // {
164: // System.out.println(" #### is streaming = " +
165: // message.getAttribute(MessageAttributes.IS_STREAMING_ATTRIBUTE) +
166: // " is encrypted = " +
167: // message.getAttribute(MessageAttributes.ENCRYPTED_SOCKET_ATTRIBUTE));
168: // return super.deliverMessage(message, dest);
169: // }
170:
171: }
172:
173: private class ProtectedDestinationLink extends
174: DestinationLinkDelegateImplBase {
175: ProtectedDestinationLink(DestinationLink delegatee) {
176: super (delegatee);
177: }
178:
179: public MessageAttributes forwardMessage(
180: AttributedMessage message)
181: throws UnregisteredNameException, NameLookupException,
182: CommFailureException, MisdeliveredMessageException {
183: // Register Aspect as a Message Streaming filter
184: message.addFilter(MessageProtectionAspect.this);
185:
186: return super.forwardMessage(message);
187: }
188:
189: }
190:
191: }
|