001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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.persist;
028:
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: import org.cougaar.core.adaptivity.OMCRangeList;
033: import org.cougaar.core.adaptivity.OperatingModeImpl;
034: import org.cougaar.core.blackboard.IncrementalSubscription;
035: import org.cougaar.core.plugin.ServiceUserPlugin;
036: import org.cougaar.core.service.PersistenceControlService;
037: import org.cougaar.util.UnaryPredicate;
038:
039: /**
040: * This component creates blackboard {@link
041: * org.cougaar.core.adaptivity.OperatingMode}s that control
042: * persistence settings.
043: */
044: public class PersistenceControlPlugin extends ServiceUserPlugin {
045: private static class MyOperatingMode extends OperatingModeImpl
046: implements NotPersistable {
047: String mediaName;
048: String controlName;
049:
050: MyOperatingMode(String mediaName, String controlName,
051: OMCRangeList values) {
052: super ("Persistence." + mediaName + "." + controlName,
053: values);
054: this .mediaName = mediaName;
055: this .controlName = controlName;
056: }
057:
058: MyOperatingMode(String controlName, OMCRangeList values) {
059: super ("Persistence." + controlName, values);
060: this .mediaName = null;
061: this .controlName = controlName;
062: }
063: }
064:
065: private static UnaryPredicate myOperatingModePredicate = new UnaryPredicate() {
066: public boolean execute(Object o) {
067: return o instanceof MyOperatingMode;
068: }
069: };
070:
071: private static Class[] requiredServices = { PersistenceControlService.class };
072:
073: private boolean createdOperatingModes = false;
074:
075: private IncrementalSubscription myOperatingModes;
076:
077: private PersistenceControlService persistenceControlService;
078:
079: public PersistenceControlPlugin() {
080: super (requiredServices);
081: }
082:
083: protected boolean haveServices() {
084: if (persistenceControlService != null)
085: return true;
086: if (acquireServices()) {
087: persistenceControlService = (PersistenceControlService) getServiceBroker()
088: .getService(this , PersistenceControlService.class,
089: null);
090: return true;
091: }
092: return false;
093: }
094:
095: public void setupSubscriptions() {
096: myOperatingModes = (IncrementalSubscription) blackboard
097: .subscribe(myOperatingModePredicate);
098: }
099:
100: public void execute() {
101: if (haveServices()) {
102: if (!createdOperatingModes) {
103: createOperatingModes();
104: createdOperatingModes = true;
105: } else if (myOperatingModes.hasChanged()) {
106: updateOperatingModes(myOperatingModes
107: .getChangedCollection());
108: }
109: }
110: }
111:
112: private void createOperatingModes() {
113: String[] controlNames = persistenceControlService
114: .getControlNames();
115: for (int i = 0; i < controlNames.length; i++) {
116: String controlName = controlNames[i];
117: OMCRangeList controlValues = persistenceControlService
118: .getControlValues(controlName);
119: MyOperatingMode om = new MyOperatingMode(controlName,
120: controlValues);
121: blackboard.publishAdd(om);
122: if (logger.isDebugEnabled())
123: logger.debug("Added " + om);
124: }
125: String[] mediaNames = persistenceControlService.getMediaNames();
126: for (int j = 0; j < mediaNames.length; j++) {
127: String mediaName = mediaNames[j];
128: controlNames = persistenceControlService
129: .getMediaControlNames(mediaName);
130: for (int i = 0; i < controlNames.length; i++) {
131: String controlName = controlNames[i];
132: OMCRangeList controlValues = persistenceControlService
133: .getMediaControlValues(mediaName, controlName);
134: MyOperatingMode om = new MyOperatingMode(mediaName,
135: controlName, controlValues);
136: blackboard.publishAdd(om);
137: if (logger.isDebugEnabled())
138: logger.debug("Added " + om);
139: }
140: }
141: }
142:
143: private void updateOperatingModes(Collection changedOperatingModes) {
144: for (Iterator i = changedOperatingModes.iterator(); i.hasNext();) {
145: MyOperatingMode om = (MyOperatingMode) i.next();
146: try {
147: if (om.mediaName == null) {
148: persistenceControlService.setControlValue(
149: om.controlName, om.getValue());
150: } else {
151: persistenceControlService
152: .setMediaControlValue(om.mediaName,
153: om.controlName, om.getValue());
154: }
155: } catch (RuntimeException re) {
156: logger.error("set persistence control failed", re);
157: }
158: }
159: }
160: }
|