001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.adaptivity;
028:
029: import java.io.InputStreamReader;
030: import java.io.Reader;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import org.cougaar.bootstrap.SystemProperties;
036: import org.cougaar.core.component.ServiceBroker;
037: import org.cougaar.core.component.ServiceProvider;
038: import org.cougaar.core.plugin.ComponentPlugin;
039: import org.cougaar.core.service.LoggingService;
040: import org.cougaar.core.service.PlaybookConstrainService;
041: import org.cougaar.core.service.PlaybookReadService;
042: import org.cougaar.util.CircularQueue;
043:
044: /**
045: * A container for the active Plays. The plays are initialized from a
046: * file specified as a plugin parameter. Playbook users access the
047: * plays through two services: {@link PlaybookReadService} and
048: * {@link PlaybookConstrainService}. The former returns the constrained
049: * plays. The latter constrains the original plays with
050: * {@link OperatingModePolicy}s.
051: **/
052: public class PlaybookManager extends ComponentPlugin implements
053: ServiceProvider {
054: private Playbook playbook;
055: private LoggingService logger;
056: private List listeners = new ArrayList(2);
057: private CircularQueue todo = new CircularQueue();
058:
059: private class PlaybookReadServiceImpl implements
060: PlaybookReadService {
061: private boolean active = true;
062:
063: /**
064: * Gets an array of the current plays
065: * @return an array of the current plays
066: **/
067: public Play[] getCurrentPlays() {
068: if (!active)
069: throw new RuntimeException(
070: "Service has been released or revoked");
071: return playbook.getCurrentPlays();
072: }
073:
074: /**
075: * Add a listener to the playbook. The listener will be
076: * publishChanged if this Playbook is modified.
077: * @param l the Listener
078: **/
079: public void addListener(Listener l) {
080: if (!active)
081: throw new RuntimeException(
082: "Service has been released or revoked");
083: listeners.add(l);
084: }
085:
086: /**
087: * Remove a listener to the playbook. The listener will no longer
088: * be publishChanged if this Playbook is modified.
089: * @param l the Listener
090: **/
091: public void removeListener(Listener l) {
092: if (!active)
093: throw new RuntimeException(
094: "Service has been released or revoked");
095: listeners.remove(l);
096: }
097: }
098:
099: private class PlaybookConstrainServiceImpl implements
100: PlaybookConstrainService {
101: private boolean active = true;
102:
103: /**
104: * Add another OperatingModePolicy constraint. The plays are
105: * modified so that in all cases where the if clause of the
106: * constraint is true the OperatingMode ranges will all fall
107: * within the constraint.
108: * @param omp the constraint to add
109: **/
110: public void constrain(OperatingModePolicy omp) {
111: if (!active)
112: throw new RuntimeException(
113: "Service has been released or revoked");
114: playbook.addConstraint(omp);
115: fireListenersLater();
116: }
117:
118: /**
119: * Remove an OperatingModePolicy constraint. The current plays are
120: * recomputed to omit the removed constraint.
121: * @param omp the constraint to remove
122: **/
123: public void unconstrain(OperatingModePolicy omp) {
124: if (!active)
125: throw new RuntimeException(
126: "Service has been released or revoked");
127: playbook.removeConstraint(omp);
128: fireListenersLater();
129: }
130: }
131:
132: /**
133: * Override to register the services we provide.
134: **/
135: public void load() {
136: super .load();
137: logger = (LoggingService) getServiceBroker().getService(this ,
138: LoggingService.class, null);
139: playbook = new Playbook(logger);
140: getServiceBroker().addService(PlaybookReadService.class, this );
141: getServiceBroker().addService(PlaybookConstrainService.class,
142: this );
143: }
144:
145: /**
146: * Override to unregister the services we provide.
147: **/
148: public void unload() {
149: getServiceBroker().revokeService(
150: PlaybookConstrainService.class, this );
151: getServiceBroker().revokeService(PlaybookReadService.class,
152: this );
153: getServiceBroker().releaseService(this , LoggingService.class,
154: logger);
155: super .unload();
156: }
157:
158: /**
159: * Read the plays from a file.
160: **/
161: public void setupSubscriptions() {
162: Iterator iter = getParameters().iterator();
163: if (!iter.hasNext()) {
164: logger.error("Missing playbook file name.");
165: } else {
166: String playFileName = iter.next().toString();
167: try {
168: Reader is = new InputStreamReader(getConfigFinder()
169: .open(playFileName));
170: try {
171: Parser p = new Parser(is, logger);
172: Play[] plays = p.parsePlays();
173: playbook.setPlays(plays);
174: fireListeners();
175: } finally {
176: is.close();
177: }
178: } catch (Exception e) {
179: logger.error("Error parsing play file", e);
180: }
181: }
182: }
183:
184: /**
185: * Handle requests that arrived through our services. These requests
186: * all fire listeners. The services cannot themselves do this
187: * because of the possibility of a deadlock due to attempts to open
188: * two blackboard transactions simultaneously. The requests are
189: * placed in a queue and executed here.
190: **/
191: public void execute() {
192: synchronized (todo) {
193: while (todo.size() > 0) {
194: try {
195: ((Runnable) todo.next()).run();
196: } catch (RuntimeException e) {
197: logger.error("Error running delayed job", e);
198: }
199: }
200: }
201: }
202:
203: /**
204: * Gets (creates) one of our services. Part of the implementation of
205: * the ServiceProvider API
206: * @param sb the ServiceBroker making the request
207: * @param requestor the actual requestor on whose behalf the broker is acting
208: * @param serviceClass the class of the Service desired.
209: * @return an instance of the requested Service if it one we supply.
210: **/
211: public Object getService(ServiceBroker sb, Object requestor,
212: Class serviceClass) {
213: if (serviceClass == PlaybookReadService.class) {
214: return new PlaybookReadServiceImpl();
215: }
216: if (serviceClass == PlaybookConstrainService.class) {
217: return new PlaybookConstrainServiceImpl();
218: }
219: return null;
220: }
221:
222: /**
223: * Release one of our services. The services use no resources, so
224: * there is nothing to do.
225: **/
226: public void releaseService(ServiceBroker sb, Object requestor,
227: Class serviceClass, Object service) {
228: if (service instanceof PlaybookReadServiceImpl) {
229: ((PlaybookReadServiceImpl) service).active = false;
230: return;
231: }
232: if (service instanceof PlaybookConstrainServiceImpl) {
233: ((PlaybookConstrainServiceImpl) service).active = false;
234: return;
235: }
236: throw new IllegalArgumentException("Not my service: " + service);
237: }
238:
239: private void fireListenersLater() {
240: synchronized (todo) {
241: todo.add(new Runnable() {
242: public void run() {
243: fireListeners();
244: }
245: });
246: }
247: blackboard.signalClientActivity();
248: }
249:
250: private void fireListeners() {
251: for (Iterator i = listeners.iterator(); i.hasNext();) {
252: PlaybookReadService.Listener l = (PlaybookReadService.Listener) i
253: .next();
254: blackboard.publishChange(l);
255: }
256: if (logger.isDebugEnabled())
257: logger.debug("New constrained plays"
258: + SystemProperties.getProperty("line.separator")
259: + this);
260: }
261: }
|