Source Code Cross Referenced for LeafOnlyConfigWriter.java in  » Science » Cougaar12_4 » org » cougaar » tools » csmart » experiment » 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.experiment 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 2001-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.experiment;
027:
028:        import org.cougaar.tools.csmart.core.cdata.AgentComponentData;
029:        import org.cougaar.tools.csmart.core.cdata.ComponentData;
030:        import org.cougaar.tools.csmart.core.cdata.GenericComponentData;
031:        import org.cougaar.tools.csmart.core.cdata.LeafComponentData;
032:        import org.cougaar.tools.csmart.core.property.BaseComponent;
033:        import org.cougaar.tools.csmart.core.property.name.ComponentName;
034:        import org.cougaar.tools.csmart.society.AgentComponent;
035:        import org.cougaar.tools.csmart.ui.viewer.CSMART;
036:        import org.cougaar.util.log.Logger;
037:
038:        import java.io.File;
039:        import java.io.FileWriter;
040:        import java.io.IOException;
041:        import java.io.OutputStream;
042:        import java.io.PrintWriter;
043:        import java.util.ArrayList;
044:        import java.util.Collection;
045:        import java.util.Iterator;
046:        import java.util.List;
047:
048:        /**
049:         * Config writer that writes out only the LeafComponentData - that is, only writes files
050:         * that are parameters to Plugins and are not stored in the ConfigDB
051:         * Note this should be fixed - it constructs the _complete_ ComponentData
052:         * and then inefficiently finds the Leaf files to write them.
053:         **/
054:        public class LeafOnlyConfigWriter implements  ConfigurationWriter {
055:            private transient NodeComponent[] nodesToWrite;
056:            private transient List components;
057:            private ComponentData theSoc;
058:            private transient Logger log;
059:
060:            public LeafOnlyConfigWriter(ComponentData theSoc) {
061:                createLogger();
062:                this .theSoc = theSoc;
063:            }
064:
065:            public LeafOnlyConfigWriter(List components,
066:                    NodeComponent[] nodesToWrite, ExperimentBase exp) {
067:                createLogger();
068:                this .nodesToWrite = nodesToWrite;
069:                this .components = components;
070:                theSoc = new GenericComponentData();
071:                theSoc.setType(ComponentData.SOCIETY);
072:                theSoc.setName(exp.getExperimentName()); // this should be experiment: trial FIXME
073:                theSoc.setClassName(""); // leave this out? FIXME
074:                theSoc.setOwner(exp); // the experiment
075:                theSoc.setParent(null);
076:                // For each node, create a GenericComponentData, and add it to the society
077:                addNodes(exp);
078:
079:                // Some components will want access to the complete set of Nodes in the society, etc.
080:                // To get that, they must get back to the root soc object,
081:                // and do a getOwner and go from there. Ugly.
082:
083:                // Now ask each component in turn to add its stuff
084:                for (int i = 0; i < components.size(); i++) {
085:                    BaseComponent soc = (BaseComponent) components.get(i);
086:                    soc.addComponentData(theSoc);
087:                }
088:                // Then give everyone a chance to modify what they've collectively produced
089:                for (int i = components.size() - 1; i >= 0; i--) {
090:                    BaseComponent soc = (BaseComponent) components.get(i);
091:                    soc.modifyComponentData(theSoc);
092:                }
093:            }
094:
095:            /**
096:             * Obtains a list of all file names that need to be written
097:             * out by the server.  If there are no extra (non-db) files
098:             * for this society, null is returned.
099:             *
100:             * @return an <code>Iterator</code> of all names or null if none.
101:             */
102:            public Iterator getFileNames() {
103:                ArrayList files = new ArrayList();
104:                Collection c;
105:
106:                if (log.isDebugEnabled() && theSoc == null) {
107:                    log.debug("theSoc is null");
108:                }
109:                ComponentData[] nodes = theSoc.getChildren();
110:                for (int i = 0; i < theSoc.childCount(); i++) {
111:                    if ((c = getNodeFiles(nodes[i])) != null) {
112:                        files.addAll(c);
113:                    }
114:                }
115:
116:                return files.iterator();
117:            }
118:
119:            private Collection getNodeFiles(ComponentData nc) {
120:                ArrayList files = new ArrayList();
121:                Collection c;
122:
123:                // loop over children
124:                // if there are binder or such, do those
125:                ComponentData[] children = nc.getChildren();
126:                for (int i = 0; i < nc.childCount(); i++) {
127:                    // write out any leaf components
128:                    if ((c = getLeafNames(children[i])) != null) {
129:                        files.addAll(c);
130:                    }
131:                }
132:                for (int i = 0; i < nc.childCount(); i++) {
133:                    if (children[i] instanceof  AgentComponentData) {
134:                        if ((c = getAgentFiles((AgentComponentData) children[i])) != null) {
135:                            files.addAll(c);
136:                        }
137:                    }
138:                }
139:
140:                return files;
141:            }
142:
143:            private Collection getLeafNames(ComponentData me) {
144:                ArrayList files = new ArrayList();
145:
146:                if (me.leafCount() < 1)
147:                    return null;
148:                LeafComponentData[] leaves = me.getLeafComponents();
149:                for (int i = 0; i < me.leafCount(); i++) {
150:                    LeafComponentData leaf = leaves[i];
151:                    if (leaf == null)
152:                        continue;
153:                    if (!leaf.getType().equals(LeafComponentData.FILE)) {
154:                        if (log.isErrorEnabled()) {
155:                            log.error("Got unknown LeafComponent type: "
156:                                    + leaf.getType());
157:                        }
158:                        continue;
159:                    }
160:
161:                    files.add(leaf.getName());
162:                } // end of loop over leaves
163:
164:                return files;
165:            }
166:
167:            private Collection getAgentFiles(AgentComponentData ac) {
168:                ArrayList files = new ArrayList();
169:                Collection c, d;
170:
171:                if ((c = getChildrenOfComp((ComponentData) ac)) != null) {
172:                    files.addAll(c);
173:                }
174:
175:                if ((d = getLeafNames((ComponentData) ac)) != null) {
176:                    files.addAll(d);
177:                }
178:
179:                return files;
180:            }
181:
182:            private Collection getChildrenOfComp(ComponentData comp) {
183:                ArrayList files = new ArrayList();
184:                Collection c;
185:
186:                if (comp == null || comp.childCount() == 0)
187:                    return null;
188:                ComponentData[] children = comp.getChildren();
189:                for (int i = 0; i < children.length; i++) {
190:                    // write out any leaf components
191:                    if ((c = getLeafNames(children[i])) != null) {
192:                        files.addAll(c);
193:                    }
194:                }
195:
196:                return files;
197:            }
198:
199:            /**
200:             * Describe <code>writeFile</code> method here.
201:             *
202:             * @param filename
203:             * @param out
204:             * @exception Exception if an error occurs
205:             */
206:            public void writeFile(String filename, OutputStream out)
207:                    throws Exception {
208:                // find the file, the write it.
209:                ComponentData[] nodes = theSoc.getChildren();
210:                for (int i = 0; i < theSoc.childCount(); i++) {
211:                    String contents = findFile(nodes[i], filename);
212:                    if (contents != null) {
213:                        out.write(contents.getBytes());
214:                    } else {
215:                        if (log.isWarnEnabled()) {
216:                            log.warn("Warning: Could not locate: " + filename
217:                                    + " + in ComponentData");
218:                        }
219:                    }
220:                }
221:            }
222:
223:            private String findFile(ComponentData nc, String filename) {
224:                // loop over children
225:                ComponentData[] children = nc.getChildren();
226:                for (int i = 0; i < nc.childCount(); i++) {
227:                    // write out any leaf components
228:                    String contents = findInLeaf(children[i], filename);
229:                    if (contents != null) {
230:                        return contents;
231:                    }
232:                }
233:                for (int i = 0; i < nc.childCount(); i++) {
234:                    if (children[i] instanceof  AgentComponentData) {
235:                        String contents = findInAgent(
236:                                (AgentComponentData) children[i], filename);
237:                        if (contents != null) {
238:                            return contents;
239:                        }
240:                    }
241:                }
242:
243:                return null;
244:            }
245:
246:            private String findInAgent(AgentComponentData ac, String filename) {
247:                String contents = null;
248:
249:                contents = findInChildren((ComponentData) ac, filename);
250:                if (contents == null) {
251:                    contents = findInLeaf((ComponentData) ac, filename);
252:                }
253:
254:                return contents;
255:            }
256:
257:            private String findInChildren(ComponentData comp, String filename) {
258:                if (comp == null || comp.childCount() == 0)
259:                    return null;
260:                ComponentData[] children = comp.getChildren();
261:                for (int i = 0; i < children.length; i++) {
262:                    // write out any leaf components
263:                    String contents = findInLeaf(children[i], filename);
264:                    if (contents != null) {
265:                        return contents;
266:                    }
267:                }
268:
269:                return null;
270:            }
271:
272:            private String findInLeaf(ComponentData me, String filename) {
273:                if (me.leafCount() < 1)
274:                    return null;
275:                LeafComponentData[] leaves = me.getLeafComponents();
276:                for (int i = 0; i < me.leafCount(); i++) {
277:                    LeafComponentData leaf = leaves[i];
278:                    if (leaf == null)
279:                        continue;
280:                    if (!leaf.getType().equals(LeafComponentData.FILE)) {
281:                        if (log.isErrorEnabled()) {
282:                            log.error("Got unknown LeafComponent type: "
283:                                    + leaf.getType());
284:                        }
285:                        continue;
286:                    }
287:
288:                    if (leaf.getName().equals(filename)) {
289:                        return (String) leaf.getValue();
290:                    }
291:                } // end of loop over leaves
292:
293:                return null;
294:            }
295:
296:            /**
297:             * @deprecated
298:             */
299:            public void writeConfigFiles(File configDir) throws IOException {
300:                // Call writeNodeFile for each of the nodes in theSoc.
301:                ComponentData[] nodes = theSoc.getChildren();
302:                for (int i = 0; i < theSoc.childCount(); i++) {
303:                    writeNodeFile(configDir, nodes[i]);
304:                }
305:            }
306:
307:            /**
308:             * @deprecated
309:             */
310:            private void writeNodeFile(File configDir, ComponentData nc)
311:                    throws IOException {
312:                // loop over children
313:                // if there are binder or such, do those
314:                ComponentData[] children = nc.getChildren();
315:                for (int i = 0; i < nc.childCount(); i++) {
316:                    // write out any leaf components
317:                    writeLeafData(configDir, children[i]);
318:                }
319:                for (int i = 0; i < nc.childCount(); i++) {
320:                    if (children[i] instanceof  AgentComponentData) {
321:                        writeAgentFile(configDir,
322:                                (AgentComponentData) children[i]);
323:                    }
324:                }
325:            }
326:
327:            /**
328:             * @deprecated
329:             */
330:            private void writeChildrenOfComp(File configDir, ComponentData comp)
331:                    throws IOException {
332:                if (comp == null || comp.childCount() == 0)
333:                    return;
334:                ComponentData[] children = comp.getChildren();
335:                for (int i = 0; i < children.length; i++) {
336:                    // write out any leaf components
337:                    writeLeafData(configDir, children[i]);
338:                }
339:            }
340:
341:            /**
342:             * @deprecated
343:             */
344:            private void writeAgentFile(File configDir, AgentComponentData ac)
345:                    throws IOException {
346:                writeChildrenOfComp(configDir, (ComponentData) ac);
347:                // write any other leaf component data files
348:                writeLeafData(configDir, (ComponentData) ac);
349:            }
350:
351:            /**
352:             * @deprecated
353:             */
354:            private void addNodes(ExperimentBase exp) {
355:                for (int i = 0; i < nodesToWrite.length; i++) {
356:                    ComponentData nc = new GenericComponentData();
357:                    nc.setType(ComponentData.NODE);
358:                    nc.setName(nodesToWrite[i].getShortName());
359:                    nc.setClassName(""); // leave this out?? FIXME
360:                    nc.setOwner(exp); // the experiment? FIXME
361:                    nc.setParent(theSoc);
362:                    ComponentName name = new ComponentName(
363:                            (BaseComponent) nodesToWrite[i],
364:                            "ConfigurationFileName");
365:                    nc.addParameter(nodesToWrite[i].getProperty(name)
366:                            .getValue().toString());
367:                    theSoc.addChild(nc);
368:                    addAgents(nodesToWrite[i], nc);
369:                }
370:            }
371:
372:            /**
373:             * @deprecated
374:             */
375:            private void addAgents(NodeComponent node, ComponentData nc) {
376:                AgentComponent[] agents = node.getAgents();
377:                if (agents == null || agents.length == 0)
378:                    return;
379:                for (int i = 0; i < agents.length; i++) {
380:                    AgentComponentData ac = new AgentComponentData();
381:                    ac.setName(agents[i].getFullName().toString());
382:                    // FIXME!!
383:                    ac.setOwner(null); // the society that contains this agent FIXME!!!
384:                    ac.setParent(nc);
385:                    nc.addChild((ComponentData) ac);
386:                }
387:            }
388:
389:            /**
390:             * @deprecated
391:             */
392:            private void writeLeafData(File configDir, ComponentData me)
393:                    throws IOException {
394:                if (me.leafCount() < 1)
395:                    return;
396:                LeafComponentData[] leaves = me.getLeafComponents();
397:                for (int i = 0; i < me.leafCount(); i++) {
398:                    LeafComponentData leaf = leaves[i];
399:                    if (leaf == null)
400:                        continue;
401:                    if (!leaf.getType().equals(LeafComponentData.FILE)) {
402:                        if (log.isErrorEnabled()) {
403:                            log.error("Got unknown LeafComponent type: "
404:                                    + leaf.getType());
405:                        }
406:                        continue;
407:                    }
408:                    PrintWriter writer = new PrintWriter(new FileWriter(
409:                            new File(configDir, leaf.getName())));
410:                    try {
411:                        writer.println(leaf.getValue().toString());
412:                    } catch (Exception e) {
413:                        if (log.isErrorEnabled()) {
414:                            log.error("Error writing config file: " + e);
415:                        }
416:                    } finally {
417:                        writer.close();
418:                    }
419:                } // end of loop over leaves
420:            } // end of writeLeafData
421:
422:            private void createLogger() {
423:                log = CSMART.createLogger(this.getClass().getName());
424:            }
425:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.