PageRenderTime 103ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/cli/src/main/java/org/jboss/as/cli/impl/CliLauncher.java

https://bitbucket.org/cprenzberg/wildfly
Java | 334 lines | 283 code | 19 blank | 32 comment | 102 complexity | 048d7bf35f47b133a901b6c348b79277 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.cli.impl;
  23. import java.io.BufferedReader;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileReader;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.Properties;
  31. import org.jboss.as.cli.CliInitializationException;
  32. import org.jboss.as.cli.CommandContext;
  33. import org.jboss.as.cli.CommandContextFactory;
  34. import org.jboss.as.cli.CommandLineException;
  35. import org.jboss.as.cli.Util;
  36. import org.jboss.as.cli.gui.GuiMain;
  37. import org.jboss.as.cli.handlers.VersionHandler;
  38. import org.jboss.as.protocol.StreamUtils;
  39. import org.wildfly.security.manager.WildFlySecurityManager;
  40. /**
  41. *
  42. * @author Alexey Loubyansky
  43. */
  44. public class CliLauncher {
  45. public static void main(String[] args) throws Exception {
  46. int exitCode = 0;
  47. CommandContext cmdCtx = null;
  48. boolean gui = false;
  49. try {
  50. String argError = null;
  51. List<String> commands = null;
  52. File file = null;
  53. boolean connect = false;
  54. String defaultControllerProtocol = "http-remoting";
  55. String defaultControllerHost = null;
  56. int defaultControllerPort = -1;
  57. boolean version = false;
  58. String username = null;
  59. char[] password = null;
  60. int connectionTimeout = -1;
  61. for(String arg : args) {
  62. if(arg.startsWith("--controller=") || arg.startsWith("controller=")) {
  63. final String fullValue;
  64. final String value;
  65. if(arg.startsWith("--")) {
  66. fullValue = arg.substring(13);
  67. } else {
  68. fullValue = arg.substring(11);
  69. }
  70. final int protocolEnd = fullValue.lastIndexOf("://");
  71. if (protocolEnd == -1) {
  72. value = fullValue;
  73. } else {
  74. value = fullValue.substring(protocolEnd + 3);
  75. defaultControllerProtocol = fullValue.substring(0, protocolEnd);
  76. }
  77. String portStr = null;
  78. int colonIndex = value.lastIndexOf(':');
  79. if(colonIndex < 0) {
  80. // default port
  81. defaultControllerHost = value;
  82. } else if(colonIndex == 0) {
  83. // default host
  84. portStr = value.substring(1);
  85. } else {
  86. final boolean hasPort;
  87. int closeBracket = value.lastIndexOf(']');
  88. if (closeBracket != -1) {
  89. //possible ip v6
  90. if (closeBracket > colonIndex) {
  91. hasPort = false;
  92. } else {
  93. hasPort = true;
  94. }
  95. } else {
  96. //probably ip v4
  97. hasPort = true;
  98. }
  99. if (hasPort) {
  100. defaultControllerHost = value.substring(0, colonIndex).trim();
  101. portStr = value.substring(colonIndex + 1).trim();
  102. } else {
  103. defaultControllerHost = value;
  104. }
  105. }
  106. if(portStr != null) {
  107. int port = -1;
  108. try {
  109. port = Integer.parseInt(portStr);
  110. if(port < 0) {
  111. argError = "The port must be a valid non-negative integer: '" + args + "'";
  112. } else {
  113. defaultControllerPort = port;
  114. }
  115. } catch(NumberFormatException e) {
  116. argError = "The port must be a valid non-negative integer: '" + arg + "'";
  117. }
  118. }
  119. } else if("--connect".equals(arg) || "-c".equals(arg)) {
  120. connect = true;
  121. } else if("--version".equals(arg)) {
  122. version = true;
  123. } else if ("--gui".equals(arg)) {
  124. gui = true;
  125. } else if(arg.startsWith("--file=") || arg.startsWith("file=")) {
  126. if(file != null) {
  127. argError = "Duplicate argument '--file'.";
  128. break;
  129. }
  130. if(commands != null) {
  131. argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time.";
  132. break;
  133. }
  134. final String fileName = arg.startsWith("--") ? arg.substring(7) : arg.substring(5);
  135. if(!fileName.isEmpty()) {
  136. file = new File(fileName);
  137. if(!file.exists()) {
  138. argError = "File " + file.getAbsolutePath() + " doesn't exist.";
  139. break;
  140. }
  141. } else {
  142. argError = "Argument '--file' is missing value.";
  143. break;
  144. }
  145. } else if(arg.startsWith("--commands=") || arg.startsWith("commands=")) {
  146. if(file != null) {
  147. argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time.";
  148. break;
  149. }
  150. if(commands != null) {
  151. argError = "Duplicate argument '--command'/'--commands'.";
  152. break;
  153. }
  154. final String value = arg.startsWith("--") ? arg.substring(11) : arg.substring(9);
  155. commands = Util.splitCommands(value);
  156. } else if(arg.startsWith("--command=") || arg.startsWith("command=")) {
  157. if(file != null) {
  158. argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time.";
  159. break;
  160. }
  161. if(commands != null) {
  162. argError = "Duplicate argument '--command'/'--commands'.";
  163. break;
  164. }
  165. final String value = arg.startsWith("--") ? arg.substring(10) : arg.substring(8);
  166. commands = Collections.singletonList(value);
  167. } else if (arg.startsWith("--user=")) {
  168. username = arg.startsWith("--") ? arg.substring(7) : arg.substring(5);
  169. } else if (arg.startsWith("--password=")) {
  170. password = (arg.startsWith("--") ? arg.substring(11) : arg.substring(9)).toCharArray();
  171. } else if (arg.startsWith("--timeout=")) {
  172. if (connectionTimeout > 0) {
  173. argError = "Duplicate argument '--timeout'";
  174. break;
  175. }
  176. final String value = arg.substring(10);
  177. try {
  178. connectionTimeout = Integer.parseInt(value);
  179. } catch (final NumberFormatException e) {
  180. //
  181. }
  182. if (connectionTimeout <= 0) {
  183. argError = "The timeout must be a valid positive integer: '" + value + "'";
  184. }
  185. } else if (arg.equals("--help") || arg.equals("-h")) {
  186. commands = Collections.singletonList("help");
  187. } else if (arg.startsWith("--properties=")) {
  188. final String value = arg.substring(13);
  189. final File propertiesFile = new File(value);
  190. if(!propertiesFile.exists()) {
  191. argError = "File doesn't exist: " + propertiesFile.getAbsolutePath();
  192. break;
  193. }
  194. final Properties props = new Properties();
  195. FileInputStream fis = null;
  196. try {
  197. fis = new FileInputStream(propertiesFile);
  198. props.load(fis);
  199. } catch(FileNotFoundException e) {
  200. argError = e.getLocalizedMessage();
  201. break;
  202. } catch(java.io.IOException e) {
  203. argError = "Failed to load properties from " + propertiesFile.getAbsolutePath() + ": " + e.getLocalizedMessage();
  204. break;
  205. } finally {
  206. if(fis != null) {
  207. try {
  208. fis.close();
  209. } catch(java.io.IOException e) {
  210. }
  211. }
  212. }
  213. for(Object prop : props.keySet()) {
  214. WildFlySecurityManager.setPropertyPrivileged((String) prop, (String) props.get(prop));
  215. }
  216. } else if(!(arg.startsWith("-D") || arg.equals("-XX:"))) {// skip system properties and jvm options
  217. // assume it's commands
  218. if(file != null) {
  219. argError = "Only one of '--file', '--commands' or '--command' can appear as the argument at a time: " + arg;
  220. break;
  221. }
  222. if(commands != null) {
  223. argError = "Duplicate argument '--command'/'--commands'.";
  224. break;
  225. }
  226. commands = Util.splitCommands(arg);
  227. }
  228. }
  229. if(argError != null) {
  230. System.err.println(argError);
  231. exitCode = 1;
  232. return;
  233. }
  234. if(version) {
  235. cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, connect, connectionTimeout);
  236. VersionHandler.INSTANCE.handle(cmdCtx);
  237. return;
  238. }
  239. if(file != null) {
  240. cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, connect, connectionTimeout);
  241. processFile(file, cmdCtx);
  242. return;
  243. }
  244. if(commands != null) {
  245. cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, connect, connectionTimeout);
  246. processCommands(commands, cmdCtx);
  247. return;
  248. }
  249. if (gui) {
  250. cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, false, true, connectionTimeout);
  251. processGui(cmdCtx);
  252. return;
  253. }
  254. // Interactive mode
  255. cmdCtx = initCommandContext(defaultControllerProtocol, defaultControllerHost, defaultControllerPort, username, password, true, connect, connectionTimeout);
  256. cmdCtx.interact();
  257. } catch(Throwable t) {
  258. t.printStackTrace();
  259. exitCode = 1;
  260. } finally {
  261. if(cmdCtx != null && cmdCtx.getExitCode() != 0) {
  262. exitCode = cmdCtx.getExitCode();
  263. }
  264. if (!gui) {
  265. System.exit(exitCode);
  266. }
  267. }
  268. System.exit(exitCode);
  269. }
  270. private static CommandContext initCommandContext(String defaultProtocol, String defaultHost, int defaultPort, String username, char[] password, boolean initConsole, boolean connect, final int connectionTimeout) throws CliInitializationException {
  271. final CommandContext cmdCtx = CommandContextFactory.getInstance().newCommandContext(defaultProtocol, defaultHost, defaultPort, username, password, initConsole, connectionTimeout);
  272. if(connect) {
  273. try {
  274. cmdCtx.connectController();
  275. } catch (CommandLineException e) {
  276. throw new CliInitializationException("Failed to connect to the controller", e);
  277. }
  278. }
  279. return cmdCtx;
  280. }
  281. private static void processGui(final CommandContext cmdCtx) {
  282. try {
  283. GuiMain.start(cmdCtx);
  284. } catch(Throwable t) {
  285. t.printStackTrace();
  286. }
  287. }
  288. private static void processCommands(List<String> commands, CommandContext cmdCtx) {
  289. int i = 0;
  290. try {
  291. while (cmdCtx.getExitCode() == 0 && i < commands.size() && !cmdCtx.isTerminated()) {
  292. cmdCtx.handleSafe(commands.get(i));
  293. ++i;
  294. }
  295. } finally {
  296. cmdCtx.terminateSession();
  297. }
  298. }
  299. private static void processFile(File file, final CommandContext cmdCtx) {
  300. BufferedReader reader = null;
  301. try {
  302. reader = new BufferedReader(new FileReader(file));
  303. String line = reader.readLine();
  304. while (cmdCtx.getExitCode() == 0 && !cmdCtx.isTerminated() && line != null) {
  305. cmdCtx.handleSafe(line.trim());
  306. line = reader.readLine();
  307. }
  308. } catch (Throwable e) {
  309. throw new IllegalStateException("Failed to process file '" + file.getAbsolutePath() + "'", e);
  310. } finally {
  311. StreamUtils.safeClose(reader);
  312. cmdCtx.terminateSession();
  313. }
  314. }
  315. }