KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > activation > ServerTool


1 /*
2  * @(#)ServerTool.java 1.38 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.activation;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.PrintStream JavaDoc;
13 import java.util.Vector JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.omg.CORBA.ORB JavaDoc;
18 import org.omg.CORBA.INITIALIZE JavaDoc;
19 import org.omg.CORBA.CompletionStatus JavaDoc;
20 import com.sun.corba.se.impl.orbutil.ORBConstants;
21 import com.sun.corba.se.impl.orbutil.CorbaResourceUtil;
22 import com.sun.corba.se.spi.activation.*;
23 import com.sun.corba.se.spi.activation.ServerHeldDown;
24 import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef;
25 import com.sun.corba.se.spi.activation.LocatorPackage.ServerLocation;
26 import com.sun.corba.se.spi.activation.LocatorPackage.ServerLocationPerORB;
27
28 /**
29  *
30  * @version 1.7, 97/10/19
31  * @author Anita Jindal
32  * @since JDK1.3
33  */

34 public class ServerTool
35 {
36     final static String JavaDoc helpCommand = "help";
37     final static String JavaDoc toolName = "servertool";
38     final static String JavaDoc commandArg = "-cmd";
39
40     static int getServerIdForAlias( ORB JavaDoc orb, String JavaDoc applicationName ) throws ServerNotRegistered
41     {
42     try {
43         Repository rep = RepositoryHelper.narrow(
44         orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ) ) ;
45         int serverid = rep.getServerID(applicationName);
46
47         return rep.getServerID( applicationName ) ;
48     } catch (Exception JavaDoc ex) {
49         throw (new ServerNotRegistered());
50     }
51     }
52
53     void run(String JavaDoc[] args)
54     {
55     String JavaDoc[] cmd = null;
56
57     // if command specified in the args, get it
58
for (int i=0; i < args.length; i++) {
59
60         if (args[i].equals(commandArg)) {
61         // get the command
62
int cmdLen = args.length - i - 1;
63         cmd = new String JavaDoc[cmdLen];
64         for (int j=0; j < cmdLen; j++) cmd[j] = args[++i];
65
66         break;
67         }
68     }
69
70     try {
71         // create the POA ORB
72
Properties JavaDoc props = System.getProperties() ;
73         props.put("org.omg.CORBA.ORBClass",
74         "com.sun.corba.se.impl.orb.ORBImpl" );
75         orb = (ORB JavaDoc) ORB.init(args, props);
76
77         // if command specified in the args, process it
78
if (cmd != null) executeCommand(cmd);
79         else { // process commands interactively
80

81             // create a buffered reader to read commands from standard in
82
BufferedReader JavaDoc in = new
83             BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
84
85             // print tool banner
86
System.out.println(CorbaResourceUtil.getText("servertool.banner"));
87
88             // process commands until user quits
89
while (true) {
90             cmd = readCommand(in);
91             if (cmd != null) executeCommand(cmd);
92             else printAvailableCommands();
93             }
94         }
95     } catch (Exception JavaDoc ex) {
96         System.out.println(CorbaResourceUtil.getText("servertool.usage", "servertool"));
97         System.out.println();
98         ex.printStackTrace();
99     }
100     }
101
102     public static void main(String JavaDoc[] args)
103     {
104     ServerTool tool = new ServerTool();
105     tool.run(args);
106     }
107
108     String JavaDoc[] readCommand(BufferedReader JavaDoc in)
109     {
110     System.out.print(toolName + " > ");
111
112     try {
113         int i = 0;
114         String JavaDoc cmd[] = null;
115
116         String JavaDoc cmdLine = in.readLine();
117
118         if (cmdLine != null) {
119         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(cmdLine);
120         if (st.countTokens() != 0) {
121             cmd = new String JavaDoc[st.countTokens()];
122             while (st.hasMoreTokens()) cmd[i++] = st.nextToken();
123         }
124         }
125
126         return cmd;
127     } catch (Exception JavaDoc ex) {
128         System.out.println(CorbaResourceUtil.getText("servertool.usage", "servertool"));
129         System.out.println();
130         ex.printStackTrace();
131     }
132
133     return null;
134     }
135
136     void printAvailableCommands()
137     {
138     CommandHandler handler;
139
140     // print short help
141
System.out.println(CorbaResourceUtil.getText("servertool.shorthelp"));
142
143     for (int i=0; i < handlers.size(); i++) {
144         handler = (CommandHandler) handlers.elementAt(i);
145         System.out.print("\t" + handler.getCommandName());
146         for (int j=handler.getCommandName().length();
147          j < maxNameLen; j++) System.out.print(" ");
148         System.out.print(" - ");
149         handler.printCommandHelp(System.out,
150                      CommandHandler.shortHelp);
151     }
152
153     System.out.println();
154     }
155
156     void executeCommand(String JavaDoc[] cmd)
157     {
158     boolean result;
159     CommandHandler handler;
160
161     // handle the help command
162
if (cmd[0].equals(helpCommand)) {
163         if (cmd.length == 1) printAvailableCommands();
164         else {
165         // print long help for a specific command
166
for (int i=0; i < handlers.size(); i++) {
167                 handler = (CommandHandler) handlers.elementAt(i);
168             if (handler.getCommandName().equals(cmd[1])) {
169             handler.printCommandHelp(System.out,
170                          CommandHandler.longHelp);
171             }
172         }
173         }
174
175         return;
176     }
177
178     // determine the subcommand and execute it
179
for (int i=0; i < handlers.size(); i++) {
180         handler = (CommandHandler) handlers.elementAt(i);
181         if (handler.getCommandName().equals(cmd[0])) {
182         String JavaDoc[] cmdArgs = new String JavaDoc[cmd.length - 1];
183
184         // construct args to the command
185
for (int j=0; j < cmdArgs.length; j++)
186             cmdArgs[j] = cmd[j+1];
187
188         // execute the command
189
try {
190             System.out.println();
191
192             result = handler.processCommand(cmdArgs, orb, System.out);
193
194             if (result == CommandHandler.parseError) {
195             handler.printCommandHelp(System.out,
196                          CommandHandler.longHelp);
197             }
198
199             System.out.println();
200
201         } catch (Exception JavaDoc ex) {}
202
203         return;
204         }
205     }
206
207     // unknown command - print available commands
208
printAvailableCommands();
209     }
210
211     final private static boolean debug = false;
212
213     ORB JavaDoc orb = null;
214
215     static Vector JavaDoc handlers;
216     static int maxNameLen;
217
218     static {
219     handlers = new Vector JavaDoc();
220     handlers.addElement(new RegisterServer());
221     handlers.addElement(new UnRegisterServer());
222     handlers.addElement(new GetServerID());
223     handlers.addElement(new ListServers());
224     handlers.addElement(new ListAliases());
225     handlers.addElement(new ListActiveServers());
226     handlers.addElement(new LocateServer());
227     handlers.addElement(new LocateServerForORB());
228     handlers.addElement(new ListORBs());
229     handlers.addElement(new ShutdownServer());
230     handlers.addElement(new StartServer());
231     handlers.addElement(new Help());
232     handlers.addElement(new Quit());
233
234     // determine longest command name
235
maxNameLen = 0;
236     int cmdNameLen;
237     for (int i=0; i < handlers.size(); i++) {
238         CommandHandler handler = (CommandHandler) handlers.elementAt(i);
239         cmdNameLen = handler.getCommandName().length();
240         if (cmdNameLen > maxNameLen) maxNameLen = cmdNameLen;
241     }
242     }
243 }
244
245 class RegisterServer implements CommandHandler
246 {
247     public String JavaDoc getCommandName() {return "register";}
248    
249     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
250     {
251     if (helpType == longHelp) {
252         out.println(CorbaResourceUtil.getText("servertool.register"));
253     } else {
254         out.println(CorbaResourceUtil.getText("servertool.register1"));
255     }
256     }
257
258     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
259     {
260     int i=0;
261     String JavaDoc applicationName = "";
262     String JavaDoc name = "";
263     String JavaDoc classpath = "";
264     String JavaDoc args = "";
265     String JavaDoc vmargs = "";
266     int serverId = 0;
267
268     // parse register server command
269
String JavaDoc arg;
270     while (i < cmdArgs.length) {
271
272         arg = cmdArgs[i++];
273
274         if (arg.equals("-server")) {
275         if (i < cmdArgs.length) name = cmdArgs[i++];
276         else return parseError;
277         } else if (arg.equals("-applicationName")) {
278         if (i < cmdArgs.length) applicationName = cmdArgs[i++];
279         else return parseError;
280         } else if (arg.equals("-classpath")) {
281         if (i < cmdArgs.length) classpath = cmdArgs[i++];
282         else return parseError;
283         } else if (arg.equals("-args")) {
284         while ((i < cmdArgs.length) && !cmdArgs[i].equals("-vmargs")){
285             args = args.equals("") ? cmdArgs[i] :
286             args + " " + cmdArgs[i];
287             i++;
288         }
289         if (args.equals("")) return parseError;
290         } else if (arg.equals("-vmargs")) {
291         while ((i < cmdArgs.length) && !cmdArgs[i].equals("-args")){
292             vmargs = vmargs.equals("") ? cmdArgs[i] :
293             vmargs + " " + cmdArgs[i];
294             i++;
295         }
296         if (vmargs.equals("")) return parseError;
297         } else return parseError;
298     }
299
300     // minimally the server class name has to be specified
301
if (name.equals("")) return parseError;
302
303     // register server and activate it
304
try {
305         // register the server with the repository
306
Repository repository = RepositoryHelper.narrow(
307         orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ));
308
309         ServerDef server = new ServerDef(applicationName, name, classpath, args, vmargs);
310         serverId = repository.registerServer(server);
311
312         // activate the server
313
Activator activator = ActivatorHelper.narrow(
314         orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
315         activator.activate(serverId);
316         activator.install(serverId);
317
318         // print success message
319
out.println(CorbaResourceUtil.getText("servertool.register2", serverId));
320     } catch (ServerNotRegistered ex) {
321     } catch (ServerAlreadyActive ex) {
322     } catch (ServerHeldDown ex) {
323         out.println(CorbaResourceUtil.getText("servertool.register3", serverId));
324     } catch (ServerAlreadyRegistered ex) {
325         out.println(CorbaResourceUtil.getText("servertool.register4", serverId));
326     } catch (BadServerDefinition ex) {
327         out.println(CorbaResourceUtil.getText("servertool.baddef", ex.reason));
328     } catch (Exception JavaDoc ex) {
329         ex.printStackTrace();
330     }
331
332     return commandDone;
333     }
334 }
335
336 class UnRegisterServer implements CommandHandler
337 {
338     public String JavaDoc getCommandName() {return "unregister";}
339    
340     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
341     {
342     if (helpType == longHelp) {
343         out.println(CorbaResourceUtil.getText("servertool.unregister"));
344     } else {
345         out.println(CorbaResourceUtil.getText("servertool.unregister1"));
346     }
347 }
348
349     final static int illegalServerId = -1;
350
351     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
352     {
353     int serverId = illegalServerId;
354
355     try {
356         if (cmdArgs.length == 2) {
357             if (cmdArgs[0].equals("-serverid"))
358             serverId = (Integer.valueOf(cmdArgs[1])).intValue();
359             else if (cmdArgs[0].equals("-applicationName"))
360             serverId = ServerTool.getServerIdForAlias( orb, cmdArgs[1] ) ;
361         }
362
363         // the server id has to be specified
364
if (serverId == illegalServerId)
365             return parseError;
366
367         // deactivate server, hold it down and and unregister it
368
// deactivate the server
369
try {
370             Activator activator = ActivatorHelper.narrow(
371              orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
372             activator.uninstall(serverId);
373         } catch (ServerHeldDown ex) {}
374
375         // unregister the server from the repository
376
Repository repository = RepositoryHelper.narrow(
377         orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ));
378         repository.unregisterServer(serverId);
379
380         // print success message
381
out.println(CorbaResourceUtil.getText("servertool.unregister2"));
382         } catch (ServerNotRegistered ex) {
383         out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
384         } catch (Exception JavaDoc ex) {
385         ex.printStackTrace();
386     }
387
388         return commandDone;
389     }
390 }
391
392 class LocateServer implements CommandHandler
393 {
394     public String JavaDoc getCommandName() {return "locate";}
395    
396     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
397     {
398     if (helpType == longHelp) {
399         out.println(CorbaResourceUtil.getText("servertool.locate"));
400     } else {
401         out.println(CorbaResourceUtil.getText("servertool.locate1"));
402     }
403     }
404
405     final static int illegalServerId = -1;
406
407     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
408     {
409     int serverId = illegalServerId;
410
411     String JavaDoc endPointType = IIOP_CLEAR_TEXT.value;
412     try {
413
414             // parse command
415
String JavaDoc arg;
416         int i = 0;
417             while (i < cmdArgs.length) {
418
419                 arg = cmdArgs[i++];
420
421                 if (arg.equals("-serverid")) {
422                     if (i < cmdArgs.length)
423                 serverId = (Integer.valueOf(cmdArgs[i++])).intValue();
424                     else
425                         return parseError;
426                 } else if (arg.equals("-applicationName")) {
427                     if (i < cmdArgs.length)
428                 serverId = ServerTool.getServerIdForAlias( orb, cmdArgs[i++] ) ;
429                     else
430                         return parseError;
431             } else if (arg.equals("-endpointType")) {
432             if (i < cmdArgs.length)
433                     endPointType = cmdArgs[i++];
434             }
435             }
436
437         // the server id has to be specified
438
if (serverId == illegalServerId)
439             return parseError;
440
441         // locate the server
442
// deactivate the server
443
Locator locator = LocatorHelper.narrow(
444             orb.resolve_initial_references( ORBConstants.SERVER_LOCATOR_NAME ));
445          
446         ServerLocation location = locator.locateServer(serverId, endPointType);
447
448         // print success message
449
out.println(CorbaResourceUtil.getText("servertool.locate2", location.hostname));
450         int numEntries = location.ports.length;
451         for (i = 0; i < numEntries; i++) {
452             ORBPortInfo orbPort = location.ports[i];
453             out.println("\t\t"+ orbPort.port + "\t\t" + endPointType + "\t\t" + orbPort.orbId );
454         }
455     } catch (NoSuchEndPoint ex) {
456     } catch (ServerHeldDown ex) {
457         out.println(CorbaResourceUtil.getText("servertool.helddown"));
458     } catch (ServerNotRegistered ex) {
459         out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
460     } catch (Exception JavaDoc ex) {
461         ex.printStackTrace();
462     }
463
464     return commandDone;
465     }
466 }
467
468 class LocateServerForORB implements CommandHandler
469 {
470     public String JavaDoc getCommandName() {return "locateperorb";}
471    
472     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
473     {
474     if (helpType == longHelp) {
475         out.println(CorbaResourceUtil.getText("servertool.locateorb"));
476     } else {
477         out.println(CorbaResourceUtil.getText("servertool.locateorb1"));
478     }
479     }
480
481     final static int illegalServerId = -1;
482
483     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
484     {
485     int serverId = illegalServerId;
486
487     String JavaDoc orbId = "";
488     try {
489
490             // parse command
491
String JavaDoc arg;
492         int i = 0;
493             while (i < cmdArgs.length) {
494
495                 arg = cmdArgs[i++];
496
497                 if (arg.equals("-serverid")) {
498                     if (i < cmdArgs.length)
499                 serverId = (Integer.valueOf(cmdArgs[i++])).intValue();
500                     else
501                         return parseError;
502                 } else if (arg.equals("-applicationName")) {
503                     if (i < cmdArgs.length)
504                 serverId = ServerTool.getServerIdForAlias( orb, cmdArgs[i++] ) ;
505                     else
506                         return parseError;
507             } else if (arg.equals("-orbid")) {
508             if (i < cmdArgs.length)
509                     orbId = cmdArgs[i++];
510             }
511             }
512
513         // the server id has to be specified
514
if (serverId == illegalServerId)
515             return parseError;
516
517         // locate the server
518
// deactivate the server
519
Locator locator = LocatorHelper.narrow(
520         orb.resolve_initial_references( ORBConstants.SERVER_LOCATOR_NAME ));
521
522         ServerLocationPerORB location = locator.locateServerForORB(serverId,
523                         orbId);
524
525         // print success message
526
out.println(CorbaResourceUtil.getText("servertool.locateorb2", location.hostname));
527         int numEntries = location.ports.length;
528         for (i = 0; i < numEntries; i++) {
529             EndPointInfo Port = location.ports[i];
530             out.println("\t\t"+ Port.port + "\t\t" + Port.endpointType + "\t\t" + orbId );
531         }
532     } catch (InvalidORBid ex) {
533         out.println(CorbaResourceUtil.getText("servertool.nosuchorb"));
534     } catch (ServerHeldDown ex) {
535         out.println(CorbaResourceUtil.getText("servertool.helddown"));
536     } catch (ServerNotRegistered ex) {
537         out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
538     } catch (Exception JavaDoc ex) {
539         ex.printStackTrace();
540     }
541
542     return commandDone;
543     }
544 }
545
546 class GetServerID implements CommandHandler
547 {
548     public String JavaDoc getCommandName() {return "getserverid" ; }
549
550     public void printCommandHelp( PrintStream JavaDoc out, boolean helpType )
551     {
552     if (helpType == longHelp) {
553         out.println(CorbaResourceUtil.getText("servertool.getserverid"));
554     } else {
555         out.println(CorbaResourceUtil.getText("servertool.getserverid1"));
556     }
557     }
558
559     public boolean processCommand( String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out )
560     {
561     if ((cmdArgs.length == 2) && cmdArgs[0].equals( "-applicationName" )) {
562         String JavaDoc str = (String JavaDoc)cmdArgs[1] ;
563
564         try {
565         Repository repository = RepositoryHelper.narrow(
566             orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ));
567
568         try {
569             int result = repository.getServerID( str ) ;
570             out.println() ;
571                 out.println(CorbaResourceUtil.getText("servertool.getserverid2", str, Integer.toString(result)));
572             out.println() ;
573         } catch (ServerNotRegistered e) {
574                 out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
575         }
576         } catch (Exception JavaDoc ex) {
577         ex.printStackTrace() ;
578         }
579
580         return commandDone ;
581     } else
582         return parseError ;
583     }
584 }
585
586 class ListServers implements CommandHandler
587 {
588     public String JavaDoc getCommandName() {return "list";}
589    
590     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
591     {
592     if (helpType == longHelp) {
593         out.println(CorbaResourceUtil.getText("servertool.list"));
594     } else {
595         out.println(CorbaResourceUtil.getText("servertool.list1"));
596     }
597     }
598
599     final static int illegalServerId = -1;
600
601     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
602     {
603     int serverId = illegalServerId;
604     boolean listOneServer = false;
605     ServerDef serverDef;
606
607     // determine if list single server or all servers
608
listOneServer = (cmdArgs.length!=0) ;
609     if ((cmdArgs.length == 2) && cmdArgs[0].equals("-serverid"))
610         serverId = (Integer.valueOf(cmdArgs[1])).intValue();
611
612     if ((serverId == illegalServerId) && listOneServer)
613         return parseError;
614
615     // process the list server command
616
try {
617         Repository repository = RepositoryHelper.narrow(
618         orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ));
619
620         if (listOneServer) {
621
622         try {
623             serverDef = repository.getServer(serverId);
624             out.println();
625             printServerDef(serverDef, serverId, out);
626             out.println();
627         } catch (ServerNotRegistered e) {
628                 out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
629         }
630
631         } else {
632         int[] servers = repository.listRegisteredServers();
633             out.println(CorbaResourceUtil.getText("servertool.list2"));
634
635         sortServers(servers);
636         for (int i=0; i < servers.length; i++) {
637             try {
638                 serverDef = repository.getServer(servers[i]);
639             out.println("\t " + servers[i] + "\t\t" +
640                     serverDef.serverName + "\t\t"
641                     + serverDef.applicationName);
642             } catch (ServerNotRegistered e) {}
643         }
644
645         }
646     } catch (Exception JavaDoc ex) {
647         ex.printStackTrace();
648     }
649
650     return commandDone;
651     }
652
653 static void printServerDef(ServerDef serverDef, int serverId,
654                PrintStream JavaDoc out)
655 {
656     out.println(CorbaResourceUtil.getText("servertool.appname", serverDef.applicationName));
657     out.println(CorbaResourceUtil.getText("servertool.name", serverDef.serverName));
658     out.println(CorbaResourceUtil.getText("servertool.classpath", serverDef.serverClassPath));
659     out.println(CorbaResourceUtil.getText("servertool.args", serverDef.serverArgs));
660     out.println(CorbaResourceUtil.getText("servertool.vmargs", serverDef.serverVmArgs));
661     out.println(CorbaResourceUtil.getText("servertool.serverid", serverId));
662 }
663
664 /**
665  * Do a simple bubble sort to sort the server ids in ascending
666  * order.
667  */

