Source Code Cross Referenced for GLSClient.java in  » Science » Cougaar12_4 » org » cougaar » mlm » ui » glsinit » 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.mlm.ui.glsinit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * <copyright>
003:         *  
004:         *  Copyright 1997-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:
027:        package org.cougaar.mlm.ui.glsinit;
028:
029:        import java.awt.FlowLayout;
030:        import java.awt.GridBagConstraints;
031:        import java.awt.GridBagLayout;
032:        import java.awt.Insets;
033:        import java.awt.LayoutManager;
034:        import java.awt.event.ActionEvent;
035:        import java.awt.event.ActionListener;
036:        import java.io.BufferedReader;
037:        import java.io.InputStreamReader;
038:        import java.net.HttpURLConnection;
039:        import java.net.URL;
040:        import java.net.URLConnection;
041:
042:        import javax.swing.BorderFactory;
043:        import javax.swing.BoxLayout;
044:        import javax.swing.JButton;
045:        import javax.swing.JComponent;
046:        import javax.swing.JFrame;
047:        import javax.swing.JLabel;
048:        import javax.swing.JOptionPane;
049:        import javax.swing.JPanel;
050:        import javax.swing.JRootPane;
051:        import javax.swing.JTextField;
052:
053:        import org.cougaar.mlm.ui.SwingWorker;
054:
055:        /**
056:         * UI for invoking the GLSInitServlet to publish the OPLAN and send GLS Root.
057:         * This may be run stand-alone, or from the CSMART console.
058:         * @property org.cougaar.ui.userAuthClass used to specify the class to provide 
059:         *             User Authentication support. Defaults to 
060:         *             <code>org.cougaar.core.security.userauth.UserAuthenticatorImpl</code>, 
061:         *             not delivered with Cougaar.
062:         **/
063:        public class GLSClient extends JPanel {
064:            String agentURL = null; // of the form http://host:port/$agent
065:            GLSClient glsClient; // for reference by inner classes
066:
067:            /** optional connection fields **/
068:            private JTextField protocolField = new JTextField(10);
069:            private JTextField hostField = new JTextField(10);
070:            private JTextField portField = new JTextField(10);
071:            private JTextField agentField = new JTextField(10);
072:
073:            /** for feedback to user on whether root GLS was successful **/
074:            private JLabel initLabel = new JLabel("Next Stage Description");
075:            private JLabel oplanPubLabel = new JLabel("No Oplan Info retrieved");
076:
077:            /** A button to push to kick things off **/
078:            private JButton initButton = new JButton("Send Next Stage");
079:            private JButton oplanButton = new JButton("Get Oplan Info");
080:            //   private JButton rescindButton = new JButton("Rescind GLS root");
081:            private JButton connectButton = new JButton("Connect");
082:
083:            /** A label for displaying the given Oplan **/
084:            private JLabel oplanLabel = new JLabel();
085:
086:            private String myOplanId;
087:            //private String myStageName;
088:
089:            /** A label for displaying the current Stage **/
090:            private JLabel currStageLabel = new JLabel(
091:                    "Current Stage Description");
092:            private String currentStage;
093:
094:            /** A text field for setting the CDay **/
095:            private JTextField cDayField = new JTextField(10);
096:
097:            //    /** number of GLS tasks published **/
098:            //    private int numGLS = 0;
099:
100:            private ButtonListener buttonListener = new ButtonListener();
101:
102:            /** servlets we send to; must match definitions in servlet classes **/
103:            private static final String GLS_INIT_SERVLET = "glsinit";
104:            private static final String GLS_REPLY_SERVLET = "glsreply";
105:            /** commands and parameters sent to those servlets **/
106:            private static final String CONNECT_COMMAND = "connect";
107:            private static final String INIT_COMMAND = "publishgls";
108:            //   private static final String RESCIND_COMMAND = "rescindgls";
109:            private static final String OPLAN_COMMAND = "getopinfo";
110:            private static final String OPLAN_PARAM_NAME = "&oplanID=";
111:            private static final String CDATE_PARAM_NAME = "&c0_date=";
112:
113:            private boolean stopping = false;
114:
115:            /**
116:             * A GUI panel that can be enclosed in a frame or internal frame.
117:             * GUI that contacts servlets in society to publish oplan
118:             * and send and rescind GLS tasks.
119:             * Specify default host, port and agent; 
120:             * these are displayed in text fields, so the user can edit them.
121:             * This constructs a url of the form: http://host:port/$agent
122:             * @param host host name
123:             * @param port port number as a string
124:             * @param agent agentname
125:             */
126:
127:            public GLSClient(String host, String port, String agent) {
128:                //      super(null);
129:                //      hostField.setText(host);
130:                //      portField.setText(port);
131:                //      agentField.setText(agent);
132:                //      init(true);
133:                this ("http", host, port, agent);
134:            }
135:
136:            /**
137:             * A GUI panel that can be enclosed in a frame or internal frame.
138:             * GUI that contacts servlets in society to publish oplan
139:             * and send and rescind GLS tasks.
140:             * Specify default host, port and agent; 
141:             * these are displayed in text fields, so the user can edit them.
142:             * This constructs a url of the form: protocol://host:port/$agent
143:             * @param protocol http or https
144:             * @param host host name
145:             * @param port port number as a string
146:             * @param agent agentname
147:             */
148:            public GLSClient(String protocol, String host, String port,
149:                    String agent) {
150:                super (null);
151:                protocolField.setText(protocol);
152:                hostField.setText(host);
153:                portField.setText(port);
154:                agentField.setText(agent);
155:                cDayField.setEditable(false);
156:                init(true);
157:            }
158:
159:            /**
160:             * A GUI panel that can be enclosed in a frame or internal frame.
161:             * GUI that contacts servlets in society to publish oplan
162:             * and send and rescind GLS tasks.
163:             * Specify an agentURL, which is not displayed, and is not editable.
164:             * @param agentURL agent url of the form protocol://host:port/$agent
165:             */
166:            public GLSClient(String agentURL) {
167:                super (null);
168:                this .agentURL = agentURL;
169:                init(false);
170:            }
171:
172:            private void init(boolean displayConnectFields) {
173:                glsClient = this ;
174:
175:                // Could only do this secure init in certain circumstances?
176:                // move to getURLString()?
177:                doSecureUserAuthInit();
178:
179:                setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
180:                if (displayConnectFields)
181:                    createConnectionPanel();
182:                else
183:                    createSimpleConnectionPanel();
184:                createOplanPanel();
185:                createStageDescPanel();
186:                createInitPanel();
187:            }
188:
189:            // Invoke the NAI security code, if available
190:            private void doSecureUserAuthInit() {
191:                String securityUIClass = System
192:                        .getProperty("org.cougaar.ui.userAuthClass");
193:
194:                if (securityUIClass == null) {
195:                    securityUIClass = "org.cougaar.core.security.userauth.UserAuthenticatorImpl";
196:                }
197:
198:                Class cls = null;
199:                try {
200:                    cls = Class.forName(securityUIClass);
201:                } catch (ClassNotFoundException e) {
202:                    System.err.println("Not using secure User Authentication: "
203:                            + securityUIClass);
204:                } catch (ExceptionInInitializerError e) {
205:                    System.err
206:                            .println("Unable to use secure User Authentication: "
207:                                    + securityUIClass + ". ");
208:                    e.printStackTrace();
209:                } catch (LinkageError e) {
210:                    System.err.println("Not using secure User Authentication: "
211:                            + securityUIClass);
212:                }
213:
214:                if (cls != null) {
215:                    try {
216:                        cls.newInstance();
217:                    } catch (Exception e) {
218:                        System.err
219:                                .println("Error using secure User Authentication ("
220:                                        + securityUIClass + ")");
221:                        e.printStackTrace();
222:                    }
223:                }
224:            }
225:
226:            /**
227:             * Set the initial default button to be the connect button.
228:             * Must be done after placing the GUI in a frame or internal frame.
229:             * Also enables the connect button.
230:             */
231:            public void setDefaultButton() {
232:                JRootPane rootPane = getRootPane();
233:                if (rootPane != null) {
234:                    rootPane.setDefaultButton(connectButton);
235:                    connectButton.setEnabled(true);
236:                }
237:            }
238:
239:            /**
240:             * Stop reading from any open input streams.
241:             */
242:
243:            public void stop() {
244:                stopping = true;
245:                disableButtons();
246:            }
247:
248:            private void disableButtons() {
249:                initButton.setEnabled(false);
250:                oplanButton.setEnabled(false);
251:                //rescindButton.setEnabled(false);
252:                connectButton.setEnabled(false);
253:            }
254:
255:            /** creates an x_axis oriented panel containing two components */
256:            private JPanel createXPanel(JComponent comp1, JComponent comp2) {
257:                JPanel panel = new JPanel((LayoutManager) null);
258:                panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
259:
260:                panel.add(comp1);
261:                if (comp2 != null)
262:                    panel.add(comp2);
263:                return panel;
264:            }
265:
266:            /** creates an y_axis oriented panel containing two components */
267:            private JPanel createYPanel(JComponent comp1, JComponent comp2) {
268:                JPanel panel = new JPanel((LayoutManager) null);
269:                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
270:
271:                panel.add(comp1);
272:                panel.add(comp2);
273:                return panel;
274:            }
275:
276:            private JPanel createConnectionPanelWorker() {
277:                JPanel connectPanel = new JPanel();
278:                connectPanel.setBorder(BorderFactory
279:                        .createTitledBorder("Connection"));
280:                connectButton.setFocusPainted(false);
281:                connectButton.addActionListener(new ConnectionButtonListener());
282:                add(connectPanel);
283:                return connectPanel;
284:            }
285:
286:            /** creates a connection panel with just the connect button **/
287:            private void createSimpleConnectionPanel() {
288:                JPanel connectPanel = createConnectionPanelWorker();
289:                connectPanel.add(connectButton);
290:            }
291:
292:            /** creates connection panel with fields for editing host, port and agent **/
293:            private void createConnectionPanel() {
294:                JPanel connectPanel = createConnectionPanelWorker();
295:                connectPanel.setLayout(new GridBagLayout());
296:                JPanel buttons = new JPanel(new GridBagLayout());
297:                int x = 0;
298:                int y = 0;
299:                buttons.add(new JLabel("Protocol"), new GridBagConstraints(x++,
300:                        y, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
301:                        GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
302:                buttons.add(protocolField, new GridBagConstraints(x, y++, 1, 1,
303:                        1.0, 0.0, GridBagConstraints.CENTER,
304:                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
305:                        0, 0));
306:                x = 0;
307:                buttons.add(new JLabel("Host"), new GridBagConstraints(x++, y,
308:                        1, 1, 0.0, 0.0, GridBagConstraints.WEST,
309:                        GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
310:                buttons.add(hostField, new GridBagConstraints(x, y++, 1, 1,
311:                        1.0, 0.0, GridBagConstraints.CENTER,
312:                        GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5),
313:                        0, 0));
314:                x = 0;
315:                buttons.add(new JLabel("Port"), new GridBagConstraints(x++, y,
316:                        1, 1, 0.0, 0.0, GridBagConstraints.WEST,
317:                        GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
318:                buttons.add(portField, new GridBagConstraints(x, y++, 1, 1,
319:                        1.0, 0.0, GridBagConstraints.CENTER,
320:                        GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5),
321:                        0, 0));
322:                x = 0;
323:                buttons.add(new JLabel("Agent"), new GridBagConstraints(x++, y,
324:                        1, 1, 0.0, 0.0, GridBagConstraints.WEST,
325:                        GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
326:                buttons.add(agentField, new GridBagConstraints(x, y++, 1, 1,
327:                        1.0, 0.0, GridBagConstraints.CENTER,
328:                        GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5),
329:                        0, 0));
330:                connectPanel.add(createYPanel(buttons, connectButton));
331:            }
332:
333:            private void createOplanPanel() {
334:                oplanButton.setFocusPainted(false);
335:                oplanButton.addActionListener(buttonListener);
336:                oplanButton.setActionCommand(OPLAN_COMMAND);
337:
338:                // turn this off until connection is made (even though it'll work)
339:                oplanButton.setEnabled(false);
340:
341:                JPanel opButtonPanel = new JPanel(new GridBagLayout());
342:                int x = 0;
343:                int y = 0;
344:                opButtonPanel.add(oplanPubLabel, new GridBagConstraints(x, y++,
345:                        1, 1, 0.0, 0.0, GridBagConstraints.WEST,
346:                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
347:                        0, 0));
348:                opButtonPanel.add(oplanButton, new GridBagConstraints(x, y++,
349:                        1, 1, 0.0, 0.0, GridBagConstraints.WEST,
350:                        GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5),
351:                        0, 0));
352:
353:                JPanel infoComboPanel = new JPanel(new GridBagLayout());
354:                x = 0;
355:                y = 0;
356:                infoComboPanel.add(new JLabel("Oplan: "),
357:                        new GridBagConstraints(x++, y, 1, 1, 0.0, 0.0,
358:                                GridBagConstraints.WEST,
359:                                GridBagConstraints.NONE,
360:                                new Insets(5, 5, 5, 5), 0, 0));
361:                infoComboPanel.add(oplanLabel, new GridBagConstraints(x, y++,
362:                        1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
363:                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
364:                        0, 0));
365:
366:                x = 0;
367:                infoComboPanel.add(new JLabel("Set C0 Day"),
368:                        new GridBagConstraints(x++, y, 1, 1, 0.0, 0.0,
369:                                GridBagConstraints.WEST,
370:                                GridBagConstraints.NONE,
371:                                new Insets(5, 5, 5, 5), 0, 0));
372:
373:                infoComboPanel.add(cDayField, new GridBagConstraints(x, y++, 1,
374:                        1, 1.0, 0.0, GridBagConstraints.CENTER,
375:                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
376:                        0, 0));
377:
378:                JPanel oplanInfoPanel = createXPanel(opButtonPanel,
379:                        infoComboPanel);
380:                oplanInfoPanel.setBorder(BorderFactory
381:                        .createTitledBorder("Oplan"));
382:                add(oplanInfoPanel);
383:            }
384:
385:            private void createStageDescPanel() {
386:                JPanel stagePanel = new JPanel(new FlowLayout());
387:                stagePanel.setBorder(BorderFactory
388:                        .createTitledBorder("Current Stage"));
389:                stagePanel.add(currStageLabel);
390:
391:                add(stagePanel);
392:            }
393:
394:            private void createInitPanel() {
395:                initButton.setFocusPainted(false);
396:                initButton.setEnabled(false); // Leave this disabled until we have oplans
397:                //     rescindButton.setFocusPainted(false);
398:                //     rescindButton.setEnabled(false); // Leave this disabled until we have oplans
399:                initButton.addActionListener(buttonListener);
400:                initButton.setActionCommand(INIT_COMMAND);
401:                //     rescindButton.addActionListener(buttonListener);
402:                //     rescindButton.setActionCommand(RESCIND_COMMAND);
403:
404:                JPanel glsButtons = new JPanel(new GridBagLayout());
405:                int x = 0;
406:                int y = 0;
407:                glsButtons.add(initLabel, new GridBagConstraints(x, y++, 1, 1,
408:                        0.0, 0.0, GridBagConstraints.WEST,
409:                        GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
410:                        0, 0));
411:                glsButtons.add(initButton, new GridBagConstraints(x, y++, 1, 1,
412:                        0.0, 0.0, GridBagConstraints.WEST,
413:                        GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5),
414:                        0, 0));
415:
416:                //     JPanel rescindPanel = new JPanel(new GridBagLayout());
417:                //     x = 0;
418:                //     y = 0;
419:                //     rescindPanel.add(rescindButton,
420:                //                    new GridBagConstraints(x, y, 1, 1, 0.0, 0.0,
421:                //                                           GridBagConstraints.WEST,
422:                //                                           GridBagConstraints.HORIZONTAL,
423:                //                                           new Insets(0, 5, 5, 5),
424:                //                                           0, 0));
425:
426:                //     JPanel glsPanel = createXPanel(glsButtons, rescindPanel);
427:                JPanel glsPanel = createXPanel(glsButtons, null);
428:                glsPanel.setBorder(BorderFactory
429:                        .createTitledBorder("Next Stage"));
430:                add(glsPanel);
431:            }
432:
433:            /**
434:             * Creates url of the form:
435:             * protocol://hostname:port/$agentname/servlet/?command=commandname&oplanID=oplanid&cDate=cdate
436:             */
437:            private String getURLString(String servlet, String command,
438:                    String oplanId, String cDate) {
439:                StringBuffer urlString = new StringBuffer();
440:                if (agentURL != null)
441:                    urlString.append(agentURL);
442:                else {
443:                    urlString.append(protocolField.getText());
444:                    urlString.append("://");
445:                    urlString.append(hostField.getText());
446:                    urlString.append(':');
447:                    urlString.append(portField.getText());
448:                    urlString.append("/$");
449:                    urlString.append(agentField.getText());
450:                }
451:                urlString.append('/');
452:                urlString.append(servlet);
453:                urlString.append("?command=");
454:                urlString.append(command);
455:                if (oplanId != null) {
456:                    urlString.append(OPLAN_PARAM_NAME);
457:                    urlString.append(oplanId);
458:                }
459:                if (cDate != null) {
460:                    urlString.append(CDATE_PARAM_NAME);
461:                    urlString.append(cDate);
462:                }
463:                return urlString.toString();
464:            }
465:
466:            /**
467:             * Invoked when the user selects buttons:
468:             * Publish Oplan, Send GLS Root, Rescind GLS Root
469:             * Sends appropriate command to agent.
470:             */
471:            private class ButtonListener implements  ActionListener {
472:                public void actionPerformed(ActionEvent ae) {
473:                    String command = ae.getActionCommand();
474:                    String c0Date = null;
475:                    if (command.equals(INIT_COMMAND)) {
476:                        initButton.setEnabled(false);
477:                        initLabel.setText("");
478:                        currStageLabel.setText(currentStage);
479:                        c0Date = (String) cDayField.getText();
480:                    }
481:                    //       if (command.equals(INIT_COMMAND) || command.equals(RESCIND_COMMAND)) {
482:                    //         c0Date = (String)cDayField.getText();
483:                    //       }
484:                    String urlString = getURLString(GLS_INIT_SERVLET, command,
485:                            myOplanId, c0Date);
486:                    URL url;
487:                    URLConnection urlConnection;
488:                    try {
489:                        url = new URL(urlString.toString());
490:                        if (url == null)
491:                            return;
492:                        urlConnection = url.openConnection();
493:                        if (urlConnection == null)
494:                            return;
495:                        BufferedReader in = new BufferedReader(
496:                                new InputStreamReader(urlConnection
497:                                        .getInputStream()));
498:                        String inputLine;
499:                        while ((inputLine = in.readLine()) != null && !stopping) {
500:                            // read and discard any feedback
501:                        }
502:                        in.close();
503:                        if (stopping)
504:                            return;
505:                    } catch (java.net.MalformedURLException me) {
506:                        JOptionPane.showMessageDialog(glsClient,
507:                                "Invalid Host, Port, Agent or Servlet.",
508:                                "Bad URL", JOptionPane.ERROR_MESSAGE);
509:                        me.printStackTrace();
510:                        return;
511:                    } catch (java.io.IOException ioe) {
512:                        if (stopping)
513:                            return;
514:                        JOptionPane
515:                                .showMessageDialog(
516:                                        glsClient,
517:                                        "Could not connect to Host, Port, or Agent.\nThe Servlet may not be running there.",
518:                                        "No Servlet", JOptionPane.ERROR_MESSAGE);
519:                        ioe.printStackTrace();
520:                    }
521:                }
522:            } // end of ButtonListener class
523:
524:            /**
525:             * Invoked when user selects "Connect" button.
526:             * Attempts to connect to the OSD.GOV(NCA) agent in the society running
527:             * in the current experiment.
528:             */
529:            private class ConnectionButtonListener implements  ActionListener {
530:                public void actionPerformed(ActionEvent ae) {
531:                    String urlString = getURLString(GLS_REPLY_SERVLET,
532:                            CONNECT_COMMAND, null, null);
533:                    URLConnection urlConnection = null;
534:                    try {
535:                        URL url = new URL(urlString);
536:                        urlConnection = url.openConnection();
537:                    } catch (java.net.MalformedURLException me) {
538:                        JOptionPane.showMessageDialog(glsClient,
539:                                "Invalid Host, Port, Agent or Servlet.",
540:                                "Bad URL", JOptionPane.ERROR_MESSAGE);
541:                        me.printStackTrace();
542:                        return;
543:                    } catch (java.io.IOException ioe) {
544:                        JOptionPane
545:                                .showMessageDialog(
546:                                        glsClient,
547:                                        "Could not connect to Host, Port, or Agent.\nThe Servlet may not be running there.",
548:                                        "No Servlet", JOptionPane.ERROR_MESSAGE);
549:                        ioe.printStackTrace();
550:                        return;
551:                    }
552:                    // read from open connection
553:                    if (urlConnection != null)
554:                        (new LineReader(urlConnection)).start();
555:                }
556:            } // end ConnectionButtonListener
557:
558:            /**
559:             * Reads information from an open URL connection.
560:             * Note that this keeps the URL connection open and
561:             * reads whenever new information is available.
562:             */
563:            private class LineReader extends SwingWorker {
564:                private URLConnection urlConnection;
565:
566:                public LineReader(URLConnection urlConnection) {
567:                    super ();
568:                    this .urlConnection = urlConnection;
569:                }
570:
571:                public Object construct() {
572:                    connectButton.setEnabled(false);
573:                    oplanButton.setEnabled(true);
574:                    getRootPane().setDefaultButton(oplanButton);
575:                    try {
576:                        BufferedReader in = new BufferedReader(
577:                                new InputStreamReader(urlConnection
578:                                        .getInputStream()));
579:
580:                        // check for errors and discard any associated text
581:                        int responseCode = ((HttpURLConnection) urlConnection)
582:                                .getResponseCode();
583:                        if (responseCode != HttpURLConnection.HTTP_OK) {
584:                            String inputLine;
585:                            while ((inputLine = in.readLine()) != null
586:                                    && !stopping) {
587:                            }
588:                            in.close();
589:                            if (stopping)
590:                                return null;
591:                            throw new RuntimeException("Unable to contact: "
592:                                    + urlConnection.getURL().getProtocol()
593:                                    + "://" + urlConnection.getURL().getHost()
594:                                    + ":" + urlConnection.getURL().getPort());
595:                        }
596:
597:                        String inputLine;
598:                        while ((inputLine = in.readLine()) != null && !stopping) {
599:                            if (inputLine.indexOf("oplan") > 0) {//>=
600:                                addOplanInfo(inputLine);
601:                            }
602:                            // 	  else if (inputLine.indexOf("GLS") > 0)//>=
603:                            //             updateGLSTasks(inputLine);
604:                        }
605:                        in.close();
606:                        if (stopping)
607:                            return null;
608:                    } catch (java.io.IOException e) {
609:                        if (stopping)
610:                            return null;
611:                        connectButton.setEnabled(true);
612:                        JOptionPane.showMessageDialog(glsClient,
613:                                "Could not read from servlet at: "
614:                                        + urlConnection.getURL().getProtocol()
615:                                        + "://"
616:                                        + urlConnection.getURL().getHost()
617:                                        + ":"
618:                                        + urlConnection.getURL().getPort(),
619:                                "Bad Connection", JOptionPane.ERROR_MESSAGE);
620:                        oplanButton.setEnabled(false);
621:                        getRootPane().setDefaultButton(connectButton);
622:                        return null;
623:                    } catch (RuntimeException re) {
624:                        if (stopping)
625:                            return null;
626:                        connectButton.setEnabled(true);
627:                        JOptionPane
628:                                .showMessageDialog(
629:                                        glsClient,
630:                                        "Could not contact servlet at: "
631:                                                + urlConnection.getURL()
632:                                                        .getProtocol()
633:                                                + "://"
634:                                                + urlConnection.getURL()
635:                                                        .getHost()
636:                                                + ":"
637:                                                + urlConnection.getURL()
638:                                                        .getPort()
639:                                                + " ; servlet may not be initialized; retry later.",
640:                                        "Initialization Not Complete",
641:                                        JOptionPane.ERROR_MESSAGE);
642:                        oplanButton.setEnabled(false);
643:                        getRootPane().setDefaultButton(connectButton);
644:                        return null;
645:                    } catch (Exception e) {
646:                        if (stopping)
647:                            return null;
648:                        e.printStackTrace();
649:                        return null;
650:                    }
651:
652:                    return null;
653:                }
654:
655:                /**
656:                 * Add OPlan c0 date read from the agent to the display,
657:                 * and update the GUI for the oplan name.  Also read 
658:                 * next stage of oplan to send.
659:                 */
660:                private void addOplanInfo(String s) {
661:                    int nameIndex = s.indexOf("name=") + 5;
662:                    int idIndex = s.indexOf("id=");
663:                    String name = s.substring(nameIndex, idIndex).trim();
664:                    oplanLabel.setText(name);
665:
666:                    myOplanId = s.substring(idIndex + 3, idIndex + 8);
667:
668:                    int dateIndex = s.indexOf("c0_date=") + 8;
669:                    int endOfDateInd = s.indexOf("nextStage") - 1;
670:                    String c0Date = s.substring(dateIndex, endOfDateInd);
671:                    cDayField.setEditable(true);
672:                    cDayField.setText(c0Date);
673:
674:                    oplanButton.setEnabled(false);
675:                    oplanPubLabel.setText("Oplan Info Retrieved");
676:
677:                    int stageIndex = s.indexOf("nextStage=") + 10;
678:                    int endOfStageIndex = s.indexOf("stageDesc") - 1;
679:                    String stageName = s.substring(stageIndex, endOfStageIndex);
680:                    if (stageName.equals("All Stages Sent")) {
681:                        initButton.setText(stageName);
682:                        initLabel.setText("");
683:                    } else {
684:                        int stageDescIndex = s.indexOf("stageDesc=") + 10;
685:                        String stageDesc = s.substring(stageDescIndex, s
686:                                .length() - 1);
687:                        initButton.setText("Send " + stageName);
688:                        initButton.setEnabled(true);
689:                        String nameAndDesc = stageName + ": " + stageDesc;
690:                        initLabel.setText(nameAndDesc);
691:                        currentStage = nameAndDesc;
692:                    }
693:                    getRootPane().setDefaultButton(initButton);
694:                }
695:
696:                //     /**
697:                //      * Update information about GLS tasks from information
698:                //      * read from the agent and update the GUI.
699:                //      */
700:                //     private void updateGLSTasks(String s) {
701:                //       int numGLS = Integer.parseInt(s.substring(s.indexOf("GLS")+4, 
702:                //                                                 s.length()-1));
703:                //       initLabel.setText(numGLS + " GLS Tasks published");
704:                //Disable rescind button temporarily until its working again.
705:                //       if (numGLS > 0) 
706:                //         rescindButton.setEnabled(true);
707:                //       else
708:                //         rescindButton.setEnabled(false);
709:                //     }
710:
711:            } // end LineReader class
712:
713:            /**
714:             * Create a GUI panel; enclose it in a frame, and display the frame.
715:             * Creates the GUI with a default host, port, and agent
716:             * which the user can edit.
717:             */
718:            public static void main(String args[]) {
719:                GLSClient gui = new GLSClient("localhost", "8800", "OSD.GOV");
720:                JFrame frame = new JFrame("OPlan Control");
721:                frame.getContentPane().add(gui);
722:                gui.setDefaultButton();
723:                String EXIT_ON_CLOSE_PROP = "org.cougaar.mlm.plugin.organization.GLSClient.exitOnClose";
724:                boolean exitOnClose = System.getProperty(EXIT_ON_CLOSE_PROP,
725:                        "true").equals("true");
726:                if (exitOnClose)
727:                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
728:                frame.setLocation(0, 0);
729:                frame.setContentPane(gui);
730:                frame.pack();
731:                frame.setVisible(true);
732:            }
733:
734:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.