PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/replicator/src/java/com/continuent/tungsten/replicator/management/OpenReplicatorSignaler.java

http://tungsten-replicator.googlecode.com/
Java | 302 lines | 262 code | 9 blank | 31 comment | 2 complexity | 14d46cea5ad23440b343cea6e97489dc MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * Tungsten Scale-Out Stack
  3. * Copyright (C) 2007-2008 Continuent Inc.
  4. * Contact: tungsten@continuent.org
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  18. *
  19. * Initial developer(s): Seppo Jaakola
  20. * Contributor(s):
  21. */
  22. package com.continuent.tungsten.replicator.management;
  23. import java.io.BufferedReader;
  24. import java.io.InputStreamReader;
  25. import java.rmi.ConnectException;
  26. import java.rmi.RemoteException;
  27. import javax.management.remote.JMXConnector;
  28. import com.continuent.tungsten.common.exec.ArgvIterator;
  29. import com.continuent.tungsten.common.jmx.JmxManager;
  30. import com.continuent.tungsten.common.jmx.ServerRuntimeException;
  31. import com.continuent.tungsten.replicator.conf.ReplicatorConf;
  32. /**
  33. * This class defines a OpenReplicatorSignaler that implements a simple utility
  34. * to access ReplicatorManager JMX interface and send signal for the replicator manager.
  35. * See the printHelp() command for a description of current commands.
  36. *
  37. * @author <a href="mailto:seppo.jaakola@continuent.com">Seppo Jaakola</a>
  38. * @version 1.0
  39. */
  40. public class OpenReplicatorSignaler
  41. {
  42. // Statics to read from stdin.
  43. static InputStreamReader converter = new InputStreamReader(
  44. System.in);
  45. static BufferedReader stdin = new BufferedReader(
  46. converter);
  47. private static final String RMI_HOST = "localhost";
  48. private static final int RMI_PORT = 10000;
  49. // Instance variables.
  50. private boolean expectLostConnection = false;
  51. private ArgvIterator argvIterator;
  52. OpenReplicatorSignaler(String[] argv)
  53. {
  54. argvIterator = new ArgvIterator(argv);
  55. }
  56. static void printHelp()
  57. {
  58. println("Replicator Manager Signaler Utility");
  59. println("Syntax: [java " + OpenReplicatorSignaler.class.getName()
  60. + " \\");
  61. println(" [global-options] command [command-options]");
  62. println("Global Options:");
  63. println("\t-host name - Host name of replicator [default: localhost]");
  64. println("\t-port number - Port number of replicator [default: 10000]");
  65. println("Commands:");
  66. println("\thelp - Print this help");
  67. println("\terror msg - send error notification <msg>");
  68. println("\tsynced msg - send error notification <msg>");
  69. println("\tonline msg - send online reached notification <msg>");
  70. println("\toffline msg - send offline reached notification <msg>");
  71. println("\tpaused msg - send paused state reached notification <msg>");
  72. println("Omitting a command prints help text");
  73. }
  74. /**
  75. * Main method to run utility.
  76. *
  77. * @param argv optional command string
  78. */
  79. public static void main(String argv[])
  80. {
  81. OpenReplicatorSignaler ctrl = new OpenReplicatorSignaler(argv);
  82. ctrl.go();
  83. }
  84. /**
  85. * Process replicator command.
  86. */
  87. public void go()
  88. {
  89. // Set defaults for properties.
  90. String rmiHost = RMI_HOST;
  91. int rmiPort = RMI_PORT;
  92. boolean verbose = false;
  93. String command = null;
  94. // Parse global options and command.
  95. String curArg = null;
  96. try
  97. {
  98. while (argvIterator.hasNext())
  99. {
  100. curArg = argvIterator.next();
  101. if ("-host".equals(curArg))
  102. rmiHost = argvIterator.next();
  103. else if ("-port".equals(curArg))
  104. rmiPort = Integer.parseInt(argvIterator.next());
  105. else if ("-verbose".equals(curArg))
  106. verbose = true;
  107. else if (curArg.startsWith("-"))
  108. {
  109. fatal("Unrecognized global option: " + curArg, null);
  110. }
  111. else
  112. {
  113. command = curArg;
  114. break;
  115. }
  116. }
  117. }
  118. catch (NumberFormatException e)
  119. {
  120. fatal("Bad numeric argument for " + curArg, null);
  121. }
  122. catch (ArrayIndexOutOfBoundsException e)
  123. {
  124. fatal("Missing value for " + curArg, null);
  125. }
  126. OpenReplicatorManagerMBean manager = null;
  127. try
  128. {
  129. // Connect with appropriate protection against a lost connection.
  130. try
  131. {
  132. // Connect.
  133. JMXConnector conn = JmxManager.getRMIConnector(rmiHost,
  134. rmiPort, ReplicatorConf.RMI_DEFAULT_SERVICE_NAME);
  135. manager = (OpenReplicatorManagerMBean) JmxManager.getMBeanProxy(
  136. conn, OpenReplicatorManager.class, false);
  137. }
  138. catch (ServerRuntimeException e)
  139. {
  140. fatal("Connection failed: " + e, e);
  141. }
  142. if (command != null)
  143. {
  144. // TODO: is start command needed?
  145. if (command.equals(Commands.HELP))
  146. manager.online();
  147. else if (command.equals(Commands.ERROR))
  148. {
  149. // Check for error message
  150. StringBuffer msg = new StringBuffer();
  151. while (argvIterator.hasNext())
  152. {
  153. msg.append(argvIterator.next());
  154. }
  155. manager.signal(OpenReplicatorManagerMBean.signalError, msg.toString());
  156. }
  157. else if (command.equals(Commands.SYNCED))
  158. {
  159. StringBuffer msg = new StringBuffer();
  160. while (argvIterator.hasNext())
  161. {
  162. msg.append(argvIterator.next());
  163. }
  164. manager.signal(OpenReplicatorManagerMBean.signalSynced, msg.toString());
  165. }
  166. else if (command.equals(Commands.RESTORED))
  167. {
  168. StringBuffer msg = new StringBuffer();
  169. while (argvIterator.hasNext())
  170. {
  171. msg.append(argvIterator.next());
  172. }
  173. manager.signal(OpenReplicatorManagerMBean.signalRestored, msg.toString());
  174. }
  175. else if (command.equals(Commands.OFFLINE))
  176. {
  177. StringBuffer msg = new StringBuffer();
  178. while (argvIterator.hasNext())
  179. {
  180. msg.append(argvIterator.next());
  181. }
  182. manager.signal(OpenReplicatorManagerMBean.signalOfflineReached, msg.toString());
  183. }
  184. else if (command.equals(Commands.SHUTDOWN))
  185. {
  186. StringBuffer msg = new StringBuffer();
  187. while (argvIterator.hasNext())
  188. {
  189. msg.append(argvIterator.next());
  190. }
  191. manager.signal(OpenReplicatorManagerMBean.signalShutdown, msg.toString());
  192. }
  193. else if (command.equals(Commands.CONSISTENCY))
  194. {
  195. StringBuffer msg = new StringBuffer();
  196. while (argvIterator.hasNext())
  197. {
  198. msg.append(argvIterator.next());
  199. }
  200. manager.signal(OpenReplicatorManagerMBean.signalConsistencyFail, msg.toString());
  201. }
  202. else if (command.equals(Commands.HELP))
  203. {
  204. printHelp();
  205. }
  206. else
  207. {
  208. println("Unknown command: '" + command + "'");
  209. printHelp();
  210. }
  211. }
  212. // Check status assuming we didn't expect to lose the connection.
  213. println("State: " + manager.getState());
  214. if (manager.getPendingError() != null)
  215. {
  216. println("Error: " + manager.getPendingError());
  217. println("Exception Message: "
  218. + manager.getPendingExceptionMessage());
  219. }
  220. }
  221. catch (ConnectException e)
  222. {
  223. // This occurs if JMX fails to connect via RMI.
  224. if (expectLostConnection)
  225. println("RMI connection lost!");
  226. else
  227. fatal("RMI connection lost!", e);
  228. }
  229. catch (RemoteException e)
  230. {
  231. // Occurs if there is an MBean server error, for example because
  232. // the server exited.
  233. if (expectLostConnection)
  234. println("Replicator appears to be stopped");
  235. else
  236. {
  237. fatal("Fatal RMI communication error: " + e.getMessage(), e);
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. // Occurs when there is a server-side application exception.
  243. println("Operation failed: " + e.getMessage());
  244. if (verbose)
  245. e.printStackTrace();
  246. if (manager.getPendingError() != null)
  247. {
  248. println("Error: " + manager.getPendingError());
  249. println("Exception Message: "
  250. + manager.getPendingExceptionMessage());
  251. }
  252. }
  253. catch (Throwable t)
  254. {
  255. // Occurs if there is a really bad problem.
  256. fatal("Fatal error: " + t.getMessage(), t);
  257. }
  258. }
  259. // Print a message to stdout.
  260. private static void println(String msg)
  261. {
  262. System.out.println(msg);
  263. }
  264. // Abort following a fatal error.
  265. private static void fatal(String msg, Throwable t)
  266. {
  267. System.out.println(msg);
  268. if (t != null)
  269. t.printStackTrace();
  270. System.exit(1);
  271. }
  272. // List of commands
  273. class Commands
  274. {
  275. public static final String ERROR = "error";
  276. public static final String SYNCED = "synced";
  277. public static final String SHUTDOWN = "shutdown";
  278. public static final String OFFLINE = "offline";
  279. public static final String RESTORED = "restored";
  280. public static final String CONSISTENCY = "consistency";
  281. public static final String HELP = "help";
  282. }
  283. }