668 static void sortServers(int[] serverIds)
669 {
670     int size = serverIds.length;
671     int lowest;
672
673     for (int i=0; i < size; i++) {
674
675     lowest = i;
676
677     for (int j=i+1; j < size; j++) {
678         if (serverIds[j] < serverIds[lowest]) lowest = j;
679     }
680
681     if (lowest != i) {
682         int temp = serverIds[i];
683         serverIds[i] = serverIds[lowest];
684         serverIds[lowest] = temp;
685     }
686     }
687 }
688 }
689
690 class ListActiveServers implements CommandHandler
691 {
692     public String JavaDoc getCommandName() {return "listactive";}
693    
694     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
695     {
696     if (helpType == longHelp) {
697         out.println(CorbaResourceUtil.getText("servertool.listactive"));
698     } else {
699         out.println(CorbaResourceUtil.getText("servertool.listactive1"));
700     }
701     }
702
703     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
704     {
705     ServerDef serverDef;
706
707     // process the list active servers command
708
try {
709         Repository repository = RepositoryHelper.narrow(
710         orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ));
711
712         Activator activator = ActivatorHelper.narrow(
713         orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
714         
715         int[] servers = activator.getActiveServers();
716
717         out.println(CorbaResourceUtil.getText("servertool.list2"));
718
719         ListServers.sortServers(servers);
720         for (int i=0; i < servers.length; i++) {
721             try {
722                 serverDef = repository.getServer(servers[i]);
723             out.println("\t " + servers[i] + "\t\t" +
724                 serverDef.serverName + "\t\t" +
725                 serverDef.applicationName);
726             } catch (ServerNotRegistered e) {}
727         }
728     } catch (Exception JavaDoc ex) {
729         ex.printStackTrace();
730     }
731
732     return commandDone;
733     }
734 }
735
736 class ListAliases implements CommandHandler
737 {
738     public String JavaDoc getCommandName() {return "listappnames";}
739    
740     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
741     {
742     if (helpType == longHelp) {
743         out.println(CorbaResourceUtil.getText("servertool.listappnames"));
744     } else {
745         out.println(CorbaResourceUtil.getText("servertool.listappnames1"));
746     }
747     }
748
749     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
750     {
751     try {
752         Repository repository = RepositoryHelper.narrow(
753         orb.resolve_initial_references( ORBConstants.SERVER_REPOSITORY_NAME ));
754
755         String JavaDoc[] applicationNames = repository.getApplicationNames();
756
757         out.println(CorbaResourceUtil.getText("servertool.listappnames2"));
758         out.println();
759         for (int i=0; i < applicationNames.length; i++)
760         out.println( "\t" + applicationNames[i] ) ;
761     } catch (Exception JavaDoc ex) {
762         ex.printStackTrace();
763     }
764
765     return commandDone;
766     }
767 }
768
769 class ShutdownServer implements CommandHandler
770 {
771     public String JavaDoc getCommandName() {return "shutdown";}
772    
773     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
774     {
775     if (helpType == longHelp) {
776         out.println(CorbaResourceUtil.getText("servertool.shutdown"));
777     } else {
778         out.println(CorbaResourceUtil.getText("servertool.shutdown1"));
779     }
780     }
781
782     final static int illegalServerId = -1;
783
784     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
785     {
786     int serverId = illegalServerId;
787
788     try {
789         // determine the server id
790
if (cmdArgs.length == 2)
791             if (cmdArgs[0].equals("-serverid"))
792             serverId = (Integer.valueOf(cmdArgs[1])).intValue();
793             else if (cmdArgs[0].equals("-applicationName"))
794             serverId = ServerTool.getServerIdForAlias( orb, cmdArgs[1] ) ;
795
796         if (serverId == illegalServerId)
797             return parseError;
798
799         // shutdown the server
800
Activator activator = ActivatorHelper.narrow(
801         orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
802         activator.shutdown(serverId);
803
804         out.println(CorbaResourceUtil.getText("servertool.shutdown2"));
805     } catch (ServerNotActive ex) {
806         out.println(CorbaResourceUtil.getText("servertool.servernotrunning"));
807         } catch (ServerNotRegistered ex) {
808         out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
809     } catch (Exception JavaDoc ex) {
810         ex.printStackTrace();
811     }
812
813     return commandDone;
814     }
815 }
816
817 class StartServer implements CommandHandler
818 {
819     public String JavaDoc getCommandName() {return "startup";}
820    
821     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
822     {
823     if (helpType == longHelp) {
824         out.println(CorbaResourceUtil.getText("servertool.startserver"));
825     } else {
826         out.println(CorbaResourceUtil.getText("servertool.startserver1"));
827     }
828     }
829
830     final static int illegalServerId = -1;
831
832     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
833     {
834     int serverId = illegalServerId;
835
836     try {
837         // determine the server id
838
if (cmdArgs.length == 2)
839             if (cmdArgs[0].equals("-serverid"))
840             serverId = (Integer.valueOf(cmdArgs[1])).intValue();
841             else if (cmdArgs[0].equals("-applicationName"))
842             serverId = ServerTool.getServerIdForAlias( orb, cmdArgs[1] ) ;
843
844         if (serverId == illegalServerId)
845             return parseError;
846
847         // startup the server
848
Activator activator = ActivatorHelper.narrow(
849         orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
850         activator.activate(serverId);
851
852         out.println(CorbaResourceUtil.getText("servertool.startserver2"));
853     } catch (ServerNotRegistered ex) {
854         out.println(CorbaResourceUtil.getText("servertool.nosuchserver"));
855     } catch (ServerAlreadyActive ex) {
856         out.println(CorbaResourceUtil.getText("servertool.serverup"));
857     } catch (ServerHeldDown ex) {
858         out.println(CorbaResourceUtil.getText("servertool.helddown"));
859     } catch (Exception JavaDoc ex) {
860         ex.printStackTrace();
861         }
862     return commandDone;
863     }
864 }
865
866 class Quit implements CommandHandler
867 {
868     public String JavaDoc getCommandName() {return "quit";}
869    
870     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
871     {
872     if (helpType == longHelp) {
873         out.println(CorbaResourceUtil.getText("servertool.quit"));
874     } else {
875         out.println(CorbaResourceUtil.getText("servertool.quit1"));
876     }
877     }
878
879     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
880     {
881     System.exit(0);
882
883     return commandDone;
884     }
885 }
886
887 class Help implements CommandHandler
888 {
889     public String JavaDoc getCommandName() {return "help";}
890    
891     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
892     {
893     if (helpType == longHelp) {
894         out.println(CorbaResourceUtil.getText("servertool.help"));
895     } else {
896         out.println(CorbaResourceUtil.getText("servertool.help1"));
897     }
898     }
899
900     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
901     {
902     return commandDone;
903     }
904 }
905
906 class ListORBs implements CommandHandler
907 {
908     public String JavaDoc getCommandName() {return "orblist";}
909    
910     public void printCommandHelp(PrintStream JavaDoc out, boolean helpType)
911     {
912     if (helpType == longHelp) {
913         out.println(CorbaResourceUtil.getText("servertool.orbidmap"));
914     } else {
915         out.println(CorbaResourceUtil.getText("servertool.orbidmap1"));
916     }
917     }
918
919     final static int illegalServerId = -1;
920
921     public boolean processCommand(String JavaDoc[] cmdArgs, ORB JavaDoc orb, PrintStream JavaDoc out)
922     {
923
924     int serverId = illegalServerId;
925
926     try {
927         if (cmdArgs.length == 2) {
928             if (cmdArgs[0].equals("-serverid"))
929             serverId = (Integer.valueOf(cmdArgs[1])).intValue();
930             else if (cmdArgs[0].equals("-applicationName"))
931             serverId = ServerTool.getServerIdForAlias( orb, cmdArgs[1] ) ;
932         }
933
934         // the server id has to be specified
935
if (serverId == illegalServerId)
936             return parseError;
937             // activate the server
938
Activator activator = ActivatorHelper.narrow(
939         orb.resolve_initial_references( ORBConstants.SERVER_ACTIVATOR_NAME ));
940
941         String JavaDoc[] orbList = activator.getORBNames(serverId);
942
943         out.println(CorbaResourceUtil.getText("servertool.orbidmap2"));
944
945         for (int i = 0; i < orbList.length ; i++) {
946         out.println("\t "+ orbList[i]);
947         }
948         } catch (ServerNotRegistered ex) {
949         out.println("\tno such server found.");
950     } catch (Exception JavaDoc ex) {
951         ex.printStackTrace();
952     }
953
954       return commandDone;
955     }
956 }
957
Popular Tags