01: /*
02: * <copyright>
03: *
04: * Copyright 2002-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.core.adaptivity;
28:
29: import org.cougaar.core.component.ServiceBroker;
30: import org.cougaar.core.plugin.ServiceUserPlugin;
31: import org.cougaar.core.service.OperatingModeService;
32:
33: public class LateOperatingModePlugin extends ServiceUserPlugin {
34: public static final String OM_NAME = "LateOperatingModePlugin.LATE";
35:
36: private static final OMCRange[] RANGES = { new OMCRange(0.0, 1.0) };
37:
38: private static final OMCRangeList VALUES = new OMCRangeList(RANGES);
39:
40: private static final Class[] requiredServices = { OperatingModeService.class };
41:
42: private OperatingModeService omService;
43: private OperatingMode om;
44: private int step = 0;
45:
46: public LateOperatingModePlugin() {
47: super (requiredServices);
48: }
49:
50: public void setupSubscriptions() {
51: if (haveServices())
52: check();
53: }
54:
55: private boolean haveServices() {
56: if (omService != null)
57: return true;
58: if (acquireServices()) {
59: ServiceBroker sb = getServiceBroker();
60: omService = (OperatingModeService) sb.getService(this ,
61: OperatingModeService.class, null);
62: return true;
63: }
64: return false;
65: }
66:
67: public void execute() {
68: if (timerExpired()) {
69: if (haveServices())
70: check();
71: }
72: }
73:
74: private void check() {
75: cancelTimer();
76: if (++step >= 4) {
77: if (om == null) {
78: om = new OperatingModeImpl(OM_NAME, VALUES, new Double(
79: 0.0));
80: blackboard.publishAdd(om);
81: logger.info("Publishing " + om);
82: } else {
83: logger.info("Checking " + om);
84: }
85: } else {
86: logger.info("Waiting step " + step);
87: }
88: resetTimer(5000);
89: }
90: }
|