Source Code Cross Referenced for MinConsole.java in  » Science » Cougaar12_4 » org » cougaar » tools » server » examples » 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.server.examples 
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.tools.server.examples;
028:
029:        import java.util.*;
030:        import java.io.*;
031:
032:        import org.cougaar.tools.server.*;
033:        import org.cougaar.tools.server.system.ProcessStatus;
034:
035:        /**
036:         * Minimal "console" for using to test the server.
037:         * <p>
038:         * Can optionally pass these command-line arguments to
039:         * <tt>main(String[])</tt>:<ul>
040:         *   <li>killMillis=LONG   (default=20000)</li>
041:         *   <li>hostName=STRING   (default=localhost)</li>
042:         *   <li>controlPort=INT   (default=8484)</li>
043:         *   <li>nodeName=STRING   (default=MiniNode)</li>
044:         *   <li>namingAddr=STRING (default=localhost:8888)</li>
045:         * </ul>
046:         */
047:        public class MinConsole {
048:
049:            public static void main(String[] args) throws Exception {
050:
051:                long killMillis = 20 * 1000;
052:
053:                String hostName = "localhost";
054:                int controlPort = 8484;
055:                String nodeName = "MiniNode";
056:                String namingAddr = hostName + ":8888";
057:
058:                for (int i = 0; i < args.length; i++) {
059:                    String ai = args[i];
060:                    if (ai.startsWith("killMillis=")) {
061:                        String tmp = ai.substring("killMillis=".length());
062:                        killMillis = Long.parseLong(tmp);
063:                    } else if (ai.startsWith("hostName=")) {
064:                        hostName = ai.substring("hostName=".length());
065:                    } else if (ai.startsWith("controlPort=")) {
066:                        String tmp = ai.substring("controlPort=".length());
067:                        controlPort = Integer.parseInt(tmp);
068:                    } else if (ai.startsWith("nodeName=")) {
069:                        nodeName = ai.substring("nodeName=".length());
070:                    } else if (ai.startsWith("namingAddr=")) {
071:                        namingAddr = ai.substring("namingAddr=".length());
072:                    } else {
073:                        throw new IllegalArgumentException("Illegal arg[" + i
074:                                + "]: " + ai);
075:                    }
076:                }
077:
078:                String procId = nodeName;
079:
080:                System.out.println(MinConsole.class.getName() + " {"
081:                        + "\n  killMillis: " + killMillis + "\n  hostName: "
082:                        + hostName + "\n  controlPort: " + controlPort
083:                        + "\n  nodeName: " + nodeName + "\n  namingAddr: "
084:                        + namingAddr + "\n  procId: " + procId + "\n}");
085:
086:                // create a remote-host-registry instance
087:                RemoteHostRegistry hostReg = RemoteHostRegistry.getInstance();
088:
089:                // contact the host
090:                RemoteHost rhost = hostReg.lookupRemoteHost(hostName,
091:                        controlPort, true);
092:
093:                // just to test, let's try a "ping()"
094:                ping(rhost);
095:
096:                // list the running processes on the server
097:                listProcs(rhost);
098:
099:                // define the process
100:                ProcessDescription desc = createProcessDescription(procId,
101:                        nodeName, namingAddr);
102:
103:                // launch process
104:                RemoteProcess proc = createProcess(rhost, desc);
105:
106:                // wait a couple seconds
107:                Thread.sleep(2 * 1000);
108:
109:                // list the processes on the server
110:                listProcs(rhost);
111:
112:                // let it run for a while
113:                System.out
114:                        .println("Created process \""
115:                                + procId
116:                                + "\", run "
117:                                + ((killMillis <= 0) ? "forever" : "for "
118:                                        + killMillis + "milliseconds")
119:                                + "\n"
120:                                + "***************************************************");
121:                // list running processes ("ps")
122:                listProcessStatus(proc, false);
123:
124:                if (killMillis > 0) {
125:                    Thread.sleep(killMillis);
126:
127:                    // kill the process
128:                    System.out
129:                            .println("****************************************************\n"
130:                                    + "Kill process \"" + procId + "\"");
131:                    rhost.killRemoteProcess(desc.getName());
132:
133:                    // list the processes on the server
134:                    listProcs(rhost);
135:
136:                    Thread.sleep(2 * 1000);
137:
138:                    // re-launch process
139:                    System.out
140:                            .println("****************************************************\n"
141:                                    + "Re-launch process \"" + procId + "\"");
142:                    RemoteProcess proc2 = createProcess(rhost, desc);
143:
144:                    System.out
145:                            .println("****************************************************\n"
146:                                    + "Run for 5 seconds");
147:                    Thread.sleep(5 * 1000);
148:
149:                    System.out.println("exit, force server-side cleanup.");
150:
151:                    // force exit -- RMI-threads would keep this running
152:                    System.exit(0);
153:                }
154:            }
155:
156:            private static void ping(RemoteHost rhost) throws Exception {
157:                long t1 = System.currentTimeMillis();
158:                long t2 = rhost.ping();
159:                long t3 = System.currentTimeMillis();
160:                System.out.println("Ping (millis): " + "\n  sent: " + t1
161:                        + "\n  ret:  " + t2 + "\n  recv: " + t3
162:                        + "\n  (recv - sent): " + (t3 - t1));
163:            }
164:
165:            private static void listProcs(RemoteHost rhost) throws Exception {
166:                // list the running processes on the server
167:                List runningProcs = rhost.listProcessDescriptions();
168:                int nRunningProcs = ((runningProcs != null) ? runningProcs
169:                        .size() : 0);
170:                System.out
171:                        .println("Running processes[" + nRunningProcs + "]: ");
172:                for (int i = 0; i < nRunningProcs; i++) {
173:                    ProcessDescription pdi = (ProcessDescription) runningProcs
174:                            .get(i);
175:                    System.out.println("  [" + i + " / " + nRunningProcs
176:                            + "]: " + pdi);
177:                }
178:            }
179:
180:            private static ProcessDescription createProcessDescription(
181:                    String procId, String nodeName, String namingAddr)
182:                    throws Exception {
183:                // build a configuration
184:                Properties javaProps = new Properties();
185:                javaProps.put("org.cougaar.node.name", nodeName);
186:                javaProps.put("org.cougaar.name.server", namingAddr);
187:                return new ProcessDescription(procId, "group-for-" + procId,
188:                        javaProps, null);
189:            }
190:
191:            private static RemoteProcess createProcess(RemoteHost rhost,
192:                    final ProcessDescription desc) throws Exception {
193:                // create an output listener
194:                OutputListener ol = new OutputListener() {
195:                    public void handleOutputBundle(OutputBundle ob) {
196:                        // just write std-out and std-err
197:                        DualStreamBuffer dsb = ob.getDualStreamBuffer();
198:                        try {
199:                            dsb.writeTo(System.out, System.err);
200:                        } catch (IOException ioe) {
201:                            ioe.printStackTrace();
202:                        }
203:                    }
204:                };
205:
206:                // create the output filter/buffer config
207:                OutputPolicy op = new OutputPolicy(20);
208:
209:                // create a listener config
210:                RemoteListenableConfig rlc = new RemoteListenableConfig(ol, op);
211:
212:                // create the node
213:                try {
214:                    return rhost.createRemoteProcess(desc, rlc);
215:                } catch (Exception e) {
216:                    System.err.println("Unable to create process:");
217:                    e.printStackTrace();
218:                    throw e;
219:                }
220:            }
221:
222:            private static void listProcessStatus(RemoteProcess proc,
223:                    boolean showAll) throws Exception {
224:                // query
225:                ProcessStatus[] psa = proc.listProcesses(showAll);
226:                // display
227:                int n = ((psa != null) ? psa.length : 0);
228:                System.out.println(((showAll) ? "All" : "Process's")
229:                        + " ProcessStatus[" + n + "] for Process \""
230:                        + proc.getProcessDescription().getName() + "\":");
231:                for (int i = 0; i < n; i++) {
232:                    ProcessStatus pi = psa[i];
233:                    // show terse details
234:                    System.out.println("  " + pi.toString(false));
235:                }
236:            }
237:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.