001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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.node;
028:
029: import org.cougaar.core.agent.AgentContainer;
030: import org.cougaar.core.component.Component;
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceProvider;
033: import org.cougaar.core.component.ServiceRevokedListener;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.core.service.AgentIdentificationService;
036: import org.cougaar.core.service.AgentQuiescenceStateService;
037: import org.cougaar.core.service.QuiescenceReportForDistributorService;
038: import org.cougaar.core.service.QuiescenceReportService;
039: import org.cougaar.util.GenericStateModelAdapter;
040:
041: /**
042: * This component advertises the {@link QuiescenceReportService} to
043: * all agents.
044: */
045: public final class QuiescenceReportComponent extends
046: GenericStateModelAdapter implements Component {
047:
048: private ServiceBroker sb;
049: private ServiceBroker rootsb;
050:
051: private QuiescenceReportService quiescenceReportService;
052:
053: private MessageAddress localAgent = null;
054:
055: private QuiescenceReportServiceProvider qrsp;
056:
057: public void setServiceBroker(ServiceBroker sb) {
058: this .sb = sb;
059: }
060:
061: public void load() {
062: super .load();
063:
064: AgentContainer agentContainer;
065:
066: NodeControlService ncs = (NodeControlService) sb.getService(
067: this , NodeControlService.class, null);
068: if (ncs == null) {
069: // not in the node agent
070: return;
071: }
072: rootsb = ncs.getRootServiceBroker();
073: agentContainer = ncs.getRootContainer();
074: sb.releaseService(this , NodeControlService.class, ncs);
075:
076: AgentIdentificationService ais = (AgentIdentificationService) sb
077: .getService(this , AgentIdentificationService.class,
078: null);
079: if (ais != null) {
080: localAgent = ais.getMessageAddress();
081: sb.releaseService(this , AgentIdentificationService.class,
082: ais);
083: }
084:
085: qrsp = new QuiescenceReportServiceProvider(localAgent
086: .getAddress(), agentContainer, sb);
087: rootsb.addService(QuiescenceReportService.class, qrsp);
088: rootsb.addService(QuiescenceReportForDistributorService.class,
089: qrsp);
090: // Service for querying the quiescence / enabled state of agents, used
091: // by the QuiescenceStateServlet
092: rootsb.addService(AgentQuiescenceStateService.class, qrsp);
093:
094: // mark our node as non-quiescent until we are started, which
095: // will occur after all other components have been loaded.
096: //
097: // note that we pass our MessageAddress as the requestor.
098: quiescenceReportService = (QuiescenceReportService) sb
099: .getService(localAgent, QuiescenceReportService.class,
100: null);
101:
102: quiescenceReportService.clearQuiescentState();
103: }
104:
105: public void start() {
106: super .start();
107:
108: if (quiescenceReportService != null) {
109: quiescenceReportService.setQuiescentState();
110: sb.releaseService(localAgent,
111: QuiescenceReportService.class,
112: quiescenceReportService);
113: quiescenceReportService = null;
114: }
115: }
116:
117: public void unload() {
118: super .unload();
119:
120: if (qrsp != null) {
121: rootsb.revokeService(QuiescenceReportService.class, qrsp);
122: // Need to cleanup tasks performed in separate thread.
123: qrsp.revokeService();
124: qrsp = null;
125: }
126: }
127: }
|