Source Code Cross Referenced for ServletGroupInsertionRecipe.java in  » Science » Cougaar12_4 » org » cougaar » tools » csmart » recipe » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.tools.csmart.recipe 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.