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: package org.cougaar.tools.csmart.recipe;
027:
028: import org.cougaar.tools.csmart.core.cdata.ComponentData;
029: import org.cougaar.tools.csmart.core.cdata.GenericComponentData;
030: import org.cougaar.tools.csmart.core.db.PopulateDb;
031: import org.cougaar.tools.csmart.core.property.Property;
032: import org.cougaar.util.ConfigFinder;
033: import org.cougaar.util.log.Logger;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.io.RandomAccessFile;
038: import java.io.Serializable;
039: import java.net.URL;
040: import java.sql.SQLException;
041: import java.util.ArrayList;
042: import java.util.Set;
043: import java.util.StringTokenizer;
044: import java.util.Vector;
045:
046: /**
047: * Recipe to easily select a set of servlets for inclusion in a society
048: * It gets a basic set of recipes from csmart/data/common/servlets.txt
049: * See that file for format.
050: * Users can use this recipe to add any other servlet they like
051: */
052: public class ServletGroupInsertionRecipe extends RecipeBase implements
053: Serializable {
054: private static final String DESCRIPTION_RESOURCE_NAME = "servlet-group-insertion-recipe-description.html";
055: private static final String BACKUP_DESCRIPTION = "ServletGroupInsertionRecipe provides a method for adding groups of servlets into an experiment";
056:
057: private static final boolean PROP_SERVLETS_DFLT = false;
058: private static final String PROP_SERVLETS_DESC = "The complete list of all available Servlets";
059:
060: private static final String PROP_TARGET_AGENT_QUERY = "Target Agent Selection Query";
061: private static final String PROP_TARGET_AGENT_QUERY_DFLT = "recipeQueryAllAgents";
062: private static final String PROP_TARGET_AGENT_QUERY_DESC = "The query name for selecting agents to which to add servlets.";
063:
064: private static final String DFLT_SERVLET_CLASS = "org.cougaar.core.servlet.SimpleServletComponent";
065:
066: private Property[] propServlets;
067: private Vector servletList = null;
068: private Property propTargetAgentQuery;
069:
070: // private Property propNewServletCount;
071: // private Property[] propNewServlets = null; // class of servlet
072: // private Property[] propNewServletNumArgs = null; // how many args it has
073: // private Property[][] propNewServletArgs = null; // args for each new servlet
074:
075: public ServletGroupInsertionRecipe() {
076: super ("Servlet Group Insertion Recipe");
077: }
078:
079: public ServletGroupInsertionRecipe(String name) {
080: super (name);
081: }
082:
083: public void initProperties() {
084:
085: servletList = getServletList();
086: propServlets = new Property[servletList.size()];
087:
088: if (servletList != null) {
089: for (int i = 0; i < servletList.size(); i++) {
090: propServlets[i] = addBooleanProperty(
091: ((ServletListRecord) servletList.elementAt(i))
092: .getName(), PROP_SERVLETS_DFLT);
093: ((Property) propServlets[i])
094: .setToolTip(PROP_SERVLETS_DESC);
095: }
096: }
097:
098: propTargetAgentQuery = addRecipeQueryProperty(
099: PROP_TARGET_AGENT_QUERY, PROP_TARGET_AGENT_QUERY_DFLT);
100: propTargetAgentQuery.setToolTip(PROP_TARGET_AGENT_QUERY_DESC);
101: }
102:
103: private Property addRecipeQueryProperty(String name, String dflt) {
104: Property prop = addProperty(new RecipeQueryProperty(this , name,
105: dflt));
106: prop.setPropertyClass(String.class);
107: return prop;
108: }
109:
110: /**
111: * Gets the name of the html help file for this component.
112: *
113: * @return an <code>URL</code> value
114: */
115: public URL getDescription() {
116: return getClass().getResource(DESCRIPTION_RESOURCE_NAME);
117: }
118:
119: public String getRecipeName() {
120: return getShortName();
121: }
122:
123: private Vector getServletList() {
124: Vector vec = null;
125:
126: ConfigFinder cf = ConfigFinder.getInstance("csmart");
127: File inputFile = null;
128: try {
129: inputFile = cf.locateFile("servlets.txt");
130: } catch (Exception e) {
131: if (log.isErrorEnabled()) {
132: log.error("Could not read servlets file.", e);
133: }
134: }
135: if (vec == null) {
136: vec = new Vector();
137: if (inputFile != null) {
138: try {
139: RandomAccessFile servletFile = null;
140: // read servlets, one per line
141: servletFile = new RandomAccessFile(inputFile, "r");
142: while (true) {
143: String isrv = servletFile.readLine(); // get servlet line
144: // Skip comment lines
145: if (isrv == null) {
146: break;
147: }
148: isrv = isrv.trim();
149: if (isrv.startsWith("#") || isrv.equals(""))
150: continue;
151: ServletListRecord servletRecord = parseServletLine(isrv);
152: vec.add(servletRecord);
153: }
154: servletFile.close();
155: } catch (IOException e) {
156: if (log.isErrorEnabled()) {
157: log.error("Error during read/open from file: "
158: + inputFile, e);
159: }
160: }
161: } else {
162: if (log.isWarnEnabled()) {
163: log
164: .warn("Unable to find servlets.txt! Report bug 2000.");
165: log
166: .warn("config.path= "
167: + System
168: .getProperty("org.cougaar.config.path"));
169: log.warn("Trying again with verbose on: ");
170: // MIK: removed because the method was removed (it hasn't done anything for a long time)
171: //cf.setVerbose(true);
172: try {
173: inputFile = cf.locateFile("servlets.txt");
174: } catch (Exception e) {
175: log.error("Could not read servlets file.", e);
176: }
177: if (inputFile != null) {
178: log.warn(".. this time we found it?");
179: } else {
180: log.warn("... still couldn't find it?");
181: }
182: }
183: }
184: }
185: return vec;
186: }
187:
188: private ServletListRecord parseServletLine(String line) {
189: StringTokenizer tokens = new StringTokenizer(line, ",");
190: String name = tokens.nextToken();
191: String classname = tokens.nextToken();
192: name = name.trim();
193: classname = classname.trim();
194: ArrayList args = new ArrayList();
195: while (tokens.hasMoreTokens()) {
196: args.add(tokens.nextToken().trim());
197: }
198: String[] arguments = new String[args.size()];
199: arguments = (String[]) args.toArray(arguments);
200:
201: ServletListRecord servRecord = new ServletListRecord(name,
202: classname, arguments);
203: return servRecord;
204: }
205:
206: public ComponentData modifyComponentData(ComponentData data,
207: PopulateDb pdb) {
208: try {
209: Set targets = pdb.executeQuery(propTargetAgentQuery
210: .getValue().toString());
211: modifyComponentData(data, pdb, targets);
212: } catch (SQLException sqle) {
213: if (log.isErrorEnabled()) {
214: log.error("Cant run agent target query", sqle);
215: }
216: }
217: return data;
218: }
219:
220: private void modifyComponentData(ComponentData data,
221: PopulateDb pdb, Set targets) throws SQLException {
222: if (targets.contains(pdb.getComponentAlibId(data))) {
223: if (log.isDebugEnabled()) {
224: log.debug("Adding servlets to "
225: + pdb.getComponentAlibId(data));
226: }
227: //if the set of targets (agents) contains the one we're at now
228: // do insertion of the correct set of servlets into this agent
229: // need to get the set of servlets the user selected
230:
231: if (propServlets != null) {
232: for (int i = 0; i < propServlets.length; i++) {
233: ServletListRecord servlet = (ServletListRecord) servletList
234: .elementAt(i);
235: if (((Boolean) (propServlets[i].getValue()))
236: .booleanValue()) {
237: GenericComponentData plugin = new GenericComponentData();
238: plugin.setType(ComponentData.PLUGIN);
239:
240: String[] arguments = servlet.getArguments();
241: for (int j = 0; j < arguments.length; j++)
242: plugin.addParameter(arguments[j]);
243:
244: plugin.setClassName(servlet.getClassname());
245: plugin.setParent(data);
246: plugin.setOwner(this );
247: if (GenericComponentData.alreadyAdded(data,
248: plugin)) {
249: if (log.isDebugEnabled()) {
250: log
251: .debug("Not re-adding servlet. "
252: + data.getName()
253: + " already contains "
254: + plugin);
255: }
256: } else {
257: plugin.setName(GenericComponentData
258: .getSubComponentUniqueName(data,
259: plugin));
260: data.addChildDefaultLoc(plugin);
261: }
262:
263: }
264: }
265: }
266: }
267:
268: if (data.childCount() > 0) {
269: // for each child, call this same method.
270: ComponentData[] children = data.getChildren();
271: for (int i = 0; i < children.length; i++) {
272: // If the child is a plugins or AgentBinder, no need to look at it
273: if (children[i].getType().equals(ComponentData.PLUGIN)
274: || children[i].getType().equals(
275: ComponentData.AGENTBINDER))
276: continue;
277: // if (log.isDebugEnabled()) {
278: // log.debug("modify recursing into " + children[i]);
279: // }
280: modifyComponentData(children[i], pdb, targets);
281: }
282: }
283: }
284:
285: class ServletListRecord extends Object implements Serializable {
286:
287: private String name;
288: private String classname;
289: private String[] argument;
290:
291: public ServletListRecord() {
292: }
293:
294: public ServletListRecord(String name, String classname,
295: String argument[]) {
296: this .name = name;
297: this .classname = classname;
298: this .argument = argument;
299: }
300:
301: public String getName() {
302: return name;
303: }
304:
305: public String getClassname() {
306: return classname;
307: }
308:
309: public String[] getArguments() {
310: return argument;
311: }
312:
313: public String toString() {
314: StringBuffer buf = new StringBuffer();
315: buf.append(name + " " + classname);
316: for (int i = 0; i < argument.length; i++) {
317: buf.append(" " + argument[i]);
318: }
319: return buf.toString();
320: }
321: }
322: }
|