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.plugin.ServiceUserPlugin;
034: import org.cougaar.core.service.PlaybookConstrainService;
035:
036: /**
037: * Test plugin that alternately constrains and unconstrains
038: * the playbook with an operating mode policy it read froma file
039: */
040: public class PolicyTestPlugin extends ServiceUserPlugin {
041: private PlaybookConstrainService playbookConstrainService;
042:
043: private boolean constrained = false;
044:
045: private OperatingModePolicy[] policies;
046:
047: private static final Class[] requiredServices = { PlaybookConstrainService.class };
048:
049: public PolicyTestPlugin() {
050: super (requiredServices);
051: }
052:
053: public void setupSubscriptions() {
054: String policyFileName = getParameters().iterator().next()
055: .toString();
056: try {
057: Reader is = new InputStreamReader(getConfigFinder().open(
058: policyFileName));
059: try {
060: Parser p = new Parser(is, logger);
061: policies = p.parseOperatingModePolicies();
062: } finally {
063: is.close();
064: }
065: } catch (Exception e) {
066: logger.error("Error parsing policy file", e);
067: }
068: if (haveServices())
069: setPolicies();
070: }
071:
072: private boolean haveServices() {
073: if (playbookConstrainService != null)
074: return true;
075: if (acquireServices()) {
076: ServiceBroker sb = getServiceBroker();
077: playbookConstrainService = (PlaybookConstrainService) sb
078: .getService(this , PlaybookConstrainService.class,
079: null);
080: return true;
081: }
082: return false;
083: }
084:
085: public void execute() {
086: if (timerExpired()) {
087: if (haveServices()) {
088: cancelTimer();
089: setPolicies();
090: }
091: }
092: }
093:
094: private void setPolicies() {
095: // This method is meant to alternately add & remove
096: // threatcon policies, based on the constarained flag
097: if (constrained) {
098: if (logger.isInfoEnabled())
099: logger.info("Adding threatcon policy");
100: for (int i = 0; i < policies.length; i++) {
101: playbookConstrainService.constrain(policies[i]);
102: }
103: constrained = false;
104: } else {
105: if (logger.isInfoEnabled())
106: logger.info("Removing threatcon policy");
107: for (int i = 0; i < policies.length; i++) {
108: playbookConstrainService.unconstrain(policies[i]);
109: }
110: constrained = true;
111: }
112: resetTimer(75000);
113: }
114: }
|