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.planning.plugin.legacy;
028:
029: import org.cougaar.core.component.BindingSite;
030: import org.cougaar.core.component.ServiceBroker;
031: import org.cougaar.core.plugin.PluginBase;
032: import org.cougaar.core.service.SchedulerService;
033: import org.cougaar.util.GenericStateModelAdapter;
034: import org.cougaar.util.SyncTriggerModelImpl;
035: import org.cougaar.util.Trigger;
036: import org.cougaar.util.TriggerModel;
037:
038: /**
039: * A minimal Plugin that illustrates use of the scheduler
040: * service.
041: */
042: public abstract class MinPlugin extends GenericStateModelAdapter
043: implements PluginBase {
044:
045: protected Object parameter;
046:
047: protected BindingSite bindingSite;
048: protected ServiceBroker serviceBroker;
049:
050: protected SchedulerService scheduler;
051: protected TriggerModel tm;
052:
053: public MinPlugin() {
054: }
055:
056: /**
057: * Called just after construction (via introspection) by the
058: * loader if a non-null parameter Object was specified by
059: * the ComponentDescription.
060: **/
061: public void setParameter(Object param) {
062: this .parameter = param;
063: }
064:
065: /**
066: * Binding site is set by reflection at creation-time.
067: */
068: public void setBindingSite(BindingSite bs) {
069: this .bindingSite = bs;
070: this .serviceBroker = bs.getServiceBroker();
071: }
072:
073: //
074: // implement GenericStateModel:
075: //
076:
077: public void initialize() {
078: super .initialize();
079: }
080:
081: public void load() {
082: super .load();
083:
084: // obtain the scheduler service
085: scheduler = (SchedulerService) serviceBroker.getService(this ,
086: SchedulerService.class, null);
087: if (scheduler == null) {
088: throw new RuntimeException(this
089: + " unable to obtain scheduler");
090: }
091:
092: // create a callback for running this component
093: Trigger myTrigger = new Trigger() {
094: public void trigger() {
095: cycle();
096: }
097:
098: public String toString() {
099: return MinPlugin.this .toString();
100: }
101: };
102:
103: // create the trigger model
104: this .tm = new SyncTriggerModelImpl(scheduler, myTrigger);
105:
106: // activate the trigger model
107: tm.initialize();
108: tm.load();
109: }
110:
111: public void start() {
112: super .start();
113: tm.start();
114: // run "cycle()" at least once
115: tm.trigger();
116: }
117:
118: public void suspend() {
119: super .suspend();
120: tm.suspend();
121: }
122:
123: public void resume() {
124: super .resume();
125: tm.resume();
126: }
127:
128: public void stop() {
129: super .stop();
130: tm.stop();
131: }
132:
133: public void halt() {
134: super .halt();
135: tm.halt();
136: }
137:
138: public void unload() {
139: super .unload();
140: if (tm != null) {
141: tm.unload();
142: tm = null;
143: }
144: if (scheduler != null) {
145: serviceBroker.releaseService(this , SchedulerService.class,
146: scheduler);
147: scheduler = null;
148: }
149: }
150:
151: /**
152: * "cycle()" is called by the scheduler thread to perform
153: * MinPlugin activity.
154: * <p>
155: * The first "cycle()" is requested by "start()". Later cycles
156: * can be requested by using "tm.trigger()", from either within
157: * "cycle()" itself or from another class (e.g. a blackboard
158: * watcher).
159: * <p>
160: * Typically the first "cycle()" is used as an initialization
161: * step, so a "boolean didPrecycle" is kept in the subclass.
162: * <p>
163: * This method should not be synchronized, since it is only
164: * called by the scheduler's (synchronized) trigger model.
165: */
166: protected abstract void cycle();
167:
168: }
|