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.component.ServiceBroker;
030: import org.cougaar.core.mts.MessageAddress;
031: import org.cougaar.core.mts.MessageAttributes;
032: import org.cougaar.mts.base.CommFailureException;
033: import org.cougaar.mts.base.DestinationLink;
034: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
035: import org.cougaar.mts.base.LinkProtocol;
036: import org.cougaar.mts.base.LinkProtocolService;
037: import org.cougaar.mts.base.MessageDeliverer;
038: import org.cougaar.mts.base.MessageDelivererDelegateImplBase;
039: import org.cougaar.mts.base.MisdeliveredMessageException;
040: import org.cougaar.mts.base.NameLookupException;
041: import org.cougaar.mts.base.RPCLinkProtocol;
042: import org.cougaar.mts.base.StandardAspect;
043: import org.cougaar.mts.base.UnregisteredNameException;
044:
045: /**
046: * This test Aspect is an example of using a {@link
047: * LinkProtocol}-specific service.
048: */
049: public class ServiceTestAspect extends StandardAspect {
050: private LinkProtocolService svc;
051:
052: public ServiceTestAspect() {
053: }
054:
055: private void test(String text, MessageAddress addr) {
056: synchronized (this ) {
057: if (svc == null) {
058: ServiceBroker sb = getServiceBroker();
059: Object raw = sb.getService(this ,
060: RPCLinkProtocol.Service.class, null);
061: svc = (LinkProtocolService) raw;
062: }
063: }
064:
065: if (svc != null && loggingService.isInfoEnabled()) {
066: loggingService.info("LinkProtocol Service " + text + ":"
067: + addr + "->" + svc.addressKnown(addr));
068: }
069:
070: }
071:
072: private AttributedMessage send(AttributedMessage message) {
073: test("send", message.getTarget());
074: return message;
075: }
076:
077: private AttributedMessage receive(AttributedMessage message) {
078: test("receive", message.getOriginator());
079: return message;
080: }
081:
082: public Object getDelegate(Object delegate, Class type) {
083: if (type == DestinationLink.class) {
084: DestinationLink link = (DestinationLink) delegate;
085: return new TestDestinationLink(link);
086: } else {
087: return null;
088: }
089: }
090:
091: public Object getReverseDelegate(Object delegate, Class type) {
092: if (type == MessageDeliverer.class) {
093: return new TestDeliverer((MessageDeliverer) delegate);
094: } else {
095: return null;
096: }
097: }
098:
099: private class TestDestinationLink extends
100: DestinationLinkDelegateImplBase {
101: private TestDestinationLink(DestinationLink link) {
102: super (link);
103: }
104:
105: public MessageAttributes forwardMessage(
106: AttributedMessage message)
107: throws UnregisteredNameException, NameLookupException,
108: CommFailureException, MisdeliveredMessageException {
109: return super .forwardMessage(send(message));
110: }
111:
112: }
113:
114: private class TestDeliverer extends
115: MessageDelivererDelegateImplBase {
116: private TestDeliverer(MessageDeliverer deliverer) {
117: super (deliverer);
118: }
119:
120: public MessageAttributes deliverMessage(AttributedMessage m,
121: MessageAddress dest)
122: throws MisdeliveredMessageException {
123: return super.deliverMessage(receive(m), dest);
124: }
125:
126: }
127: }
|