001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 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: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.cluster.daemon;
025:
026: import javax.management.MBeanServerConnection;
027: import javax.management.ObjectInstance;
028:
029: import org.objectweb.jonas.jmx.commons.JMXRemoteHelper;
030:
031: /**
032: * This class implements a jmx client to administrate the cluster daemon.
033: *
034: * @author Benoit Pelletier
035: */
036: public class ClusterDaemonAdmin {
037: /**
038: * Connection to the MBean server
039: */
040: private static MBeanServerConnection cnx = null;
041:
042: /**
043: * Object Instance
044: */
045: private static ObjectInstance oi = null;
046:
047: /**
048: * STOP command
049: */
050: private static final int STOP_CMD = 0;
051:
052: /**
053: * clusterd.xml filename (by default $JONAS_ROOT/conf/clusterd.xml)
054: */
055: private static String confFile = null;
056:
057: /**
058: * Private constructor
059: */
060: private ClusterDaemonAdmin() {
061:
062: }
063:
064: /**
065: * Init MBean server connection
066: *
067: */
068: private static void initJmxCnt() {
069:
070: try {
071: cnx = JMXRemoteHelper.connect(ClusterDaemonTools
072: .getJmxUrl(ClusterDaemonTools
073: .getCurrentConfiguration()
074: .getClusterDaemon().getName()));
075: oi = JMXRemoteHelper.getInstance(cnx,
076: "*:type=ClusterDaemon");
077: } catch (Exception e) {
078: System.err.println("Cannot init MBean server connection : "
079: + e);
080: System.exit(2);
081: }
082:
083: }
084:
085: /**
086: * Usage
087: */
088: private static void usage() {
089: System.err.println("usage : jclusterd <options>");
090: System.err.println("list of available options:");
091: System.err.println(" start : start the cluster daemon.");
092: System.err.println(" stop : stop the cluster daemon.");
093: }
094:
095: /**
096: * Main method
097: * @param args arguments
098: */
099: public static void main(String[] args) {
100:
101: int cmd = -1;
102:
103: // Get command args
104: for (int argn = 0; argn < args.length; argn++) {
105: String arg = args[argn];
106:
107: if (arg.equals("-stop")) {
108:
109: cmd = STOP_CMD;
110:
111: }
112: if (arg.equals("-confFile")) {
113: confFile = args[++argn];
114: continue;
115: }
116:
117: // load the clusterd.xml file
118: try {
119: ClusterDaemonTools
120: .loadClusterDaemonConfiguration(confFile);
121: } catch (Exception e) {
122: System.err
123: .println("Error during the loading of the configuration file : "
124: + e);
125: System.exit(2);
126:
127: }
128: // init the jmx connection with the cluster daemon
129: initJmxCnt();
130:
131: // execute the command
132: switch (cmd) {
133: case STOP_CMD:
134: try {
135: // call the stop() operation
136: Object[] params = new Object[] {};
137: String[] sig = new String[] {};
138: cnx.invoke(oi.getObjectName(), "stopClusterDaemon",
139: params, sig);
140: System.out.println("Cluster daemon stopped");
141: } catch (Exception e) {
142: System.err.println("MBean invocation exception : "
143: + e);
144: System.exit(2);
145: }
146: break;
147: default:
148: System.err.println("Bad cmd: " + cmd);
149: usage();
150: System.exit(2);
151: break;
152: }
153: }
154: }
155: }
|