001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.mlm.plugin.organization;
028:
029: import org.cougaar.glm.ldm.asset.Organization;
030: import org.cougaar.core.plugin.ComponentPlugin;
031: import org.cougaar.core.logging.LoggingServiceWithPrefix;
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.core.blackboard.IncrementalSubscription;
034: import org.cougaar.util.UnaryPredicate;
035: import java.util.ArrayList;
036: import java.util.Enumeration;
037: import java.util.Collection;
038: import java.util.Iterator;
039:
040: /** OpConInfoPlugin subscribes to OpConInfoRelay's and
041: * places a response on the Relay. This response is
042: * an ArrayList of the ItemIdentification and
043: * TypeIdentification for the target agent (itself).
044: * This information is used by the sending agent to
045: * construct new ReportForDuty tasks for a new
046: * OperationalSuperior.
047: **/
048:
049: public class OpConInfoPlugin extends ComponentPlugin {
050: ArrayList idInfo = new ArrayList();
051:
052: private static UnaryPredicate selfOrgAssetPred = new UnaryPredicate() {
053: public boolean execute(Object o) {
054: if (o instanceof Organization) {
055: Organization org = (Organization) o;
056: return org.isSelf();
057: }
058: return false;
059: }
060: };
061:
062: private UnaryPredicate myOpConInfoRelayPred = new UnaryPredicate() {
063: public boolean execute(Object o) {
064: return o instanceof OpConInfoRelay;
065: }
066: };
067:
068: /** rely upon load-time introspection to set these services - don't worry about revokation. */
069: public final void setLoggingService(LoggingService logger) {
070: this .logger = logger;
071: }
072:
073: protected LoggingService logger;
074:
075: IncrementalSubscription mySelfOrgs;
076: IncrementalSubscription myOpConInfoRelaySubscription;
077:
078: public void setupSubscriptions() {
079: String me = getAgentIdentifier().toString();
080: logger = LoggingServiceWithPrefix.add(logger,
081: getAgentIdentifier().toString() + ": ");
082: mySelfOrgs = (IncrementalSubscription) getBlackboardService()
083: .subscribe(selfOrgAssetPred);
084: myOpConInfoRelaySubscription = (IncrementalSubscription) getBlackboardService()
085: .subscribe(myOpConInfoRelayPred);
086:
087: initIdInfo();
088: }
089:
090: protected void execute() {
091: if (mySelfOrgs.hasChanged()) {
092: initIdInfo();
093: }
094:
095: if (myOpConInfoRelaySubscription.hasChanged()) {
096: if (logger.isDebugEnabled()) {
097: logger
098: .debug(": myOpConInfoRelaySubscription has changed!");
099: }
100: Collection addedOpConInfoRelays = myOpConInfoRelaySubscription
101: .getAddedCollection();
102:
103: for (Iterator adds = addedOpConInfoRelays.iterator(); adds
104: .hasNext();) {
105: OpConInfoRelay opiRelay = (OpConInfoRelay) adds.next();
106: if (logger.isDebugEnabled()) {
107: logger.debug("Updating the relay with: " + idInfo);
108: }
109: opiRelay.updateResponse(null, idInfo);
110: getBlackboardService().publishChange(opiRelay);
111: }
112: }
113: }
114:
115: protected void initIdInfo() {
116: // Check whether idInfo has already been initialized
117: if ((idInfo.size() == 2) && (idInfo.get(0) != null)
118: && (idInfo.get(1) != null)) {
119: if (logger.isDebugEnabled()) {
120: logger
121: .debug("initIdInfo(): idInfoMy is already initialized - "
122: + idInfo);
123: }
124: return;
125: }
126:
127: if (!mySelfOrgs.isEmpty()) {
128: Organization selfOrgAsset = (Organization) mySelfOrgs
129: .iterator().next();
130: String itemId = selfOrgAsset.getItemIdentificationPG()
131: .getItemIdentification();
132: String typeId = selfOrgAsset.getTypeIdentificationPG()
133: .getTypeIdentification();
134: if (logger.isDebugEnabled()) {
135: logger.debug("My itemId is " + itemId
136: + " and my typeId is " + typeId);
137: }
138: idInfo.add(0, itemId);
139: idInfo.add(1, typeId);
140: } else {
141: if (logger.isDebugEnabled()) {
142: logger
143: .debug("initIdInfo(): can't initialize idInfo, self org subscription is empty.");
144: }
145: }
146: }
147: }
|