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


0001:        /*
0002:         * <copyright>
0003:         *  
0004:         *  Copyright 2000-2004 BBNT Solutions, LLC
0005:         *  under sponsorship of the Defense Advanced Research Projects
0006:         *  Agency (DARPA).
0007:         * 
0008:         *  You can redistribute this software and/or modify it under the
0009:         *  terms of the Cougaar Open Source License as published on the
0010:         *  Cougaar Open Source Website (www.cougaar.org).
0011:         * 
0012:         *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0013:         *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0014:         *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0015:         *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0016:         *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0017:         *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0018:         *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0019:         *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0020:         *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0021:         *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0022:         *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0023:         *  
0024:         * </copyright>
0025:         */
0026:
0027:        package org.cougaar.tools.csmart.ui.monitor.viewer;
0028:
0029:        import org.cougaar.bootstrap.Bootstrapper;
0030:        import org.cougaar.core.util.UID;
0031:        import org.cougaar.tools.csmart.experiment.DBExperiment;
0032:        import org.cougaar.tools.csmart.experiment.HostComponent;
0033:        import org.cougaar.tools.csmart.experiment.NodeComponent;
0034:        import org.cougaar.tools.csmart.experiment.Experiment;
0035:        import org.cougaar.tools.csmart.society.AgentComponent;
0036:        import org.cougaar.tools.csmart.ui.Browser;
0037:        import org.cougaar.tools.csmart.ui.monitor.PropertyNames;
0038:        import org.cougaar.tools.csmart.ui.monitor.community.ULCommunityFrame;
0039:        import org.cougaar.tools.csmart.ui.monitor.community.ULCommunityNode;
0040:        import org.cougaar.tools.csmart.ui.monitor.generic.CSMARTGraph;
0041:        import org.cougaar.tools.csmart.ui.monitor.generic.ExtensionFileFilter;
0042:        import org.cougaar.tools.csmart.ui.monitor.generic.UIProperties;
0043:        import org.cougaar.tools.csmart.ui.monitor.metrics.CSMARTMetricsFrame;
0044:        import org.cougaar.tools.csmart.ui.monitor.plan.ULPlanFilter;
0045:        import org.cougaar.tools.csmart.ui.monitor.plan.ULPlanFrame;
0046:        import org.cougaar.tools.csmart.ui.monitor.plan.ULPlanNode;
0047:        import org.cougaar.tools.csmart.ui.monitor.society.ULSocietyFrame;
0048:        import org.cougaar.tools.csmart.ui.monitor.society.ULSocietyNode;
0049:        import org.cougaar.tools.csmart.ui.monitor.xml.XMLFrame; //import org.cougaar.tools.csmart.ui.monitor.topology.TopologyFrame;
0050:        import org.cougaar.tools.csmart.ui.util.ClientServletUtil;
0051:        import org.cougaar.tools.csmart.ui.util.NamedFrame;
0052:        import org.cougaar.tools.csmart.ui.util.ServletResponse;
0053:        import org.cougaar.tools.csmart.ui.util.ServletResult;
0054:        import org.cougaar.tools.csmart.ui.viewer.CSMART;
0055:        import org.cougaar.util.PropertyTree;
0056:        import org.cougaar.util.log.Logger;
0057:
0058:        import javax.swing.*;
0059:        import java.awt.*;
0060:        import java.awt.event.ActionEvent;
0061:        import java.awt.event.ActionListener;
0062:        import java.awt.event.WindowAdapter;
0063:        import java.awt.event.WindowEvent;
0064:        import java.awt.event.WindowListener;
0065:        import java.io.File;
0066:        import java.io.IOException;
0067:        import java.io.ObjectInputStream;
0068:        import java.net.URL;
0069:        import java.util.*;
0070:        import java.util.List;
0071:
0072:        /**
0073:         * The user interface for the CSMART Society.
0074:         * Displays a tool panel with buttons for invoking various graph and
0075:         * chart views.
0076:         * @property org.cougaar.install.path Used to find saved graphs to read
0077:         * @property org.cougaar.useBootstrapper See the Bootstrapper for usage
0078:         */
0079:        public class CSMARTUL extends JFrame implements  ActionListener,
0080:                Observer {
0081:            private static final String FILE_MENU = "File";
0082:            private static final String NEW_MENU_ITEM = "New";
0083:            private static final String OPEN_MENU_ITEM = "Open";
0084:            private static final String MONITOR_MENU_ITEM = "Monitor...";
0085:            private static final String EXIT_MENU_ITEM = "Exit";
0086:            private static final String WINDOW_MENU = "Window";
0087:            private static final String OPEN_GRAPH_MENU_ITEM = "Open Graph...";
0088:            private static final String OPEN_XML_MENU_ITEM = "Open XML File...";
0089:            private static final String OPEN_METRIC_MENU_ITEM = "Open Metric...";
0090:            private static final String SEPARATOR = "Separator";
0091:
0092:            protected static final String HELP_MENU = "Help";
0093:            protected static final String HELP_DOC = "help.html";
0094:            protected static final String ABOUT_CSMART_ITEM = "About CSMART";
0095:            protected static final String ABOUT_DOC = "/org/cougaar/tools/csmart/ui/help/about-csmart.html";
0096:            protected static final String HELP_MENU_ITEM = "Help";
0097:
0098:            private String[] helpMenuItems = { HELP_MENU_ITEM,
0099:                    ABOUT_CSMART_ITEM };
0100:
0101:            private static CSMART csmart; // top level viewer
0102:            private JMenu windowMenu;
0103:            private static String agentURL = null; // agent to contact initially
0104:            private static String agentProtocol = "http";
0105:            private static String agentHost = "localhost";
0106:            // default agent port
0107:            public static int agentPort = 8800;
0108:            // node component argument that specifies alternate port
0109:            public static String AGENT_HTTP_PORT = "org.cougaar.lib.web.http.port";
0110:            public static String AGENT_HTTPS_PORT = "org.cougaar.lib.web.https.port";
0111:
0112:            // maps host to port
0113:            private static CSMARTAgentMap agentMap;
0114:            // these mappings are determined once per experiment
0115:            private static Hashtable communityToAgents = null;
0116:            private static Hashtable agentToCommunity = null;
0117:            private static Hashtable agentToURL = null;
0118:            private static Hashtable titleToMenuItem = new Hashtable();
0119:            private static UIProperties properties = new UIProperties();
0120:            private static ArrayList myWindows = new ArrayList();
0121:            private static Experiment experiment;
0122:
0123:            private transient Logger log;
0124:
0125:            // configure what views to support
0126:            private static final String[] views = { NamedFrame.COMMUNITY,
0127:                    NamedFrame.AGENT, NamedFrame.PLAN, NamedFrame.THREAD,
0128:                    NamedFrame.METRICS,
0129:            //     NamedFrame.TOPOLOGY,
0130:            };
0131:
0132:            private static final String[] tooltips = { "Display communities.",
0133:                    "Display agents and their relationships.",
0134:                    "Display plan objects.",
0135:                    "Display plan objects related to a specified plan object.",
0136:                    "Display metrics.",
0137:            //    "Display topology."
0138:            };
0139:
0140:            // TODO: need new gif for topology
0141:            private static final String[] iconFilenames = { "community.gif",
0142:                    "society.gif", "event.gif", "thread.gif", "metric.gif",
0143:            //    "community.gif"
0144:            };
0145:
0146:            /**
0147:             * Start the society monitor.  If there is a single running
0148:             * experiment in CSMART, then monitor it; otherwise, ask the
0149:             * user which host and port to contact to monitor an experiment.
0150:             */
0151:
0152:            public CSMARTUL(CSMART csmart, Experiment experimentToMonitor) {
0153:                this .csmart = csmart;
0154:                experiment = experimentToMonitor;
0155:                createLogger();
0156:
0157:                // find host and port to contact
0158:                if (experiment != null)
0159:                    setHostToMonitor();
0160:                // create one version of properties for all objects
0161:                // and set them in CSMARTGraph because it can't address CSMARTUL
0162:                // TODO: better handling of properties
0163:                CSMARTGraph.setProperties(properties);
0164:                JMenuBar menuBar = new JMenuBar();
0165:                getRootPane().setJMenuBar(menuBar);
0166:                JMenu fileMenu = new JMenu(FILE_MENU);
0167:                fileMenu
0168:                        .setToolTipText("Create new views, open saved views, or exit.");
0169:                JMenu newMenu = new JMenu(NEW_MENU_ITEM);
0170:                newMenu.setToolTipText("Create new views.");
0171:                fileMenu.add(newMenu);
0172:
0173:                JMenu openMenu = new JMenu(OPEN_MENU_ITEM);
0174:                openMenu.setToolTipText("Open a saved view.");
0175:                JMenuItem openGraphMenuItem = new JMenuItem(
0176:                        OPEN_GRAPH_MENU_ITEM);
0177:                openGraphMenuItem.setToolTipText("Open a saved graph view.");
0178:                openGraphMenuItem.addActionListener(this );
0179:                openMenu.add(openGraphMenuItem);
0180:                JMenuItem openXMLMenuItem = new JMenuItem(OPEN_XML_MENU_ITEM);
0181:                openXMLMenuItem.setToolTipText("Open an xml file.");
0182:                openXMLMenuItem.addActionListener(this );
0183:                openMenu.add(openXMLMenuItem);
0184:                JMenuItem openMetricMenuItem = new JMenuItem(
0185:                        OPEN_METRIC_MENU_ITEM);
0186:                openMetricMenuItem.setToolTipText("Open a saved metrics view.");
0187:                openMetricMenuItem.addActionListener(this );
0188:                //openMetricMenuItem.setEnabled(false);
0189:                openMenu.add(openMetricMenuItem);
0190:                fileMenu.add(openMenu);
0191:
0192:                JMenuItem monitorMenuItem = new JMenuItem(MONITOR_MENU_ITEM);
0193:                monitorMenuItem
0194:                        .setToolTipText("Monitor an experiment or other URL.");
0195:                monitorMenuItem.addActionListener(this );
0196:                fileMenu.add(monitorMenuItem);
0197:
0198:                JMenuItem exitMenuItem = new JMenuItem(EXIT_MENU_ITEM);
0199:                exitMenuItem.setToolTipText("Exit this tool.");
0200:                exitMenuItem.addActionListener(this );
0201:                fileMenu.add(exitMenuItem);
0202:
0203:                windowMenu = new JMenu(WINDOW_MENU);
0204:                windowMenu.setToolTipText("Display selected window.");
0205:
0206:                menuBar.add(fileMenu);
0207:
0208:                // if running stand alone, then add a windows menu
0209:                // to keep track of windows created by this tool
0210:                // TODO: replicated NamedWindow capability at this level, but under csmart
0211:                if (csmart == null) {
0212:                    menuBar.add(windowMenu);
0213:                    setTitle("Society Monitor");
0214:                }
0215:
0216:                // Add a Help menu item
0217:                JMenu helpmenu = new JMenu(HELP_MENU);
0218:                for (int i = 0; i < helpMenuItems.length; i++) {
0219:                    JMenuItem mItem = new JMenuItem(helpMenuItems[i]);
0220:                    mItem.addActionListener(this );
0221:                    helpmenu.add(mItem);
0222:                }
0223:                menuBar.add(helpmenu);
0224:
0225:                JToolBar toolBar = new JToolBar();
0226:                toolBar.setLayout(new GridLayout(1, 5, 2, 2));
0227:                getContentPane().add("North", toolBar);
0228:
0229:                for (int i = 0; i < views.length; i++) {
0230:                    JButton button = makeButton(views[i], iconFilenames[i]);
0231:                    button.setToolTipText(tooltips[i]);
0232:                    button.setHorizontalTextPosition(JButton.CENTER);
0233:                    button.setVerticalTextPosition(JButton.BOTTOM);
0234:                    button.addActionListener(this );
0235:                    toolBar.add(button);
0236:                    JMenuItem menuItem = new JMenuItem(views[i]);
0237:                    menuItem.addActionListener(this );
0238:                    newMenu.add(menuItem);
0239:                }
0240:
0241:                if (csmart == null) {
0242:                    addWindowListener(new WindowAdapter() {
0243:                        public void windowClosing(WindowEvent e) {
0244:                            System.exit(0);
0245:                        }
0246:                    });
0247:                    // if running standalone, then manage the windows created by this tool
0248:                    NamedFrame.getNamedFrame().addObserver(this );
0249:                } else {
0250:                    // if running under csmart, close windows we created when we're closed
0251:                    addWindowListener(new WindowAdapter() {
0252:                        public void windowClosing(WindowEvent e) {
0253:                            closeSubWindows();
0254:                        }
0255:                    });
0256:                }
0257:
0258:                pack();
0259:                setVisible(true);
0260:            }
0261:
0262:            private void createLogger() {
0263:                log = CSMART.createLogger(this .getClass().getName());
0264:            }
0265:
0266:            /**
0267:             * Find a URL from the current running experiment.
0268:             * From the list of hosts which have nodes which have agents,
0269:             * finds a node on which org.cougaar.lib.web.http.port is defined
0270:             * and uses the corresponding host, OR
0271:             * finds a node which has agents, and uses the corresponding host
0272:             * and the default port.
0273:             */
0274:            private static void setHostToMonitor() {
0275:                Logger log = CSMART.createLogger(CSMARTUL.class.getName());
0276:                HostComponent[] hosts = experiment.getHostComponents();
0277:                for (int i = 0; i < hosts.length; i++) {
0278:                    NodeComponent[] nodes = hosts[i].getNodes();
0279:                    // skip hosts that have no nodes
0280:                    if (nodes == null || nodes.length == 0)
0281:                        continue;
0282:                    for (int j = 0; j < nodes.length; j++) {
0283:                        AgentComponent[] agents = nodes[j].getAgents();
0284:                        // skip nodes that have no agents
0285:                        if (agents == null || agents.length == 0)
0286:                            continue;
0287:                        // potential hosts and nodes
0288:                        agentHost = hosts[i].getShortName();
0289:                        Properties arguments = nodes[j].getArguments();
0290:                        if (arguments == null)
0291:                            continue;
0292:                        String port = arguments.getProperty(AGENT_HTTP_PORT);
0293:                        if (port != null) {
0294:                            try {
0295:                                agentPort = Integer.parseInt(port);
0296:                                break; // have host and specific port
0297:                            } catch (Exception e) {
0298:                                if (log.isErrorEnabled())
0299:                                    log.error("Exception parsing "
0300:                                            + AGENT_HTTP_PORT + " : ", e);
0301:                            }
0302:                        }
0303:                    }
0304:                }
0305:                // FIXME: Use secure mode here if necessary! Perhaps the experiment / host knows?
0306:                agentURL = ClientServletUtil.makeURL(agentHost, agentPort);
0307:                reset(agentHost, agentPort);
0308:            }
0309:
0310:            private JButton makeButton(String label, String iconFilename) {
0311:                URL iconURL = getClass().getResource(iconFilename);
0312:                if (iconURL == null)
0313:                    return new JButton(label);
0314:                ImageIcon icon = new ImageIcon(iconURL);
0315:                return new JButton(label, icon);
0316:            }
0317:
0318:            /**
0319:             * The Observer interface.
0320:             * Notified by NamedFrame addFrame method.
0321:             * Argument is vector of frame title (String) and the frame (JFrame).
0322:             * Add title to window menu, and when user selects title,
0323:             * pop frame to front.
0324:             * Used only if this tool is run standalone (outside of CSMART).
0325:             * @see NamedFrame#addFrame
0326:             */
0327:            public void update(Observable o, Object arg) {
0328:                if (o instanceof  NamedFrame) {
0329:                    //      NamedFrame namedFrame = (NamedFrame) o;
0330:                    NamedFrame.Event event = (NamedFrame.Event) arg;
0331:                    if (event.eventType == NamedFrame.Event.ADDED) {
0332:                        JMenuItem menuItem = new JMenuItem(event.title);
0333:                        titleToMenuItem.put(event.title, menuItem);
0334:                        menuItem.addActionListener(this );
0335:                        windowMenu.add(menuItem);
0336:                    } else if (event.eventType == NamedFrame.Event.REMOVED) {
0337:                        JMenuItem menuItem = (JMenuItem) titleToMenuItem
0338:                                .get(event.title);
0339:                        if (menuItem == null) {
0340:                            if (log.isErrorEnabled()) {
0341:                                log.error("CSMARTUL: No window menu item for "
0342:                                        + event.title);
0343:                            }
0344:                        } else {
0345:                            windowMenu.remove(menuItem);
0346:                            titleToMenuItem.remove(event.title);
0347:                        }
0348:                    } else if (event.eventType == NamedFrame.Event.CHANGED) {
0349:                        JMenuItem menuItem = (JMenuItem) titleToMenuItem
0350:                                .get(event.prevTitle);
0351:                        if (menuItem == null) {
0352:                            if (log.isErrorEnabled()) {
0353:                                log.error("CSMARTUL: No window menu item for "
0354:                                        + event.title);
0355:                            }
0356:                        } else {
0357:                            windowMenu.remove(menuItem);
0358:                            titleToMenuItem.remove(event.prevTitle);
0359:                            JMenuItem newMenuItem = new JMenuItem(event.title);
0360:                            titleToMenuItem.put(event.title, newMenuItem);
0361:                            newMenuItem.addActionListener(this );
0362:                            windowMenu.add(newMenuItem);
0363:                        }
0364:                    }
0365:                }
0366:            }
0367:
0368:            public void actionPerformed(ActionEvent e) {
0369:                String s = ((AbstractButton) e.getSource()).getActionCommand();
0370:                if (s.equals(NamedFrame.COMMUNITY)) {
0371:                    makeCommunityGraph();
0372:                } else if (s.equals(NamedFrame.AGENT)) {
0373:                    makeSocietyGraph(); // only the name changes!
0374:                } else if (s.equals(NamedFrame.PLAN)) {
0375:                    makePlanGraph();
0376:                } else if (s.equals(NamedFrame.THREAD)) {
0377:                    makeThreadGraph();
0378:                } else if (s.equals(NamedFrame.METRICS)) {
0379:                    makeMetricsGraph();
0380:                    //     } else if (s.equals(NamedFrame.TOPOLOGY)) {
0381:                    //       makeTopologyGraph();
0382:                } else if (s.equals(OPEN_GRAPH_MENU_ITEM)) {
0383:                    openGraph();
0384:                } else if (s.equals(OPEN_XML_MENU_ITEM)) {
0385:                    openXMLFile();
0386:                } else if (s.equals(OPEN_METRIC_MENU_ITEM)) {
0387:                    openMetrics();
0388:                } else if (s.equals(MONITOR_MENU_ITEM)) {
0389:                    getAgentURL();
0390:                } else if (s.equals(EXIT_MENU_ITEM)) {
0391:                    if (csmart == null)
0392:                        System.exit(0); // if running standalone, exit
0393:                    else {
0394:                        closeSubWindows();
0395:                        NamedFrame.getNamedFrame().removeFrame(this );
0396:                        dispose();
0397:                    }
0398:                } else if (s.equals(HELP_MENU_ITEM)) {
0399:                    URL help = (URL) getClass().getResource(HELP_DOC);
0400:                    if (help != null)
0401:                        Browser.setPage(help);
0402:                } else if (s.equals(ABOUT_CSMART_ITEM)) {
0403:                    URL about = (URL) getClass().getResource(ABOUT_DOC);
0404:                    if (about != null)
0405:                        Browser.setPage(about);
0406:                } else {
0407:                    JFrame f = NamedFrame.getNamedFrame().getFrame(s);
0408:                    if (f != null) {
0409:                        f.toFront();
0410:                        f.setState(Frame.NORMAL);
0411:                    }
0412:                }
0413:            }
0414:
0415:            /**
0416:             * Close windows created by this tool by calling their window listeners;
0417:             * those window listeners close their associated frames
0418:             * such as attribute tables and overview windows.
0419:             */
0420:
0421:            private void closeSubWindows() {
0422:                for (int i = 0; i < myWindows.size(); i++) {
0423:                    Window w = (Window) myWindows.get(i);
0424:                    WindowListener[] wls = (WindowListener[]) (w
0425:                            .getListeners(WindowListener.class));
0426:                    for (int j = 0; j < wls.length; j++)
0427:                        wls[j].windowClosing(new WindowEvent(w,
0428:                                WindowEvent.WINDOW_CLOSED));
0429:                }
0430:                myWindows.clear();
0431:            }
0432:
0433:            private void openMetrics() {
0434:                File file = readFile("mtr", "Metrics files");
0435:                if (file != null && file.canRead()) {
0436:                    Window w = (Window) new CSMARTMetricsFrame(
0437:                            NamedFrame.METRICS, file);
0438:                    myWindows.add(w);
0439:                } else {
0440:                    if (log.isInfoEnabled())
0441:                        log.info("openMetrics: no (readable) file selected");
0442:                }
0443:            }
0444:
0445:            private void openXMLFile() {
0446:                File file = readFile("xml", "xml files");
0447:                if (file != null && file.canRead()) {
0448:                    CSMARTGraph graph = XMLFrame.createGraph(file);
0449:                    if (graph != null)
0450:                        new XMLFrame("Test", graph);
0451:                }
0452:            }
0453:
0454:            private void openGraph() {
0455:                File file = readFile("dot", "graph files");
0456:                if (file != null && file.canRead()) {
0457:                    CSMARTGraph graph = readGraphFromFile(file);
0458:                    if (graph == null)
0459:                        return;
0460:                    String fileName = file.getName();
0461:                    String graphType = (String) graph
0462:                            .getAttributeValue(CSMARTGraph.GRAPH_TYPE);
0463:                    if (graphType.equals(CSMARTGraph.GRAPH_TYPE_XML))
0464:                        new XMLFrame("Test", graph);
0465:                    else if (graphType.equals(CSMARTGraph.GRAPH_TYPE_COMMUNITY))
0466:                        new ULCommunityFrame(NamedFrame.COMMUNITY + ": <"
0467:                                + fileName + ">", graph);
0468:                    else if (graphType.equals(CSMARTGraph.GRAPH_TYPE_SOCIETY))
0469:                        new ULSocietyFrame(NamedFrame.AGENT + ": <" + fileName
0470:                                + ">", graph);
0471:                    else if (graphType.equals(CSMARTGraph.GRAPH_TYPE_THREAD))
0472:                        new ULPlanFrame(NamedFrame.THREAD + ": <" + fileName
0473:                                + ">", graph, null);
0474:                    else if (graphType.equals(CSMARTGraph.GRAPH_TYPE_PLAN))
0475:                        new ULPlanFrame(NamedFrame.PLAN + ": <" + fileName
0476:                                + ">", graph, new ULPlanFilter(graph));
0477:                    else
0478:                        JOptionPane.showMessageDialog(this ,
0479:                                "Unknown graph type: " + graphType);
0480:                } else {
0481:                    if (log.isInfoEnabled())
0482:                        log.info("openGraph: no (readable) file selected");
0483:                }
0484:            }
0485:
0486:            private File readFile(String extension, String description) {
0487:                String[] filters = { extension };
0488:                ExtensionFileFilter filter = new ExtensionFileFilter(filters,
0489:                        description);
0490:                JFileChooser jfc = new JFileChooser(System
0491:                        .getProperty("org.cougaar.install.path"));
0492:                jfc.addChoosableFileFilter(filter);
0493:                if (jfc.showOpenDialog(this ) == JFileChooser.CANCEL_OPTION)
0494:                    return null;
0495:                File file = jfc.getSelectedFile();
0496:                if (file == null)
0497:                    return null;
0498:                String ext = filter.getExtension(file);
0499:                if (ext == null || !ext.equals(extension)) {
0500:                    JOptionPane.showMessageDialog(this , "File ("
0501:                            + file.getName() + ") must have extension: '"
0502:                            + extension + "'.");
0503:                    return null;
0504:                }
0505:
0506:                return file;
0507:            }
0508:
0509:            private CSMARTGraph readGraphFromFile(File file) {
0510:                CSMARTGraph graph = CSMARTGraph.createGraphFromDotFile(file);
0511:                if (graph == null)
0512:                    JOptionPane.showMessageDialog(this ,
0513:                            "Cannot read graph from file: " + file.getName());
0514:                return graph;
0515:            }
0516:
0517:            /**
0518:             * Query user for agent URL if running multiple experiments
0519:             * or not running under CSMART.
0520:             */
0521:            private static void getAgentURL() {
0522:                //    Logger log = CSMART.createLogger(CSMARTUL.class.getName());
0523:
0524:                if (csmart != null)
0525:                    setExperimentToMonitor();
0526:                else
0527:                    setURLToMonitor();
0528:            }
0529:
0530:            /**
0531:             * If running under csmart, ask user for experiment to monitor.
0532:             */
0533:            private static void setExperimentToMonitor() {
0534:                // ask user to select experiment from running experiments
0535:                Experiment[] exp = CSMART.getRunningExperiments();
0536:                Vector experimentNames = new Vector(exp.length + 1);
0537:                for (int i = 0; i < exp.length; i++) {
0538:                    if (!experimentNames.contains(exp[i].getExperimentName()))
0539:                        experimentNames.add(exp[i].getExperimentName());
0540:                }
0541:                experimentNames.add("No experiment -- custom URL");
0542:                JComboBox cb = new JComboBox(experimentNames);
0543:                JPanel panel = new JPanel();
0544:                panel.add(new JLabel("Experiment to Monitor:"));
0545:                panel.add(cb);
0546:                panel.add(new JLabel("Select last item to enter custom URL"));
0547:                int result = JOptionPane.showConfirmDialog(null, panel,
0548:                        "Experiment To Monitor", JOptionPane.OK_CANCEL_OPTION,
0549:                        JOptionPane.PLAIN_MESSAGE);
0550:                if (result != JOptionPane.OK_OPTION)
0551:                    return;
0552:                if (cb.getSelectedIndex() == exp.length) {
0553:                    experiment = null;
0554:                    setURLToMonitor();
0555:                } else {
0556:                    experiment = exp[cb.getSelectedIndex()];
0557:                    setHostToMonitor();
0558:                }
0559:            }
0560:
0561:            /**
0562:             * If not running under csmart, ask user for url (host and port)
0563:             * to monitor.
0564:             */
0565:            private static void setURLToMonitor() {
0566:                Logger log = CSMART.createLogger(CSMARTUL.class.getName());
0567:                // FIXME: Use secure mode if necessary here!
0568:                // How do I decide?
0569:                // Must I use a different port as well?
0570:                JTextField tf = new JTextField(ClientServletUtil.makeURL(
0571:                        "localhost", agentPort));
0572:                JPanel panel = new JPanel();
0573:                panel.add(new JLabel("Agent URL:"));
0574:                panel.add(tf);
0575:                int result = JOptionPane.showOptionDialog(null, panel,
0576:                        "Agent URL", JOptionPane.OK_CANCEL_OPTION,
0577:                        JOptionPane.PLAIN_MESSAGE, null, null, null);
0578:                if (result != JOptionPane.OK_OPTION)
0579:                    return;
0580:                agentURL = tf.getText();
0581:                int startIndex = 0;
0582:                int endIndex = -1;
0583:
0584:                if (agentURL.indexOf("://") != -1) {
0585:                    agentProtocol = agentURL.substring(0, agentURL
0586:                            .indexOf("://"));
0587:                    startIndex = agentURL.indexOf("://") + 3;
0588:                }
0589:
0590:                endIndex = agentURL.indexOf(":", startIndex);
0591:                if (endIndex != -1)
0592:                    agentHost = agentURL.substring(startIndex, endIndex);
0593:                else
0594:                    agentHost = agentURL.substring(startIndex);
0595:
0596:                startIndex = endIndex;
0597:                if (startIndex != -1) {
0598:                    String s = agentURL.substring(startIndex + 1);
0599:                    try {
0600:                        agentPort = Integer.parseInt(s);
0601:                    } catch (Exception e) {
0602:                        if (log.isErrorEnabled()) {
0603:                            log
0604:                                    .error("setURLToMonitor got bad agentPort: "
0605:                                            + s);
0606:                        }
0607:                        agentURL = agentURL.substring(0, startIndex + 1)
0608:                                + agentPort;
0609:                    }
0610:                } else {
0611:                    if (log.isDebugEnabled())
0612:                        log
0613:                                .debug("setURLToMonitor got not port, using protocol defaults");
0614:                    // no port given
0615:                    if (agentProtocol.equalsIgnoreCase("http"))
0616:                        agentPort = 80;
0617:                    else if (agentProtocol.equalsIgnoreCase("https"))
0618:                        agentPort = 443;
0619:                }
0620:
0621:                if (agentURL.indexOf("://") == -1)
0622:                    agentURL = agentProtocol + "://" + agentURL;
0623:
0624:                //    log.debug("protocol: " + agentProtocol + ", host: " + agentHost + ", port: " + agentPort);
0625:                //      if (startIndex != -1) {
0626:                //        endIndex = agentURL.lastIndexOf('/');
0627:                //        if (endIndex != -1) {
0628:                //  	String s = agentURL.substring(startIndex+1, endIndex);
0629:                //  	try {
0630:                //  	  agentPort = Integer.parseInt(s);
0631:                //  	} catch (Exception e) {
0632:                //            if(log.isDebugEnabled()) {
0633:                //              log.error("CSMARTUL: " + e);
0634:                //            }
0635:                //  	}
0636:                //        }
0637:                //      }
0638:                reset(agentHost, agentPort);
0639:            }
0640:
0641:            private static void reset(String agentHost, int agentPort) {
0642:                agentMap = new CSMARTAgentMap(agentHost, agentPort);
0643:                communityToAgents = null;
0644:                agentToCommunity = null;
0645:                agentToURL = null;
0646:            }
0647:
0648:            /**
0649:             * Query user for agent URL and get names of agents from that URL.
0650:             * The user is only queried once, unless we fail to contact the agent.
0651:             */
0652:            public static Vector getAgentURLs() {
0653:                if (agentURL == null)
0654:                    getAgentURL();
0655:                if (agentURL == null)
0656:                    return null;
0657:                Vector agentURLs = null;
0658:                try {
0659:                    agentURLs = ClientServletUtil.getAgentURLs(agentURL);
0660:                    if (agentURLs == null)
0661:                        JOptionPane.showMessageDialog(null, agentURL + "/"
0662:                                + ClientServletUtil.AGENT_PROVIDER_SERVLET
0663:                                + " returned null; no information to graph.");
0664:                    else if (agentURLs.size() == 0)
0665:                        JOptionPane
0666:                                .showMessageDialog(
0667:                                        null,
0668:                                        agentURL
0669:                                                + "/"
0670:                                                + ClientServletUtil.AGENT_PROVIDER_SERVLET
0671:                                                + " returned no agents; no information to graph.");
0672:
0673:                    // FIXME: Ignore NodeAgents here?
0674:                } catch (Exception e) {
0675:                    JOptionPane.showMessageDialog(null, "Failed to contact: "
0676:                            + agentURL + "/"
0677:                            + ClientServletUtil.AGENT_PROVIDER_SERVLET
0678:                            + "; no information to graph.");
0679:                }
0680:
0681:                // if failed to contact agents, reset agentURL to null,
0682:                // so user is queried again
0683:                if (agentURLs == null)
0684:                    agentURL = null;
0685:                return agentURLs;
0686:            }
0687:
0688:            /**
0689:             * Query user for an agent URL in the society,
0690:             * get the URLs for all agents in that society,
0691:             * and get objects from the specified servlet at all agents.
0692:             */
0693:
0694:            public static Collection getObjectsFromServlet(String servletName) {
0695:                Vector agentURLs = getAgentURLs();
0696:                if (agentURLs == null)
0697:                    return null;
0698:                return getObjectsFromServlet(agentURLs, servletName, null,
0699:                        null, -1);
0700:            }
0701:
0702:            /**
0703:             * Invoke specified servlet with parameter names, values, and limit,
0704:             * at specified agents, and return results.
0705:             */
0706:
0707:            private static Collection getObjectsFromServlet(Vector agentURLs,
0708:                    String servletName, ArrayList parameterNames,
0709:                    ArrayList parameterValues, int limit) {
0710:                return getObjectsFromServletWorker(agentURLs, servletName,
0711:                        parameterNames, parameterValues, limit);
0712:            }
0713:
0714:            private static Collection getObjectsFromServletWorker(
0715:                    Vector agentURLs, String servletName,
0716:                    ArrayList parameterNames, ArrayList parameterValues,
0717:                    int limit) {
0718:
0719:                Logger log = CSMART
0720:                        .createLogger("org.cougaar.tools.csmart.ui.monitor.viewer.CSMARTUL");
0721:
0722:                ServletResult servletResult = ClientServletUtil
0723:                        .getCollectionFromAgents(agentURLs, servletName,
0724:                                parameterNames, parameterValues, limit);
0725:                int n = servletResult.getNumberOfResponses();
0726:                StringBuffer buf = new StringBuffer(100);
0727:                Collection objectsFromServlet = null;
0728:                int nAgents = 0;
0729:                for (int i = 0; i < n; i++) {
0730:                    ServletResponse response = servletResult
0731:                            .getServletResponse(i);
0732:                    String s = response.getErrorMessage();
0733:                    if (s != null) {
0734:                        buf.append("Contacting: ");
0735:                        buf.append(response.getURL());
0736:                        buf.append(" ");
0737:                        buf.append(response.getErrorMessage());
0738:                        buf.append("\n");
0739:                    } else {
0740:                        Collection c = response.getCollection();
0741:                        if (c != null) {
0742:                            nAgents++;
0743:                            if (objectsFromServlet == null)
0744:                                objectsFromServlet = c;
0745:                            else {
0746:                                try {
0747:                                    objectsFromServlet.addAll(c);
0748:                                } catch (Exception e) {
0749:                                    if (log.isErrorEnabled()) {
0750:                                        log
0751:                                                .error(
0752:                                                        "CSMARTUL can't add results to collection: ",
0753:                                                        e);
0754:                                    }
0755:                                }
0756:                            }
0757:                        }
0758:                    }
0759:                }
0760:                if (servletResult.isLimitExceeded())
0761:                    JOptionPane.showMessageDialog(null,
0762:                            "Exceeded limit, producing a trimmed graph from "
0763:                                    + nAgents + " agents.");
0764:                else if (buf.length() != 0)
0765:                    JOptionPane.showMessageDialog(null, buf.toString());
0766:                return objectsFromServlet;
0767:            }
0768:
0769:            /**
0770:             * Query servlet to get community <-> agent mapping.
0771:             */
0772:
0773:            private static void getCommunities() {
0774:                if (communityToAgents != null)
0775:                    return; // only do this once
0776:                // get agent and community names
0777:                Collection orgAssets = getObjectsFromServlet(ClientServletUtil.COMMUNITY_SERVLET);
0778:                if (orgAssets == null)
0779:                    return;
0780:                if (orgAssets.size() == 0)
0781:                    JOptionPane.showMessageDialog(null,
0782:                            "No information received from agents.");
0783:                processOrganizationAssets(orgAssets);
0784:            }
0785:
0786:            /**
0787:             * Create community<->agents mapping.
0788:             */
0789:
0790:            private static void processOrganizationAssets(Collection orgAssets) {
0791:                Logger log = CSMART
0792:                        .createLogger("org.cougaar.tools.csmart.ui.monitor.viewer.CSMARTUL");
0793:
0794:                communityToAgents = new Hashtable();
0795:                agentToCommunity = new Hashtable();
0796:                agentToURL = new Hashtable();
0797:                for (Iterator i = orgAssets.iterator(); i.hasNext();) {
0798:                    Object o = i.next();
0799:                    if (!(o instanceof  PropertyTree)) {
0800:                        if (log.isWarnEnabled()) {
0801:                            log.warn("CSMARTUL: expected PropertyTree, got: "
0802:                                    + o.getClass());
0803:                        }
0804:                        continue;
0805:                    }
0806:                    PropertyTree properties = (PropertyTree) o;
0807:                    String agentName = (String) properties
0808:                            .get(PropertyNames.AGENT_NAME);
0809:                    String communityName = (String) properties
0810:                            .get(PropertyNames.AGENT_COMMUNITY_NAME);
0811:                    agentToCommunity.put(agentName, communityName);
0812:                    Vector agents = (Vector) communityToAgents
0813:                            .get(communityName);
0814:                    if (agents == null) {
0815:                        agents = new Vector();
0816:                        agents.addElement(agentName);
0817:                        if (log.isDebugEnabled()) {
0818:                            log.debug("Adding agent name: " + agentName);
0819:                        }
0820:                        communityToAgents.put(communityName, agents);
0821:                    } else
0822:                        agents.addElement(agentName);
0823:                    String url = (String) properties
0824:                            .get(PropertyNames.AGENT_URL);
0825:                    url = url.substring(0, url
0826:                            .indexOf(ClientServletUtil.COMMUNITY_SERVLET));
0827:                    agentToURL.put(agentName, url);
0828:                }
0829:            }
0830:
0831:            /**
0832:             * Make community graph.
0833:             */
0834:
0835:            private void makeCommunityGraph() {
0836:                // get agent and community names
0837:                Collection objectsFromServlet = getObjectsFromServlet(ClientServletUtil.COMMUNITY_SERVLET);
0838:                if (objectsFromServlet == null)
0839:                    return;
0840:                int n = objectsFromServlet.size();
0841:                if (n == 0)
0842:                    JOptionPane.showMessageDialog(null,
0843:                            "No information received from agents.");
0844:                // set up agent<->community mappings in hashtables
0845:                processOrganizationAssets(objectsFromServlet);
0846:                Vector nodeObjects = new Vector(n);
0847:                Hashtable nameToNodeObject = new Hashtable(n);
0848:                for (Iterator i = objectsFromServlet.iterator(); i.hasNext();) {
0849:                    PropertyTree properties = (PropertyTree) i.next();
0850:                    String communityName = (String) properties
0851:                            .get(PropertyNames.AGENT_COMMUNITY_NAME);
0852:                    if (nameToNodeObject.get(communityName) == null) { // filter duplicates
0853:                        nameToNodeObject.put(communityName,
0854:                                new ULCommunityNode(properties));
0855:                        nodeObjects.add(new ULCommunityNode(properties));
0856:                    }
0857:                }
0858:                // add member names to community nodes
0859:                Enumeration communityNames = communityToAgents.keys();
0860:                while (communityNames.hasMoreElements()) {
0861:                    String name = (String) communityNames.nextElement();
0862:                    ULCommunityNode node = (ULCommunityNode) nameToNodeObject
0863:                            .get(name);
0864:                    node.addMembers((Vector) communityToAgents.get(name));
0865:                }
0866:                if (nodeObjects.size() != 0) {
0867:                    Window w = (Window) new ULCommunityFrame(
0868:                            NamedFrame.COMMUNITY, new CSMARTGraph(nodeObjects,
0869:                                    CSMARTGraph.GRAPH_TYPE_COMMUNITY));
0870:                    myWindows.add(w);
0871:                }
0872:            }
0873:
0874:            /**
0875:             * Display a plan composed of tasks, plan elements, workflows and assets.
0876:             */
0877:
0878:            private void makePlanGraph() {
0879:                // get community<->agent mapping
0880:                getCommunities();
0881:
0882:                if (communityToAgents == null || communityToAgents.size() == 0)
0883:                    return; // nothing to graph
0884:
0885:                // get filter
0886:                ULPlanFilter filter = new ULPlanFilter(communityToAgents);
0887:                if (!filter.preFilter())
0888:                    return; // user cancelled filter
0889:
0890:                // get the list of agents
0891:                Vector agentsToContact = filter.getAgentsSelected();
0892:                if (agentsToContact == null || agentsToContact.size() == 0)
0893:                    return;
0894:
0895:                Vector agentURLs = new Vector();
0896:                for (int i = 0; i < agentsToContact.size(); i++) {
0897:                    String URL = (String) agentToURL
0898:                            .get((String) agentsToContact.elementAt(i));
0899:                    if (URL != null)
0900:                        agentURLs.add(URL);
0901:                }
0902:
0903:                String servletId = ClientServletUtil.PLAN_SERVLET;
0904:                int limit = filter.getNumberOfObjects();
0905:                ArrayList parameterNames = new ArrayList(1);
0906:                ArrayList parameterValues = new ArrayList(1);
0907:                // for example, planObjectsToIgnore=Plan_Element,Workflow,Asset
0908:                String ignoreTypes = filter.getIgnoreObjectTypes();
0909:                if (ignoreTypes != null) {
0910:                    parameterNames.add(PropertyNames.PLAN_OBJECTS_TO_IGNORE);
0911:                    parameterValues.add(ignoreTypes);
0912:                }
0913:                Collection objectsFromServlets = getObjectsFromServletWorker(
0914:                        agentURLs, servletId, parameterNames, parameterValues,
0915:                        limit);
0916:
0917:                if (objectsFromServlets == null)
0918:                    return;
0919:
0920:                int nObjs = objectsFromServlets.size();
0921:                if (nObjs == 0)
0922:                    return;
0923:
0924:                // a Vector of ULPlanNodes
0925:                Vector nodeObjects = new Vector();
0926:
0927:                int nDuplicates = 0;
0928:                Set UIDs = new HashSet();
0929:                Iterator iter = objectsFromServlets.iterator();
0930:                for (int i = 0; i < nObjs; i++) {
0931:                    PropertyTree properties = (PropertyTree) iter.next();
0932:                    // get the UID
0933:                    String UID = (String) properties
0934:                            .get(PropertyNames.UID_ATTR);
0935:                    // filter duplicates by UID
0936:                    if (!(UIDs.contains(UID))) {
0937:                        String agentName = (String) properties
0938:                                .get(PropertyNames.AGENT_ATTR);
0939:                        String communityName = ((agentName != null) ? ((String) agentToCommunity
0940:                                .get(agentName))
0941:                                : "");
0942:                        nodeObjects.add(new ULPlanNode(properties,
0943:                                communityName));
0944:                        UIDs.add(UID);
0945:                    } else {
0946:                        nDuplicates++;
0947:                    }
0948:
0949:                    // for debugging
0950:                    //       if(log.isDebugEnabled()) {
0951:                    //         Set keys = properties.keySet();
0952:                    //         log.debug("Property names/values........");
0953:                    //         for (Iterator j = keys.iterator(); j.hasNext(); ) {
0954:                    //           String s = (String)j.next();
0955:                    //           log.debug(s + "," + properties.get(s));
0956:                    //         }
0957:                    //       }
0958:                    // end for debugging
0959:
0960:                }
0961:                // only create popup if objects were found
0962:                if (nodeObjects.size() != 0) {
0963:                    Window w = (Window) new ULPlanFrame(NamedFrame.PLAN,
0964:                            new CSMARTGraph(nodeObjects,
0965:                                    CSMARTGraph.GRAPH_TYPE_PLAN), filter);
0966:                    myWindows.add(w);
0967:                }
0968:            }
0969:
0970:            /**
0971:             * Called to make a thread graph directly from the launcher window.
0972:             */
0973:
0974:            private void makeThreadGraph() {
0975:                // will gather these parameters:
0976:                String UID;
0977:                String agentName;
0978:                int limit;
0979:                boolean isDown;
0980:
0981:                // FIXME: should have a single popup:
0982:                //    UID=<default to empty>
0983:                //    AgentName=<default to empty>
0984:                //    Limit=<default to 200, add "no-limit" checkbox + grey-out>
0985:                //    Trace=<option box of "children"/"parents", default to children>
0986:
0987:                // awkward popups for now:
0988:                UID = JOptionPane.showInputDialog("Plan Object UID: ");
0989:                if (UID == null) {
0990:                    return;
0991:                }
0992:                agentName = JOptionPane.showInputDialog("Agent name: ");
0993:                if (agentName == null) {
0994:                    return;
0995:                }
0996:                String sLimit = JOptionPane.showInputDialog(
0997:                        "Limit (e.g. 200, negative for all): ", "200");
0998:                if (sLimit == null || sLimit.trim().equals("")) {
0999:                    return;
1000:                }
1001:                try {
1002:                    limit = Integer.parseInt(sLimit);
1003:                } catch (NumberFormatException nfe) {
1004:                    if (log.isErrorEnabled()) {
1005:                        log.error("Illegal number: " + sLimit);
1006:                    }
1007:                    return;
1008:                }
1009:
1010:                if (limit == 0)
1011:                    return;
1012:
1013:                String sIsDown = JOptionPane
1014:                        .showInputDialog("TraceChildren (true/false): ");
1015:                if (sIsDown == null) {
1016:                    return;
1017:                }
1018:                isDown = (!("false".equalsIgnoreCase(sIsDown)));
1019:
1020:                // create the graph
1021:                makeThreadGraph(UID, agentName, isDown, limit);
1022:            }
1023:
1024:            /**
1025:             * Make a thread graph using the plan object threads servlet.
1026:             * Called to make a thread graph when the user has selected
1027:             * an object in the plan graph.
1028:             */
1029:
1030:            public static void makeThreadGraph(String UID, String agentName,
1031:                    boolean isDown, int limit) {
1032:                getCommunities();
1033:                List objectsFromServlet = ThreadUtils.getFullThread(agentMap,
1034:                        isDown, limit, agentName, UID);
1035:                if (objectsFromServlet == null
1036:                        || objectsFromServlet.size() == 0) {
1037:                    JOptionPane
1038:                            .showMessageDialog(null,
1039:                                    "Cannot obtain thread information for specified plan object.");
1040:                    return;
1041:                }
1042:                if ((limit >= 0) && (objectsFromServlet.size() > limit)) {
1043:                    JOptionPane.showMessageDialog(null, "Exceeded limit of "
1044:                            + limit + " objects; creating a trimmed graph.");
1045:                }
1046:                Vector nodeObjects = new Vector(objectsFromServlet.size());
1047:                Vector assetPropertyTrees = new Vector();
1048:                for (Iterator i = objectsFromServlet.iterator(); i.hasNext();) {
1049:                    PropertyTree properties = (PropertyTree) i.next();
1050:                    // pick off the assets and process after we have them all
1051:                    String objectType = (String) properties
1052:                            .get(PropertyNames.OBJECT_TYPE);
1053:                    if ((objectType != null)
1054:                            && (objectType.equals(PropertyNames.ASSET_OBJECT))) {
1055:                        assetPropertyTrees.add(properties);
1056:                        continue;
1057:                    }
1058:                    String agent = (String) properties
1059:                            .get(PropertyNames.AGENT_ATTR);
1060:                    String community = "";
1061:                    if (agent != null)
1062:                        community = (String) agentToCommunity.get(agent);
1063:                    nodeObjects.add(new ULPlanNode(properties, community));
1064:                }
1065:                // now pick up the correct assets
1066:                Collection c = getAssets(assetPropertyTrees);
1067:                for (Iterator i = c.iterator(); i.hasNext();) {
1068:                    PropertyTree properties = (PropertyTree) i.next();
1069:                    String agent = (String) properties
1070:                            .get(PropertyNames.AGENT_ATTR);
1071:                    String community = "";
1072:                    if (agent != null)
1073:                        community = (String) agentToCommunity.get(agent);
1074:                    nodeObjects.add(new ULPlanNode(properties, community));
1075:                }
1076:                if (nodeObjects.size() != 0) {
1077:                    Window w = (Window) new ULPlanFrame(NamedFrame.PLAN,
1078:                            new CSMARTGraph(nodeObjects,
1079:                                    CSMARTGraph.GRAPH_TYPE_PLAN),
1080:                            new ULPlanFilter(communityToAgents));
1081:                    myWindows.add(w);
1082:                }
1083:            }
1084:
1085:            /**
1086:             * Ignore asset property trees for agents that are not from
1087:             * their own agent, iff we have an asset property tree from its own agent.
1088:             * Helper for makeThreadGraph.
1089:             */
1090:
1091:            private static Collection getAssets(Vector assetPTs) {
1092:                Hashtable assets = new Hashtable();
1093:                for (int i = 0; i < assetPTs.size(); i++) {
1094:                    PropertyTree pt = (PropertyTree) assetPTs.get(i);
1095:                    String assetKeyName = (String) pt
1096:                            .get(PropertyNames.ASSET_KEY);
1097:                    // if we have no info on this asset, then save this info
1098:                    if (!assets.containsKey(assetKeyName)) {
1099:                        assets.put(assetKeyName, pt);
1100:                    } else {
1101:                        // if this info is from the asset's agent, then save it instead
1102:                        String assetAgentName = (String) pt
1103:                                .get(PropertyNames.ASSET_AGENT);
1104:                        String assetAgentAttrName = (String) pt
1105:                                .get(PropertyNames.AGENT_ATTR);
1106:                        if (assetAgentAttrName.equals(assetAgentName)) {
1107:                            assets.put(assetKeyName, pt);
1108:                        }
1109:                    }
1110:                }
1111:                return assets.values();
1112:            }
1113:
1114:            /**
1115:             * Make a graph of the society.
1116:             */
1117:
1118:            private void makeSocietyGraph() {
1119:                Collection objectsFromServlet = getObjectsFromServlet(ClientServletUtil.AGENT_INFO_SERVLET);
1120:                if (objectsFromServlet == null)
1121:                    return;
1122:                Vector nodeObjects = new Vector(objectsFromServlet.size());
1123:                Hashtable nameToUID = new Hashtable();
1124:                for (Iterator i = objectsFromServlet.iterator(); i.hasNext();) {
1125:                    Object o = i.next();
1126:                    if (!(o instanceof  PropertyTree)) {
1127:                        if (log.isWarnEnabled()) {
1128:                            log.warn("CSMARTUL: expected PropertyTree, got: "
1129:                                    + o.getClass());
1130:                        }
1131:                        continue;
1132:                    }
1133:                    PropertyTree properties = (PropertyTree) o;
1134:                    ULSocietyNode node = new ULSocietyNode(properties);
1135:                    nodeObjects.add(node);
1136:                    String name = (String) properties
1137:                            .get(PropertyNames.ORGANIZATION_KEY_NAME);
1138:                    String UID = node.getUID();
1139:                    nameToUID.put(name, UID);
1140:                }
1141:                // organizations refer to each other by name,
1142:                // hence after creating nodes for all organizations,
1143:                // we create links from the name-to-UID mapping
1144:                for (int i = 0; i < nodeObjects.size(); i++) {
1145:                    ULSocietyNode node = (ULSocietyNode) nodeObjects
1146:                            .elementAt(i);
1147:                    PropertyTree properties = node.getProperties();
1148:                    Set keys = properties.keySet();
1149:                    for (Iterator keyIter = keys.iterator(); keyIter.hasNext();) {
1150:                        String key = (String) keyIter.next();
1151:                        if (key
1152:                                .startsWith(PropertyNames.ORGANIZATION_RELATED_TO)) {
1153:                            String relationship = key.substring(key
1154:                                    .lastIndexOf('_') + 1);
1155:                            String relatedToName = (String) properties.get(key);
1156:                            String relatedToUID = (String) nameToUID
1157:                                    .get(relatedToName);
1158:                            // this creates duplicate links but grappa catches them; see csmartgraph
1159:                            if (relatedToUID != null) {
1160:                                if (relationship.endsWith("Customer")
1161:                                        || relationship.endsWith("Superior"))
1162:                                    node.addIncomingLink(relatedToUID);
1163:                                else if (!relationship.endsWith("Provider")
1164:                                        && !relationship
1165:                                                .endsWith("Subordinate"))
1166:                                    // do nothing, assume that all relationships are dual
1167:                                    // so that graphing "one side" of the relationship is ok
1168:                                    //              node.addOutgoingLink(relatedToUID);
1169:                                    //            else
1170:                                    if (log.isInfoEnabled()) {
1171:                                        log.info("Unknown relationship: "
1172:                                                + relatedToName);
1173:                                    }
1174:                            }
1175:                        }
1176:                    }
1177:                }
1178:                if (nodeObjects.size() != 0) {
1179:                    Window w = (Window) new ULSocietyFrame(NamedFrame.AGENT,
1180:                            new CSMARTGraph(nodeObjects,
1181:                                    CSMARTGraph.GRAPH_TYPE_SOCIETY));
1182:                    myWindows.add(w);
1183:                }
1184:            }
1185:
1186:            public void makeMetricsGraph() {
1187:                Collection objectsFromServlet = getObjectsFromServlet(ClientServletUtil.METRICS_SERVLET);
1188:                if (objectsFromServlet == null)
1189:                    return;
1190:                if (log.isInfoEnabled()) {
1191:                    log.info("Received metrics: " + objectsFromServlet.size());
1192:                }
1193:
1194:                ArrayList names = new ArrayList();
1195:                ArrayList data = new ArrayList();
1196:
1197:                Iterator iter = objectsFromServlet.iterator();
1198:                while (iter.hasNext()) {
1199:                    Object obj = iter.next();
1200:                    if (obj instanceof  String) {
1201:                        names.add(obj);
1202:                    } else if (obj instanceof  Integer[]) {
1203:                        data.add(obj);
1204:                    }
1205:                }
1206:                Window w = (Window) new CSMARTMetricsFrame(NamedFrame.METRICS,
1207:                        names, data);
1208:                myWindows.add(w);
1209:            }
1210:
1211:            //   private void makeTopologyGraph() {
1212:            //     Window w =
1213:            //       new TopologyFrame("Topology", "localhost", 8800, "NCA");
1214:            //     myWindows.add(w);
1215:            //   }
1216:
1217:            /**
1218:             * Start up the CSMART Society Monitor stand-alone. <br>
1219:             * If <code>org.cougaar.useBootstrapper</code> is set false, use CLASSPATH to find classes as normal.<br>
1220:             * Otherwise, use the Cougaar Bootstrapper to search the Classpath + CIP/lib, /plugins, /sys, etc.
1221:             **/
1222:            public static void main(String[] args) {
1223:                // Note that because of the static ref to UIProperties, xerces must be in the CLASSPATH proper
1224:                // and you can't rely on the Bootstrapper
1225:                if ("true".equals(System.getProperty(
1226:                        "org.cougaar.useBootstrapper", "true"))) {
1227:                    //System.err.println("Using Bootstrapper");
1228:                    Bootstrapper.launch(CSMARTUL.class.getName(), args);
1229:                } else {
1230:                    launch(args);
1231:                }
1232:            }
1233:
1234:            public static void launch(String[] args) {
1235:                new CSMARTUL(null, null);
1236:            }
1237:
1238:            private void readObject(ObjectInputStream ois) throws IOException,
1239:                    ClassNotFoundException {
1240:                ois.defaultReadObject();
1241:                createLogger();
1242:            }
1243:
1244:        }
w__w_w__.j_a__va2___s__.c_o__m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.