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: */package org.cougaar.tools.csmart.recipe;
026:
027: import org.cougaar.core.agent.Agent;
028: import org.cougaar.core.agent.AgentManager;
029: import org.cougaar.core.plugin.PluginBase;
030: import org.cougaar.core.plugin.PluginManager;
031: import org.cougaar.tools.csmart.core.cdata.ComponentData;
032: import org.cougaar.tools.csmart.core.property.Property;
033:
034: import java.io.Serializable;
035: import java.net.URL;
036:
037: /**
038: * SwitchPluginRecipe.java
039: *
040: *
041: * Created: Wed Mar 27 09:48:41 2002
042: *
043: */
044: public class SwitchPluginRecipe extends RecipeBase implements
045: Serializable {
046:
047: private static final String PROP_OLD_CLASS = "Old Plugin Class";
048: private static final String PROP_OLD_CLASS_DFLT = "";
049: private static final String PROP_OLD_CLASS_DESC = "Plugin Class to be replaced";
050:
051: private static final String PROP_NEW_CLASS = "New Plugin Class";
052: private static final String PROP_NEW_CLASS_DFLT = "";
053: private static final String PROP_NEW_CLASS_DESC = "New Plugin Class";
054:
055: private static final String PROP_TYPE = "Component Type";
056: private static final String PROP_TYPE_DFLT = ComponentData.PLUGIN;
057: private static final String PROP_TYPE_DESC = "Insertion point or type of component to swap";
058:
059: private Property propOldPluginClass;
060: private Property propNewPluginClass;
061: private Property propCompType;
062:
063: private static final String DESCRIPTION_RESOURCE_NAME = "switch-plugin-recipe-description.html";
064:
065: private boolean compRemoved = false;
066:
067: public SwitchPluginRecipe() {
068: this ("Switch Plugin Recipe");
069: }
070:
071: public SwitchPluginRecipe(String name) {
072: super (name);
073: }
074:
075: public void initProperties() {
076: propOldPluginClass = addProperty(PROP_OLD_CLASS,
077: PROP_OLD_CLASS_DFLT);
078: propOldPluginClass.setToolTip(PROP_OLD_CLASS_DESC);
079:
080: propNewPluginClass = addProperty(PROP_NEW_CLASS,
081: PROP_NEW_CLASS_DFLT);
082: propNewPluginClass.setToolTip(PROP_NEW_CLASS_DESC);
083:
084: propCompType = addProperty(PROP_TYPE, PROP_TYPE_DFLT);
085: propCompType.setToolTip(PROP_TYPE_DESC);
086: }
087:
088: /**
089: * Gets the name of the html help file for this component.
090: *
091: * @return an <code>URL</code> value
092: */
093: public URL getDescription() {
094: return getClass().getResource(DESCRIPTION_RESOURCE_NAME);
095: }
096:
097: public ComponentData addComponentData(ComponentData data) {
098: // Reset compRemoved flag
099: compRemoved = false;
100: return data;
101: }
102:
103: // Find all Components of type Plugin - btw, this thing should
104: // be extended to do any type really - and do a setClass.
105: // Must also modify the AlibId and Name appropriately
106: public ComponentData modifyComponentData(ComponentData data) {
107: if (propOldPluginClass == null
108: || propOldPluginClass.getValue() == null
109: || propOldPluginClass.getValue().toString().equals(""))
110: return data;
111: if (propNewPluginClass == null
112: || propNewPluginClass.getValue() == null
113: || propNewPluginClass.getValue().toString().equals(""))
114: return data;
115:
116: String oldClass = propOldPluginClass.getValue().toString();
117: String newClass = propNewPluginClass.getValue().toString();
118: if (oldClass.equals(newClass))
119: return data;
120:
121: // Set the type were looking for
122: String type;
123: if (propCompType != null && propCompType.getValue() != null)
124: type = propCompType.getValue().toString();
125: else
126: return data;
127: if (type.equals(""))
128: return data;
129:
130: // If the user typed in the full insertion point of a common one, use the shorthand
131: // We really arent supporting the Agent case now...
132: if (type.equalsIgnoreCase(Agent.INSERTION_POINT))
133: type = ComponentData.AGENT;
134: else if (type.equalsIgnoreCase(AgentManager.INSERTION_POINT
135: + ".Binder"))
136: type = ComponentData.NODEBINDER;
137: else if (type.equalsIgnoreCase(PluginManager.INSERTION_POINT
138: + ".Binder"))
139: type = ComponentData.AGENTBINDER;
140: else if (type.equalsIgnoreCase(PluginBase.INSERTION_POINT))
141: type = ComponentData.PLUGIN;
142:
143: // Is this the type we're looking for?
144: if (data.getType().equals(type)) {
145: // Is it the right class?
146: if (data.getClassName().equals(oldClass)) {
147:
148: // unset AlibID - let PopulateDb recreate
149: data.setAlibID(null);
150:
151: // Then change classname
152: data.setClassName(newClass);
153:
154: // Warning: It is possible the old plugin was already there. So we need to check
155: // that the name we are setting is in fact still unique. Steal code
156: // from PluginBase
157: // Change Name
158: // FIXME!
159:
160: // Mark the fact we did a modification
161: compRemoved = true;
162: }
163: }
164:
165: // Now recurse down each of the children
166: if (data.childCount() > 0) {
167: // for each child, call this same method.
168: ComponentData[] children = data.getChildren();
169: for (int i = 0; i < children.length; i++) {
170: modifyComponentData(children[i]);
171: }
172: }
173:
174: return data;
175: } // end of modifyComponentData
176:
177: public boolean componentWasRemoved() {
178: return compRemoved;
179: }
180:
181: }// SwitchPluginRecipe
|