KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > util > ImRManager


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.imr.util;
22
23 import org.jacorb.imr.*;
24
25 import org.apache.avalon.framework.logger.Logger;
26 import org.jacorb.config.Configuration;
27
28 /**
29  * This class is a command-line tool for administering
30  * the implementation repository.
31  *
32  * @author Nicolas Noffke
33  *
34  * $Id: ImRManager.java,v 1.11 2004/05/06 12:39:59 nicolas Exp $
35  */

36
37 public class ImRManager
38 {
39     private static org.jacorb.orb.ORB m_orb;
40     private static Logger logger;
41
42     /**
43      * This method registers a server with the imr. To be called from within
44      * a program. Leave command and host to "" (not null), if automatic startup
45      * is not desired.
46      *
47      * @param edit_existing if set to true and the server already exist,
48      * the entry will be set to the supplied new values.
49      */

50     public static void autoRegisterServer( org.omg.CORBA.ORB JavaDoc orb,
51                                            String JavaDoc server,
52                                            String JavaDoc command,
53                                            String JavaDoc host,
54                                            boolean edit_existing )
55     {
56         try
57         {
58             Admin admin =
59                 AdminHelper.narrow( orb.resolve_initial_references("ImplementationRepository"));
60
61             Configuration config = ((org.jacorb.orb.ORB)orb).getConfiguration();
62             Logger logger = config.getNamedLogger("jacorb.imr.manager");
63
64             ServerInfo info = null;
65
66             try
67             {
68                 info = admin.get_server_info(server);
69             }
70             catch (UnknownServerName n)
71             {
72                 if (logger.isWarnEnabled())
73                     logger.warn("Unknown Server name: " + n.getMessage());
74             }
75
76             if (info == null)
77             {
78                 admin.register_server(server, command, host);
79             }
80             else if ((info != null) && edit_existing)
81             {
82                 admin.edit_server(server, command, host);
83             }
84         }
85         catch (Exception JavaDoc e)
86         {
87             if (logger.isWarnEnabled())
88                 logger.warn(e.getMessage());
89         }
90     }
91
92     /**
93      * Returns the name of the local host to be supplied to the imr.
94      * If this can't be queried, an empty String is returned.
95      */

96     public static String JavaDoc getLocalHostName()
97     {
98         try
99         {
100             return java.net.InetAddress.getLocalHost().getHostName();
101         }
102         catch (java.net.UnknownHostException JavaDoc e)
103         {
104             // ignore, was: Debug.output(3, e);
105
}
106
107         return "";
108     }
109
110     /**
111      * Returns an arbitrary host, on which an imr_ssd is running,
112      * or an empty String, if none is present.
113      */

114     public static String JavaDoc getAnyHostName(org.omg.CORBA.ORB JavaDoc orb)
115     {
116         try
117         {
118             Admin admin =
119                 AdminHelper.narrow( orb.resolve_initial_references("ImplementationRepository"));
120
121             Configuration config = ((org.jacorb.orb.ORB)orb).getConfiguration();
122             Logger logger = config.getNamedLogger("jacorb.imr.manager");
123
124             HostInfo[] hosts = admin.list_hosts();
125
126             if (hosts.length > 0)
127             {
128                 return hosts[0].name;
129             }
130         }
131         catch (Exception JavaDoc e)
132         {
133              if (logger.isWarnEnabled())
134                 logger.warn(e.getMessage());
135         }
136
137         return "";
138     }
139
140     private static Admin getAdmin()
141     {
142         Admin _admin = null;
143         try
144         {
145             _admin =
146                 AdminHelper.narrow( m_orb.resolve_initial_references("ImplementationRepository"));
147         }
148         catch( org.omg.CORBA.ORBPackage.InvalidName JavaDoc in )
149         {
150              if (logger.isWarnEnabled())
151                 logger.warn("Could not contact Impl. Repository!");
152         }
153
154         if (_admin == null)
155         {
156             System.out.println("Unable to connect to repository process!");
157             System.exit(-1);
158         }
159
160         return _admin;
161     }
162
163     /**
164      * Add a server to the repository or edit an existing server.
165      *
166      */

167     private static void addServer(String JavaDoc[] args)
168     {
169         if (args.length == 1)
170         {
171             System.out.println("Please specify at least server name");
172             shortUsage();
173         }
174
175         String JavaDoc _server_name = args[1];
176         String JavaDoc _host = null;
177         String JavaDoc _command = null;
178
179         try
180         {
181             //evaluate parameters
182
for (int i = 2; i < args.length ; i++)
183             {
184                 if( args[i].equals("-h") )
185                 {
186                     //is next arg available?
187
if ( (++i) < args.length )
188                     {
189                         _host = args[i];
190                     }
191                     else
192                     {
193                         System.out.println("Please provide a hostname after the -h switch");
194
195                         shortUsage();
196                     }
197                 }
198                 else if( args[i].equals("-c") )
199                 {
200                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
201                     
202                     for( int j = (i + 1); j < args.length; j++ )
203                     {
204                         sb.append( args[j] );
205                         
206                         if( j < (args.length - 1) )
207                         {
208                             sb.append( ' ' ); //append whitespace
209
//don't append, if last arg
210
}
211                     }
212                     
213                     _command = sb.toString();
214                     break; //definitely at end of args
215
}
216                 else
217                 {
218                     System.out.println("Unrecognized switch: " + args[i]);
219                     shortUsage();
220                 }
221             }
222
223             if (_host == null)
224             {
225                 _host = getLocalHostName();
226             }
227         }
228         catch (Exception JavaDoc _e)
229         {
230             _e.printStackTrace();
231             usage();
232         }
233     
234         if (_command == null)
235         {
236             _command = "";
237         }
238     
239         Admin _admin = getAdmin();
240
241         try
242         {
243             if (args[0].equals("add"))
244             {
245                 _admin.register_server(_server_name, _command, _host);
246             }
247             else
248             {
249                 //else case already checked in main
250
_admin.edit_server(_server_name, _command, _host);
251             }
252
253             System.out.println("Server " + _server_name + " successfully " +
254                                args[0] + "ed");
255             System.out.println("Host: >>" + _host + "<<");
256             
257             if( _command.length() > 0 )
258             {
259                 System.out.println("Command: >>" + _command + "<<");
260             }
261             else
262             {
263                 System.out.println("No command specified. Server can't be restarted!");
264             }
265         }
266         catch (Exception JavaDoc _e)
267         {
268             _e.printStackTrace();
269         }
270
271         System.exit(0);
272     }
273
274     /**
275      * Remove a server or host from the repository.
276      */

277     private static void remove(String JavaDoc[] args)
278     {
279         if (args.length == 1)
280         {
281             System.out.println(" Please specify if you want to remove a server or a host");
282             shortUsage();
283         }
284
285         if (args.length == 2)
286         {
287             System.out.println(" Please specify a servername / hostname");
288             shortUsage();
289         }
290     
291         Admin _admin = getAdmin();
292
293         if (args[1].equals("server"))
294         {
295             try
296             {
297                 _admin.unregister_server(args[2]);
298         
299                 System.out.println("Server " + args[2] +
300                                    " successfully removed");
301             }
302             catch (Exception JavaDoc _e)
303             {
304                 _e.printStackTrace();
305             }
306         }
307         else if (args[1].equals("host"))
308         {
309             try
310             {
311                 _admin.unregister_host(args[2]);
312         
313                 System.out.println("Host " + args[2] +
314                                    " successfully removed");
315             }
316             catch (Exception JavaDoc _e)
317             {
318                 _e.printStackTrace();
319             }
320         }
321         else
322         {
323             System.out.println("Unknown command " + args[1]);
324             shortUsage();
325         }
326
327         System.exit(0);
328     }
329
330     /**
331      * List servers or hosts.
332      */

333     private static void list(String JavaDoc[] args)
334     {
335         if (args.length == 1)
336         {
337             System.out.println("Please use (servers | hosts) in command");
338             shortUsage();
339         }
340
341         Admin _admin = getAdmin();
342     
343         try
344         {
345             if (args[1].equals("servers"))
346             {
347                 ServerInfo[] _info = _admin.list_servers();
348
349                 System.out.println("Servers (total: " + _info.length + "):");
350
351                 for(int _i = 0; _i < _info.length; _i++)
352                 {
353                     System.out.println((_i + 1) + ") " +_info[_i].name);
354
355                     System.out.println(" " + "Host: " + _info[_i].host);
356
357                     System.out.println(" " + "Command: " +
358                                        _info[_i].command);
359
360                     System.out.println(" " + "active: " +
361                                        ((_info[_i].active)?"yes":"no"));
362
363                     System.out.println(" " + "holding: " +
364                                        ((_info[_i].holding)?"yes":"no"));
365                 }
366             }
367             else if (args[1].equals("hosts"))
368             {
369                 HostInfo[] _info = _admin.list_hosts();
370
371                 System.out.println("Hosts (total: " + _info.length + "):");
372
373                 for(int _i = 0; _i < _info.length; _i++)
374                 {
375                     System.out.println((_i + 1) + ") " +_info[_i].name);
376                 }
377             }
378             else
379             {
380                 System.out.println("Unrecognized option: " + args[1]);
381                 shortUsage();
382             }
383         }
384         catch (Exception JavaDoc _e)
385         {
386             _e.printStackTrace();
387         }
388         System.exit(0);
389     }
390
391     /**
392      * Hold a server.
393      */

394     private static void holdServer(String JavaDoc[] args)
395     {
396         if (args.length == 1)
397         {
398             System.out.println("Please specify a server name");
399             shortUsage();
400         }
401
402         String JavaDoc _server_name = args[1];
403         int _timeout = 0;
404
405         Admin _admin = getAdmin();
406
407         try
408         {
409             if (args.length == 3)
410             {
411                 _timeout = Integer.parseInt(args[2]);
412             }
413
414             _admin.hold_server(_server_name);
415             System.out.println("Server " + _server_name + " set to holding");
416         
417             if (_timeout > 0)
418             {
419                 Thread.sleep(_timeout);
420
421                 _admin.release_server(_server_name);
422
423                 System.out.println("Server " + _server_name + " released");
424             }
425
426         }
427         catch (Exception JavaDoc _e)
428         {
429             _e.printStackTrace();
430         }
431         System.exit(0);
432     }
433
434     /**
435      * Release a server.
436      */

437     private static void releaseServer(String JavaDoc[] args)
438     {
439         if (args.length == 1)
440         {
441             System.out.println("Please specify a server name");
442             shortUsage();
443         }
444
445         String JavaDoc _server_name = args[1];
446         int _timeout = 0;
447
448         Admin _admin = getAdmin();
449
450         try
451         {
452             _admin.release_server(_server_name);
453
454             System.out.println("Server " + _server_name + " released");
455         }
456         catch (Exception JavaDoc _e)
457         {
458             _e.printStackTrace();
459         }
460         System.exit(0);
461     }
462
463     /**
464      * Start a server.
465      */

466     private static void startServer(String JavaDoc[] args)
467     {
468         if (args.length == 1)
469         {
470             System.out.println("Please specify a server name");
471             shortUsage();
472         }
473
474         String JavaDoc _server_name = args[1];
475
476         Admin _admin = getAdmin();
477
478         try
479         {
480             _admin.start_server(_server_name);
481
482             System.out.println("Server " + _server_name + " started");
483         }
484         catch (Exception JavaDoc _e)
485         {
486             _e.printStackTrace();
487         }
488         System.exit(0);
489     }
490
491     /**
492      * Write a backup of the server table.
493      */

494     private static void saveTable()
495     {
496         Admin _admin = getAdmin();
497
498         try
499         {
500             _admin.save_server_table();
501
502             System.out.println("Backup of server table was successfull");
503         }
504         catch (Exception JavaDoc _e)
505         {
506             _e.printStackTrace();
507         }
508         System.exit(0);
509     }
510
511     /**
512      * Shut the repository down.
513      */

514     private static void shutdownImR(String JavaDoc[] args)
515     {
516         Admin _admin = getAdmin();
517
518         boolean _wait = true;
519     
520         if (args.length == 2)
521         {
522             if (args[1].toLowerCase().equals("force"))
523             {
524                 _wait = false;
525             }
526             else
527             {
528                 System.out.println("Unrecognized option: " + args[1]);
529                 System.out.println("The only possible option is \"force\"");
530                 shortUsage();
531             }
532         }
533         
534         try
535         {
536             _admin.shutdown(_wait);
537
538             System.out.println("The Implementation Repository has been shut down without exceptions");
539         }
540         catch (Exception JavaDoc _e)
541         {
542             _e.printStackTrace();
543         }
544         System.exit(0);
545     }
546     
547     /**
548      * Set a server to not active.
549      */

550     private static void setDown(String JavaDoc[] args)
551     {
552         if (args.length == 1)
553         {
554             System.out.println("Please specify a server name");
555             shortUsage();
556         }
557
558         Registration _reg = RegistrationHelper.narrow(getAdmin());
559     
560         try
561         {
562             _reg.set_server_down(args[1]);
563             
564             System.out.println("Server " + args[1] + " set down");
565         }
566         catch (Exception JavaDoc _e)
567         {
568             _e.printStackTrace();
569         }
570         System.exit(0);
571     }
572
573     private static void shortUsage()
574     {
575         System.out.println("\nYour command has not been understood possibly due to\n" +
576                            "one or more missing arguments");
577         System.out.println("Type \"imr_mg help\" to display the help screen");
578         System.exit(-1);
579     }
580         
581
582     /**
583      * Print help messages.
584      */

585     private static void usage()
586     {
587         System.out.println("Usage: ImRManager <command> [<servername>] [switches]");
588         System.out.println("Command: (add | edit) <servername> [-h <hostname> -c <startup cmd>]");
589         System.out.println("\t -h <hostname> Restart server on this host");
590         System.out.println("\t -c <command> Restart server with this command");
591         System.out.println("\t If -h is not set, the local hosts name (that of the manager) is used.");
592         System.out.println("\t Note: The -c switch must always follow after the -h switch,");
593         System.out.println("\t because all arguments after -c are interpreted as the");
594         System.out.println("\t startup command.");
595
596         System.out.println("\nCommand: remove (server | host) <name>");
597         System.out.println("\t Removes the server or host <name> from the repository");
598
599         System.out.println("\nCommand: list (servers | hosts)");
600         System.out.println("\t Lists all servers or all hosts");
601
602         System.out.println("\nCommand: hold <servername> [<time>]");
603         System.out.println("\t Holds the server <servername> (if <time> is specified,");
604         System.out.println("\t it is released automatically)");
605
606         System.out.println("\nCommand: release <servername>");
607         System.out.println("\t Releases the server <servername>");
608
609         System.out.println("\nCommand: start <servername>");
610         System.out.println("\t Starts the server <servername> on its given host with " +
611                            "its given command");
612
613         System.out.println("\nCommand: setdown <servername>");
614         System.out.println("\t Declares the server <servername> as \"down\" to the repository.");
615         System.out.println("\t This means that the repository tries to start the server up after ");
616         System.out.println("\t receiving the next request for it.");
617         System.out.println("\t This is actually an operation only committed by the ORB, but it");
618         System.out.println("\t might be useful for server migration and recovery of crashed servers.");
619         System.out.println("\t Note: Use \"hold\" before, to avoid the server being restarted at the");
620         System.out.println("\t wrong moment.");
621
622         System.out.println("\nCommand: savetable");
623         System.out.println("\t Makes a backup of the server table");
624
625         System.out.println("\nCommand: shutdown [force]");
626         System.out.println("\t Shuts the ImR down orderly. If \"force\" is specified, the ORB ");
627         System.out.println("\t is forced down, ignoring open connections.");
628
629         System.out.println("\nCommand: gui");
630         System.out.println("\t Bring up manager GUI window");
631
632         System.out.println("\nCommand: help");
633         System.out.println("\t This screen");
634
635         System.exit(1);
636     }
637
638     /**
639      * Main method.
640      */

641     public static void main(String JavaDoc[] args)
642     {
643         if( args.length == 0 )
644         {
645             usage();
646         }
647
648         m_orb = (org.jacorb.orb.ORB) org.omg.CORBA.ORB.init(args, null);
649         logger = m_orb.getConfiguration().getNamedLogger("jacorb.imr.manager");
650
651         try
652         {
653             if (args[0].equals("add") || args[0].equals("edit"))
654                 addServer(args);
655             else if (args[0].equals("remove"))
656                 remove(args);
657             else if (args[0].equals("list"))
658                 list(args);
659             else if (args[0].equals("hold"))
660                 holdServer(args);
661             else if (args[0].equals("release"))
662                 releaseServer(args);
663             else if (args[0].equals("start"))
664                 startServer(args);
665             else if (args[0].equals("savetable"))
666                 saveTable();
667             else if (args[0].equals("shutdown"))
668                 shutdownImR(args);
669             else if (args[0].equals("setdown"))
670                 setDown(args);
671             else if (args[0].equals("gui"))
672                 Class.forName("org.jacorb.imr.util.ImRManagerGUI").newInstance();
673             else if (args[0].equals("help"))
674                 usage();
675             else
676             {
677                 System.out.println("Unrecognized command: " + args[0]);
678                 usage();
679             }
680         }
681         catch (Exception JavaDoc _e)
682         {
683             _e.printStackTrace();
684             System.exit(0);
685         }
686     }
687     
688 } // ImRManager
689

690
691
692
693
694
695
696
697
Popular Tags