001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: public class AlarmDS extends ResourceContext implements Constants {
032: static void register() {
033: ContextInstantiater cinst = new AbstractContextInstantiater() {
034: public ResourceContext instantiateContext(
035: String[] parameters, ResourceContext parent)
036: throws ResourceContext.ParameterError {
037: return new AlarmDS(parameters, parent);
038: }
039:
040: };
041: registerContextInstantiater("Alarm", cinst);
042: }
043:
044: // Alarm contexts can be the first element in a path. They have
045: // no parent or context other than the root.
046: protected ResourceContext preferredParent(RSS root) {
047: return root;
048: }
049:
050: protected DataFormula instantiateFormula(String kind) {
051: if (kind.equals("OneSecond")) {
052: return new OneSecond();
053: } else if (kind.equals("FiveSeconds")) {
054: return new FiveSeconds();
055: } else if (kind.equals("FifteenSeconds")) {
056: return new FifteenSeconds();
057: } else if (kind.equals("AlarmFormula")) {
058: return new AlarmFormula();
059: } else {
060: return null;
061: }
062: }
063:
064: protected void verifyParameters(String[] parameters)
065: throws ParameterError {
066: if (parameters == null || parameters.length != 0) {
067: throw new ParameterError(
068: "AlarmDS: wrong number of parameters");
069: }
070: }
071:
072: private AlarmDS(String[] parameters, ResourceContext parent)
073: throws ParameterError {
074: super (parameters, parent);
075: }
076:
077: static class AlarmFormula extends DataFormula implements Constants {
078:
079: private Object task;
080: private final Object task_lock = new Object();
081: private long period;
082:
083: long getPeriod() {
084: return period;
085: }
086:
087: private void updateCache() {
088: setCachedValue(new DataValue(System.currentTimeMillis(),
089: CONFIRMED_MEAS_CREDIBILITY));
090:
091: }
092:
093: protected void initialize(ResourceContext context) {
094: super .initialize(context);
095: String[] args = getArgs();
096: if (args != null && args.length > 0) {
097: period = Integer.parseInt(args[0]);
098: }
099: updateCache();
100: Runnable body = new Runnable() {
101: public void run() {
102: updateCache();
103: }
104: };
105: synchronized (task_lock) {
106: if (task != null) {
107: RSSUtils.unschedule(task);
108: }
109: task = RSSUtils.schedule(body, 0, getPeriod());
110: }
111:
112: }
113:
114: void contextDeleted() {
115: synchronized (task_lock) {
116: if (task != null) {
117: RSSUtils.unschedule(task);
118: }
119: task = null;
120: }
121: super .contextDeleted();
122: }
123:
124: protected DataValue doCalculation(Values vals) {
125: return getCachedValue();
126: }
127:
128: }
129:
130: public static class OneSecond extends AlarmFormula {
131: long getPeriod() {
132: return 1000;
133: }
134: }
135:
136: public static class FiveSeconds extends AlarmFormula {
137: long getPeriod() {
138: return 5000;
139: }
140: }
141:
142: public static class FifteenSeconds extends AlarmFormula {
143: long getPeriod() {
144: return 15000;
145: }
146: }
147:
148: }
|