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.ByteArrayOutputStream;
030: import java.io.FilterOutputStream;
031: import java.io.ObjectOutput;
032: import java.io.OutputStream;
033:
034: import org.cougaar.mts.base.MessageWriter;
035: import org.cougaar.mts.base.MessageWriterDelegateImplBase;
036: import org.cougaar.mts.base.StandardAspect;
037:
038: /**
039: * This Aspect caches the serialized message in a byte array as its
040: * btyes pass by.
041: */
042: public class CachingStreamsAspect extends StandardAspect {
043:
044: public Object getDelegate(Object delegatee, Class type) {
045: if (type == MessageWriter.class) {
046: MessageWriter wtr = (MessageWriter) delegatee;
047: return new CachingMessageWriter(wtr);
048: } else {
049: return null;
050: }
051: }
052:
053: static class TeeOutputStream extends FilterOutputStream {
054: OutputStream other;
055:
056: TeeOutputStream(OutputStream stream1, OutputStream stream2) {
057: super (stream1);
058: other = stream2;
059: }
060:
061: public void write(int b) throws java.io.IOException {
062: out.write(b);
063: other.write(b);
064: }
065:
066: public void write(byte[] b, int off, int len)
067: throws java.io.IOException {
068: out.write(b, off, len);
069: other.write(b, off, len);
070: }
071:
072: public void write(byte[] b) throws java.io.IOException {
073: out.write(b);
074: other.write(b);
075: }
076:
077: }
078:
079: private class CachingMessageWriter extends
080: MessageWriterDelegateImplBase {
081:
082: private ByteArrayOutputStream byte_os;
083: private byte[] cache;
084:
085: CachingMessageWriter(MessageWriter delegatee) {
086: super (delegatee);
087: }
088:
089: public OutputStream getObjectOutputStream(ObjectOutput out)
090: throws java.io.IOException {
091: OutputStream raw_os = super .getObjectOutputStream(out);
092: byte_os = new ByteArrayOutputStream();
093: return new TeeOutputStream(raw_os, byte_os);
094: }
095:
096: public void finishOutput() throws java.io.IOException {
097: super .finishOutput();
098: byte_os.flush();
099: }
100:
101: public void postProcess() {
102: super.postProcess();
103: cache = byte_os.toByteArray();
104: }
105:
106: }
107:
108: }
|