001: /*
002: *
003: * <copyright>
004: *
005: * Copyright 1997-2004 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: package org.cougaar.glm.servlet;
029:
030: import org.cougaar.core.adaptivity.OperatingMode;
031: import org.cougaar.core.service.AlarmService;
032: import org.cougaar.core.service.OperatingModeService;
033: import org.cougaar.glm.plugin.completion.GLMCompletionCalculator;
034: import org.cougaar.planning.plugin.completion.CompletionCalculator;
035: import org.cougaar.planning.servlet.CompletionServlet;
036:
037: /**
038: * A <code>Servlet</code> that generates a GLM-specific
039: * summary of task completion.
040: * <p><pre>
041: * Load with the default URL path "/glmcompletion" using:
042: * plugin = org.cougaar.glm.servlet.GLMCompletionServlet
043: * or specify a path such as "/foo":
044: * plugin = org.cougaar.glm.servlet.GLMCompletionServlet(/foo)
045: * </pre>
046: */
047: public class GLMCompletionServlet extends CompletionServlet {
048:
049: private static final long MILLIS_PER_DAY = 86400000L;
050:
051: private static final String LEVEL2 = "level2";
052:
053: private AlarmService alarmService;
054:
055: private OperatingMode level2Mode;
056: private long cachedMaxTime = Long.MAX_VALUE;
057:
058: public void setAlarmService(AlarmService alarmService) {
059: this .alarmService = alarmService;
060: }
061:
062: public void unload() {
063: if (alarmService != null) {
064: serviceBroker.releaseService(this , AlarmService.class,
065: alarmService);
066: alarmService = null;
067: }
068: super .unload();
069: }
070:
071: protected boolean haveLevel2Mode() {
072: if (level2Mode == null) {
073: OperatingModeService oms = (OperatingModeService) serviceBroker
074: .getService(this , OperatingModeService.class, null);
075: if (oms == null)
076: return false;
077: level2Mode = oms.getOperatingModeByName(LEVEL2);
078: serviceBroker.releaseService(this ,
079: OperatingModeService.class, oms);
080: }
081: return level2Mode != null;
082: }
083:
084: protected long getExecutionTime() {
085: return alarmService.currentTimeMillis();
086: }
087:
088: protected CompletionCalculator getCalculator() {
089: long maxTime;
090: if (haveLevel2Mode()) {
091: int days = ((Number) level2Mode.getValue()).intValue();
092: maxTime = getExecutionTime() + days * 86400000L;
093: } else {
094: maxTime = Long.MAX_VALUE;
095: }
096: if (maxTime != cachedMaxTime || calc == null) {
097: cachedMaxTime = maxTime;
098: calc = new GLMCompletionCalculator(maxTime);
099: }
100: return calc;
101: }
102:
103: protected String getTitlePrefix() {
104: return "GLM";
105: }
106: }
|