001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021: package org.jacorb.imr.util;
022:
023: import org.jacorb.imr.*;
024:
025: import org.apache.avalon.framework.logger.Logger;
026: import org.jacorb.config.Configuration;
027:
028: /**
029: * This class is a command-line tool for administering
030: * the implementation repository.
031: *
032: * @author Nicolas Noffke
033: *
034: * $Id: ImRManager.java,v 1.12 2006/05/17 12:54:33 alphonse.bendt Exp $
035: */
036:
037: public class ImRManager {
038: private static org.jacorb.orb.ORB m_orb;
039: private static Logger logger;
040:
041: /**
042: * This method registers a server with the imr. To be called from within
043: * a program. Leave command and host to "" (not null), if automatic startup
044: * is not desired.
045: *
046: * @param edit_existing if set to true and the server already exist,
047: * the entry will be set to the supplied new values.
048: */
049: public static void autoRegisterServer(org.omg.CORBA.ORB orb,
050: String server, String command, String host,
051: boolean edit_existing) {
052: try {
053: Admin admin = AdminHelper
054: .narrow(orb
055: .resolve_initial_references("ImplementationRepository"));
056:
057: Configuration config = ((org.jacorb.orb.ORB) orb)
058: .getConfiguration();
059: Logger logger = config.getNamedLogger("jacorb.imr.manager");
060:
061: ServerInfo info = null;
062:
063: try {
064: info = admin.get_server_info(server);
065: } catch (UnknownServerName n) {
066: logger.warn("Unknown Server name", n);
067: }
068:
069: if (info == null) {
070: admin.register_server(server, command, host);
071: } else if ((info != null) && edit_existing) {
072: admin.edit_server(server, command, host);
073: }
074: } catch (Exception e) {
075: logger.warn("unexpected exception", e);
076: }
077: }
078:
079: /**
080: * Returns the name of the local host to be supplied to the imr.
081: * If this can't be queried, an empty String is returned.
082: */
083: public static String getLocalHostName() {
084: try {
085: return java.net.InetAddress.getLocalHost().getHostName();
086: } catch (java.net.UnknownHostException e) {
087: // ignore, was: Debug.output(3, e);
088: }
089:
090: return "";
091: }
092:
093: /**
094: * Returns an arbitrary host, on which an imr_ssd is running,
095: * or an empty String, if none is present.
096: */
097: public static String getAnyHostName(org.omg.CORBA.ORB orb) {
098: try {
099: Admin admin = AdminHelper
100: .narrow(orb
101: .resolve_initial_references("ImplementationRepository"));
102:
103: HostInfo[] hosts = admin.list_hosts();
104:
105: if (hosts.length > 0) {
106: return hosts[0].name;
107: }
108: } catch (Exception e) {
109: logger.warn("unexpected exception", e);
110: }
111:
112: return "";
113: }
114:
115: private static Admin getAdmin() {
116: Admin _admin = null;
117: try {
118: _admin = AdminHelper
119: .narrow(m_orb
120: .resolve_initial_references("ImplementationRepository"));
121: } catch (org.omg.CORBA.ORBPackage.InvalidName in) {
122: if (logger.isWarnEnabled())
123: logger.warn("Could not contact Impl. Repository!");
124: }
125:
126: if (_admin == null) {
127: System.out
128: .println("Unable to connect to repository process!");
129: System.exit(-1);
130: }
131:
132: return _admin;
133: }
134:
135: /**
136: * Add a server to the repository or edit an existing server.
137: *
138: */
139: private static void addServer(String[] args) {
140: if (args.length == 1) {
141: System.out.println("Please specify at least server name");
142: shortUsage();
143: }
144:
145: String _server_name = args[1];
146: String _host = null;
147: String _command = null;
148:
149: try {
150: //evaluate parameters
151: for (int i = 2; i < args.length; i++) {
152: if (args[i].equals("-h")) {
153: //is next arg available?
154: if ((++i) < args.length) {
155: _host = args[i];
156: } else {
157: System.out
158: .println("Please provide a hostname after the -h switch");
159:
160: shortUsage();
161: }
162: } else if (args[i].equals("-c")) {
163: StringBuffer sb = new StringBuffer();
164:
165: for (int j = (i + 1); j < args.length; j++) {
166: sb.append(args[j]);
167:
168: if (j < (args.length - 1)) {
169: sb.append(' '); //append whitespace
170: //don't append, if last arg
171: }
172: }
173:
174: _command = sb.toString();
175: break; //definitely at end of args
176: } else {
177: System.out.println("Unrecognized switch: "
178: + args[i]);
179: shortUsage();
180: }
181: }
182:
183: if (_host == null) {
184: _host = getLocalHostName();
185: }
186: } catch (Exception _e) {
187: _e.printStackTrace();
188: usage();
189: }
190:
191: if (_command == null) {
192: _command = "";
193: }
194:
195: Admin _admin = getAdmin();
196:
197: try {
198: if (args[0].equals("add")) {
199: _admin.register_server(_server_name, _command, _host);
200: } else {
201: //else case already checked in main
202: _admin.edit_server(_server_name, _command, _host);
203: }
204:
205: System.out.println("Server " + _server_name
206: + " successfully " + args[0] + "ed");
207: System.out.println("Host: >>" + _host + "<<");
208:
209: if (_command.length() > 0) {
210: System.out.println("Command: >>" + _command + "<<");
211: } else {
212: System.out
213: .println("No command specified. Server can't be restarted!");
214: }
215: } catch (Exception _e) {
216: _e.printStackTrace();
217: }
218:
219: System.exit(0);
220: }
221:
222: /**
223: * Remove a server or host from the repository.
224: */
225: private static void remove(String[] args) {
226: if (args.length == 1) {
227: System.out
228: .println(" Please specify if you want to remove a server or a host");
229: shortUsage();
230: }
231:
232: if (args.length == 2) {
233: System.out
234: .println(" Please specify a servername / hostname");
235: shortUsage();
236: }
237:
238: Admin _admin = getAdmin();
239:
240: if (args[1].equals("server")) {
241: try {
242: _admin.unregister_server(args[2]);
243:
244: System.out.println("Server " + args[2]
245: + " successfully removed");
246: } catch (Exception _e) {
247: _e.printStackTrace();
248: }
249: } else if (args[1].equals("host")) {
250: try {
251: _admin.unregister_host(args[2]);
252:
253: System.out.println("Host " + args[2]
254: + " successfully removed");
255: } catch (Exception _e) {
256: _e.printStackTrace();
257: }
258: } else {
259: System.out.println("Unknown command " + args[1]);
260: shortUsage();
261: }
262:
263: System.exit(0);
264: }
265:
266: /**
267: * List servers or hosts.
268: */
269: private static void list(String[] args) {
270: if (args.length == 1) {
271: System.out
272: .println("Please use (servers | hosts) in command");
273: shortUsage();
274: }
275:
276: Admin _admin = getAdmin();
277:
278: try {
279: if (args[1].equals("servers")) {
280: ServerInfo[] _info = _admin.list_servers();
281:
282: System.out.println("Servers (total: " + _info.length
283: + "):");
284:
285: for (int _i = 0; _i < _info.length; _i++) {
286: System.out
287: .println((_i + 1) + ") " + _info[_i].name);
288:
289: System.out.println(" " + "Host: "
290: + _info[_i].host);
291:
292: System.out.println(" " + "Command: "
293: + _info[_i].command);
294:
295: System.out.println(" " + "active: "
296: + ((_info[_i].active) ? "yes" : "no"));
297:
298: System.out.println(" " + "holding: "
299: + ((_info[_i].holding) ? "yes" : "no"));
300: }
301: } else if (args[1].equals("hosts")) {
302: HostInfo[] _info = _admin.list_hosts();
303:
304: System.out.println("Hosts (total: " + _info.length
305: + "):");
306:
307: for (int _i = 0; _i < _info.length; _i++) {
308: System.out
309: .println((_i + 1) + ") " + _info[_i].name);
310: }
311: } else {
312: System.out.println("Unrecognized option: " + args[1]);
313: shortUsage();
314: }
315: } catch (Exception _e) {
316: _e.printStackTrace();
317: }
318: System.exit(0);
319: }
320:
321: /**
322: * Hold a server.
323: */
324: private static void holdServer(String[] args) {
325: if (args.length == 1) {
326: System.out.println("Please specify a server name");
327: shortUsage();
328: }
329:
330: String _server_name = args[1];
331: int _timeout = 0;
332:
333: Admin _admin = getAdmin();
334:
335: try {
336: if (args.length == 3) {
337: _timeout = Integer.parseInt(args[2]);
338: }
339:
340: _admin.hold_server(_server_name);
341: System.out.println("Server " + _server_name
342: + " set to holding");
343:
344: if (_timeout > 0) {
345: Thread.sleep(_timeout);
346:
347: _admin.release_server(_server_name);
348:
349: System.out.println("Server " + _server_name
350: + " released");
351: }
352:
353: } catch (Exception _e) {
354: _e.printStackTrace();
355: }
356: System.exit(0);
357: }
358:
359: /**
360: * Release a server.
361: */
362: private static void releaseServer(String[] args) {
363: if (args.length == 1) {
364: System.out.println("Please specify a server name");
365: shortUsage();
366: }
367:
368: String _server_name = args[1];
369: int _timeout = 0;
370:
371: Admin _admin = getAdmin();
372:
373: try {
374: _admin.release_server(_server_name);
375:
376: System.out.println("Server " + _server_name + " released");
377: } catch (Exception _e) {
378: _e.printStackTrace();
379: }
380: System.exit(0);
381: }
382:
383: /**
384: * Start a server.
385: */
386: private static void startServer(String[] args) {
387: if (args.length == 1) {
388: System.out.println("Please specify a server name");
389: shortUsage();
390: }
391:
392: String _server_name = args[1];
393:
394: Admin _admin = getAdmin();
395:
396: try {
397: _admin.start_server(_server_name);
398:
399: System.out.println("Server " + _server_name + " started");
400: } catch (Exception _e) {
401: _e.printStackTrace();
402: }
403: System.exit(0);
404: }
405:
406: /**
407: * Write a backup of the server table.
408: */
409: private static void saveTable() {
410: Admin _admin = getAdmin();
411:
412: try {
413: _admin.save_server_table();
414:
415: System.out
416: .println("Backup of server table was successfull");
417: } catch (Exception _e) {
418: _e.printStackTrace();
419: }
420: System.exit(0);
421: }
422:
423: /**
424: * Shut the repository down.
425: */
426: private static void shutdownImR(String[] args) {
427: Admin _admin = getAdmin();
428:
429: boolean _wait = true;
430:
431: if (args.length == 2) {
432: if (args[1].toLowerCase().equals("force")) {
433: _wait = false;
434: } else {
435: System.out.println("Unrecognized option: " + args[1]);
436: System.out
437: .println("The only possible option is \"force\"");
438: shortUsage();
439: }
440: }
441:
442: try {
443: _admin.shutdown(_wait);
444:
445: System.out
446: .println("The Implementation Repository has been shut down without exceptions");
447: } catch (Exception _e) {
448: _e.printStackTrace();
449: }
450: System.exit(0);
451: }
452:
453: /**
454: * Set a server to not active.
455: */
456: private static void setDown(String[] args) {
457: if (args.length == 1) {
458: System.out.println("Please specify a server name");
459: shortUsage();
460: }
461:
462: Registration _reg = RegistrationHelper.narrow(getAdmin());
463:
464: try {
465: _reg.set_server_down(args[1]);
466:
467: System.out.println("Server " + args[1] + " set down");
468: } catch (Exception _e) {
469: _e.printStackTrace();
470: }
471: System.exit(0);
472: }
473:
474: private static void shortUsage() {
475: System.out
476: .println("\nYour command has not been understood possibly due to\n"
477: + "one or more missing arguments");
478: System.out
479: .println("Type \"imr_mg help\" to display the help screen");
480: System.exit(-1);
481: }
482:
483: /**
484: * Print help messages.
485: */
486: private static void usage() {
487: System.out
488: .println("Usage: ImRManager <command> [<servername>] [switches]");
489: System.out
490: .println("Command: (add | edit) <servername> [-h <hostname> -c <startup cmd>]");
491: System.out
492: .println("\t -h <hostname> Restart server on this host");
493: System.out
494: .println("\t -c <command> Restart server with this command");
495: System.out
496: .println("\t If -h is not set, the local hosts name (that of the manager) is used.");
497: System.out
498: .println("\t Note: The -c switch must always follow after the -h switch,");
499: System.out
500: .println("\t because all arguments after -c are interpreted as the");
501: System.out.println("\t startup command.");
502:
503: System.out.println("\nCommand: remove (server | host) <name>");
504: System.out
505: .println("\t Removes the server or host <name> from the repository");
506:
507: System.out.println("\nCommand: list (servers | hosts)");
508: System.out.println("\t Lists all servers or all hosts");
509:
510: System.out.println("\nCommand: hold <servername> [<time>]");
511: System.out
512: .println("\t Holds the server <servername> (if <time> is specified,");
513: System.out.println("\t it is released automatically)");
514:
515: System.out.println("\nCommand: release <servername>");
516: System.out.println("\t Releases the server <servername>");
517:
518: System.out.println("\nCommand: start <servername>");
519: System.out
520: .println("\t Starts the server <servername> on its given host with "
521: + "its given command");
522:
523: System.out.println("\nCommand: setdown <servername>");
524: System.out
525: .println("\t Declares the server <servername> as \"down\" to the repository.");
526: System.out
527: .println("\t This means that the repository tries to start the server up after ");
528: System.out.println("\t receiving the next request for it.");
529: System.out
530: .println("\t This is actually an operation only committed by the ORB, but it");
531: System.out
532: .println("\t might be useful for server migration and recovery of crashed servers.");
533: System.out
534: .println("\t Note: Use \"hold\" before, to avoid the server being restarted at the");
535: System.out.println("\t wrong moment.");
536:
537: System.out.println("\nCommand: savetable");
538: System.out.println("\t Makes a backup of the server table");
539:
540: System.out.println("\nCommand: shutdown [force]");
541: System.out
542: .println("\t Shuts the ImR down orderly. If \"force\" is specified, the ORB ");
543: System.out
544: .println("\t is forced down, ignoring open connections.");
545:
546: System.out.println("\nCommand: gui");
547: System.out.println("\t Bring up manager GUI window");
548:
549: System.out.println("\nCommand: help");
550: System.out.println("\t This screen");
551:
552: System.exit(1);
553: }
554:
555: /**
556: * Main method.
557: */
558: public static void main(String[] args) {
559: if (args.length == 0) {
560: usage();
561: }
562:
563: m_orb = (org.jacorb.orb.ORB) org.omg.CORBA.ORB.init(args, null);
564: logger = m_orb.getConfiguration().getNamedLogger(
565: "jacorb.imr.manager");
566:
567: try {
568: if (args[0].equals("add") || args[0].equals("edit"))
569: addServer(args);
570: else if (args[0].equals("remove"))
571: remove(args);
572: else if (args[0].equals("list"))
573: list(args);
574: else if (args[0].equals("hold"))
575: holdServer(args);
576: else if (args[0].equals("release"))
577: releaseServer(args);
578: else if (args[0].equals("start"))
579: startServer(args);
580: else if (args[0].equals("savetable"))
581: saveTable();
582: else if (args[0].equals("shutdown"))
583: shutdownImR(args);
584: else if (args[0].equals("setdown"))
585: setDown(args);
586: else if (args[0].equals("gui"))
587: Class.forName("org.jacorb.imr.util.ImRManagerGUI")
588: .newInstance();
589: else if (args[0].equals("help"))
590: usage();
591: else {
592: System.out.println("Unrecognized command: " + args[0]);
593: usage();
594: }
595: } catch (Exception _e) {
596: _e.printStackTrace();
597: System.exit(0);
598: }
599: }
600:
601: } // ImRManager
|