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 org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.persist.NotPersistable;
031: import org.cougaar.core.plugin.ServiceUserPlugin;
032: import org.cougaar.core.service.ConditionService;
033:
034: public class ThreatconTestPlugin extends ServiceUserPlugin {
035: public static final String THREATCON_CONDITION_NAME = "ThreatconTestPlugin.THREATCON";
036:
037: private static final OMCRange[] THREATCON_RANGES = {
038: new OMCPoint("low"), new OMCPoint("high") };
039:
040: private static final OMCRangeList THREATCON_VALUES = new OMCRangeList(
041: THREATCON_RANGES);
042:
043: private ConditionService conditionService;
044:
045: private static final String[] threatconValues = { "low", "high" };
046:
047: private int threatconStep = 0;
048:
049: /**
050: * Private inner class precludes use by others to set our
051: * measurement. Others can only reference the base Condition
052: * class which has no setter method.
053: **/
054: private static class ThreatconTestCondition extends SensorCondition
055: implements NotPersistable {
056: public ThreatconTestCondition(String name,
057: OMCRangeList allowedValues, Comparable value) {
058: super (name, allowedValues, value);
059: }
060:
061: public void setValue(Comparable newValue) {
062: super .setValue(newValue);
063: }
064: }
065:
066: private static final Class[] requiredServices = { ConditionService.class };
067:
068: public ThreatconTestPlugin() {
069: super (requiredServices);
070: }
071:
072: public void setupSubscriptions() {
073: ThreatconTestCondition threatcon = new ThreatconTestCondition(
074: THREATCON_CONDITION_NAME, THREATCON_VALUES,
075: threatconValues[0]);
076: getBlackboardService().publishAdd(threatcon);
077: if (haveServices())
078: setThreatconCondition();
079: }
080:
081: private boolean haveServices() {
082: if (conditionService != null)
083: return true;
084: if (acquireServices()) {
085: ServiceBroker sb = getServiceBroker();
086: conditionService = (ConditionService) sb.getService(this ,
087: ConditionService.class, null);
088: return true;
089: }
090: return false;
091: }
092:
093: public void execute() {
094: if (timerExpired()) {
095: if (haveServices()) {
096: cancelTimer();
097: setThreatconCondition();
098: }
099: }
100: }
101:
102: private void setThreatconCondition() {
103: ThreatconTestCondition threatcon = (ThreatconTestCondition) conditionService
104: .getConditionByName(THREATCON_CONDITION_NAME);
105: if (threatcon != null) {
106: if (logger.isInfoEnabled())
107: logger.info("Setting threatcon = "
108: + threatconValues[threatconStep]);
109: threatcon.setValue(threatconValues[threatconStep]);
110: getBlackboardService().publishChange(threatcon);
111: threatconStep++;
112: if (threatconStep == threatconValues.length)
113: threatconStep = 0;
114: }
115: resetTimer(115000);
116: }
117: }
|