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.core.adaptivity;
028:
029: import java.io.InputStreamReader;
030: import java.io.Reader;
031:
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.mts.MessageAddress;
034: import org.cougaar.core.service.UIDService;
035:
036: /**
037: * Test plugin that reads policies from a file, creates
038: * InterAgentOperatingModePolicies and alternately forwards
039: * the policy to another agent and revokes the policy.
040: *
041: **/
042: public class PolicyRemoteTestPlugin extends ServiceUserPluginBase {
043:
044: private boolean published = false;
045:
046: private InterAgentOperatingModePolicy[] policies;
047:
048: private UIDService uidService;
049:
050: private static final Class[] requiredServices = { UIDService.class };
051:
052: public PolicyRemoteTestPlugin() {
053: super (requiredServices);
054: }
055:
056: public void setupSubscriptions() {
057: OperatingModePolicy[] tempPolicies = null;
058: String policyFileName = getParameters().iterator().next()
059: .toString();
060: try {
061: Reader r = new InputStreamReader(getConfigFinder().open(
062: policyFileName));
063: try {
064: Parser p = new Parser(r, logger);
065: tempPolicies = p.parseOperatingModePolicies();
066: } finally {
067: r.close();
068: }
069: } catch (Exception e) {
070: logger.error("Error parsing policy file", e);
071: }
072:
073: policies = new InterAgentOperatingModePolicy[tempPolicies.length];
074: for (int i = 0; i < tempPolicies.length; i++) {
075: InterAgentOperatingModePolicy iaomp = new InterAgentOperatingModePolicy(
076: tempPolicies[i].getName(), tempPolicies[i]
077: .getIfClause(), tempPolicies[i]
078: .getOperatingModeConstraints(),
079: getAgentIdentifier().toString());
080:
081: policies[i] = iaomp;
082: if (haveServices()) {
083: uidService.registerUniqueObject(policies[i]);
084: }
085: }
086: setPolicies();
087:
088: }
089:
090: public void execute() {
091: if (timerExpired()) {
092: cancelTimer();
093: setPolicies();
094: }
095: }
096:
097: private boolean haveServices() {
098: if (uidService != null)
099: return true;
100: if (acquireServices()) {
101: ServiceBroker sb = getServiceBroker();
102: uidService = (UIDService) sb.getService(this ,
103: UIDService.class, null);
104: return true;
105: }
106: return false;
107: }
108:
109: private void setPolicies() {
110: if (!published) {
111: if (logger.isInfoEnabled())
112: logger.info("publishing policy");
113: for (int i = 0; i < policies.length; i++) {
114: policies[i].setTarget(MessageAddress
115: .getMessageAddress("Provider"));
116: getBlackboardService().publishAdd(policies[i]);
117: published = true;
118: }
119: } else {
120: if (logger.isInfoEnabled())
121: logger.info("Removing policy");
122: for (int i = 0; i < policies.length; i++) {
123: //policies[i].setTarget((MessageAddress)null);
124: //getBlackboardService().publishChange(policies[i]);
125: getBlackboardService().publishRemove(policies[i]);
126: }
127: published = false;
128: }
129: startTimer(75000);
130: }
131: }
|