PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/process-controller/src/main/java/org/jboss/as/process/Main.java

https://github.com/aslakknutsen/jboss-as
Java | 309 lines | 233 code | 38 blank | 38 comment | 74 complexity | 59cdf5f6e7e8756c27bb724b7fa75b78 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, 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.process;
  23. import static org.jboss.as.process.ProcessMessages.MESSAGES;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.net.InetSocketAddress;
  27. import java.security.AccessController;
  28. import java.util.ArrayList;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.concurrent.Executors;
  32. import java.util.concurrent.ThreadFactory;
  33. import java.util.logging.Handler;
  34. import java.util.logging.Logger;
  35. import javax.net.ServerSocketFactory;
  36. import org.jboss.as.process.protocol.ProtocolServer;
  37. import org.jboss.as.version.ProductConfig;
  38. import org.jboss.as.version.Version;
  39. import org.jboss.logging.MDC;
  40. import org.jboss.logmanager.handlers.ConsoleHandler;
  41. import org.jboss.modules.Module;
  42. import org.jboss.threads.JBossThreadFactory;
  43. /**
  44. * The main entry point for the process controller.
  45. *
  46. * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
  47. */
  48. public final class Main {
  49. public static String getVersionString() {
  50. return Version.AS_VERSION;
  51. }
  52. public static void usage() {
  53. CommandLineArgument.printUsage(System.out);
  54. }
  55. private Main() {
  56. }
  57. public static final String HOST_CONTROLLER_PROCESS_NAME = "Host Controller";
  58. public static final String HOST_CONTROLLER_MODULE = "org.jboss.as.host-controller";
  59. public static void main(String[] args) throws IOException {
  60. start(args);
  61. }
  62. public static ProcessController start(String[] args) throws IOException {
  63. MDC.put("process", "process controller");
  64. String javaHome = SecurityActions.getSystemProperty("java.home", ".");
  65. String jvmName = javaHome + "/bin/java";
  66. String jbossHome = SecurityActions.getSystemProperty("jboss.home.dir", ".");
  67. String modulePath = null;
  68. String bootJar = null;
  69. String jaxpModule = "javax.xml.jaxp-provider";
  70. String bootModule = HOST_CONTROLLER_MODULE;
  71. final PCSocketConfig pcSocketConfig = new PCSocketConfig();
  72. String currentWorkingDir = SecurityActions.getSystemProperty("user.dir");
  73. final List<String> javaOptions = new ArrayList<String>();
  74. final List<String> smOptions = new ArrayList<String>();
  75. // target module is always SM
  76. // -mp is my module path
  77. // -jar is jboss-modules.jar in jboss-home
  78. // log config should be fixed loc
  79. OUT: for (int i = 0; i < args.length; i++) {
  80. String arg = args[i];
  81. if ("-jvm".equals(arg)) {
  82. jvmName = args[++i];
  83. } else if ("-jboss-home".equals(arg)) {
  84. jbossHome = args[++i];
  85. } else if ("-mp".equals(arg)) {
  86. modulePath = args[++i];
  87. } else if ("-jar".equals(arg)) {
  88. bootJar = args[++i];
  89. } else if ("-jaxpmodule".equals(arg)) {
  90. jaxpModule = args[++i];
  91. } else if ("--".equals(arg)) {
  92. for (i++; i < args.length; i++) {
  93. arg = args[i];
  94. if ("--".equals(arg)) {
  95. for (i++; i < args.length; i++) {
  96. arg = args[i];
  97. if (CommandLineConstants.HELP.equals(arg) || CommandLineConstants.SHORT_HELP.equals(arg)
  98. || CommandLineConstants.OLD_HELP.equals(arg)) {
  99. usage();
  100. return null;
  101. } else if (CommandLineConstants.VERSION.equals(arg) || CommandLineConstants.SHORT_VERSION.equals(arg)
  102. || CommandLineConstants.OLD_VERSION.equals(arg) || CommandLineConstants.OLD_SHORT_VERSION.equals(arg)) {
  103. System.out.println(new ProductConfig(Module.getBootModuleLoader(), jbossHome).getPrettyVersionString());
  104. return null;
  105. } else if (pcSocketConfig.processPCSocketConfigArgument(arg, args, i)) {
  106. if (pcSocketConfig.isParseFailed()) {
  107. return null;
  108. }
  109. i += pcSocketConfig.getArgIncrement();
  110. } else {
  111. smOptions.add(arg);
  112. }
  113. }
  114. break OUT;
  115. } else if (pcSocketConfig.processPCSocketConfigArgument(arg, args, i)) {
  116. // This would normally come in via the nested if ("--".equals(arg)) case above, but in case someone tweaks the
  117. // script to set it directly, we've handled it
  118. if (pcSocketConfig.isParseFailed()) {
  119. return null;
  120. }
  121. i += pcSocketConfig.getArgIncrement();
  122. } else {
  123. javaOptions.add(arg);
  124. }
  125. }
  126. break OUT;
  127. } else if (pcSocketConfig.processPCSocketConfigArgument(arg, args, i)) {
  128. // This would normally come in via the if ("--".equals(arg)) cases above, but in case someone tweaks the
  129. // script to set it directly, we've handled it
  130. if (pcSocketConfig.isParseFailed()) {
  131. return null;
  132. }
  133. i += pcSocketConfig.getArgIncrement();
  134. } else {
  135. throw MESSAGES.invalidOption(arg);
  136. }
  137. }
  138. if (modulePath == null) {
  139. // if "-mp" (i.e. module path) wasn't part of the command line args, then check the system property.
  140. // if system property not set, then default to JBOSS_HOME/modules
  141. modulePath = SecurityActions.getSystemProperty("jboss.module.path", jbossHome + File.separator + "modules");
  142. }
  143. if (bootJar == null) {
  144. // if "-jar" wasn't part of the command line args, then default to JBOSS_HOME/jboss-modules.jar
  145. bootJar = jbossHome + File.separator + "jboss-modules.jar";
  146. }
  147. Handler consoleHandler = null;
  148. final Logger rootLogger = Logger.getLogger("");
  149. for (Handler handler : rootLogger.getHandlers()) {
  150. if (handler instanceof ConsoleHandler) {
  151. if (consoleHandler != null) {
  152. // duplicate handlers
  153. rootLogger.removeHandler(handler);
  154. } else {
  155. consoleHandler = handler;
  156. ((ConsoleHandler)consoleHandler).setWriter(new SynchronizedWriter(System.out));
  157. }
  158. }
  159. }
  160. final ProtocolServer.Configuration configuration = new ProtocolServer.Configuration();
  161. configuration.setBindAddress(new InetSocketAddress(pcSocketConfig.getBindAddress(), pcSocketConfig.getBindPort()));
  162. configuration.setSocketFactory(ServerSocketFactory.getDefault());
  163. final ThreadFactory threadFactory = new JBossThreadFactory(new ThreadGroup("ProcessController-threads"), Boolean.FALSE, null, "%G - %t", null, null, AccessController.getContext());
  164. configuration.setThreadFactory(threadFactory);
  165. configuration.setReadExecutor(Executors.newCachedThreadPool(threadFactory));
  166. final ProcessController processController = new ProcessController(configuration, System.out, System.err);
  167. final InetSocketAddress boundAddress = processController.getServer().getBoundAddress();
  168. final List<String> initialCommand = new ArrayList<String>();
  169. initialCommand.add(jvmName);
  170. initialCommand.add("-D[" + HOST_CONTROLLER_PROCESS_NAME + "]");
  171. initialCommand.addAll(javaOptions);
  172. initialCommand.add("-jar");
  173. initialCommand.add(bootJar);
  174. initialCommand.add("-mp");
  175. initialCommand.add(modulePath);
  176. initialCommand.add("-jaxpmodule");
  177. initialCommand.add(jaxpModule);
  178. initialCommand.add(bootModule);
  179. initialCommand.add(CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR);
  180. initialCommand.add(boundAddress.getHostName());
  181. initialCommand.add(CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT);
  182. initialCommand.add(Integer.toString(boundAddress.getPort()));
  183. initialCommand.addAll(smOptions);
  184. initialCommand.add("-D" + "jboss.home.dir=" + jbossHome);
  185. processController.addProcess(HOST_CONTROLLER_PROCESS_NAME, initialCommand, Collections.<String, String>emptyMap(), currentWorkingDir, true, true);
  186. processController.startProcess(HOST_CONTROLLER_PROCESS_NAME);
  187. final Thread shutdownThread = new Thread(new Runnable() {
  188. public void run() {
  189. processController.shutdown();
  190. }
  191. }, "Shutdown thread");
  192. shutdownThread.setDaemon(false);
  193. Runtime.getRuntime().addShutdownHook(shutdownThread);
  194. return processController;
  195. }
  196. private static String parseValue(final String arg, final String key) {
  197. String value = null;
  198. int splitPos = key.length();
  199. if (arg.length() <= splitPos + 1 || arg.charAt(splitPos) != '=') {
  200. System.out.println(MESSAGES.noArgValue(key));
  201. usage();
  202. } else {
  203. value = arg.substring(splitPos + 1);
  204. }
  205. return value;
  206. }
  207. private static class PCSocketConfig {
  208. private String bindAddress;
  209. private int bindPort = 0;
  210. private int argIncrement = 0;
  211. private boolean parseFailed;
  212. private PCSocketConfig() {
  213. boolean preferIPv6 = Boolean.valueOf(SecurityActions.getSystemProperty("java.net.preferIPv6Addresses", "false"));
  214. bindAddress = preferIPv6 ? "::1" : "127.0.0.1";
  215. }
  216. private String getBindAddress() {
  217. return bindAddress;
  218. }
  219. private int getBindPort() {
  220. return bindPort;
  221. }
  222. private int getArgIncrement() {
  223. return argIncrement;
  224. }
  225. private boolean isParseFailed() {
  226. return parseFailed;
  227. }
  228. private boolean processPCSocketConfigArgument(final String arg, final String[] args, final int index) {
  229. boolean isPCSocketArg = true;
  230. argIncrement = 0;
  231. if (CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR.equals(arg) || CommandLineConstants.OLD_PROCESS_CONTROLLER_BIND_ADDR.equals(arg)) {
  232. bindAddress = args[index +1];
  233. argIncrement = 1;
  234. } else if (arg.startsWith(CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR)) {
  235. String addr = parseValue(arg, CommandLineConstants.PROCESS_CONTROLLER_BIND_ADDR);
  236. if (addr == null) {
  237. parseFailed = true;
  238. } else {
  239. bindAddress = addr;
  240. }
  241. } else if (arg.startsWith(CommandLineConstants.OLD_PROCESS_CONTROLLER_BIND_ADDR)) {
  242. String addr = parseValue(arg, CommandLineConstants.OLD_PROCESS_CONTROLLER_BIND_ADDR);
  243. if (addr == null) {
  244. parseFailed = true;
  245. } else {
  246. bindAddress = addr;
  247. }
  248. } else if (CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT.equals(arg) || CommandLineConstants.OLD_PROCESS_CONROLLER_BIND_PORT.equals(arg)) {
  249. bindPort = Integer.parseInt(args[index + 1]);
  250. argIncrement = 1;
  251. } else if (arg.startsWith(CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT)) {
  252. String port = parseValue(arg, CommandLineConstants.PROCESS_CONTROLLER_BIND_PORT);
  253. if (port == null) {
  254. parseFailed = true;
  255. } else {
  256. bindPort = Integer.parseInt(port);
  257. }
  258. } else if (arg.startsWith(CommandLineConstants.OLD_PROCESS_CONROLLER_BIND_PORT)) {
  259. String port = parseValue(arg, CommandLineConstants.OLD_PROCESS_CONROLLER_BIND_PORT);
  260. if (port == null) {
  261. parseFailed = true;
  262. } else {
  263. bindPort = Integer.parseInt(port);
  264. }
  265. } else {
  266. isPCSocketArg = false;
  267. }
  268. return isPCSocketArg;
  269. }
  270. }
  271. }