001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.agent.service.scheduler;
028:
029: import org.cougaar.core.component.Component;
030: import org.cougaar.core.component.ServiceBroker;
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.service.AgentIdentificationService;
033: import org.cougaar.core.service.LoggingService;
034: import org.cougaar.core.service.SchedulerService;
035: import org.cougaar.core.service.ThreadService;
036: import org.cougaar.util.GenericStateModelAdapter;
037:
038: /**
039: * This component advertises the SchedulerService, which is typically
040: * just a wrapper around the ThreadService.
041: */
042: public final class SchedulerServiceComponent extends
043: GenericStateModelAdapter implements Component {
044: private ServiceBroker sb;
045:
046: private LoggingService loggingS;
047: private ThreadService threadS;
048: private SchedulerServiceProvider schedSP;
049:
050: public void setServiceBroker(ServiceBroker sb) {
051: this .sb = sb;
052: }
053:
054: public void load() {
055: super .load();
056:
057: // get the logger
058: loggingS = (LoggingService) sb.getService(this ,
059: LoggingService.class, null);
060:
061: // get the thread service
062: threadS = (ThreadService) sb.getService(this ,
063: ThreadService.class, null);
064:
065: // get the local agent address
066: String agentName = "Anonymous";
067: AgentIdentificationService agentIdS = (AgentIdentificationService) sb
068: .getService(this , AgentIdentificationService.class,
069: null);
070: if (agentIdS != null) {
071: MessageAddress agentAddr = agentIdS.getMessageAddress();
072: if (agentAddr != null) {
073: agentName = agentAddr.getAddress();
074: }
075: sb.releaseService(this , AgentIdentificationService.class,
076: agentIdS);
077: }
078:
079: // create and advertise our service
080: this .schedSP = new SchedulerServiceProvider(threadS, loggingS,
081: agentName);
082: sb.addService(SchedulerService.class, schedSP);
083: }
084:
085: public void suspend() {
086: schedSP.suspend();
087: super .suspend();
088: }
089:
090: public void resume() {
091: super .resume();
092: schedSP.resume();
093: }
094:
095: public void unload() {
096: // revoke our service
097: if (schedSP != null) {
098: sb.revokeService(SchedulerService.class, schedSP);
099: schedSP = null;
100: }
101: // release services
102: if (threadS != null) {
103: sb.releaseService(this , ThreadService.class, threadS);
104: threadS = null;
105: }
106: if (loggingS != null) {
107: sb.releaseService(this , LoggingService.class, loggingS);
108: loggingS = null;
109: }
110: super.unload();
111: }
112: }
|