PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/server/src/main/java/org/jboss/as/server/DomainServerMain.java

https://github.com/smcgowan/wildfly
Java | 232 lines | 163 code | 26 blank | 43 comment | 4 complexity | 5b1238ae5f33299b730c2bfef964979a 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.server;
  23. import java.io.EOFException;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InterruptedIOException;
  27. import java.io.PrintStream;
  28. import java.io.Serializable;
  29. import java.net.InetAddress;
  30. import java.net.InetSocketAddress;
  31. import java.net.UnknownHostException;
  32. import java.util.Arrays;
  33. import org.jboss.as.controller.ModelController;
  34. import org.jboss.as.network.NetworkUtils;
  35. import org.jboss.as.process.protocol.StreamUtils;
  36. import org.jboss.as.protocol.mgmt.ProtocolUtils;
  37. import org.jboss.as.remoting.EndpointService;
  38. import org.jboss.as.remoting.RemotingServices;
  39. import org.jboss.as.remoting.management.ManagementRemotingServices;
  40. import org.jboss.as.server.mgmt.domain.HostControllerServerClient;
  41. import org.jboss.as.server.mgmt.domain.RemoteFileRepository;
  42. import org.jboss.logmanager.Level;
  43. import org.jboss.logmanager.handlers.ConsoleHandler;
  44. import org.jboss.logmanager.log4j.BridgeRepositorySelector;
  45. import org.jboss.marshalling.ByteInput;
  46. import org.jboss.marshalling.MarshallerFactory;
  47. import org.jboss.marshalling.Marshalling;
  48. import org.jboss.marshalling.MarshallingConfiguration;
  49. import org.jboss.marshalling.SimpleClassResolver;
  50. import org.jboss.marshalling.Unmarshaller;
  51. import org.jboss.modules.Module;
  52. import org.jboss.modules.ModuleIdentifier;
  53. import org.jboss.msc.service.ServiceActivator;
  54. import org.jboss.msc.service.ServiceActivatorContext;
  55. import org.jboss.msc.service.ServiceContainer;
  56. import org.jboss.msc.service.ServiceController;
  57. import org.jboss.msc.service.ServiceName;
  58. import org.jboss.msc.service.ServiceTarget;
  59. import org.jboss.remoting3.Endpoint;
  60. import org.jboss.stdio.LoggingOutputStream;
  61. import org.jboss.stdio.NullInputStream;
  62. import org.jboss.stdio.SimpleStdioContextSelector;
  63. import org.jboss.stdio.StdioContext;
  64. import org.jboss.threads.AsyncFuture;
  65. /**
  66. * The main entry point for domain-managed server instances.
  67. *
  68. * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
  69. */
  70. public final class DomainServerMain {
  71. private DomainServerMain() {
  72. }
  73. /**
  74. * Main entry point. Reads and executes the command object from standard input.
  75. *
  76. * @param args ignored
  77. */
  78. public static void main(String[] args) {
  79. SecurityActions.setSystemProperty("log4j.defaultInitOverride", "true");
  80. new BridgeRepositorySelector().start();
  81. final InputStream initialInput = System.in;
  82. final PrintStream initialError = System.err;
  83. // Make sure our original stdio is properly captured.
  84. try {
  85. Class.forName(ConsoleHandler.class.getName(), true, ConsoleHandler.class.getClassLoader());
  86. } catch (Throwable ignored) {
  87. }
  88. // Install JBoss Stdio to avoid any nasty crosstalk.
  89. StdioContext.install();
  90. final StdioContext context = StdioContext.create(
  91. new NullInputStream(),
  92. new LoggingOutputStream(org.jboss.logmanager.Logger.getLogger("stdout"), Level.INFO),
  93. new LoggingOutputStream(org.jboss.logmanager.Logger.getLogger("stderr"), Level.ERROR)
  94. );
  95. StdioContext.setStdioContextSelector(new SimpleStdioContextSelector(context));
  96. final byte[] authKey = new byte[16];
  97. try {
  98. org.jboss.as.process.protocol.StreamUtils.readFully(initialInput, authKey);
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. System.exit(1);
  102. throw new IllegalStateException(); // not reached
  103. }
  104. final MarshallerFactory factory = Marshalling.getMarshallerFactory("river", DomainServerMain.class.getClassLoader());
  105. final Unmarshaller unmarshaller;
  106. final ByteInput byteInput;
  107. final AsyncFuture<ServiceContainer> containerFuture;
  108. try {
  109. Module.registerURLStreamHandlerFactoryModule(Module.getBootModuleLoader().loadModule(ModuleIdentifier.create("org.jboss.vfs")));
  110. final MarshallingConfiguration configuration = new MarshallingConfiguration();
  111. configuration.setVersion(2);
  112. configuration.setClassResolver(new SimpleClassResolver(DomainServerMain.class.getClassLoader()));
  113. unmarshaller = factory.createUnmarshaller(configuration);
  114. byteInput = Marshalling.createByteInput(initialInput);
  115. unmarshaller.start(byteInput);
  116. final ServerTask task = unmarshaller.readObject(ServerTask.class);
  117. unmarshaller.finish();
  118. containerFuture = task.run(Arrays.<ServiceActivator>asList(new ServiceActivator() {
  119. @Override
  120. public void activate(final ServiceActivatorContext serviceActivatorContext) {
  121. // TODO activate host controller client service
  122. }
  123. }));
  124. } catch (Exception e) {
  125. e.printStackTrace(initialError);
  126. System.exit(1);
  127. throw new IllegalStateException(); // not reached
  128. } finally {
  129. }
  130. for (;;) try {
  131. String hostName = StreamUtils.readUTFZBytes(initialInput);
  132. int port = StreamUtils.readInt(initialInput);
  133. // TODO remove managementSubsystemEndpoint ?
  134. // This property does not make sense on reconnect, since there can't be any configuration changes
  135. // while the channel is down. Other changes are either applied to the runtime directly or require a restart.
  136. boolean managementSubsystemEndpoint = StreamUtils.readBoolean(initialInput);
  137. byte[] asAuthKey = new byte[16];
  138. StreamUtils.readFully(initialInput, asAuthKey);
  139. // Get the host-controller server client
  140. final ServiceContainer container = containerFuture.get();
  141. final ServiceController<?> controller = container.getRequiredService(HostControllerServerClient.SERVICE_NAME);
  142. final HostControllerServerClient client = (HostControllerServerClient) controller.getValue();
  143. // Reconnect to the host-controller
  144. client.reconnect(hostName, port, asAuthKey);
  145. } catch (InterruptedIOException e) {
  146. Thread.interrupted();
  147. // ignore
  148. } catch (EOFException e) {
  149. // this means it's time to exit
  150. break;
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. break;
  154. }
  155. // Once the input stream is cut off, shut down
  156. System.exit(0);
  157. throw new IllegalStateException(); // not reached
  158. }
  159. private static void addCommunicationServices(final ServiceTarget serviceTarget, final String serverName, final String serverProcessName, final byte[] authKey,
  160. final InetSocketAddress managementSocket, final boolean managementSubsystemEndpoint, final boolean isReconnect) {
  161. final ServiceName endpointName;
  162. if (!managementSubsystemEndpoint) {
  163. endpointName = ManagementRemotingServices.MANAGEMENT_ENDPOINT;
  164. if (!isReconnect) {
  165. ManagementRemotingServices.installRemotingEndpoint(serviceTarget, ManagementRemotingServices.MANAGEMENT_ENDPOINT,
  166. SecurityActions.getSystemProperty(ServerEnvironment.NODE_NAME), EndpointService.EndpointType.MANAGEMENT, null, null);
  167. }
  168. } else {
  169. endpointName = RemotingServices.SUBSYSTEM_ENDPOINT;
  170. }
  171. try {
  172. final int port = managementSocket.getPort();
  173. final String host = NetworkUtils.formatPossibleIpv6Address(InetAddress.getByName(managementSocket.getHostName()).getHostName());
  174. final HostControllerServerClient client = new HostControllerServerClient(serverName, serverProcessName, host, port, authKey);
  175. serviceTarget.addService(HostControllerServerClient.SERVICE_NAME, client)
  176. .addDependency(endpointName, Endpoint.class, client.getEndpointInjector())
  177. .addDependency(Services.JBOSS_SERVER_CONTROLLER, ModelController.class, client.getServerControllerInjector())
  178. .addDependency(RemoteFileRepository.SERVICE_NAME, RemoteFileRepository.class, client.getRemoteFileRepositoryInjector())
  179. .setInitialMode(ServiceController.Mode.ACTIVE)
  180. .install();
  181. } catch (UnknownHostException e) {
  182. throw new RuntimeException(e);
  183. }
  184. }
  185. public static final class HostControllerCommunicationActivator implements ServiceActivator, Serializable {
  186. private static final long serialVersionUID = -633960958861565102L;
  187. private final InetSocketAddress managementSocket;
  188. private final String serverName;
  189. private final String serverProcessName;
  190. private final byte[] authKey;
  191. private final boolean managementSubsystemEndpoint;
  192. public HostControllerCommunicationActivator(final InetSocketAddress managementSocket, final String serverName, final String serverProcessName, final byte[] authKey, final boolean managementSubsystemEndpoint) {
  193. this.managementSocket = managementSocket;
  194. this.serverName = serverName;
  195. this.serverProcessName = serverProcessName;
  196. this.authKey = authKey;
  197. this.managementSubsystemEndpoint = managementSubsystemEndpoint;
  198. }
  199. @Override
  200. public void activate(final ServiceActivatorContext serviceActivatorContext) {
  201. final ServiceTarget serviceTarget = serviceActivatorContext.getServiceTarget();
  202. // TODO - Correct the authKey propagation.
  203. addCommunicationServices(serviceTarget, serverName, serverProcessName, authKey, managementSocket, managementSubsystemEndpoint, false);
  204. }
  205. }
  206. }