/host-controller/src/main/java/org/jboss/as/host/controller/HostControllerEnvironment.java

https://github.com/luksa/wildfly · Java · 425 lines · 231 code · 63 blank · 131 comment · 42 complexity · 0a386c9874d050c0bf0ff490995ce965 MD5 · raw file

  1. /**
  2. *
  3. */
  4. package org.jboss.as.host.controller;
  5. import java.io.File;
  6. import java.io.InputStream;
  7. import java.io.PrintStream;
  8. import java.net.InetAddress;
  9. import java.util.Collections;
  10. import java.util.Map;
  11. import org.jboss.as.controller.persistence.ConfigurationFile;
  12. import org.jboss.as.process.DefaultJvmUtils;
  13. /**
  14. * Encapsulates the runtime environment for a host controller.
  15. *
  16. * @author Brian Stansberry
  17. */
  18. public class HostControllerEnvironment {
  19. /////////////////////////////////////////////////////////////////////////
  20. // Configuration Value Identifiers //
  21. /////////////////////////////////////////////////////////////////////////
  22. /**
  23. * Constant that holds the name of the environment property
  24. * for specifying the home directory for JBoss.
  25. */
  26. public static final String HOME_DIR = "jboss.home.dir";
  27. /**
  28. * Constant that holds the name of the environment property
  29. * for specifying the directory from which JBoss will read modules.
  30. *
  31. * <p>Defaults to <tt><em>HOME_DIR</em>/modules</tt>/
  32. */
  33. public static final String MODULES_DIR = "jboss.modules.dir";
  34. /**
  35. * Constant that holds the name of the environment property
  36. * for specifying the base directory for domain content.
  37. *
  38. * <p>Defaults to <tt><em>HOME_DIR</em>/domain</tt>.
  39. */
  40. public static final String DOMAIN_BASE_DIR = "jboss.domain.base.dir";
  41. /**
  42. * Constant that holds the name of the environment property
  43. * for specifying the server configuration URL.
  44. *
  45. * <p>Defaults to <tt><em>DOMAIN_BASE_DIR</em>/configuration</tt> .
  46. */
  47. public static final String DOMAIN_CONFIG_DIR = "jboss.domain.config.dir";
  48. /**
  49. * Constant that holds the name of the environment property
  50. * for specifying the directory which JBoss will use for
  51. * persistent data file storage.
  52. *
  53. * <p>Defaults to <tt><em>DOMAIN_BASE_DIR</em>/data</tt>.
  54. */
  55. public static final String DOMAIN_DATA_DIR = "jboss.domain.data.dir";
  56. /**
  57. * Constant that holds the name of the environment property
  58. * for specifying the domain deployment URL.
  59. *
  60. * <p>Defaults to <tt><em>DOMAIN_BASE_DIR</em>/content</tt> .
  61. */
  62. public static final String DOMAIN_DEPLOYMENT_DIR = "jboss.domain.deployment.dir";
  63. /**
  64. * Constant that holds the name of the environment property
  65. * for specifying the domain log directory for JBoss.
  66. *
  67. * <p>Defaults to <tt><em>DOMAIN_BASE_DIR</em>/<em>log</em></tt>.
  68. */
  69. public static final String DOMAIN_LOG_DIR = "jboss.domain.log.dir";
  70. /**
  71. * Constant that holds the name of the environment property
  72. * for specifying the server home directory for JBoss.
  73. *
  74. * <p>Defaults to <tt><em>DOMAIN_BASE_DIR</em>/<em>servers</em></tt>.
  75. */
  76. public static final String DOMAIN_SERVERS_DIR = "jboss.domain.servers.dir";
  77. /**
  78. * Constant that holds the name of the environment property
  79. * for specifying the directory which JBoss will use for
  80. * temporary file storage.
  81. *
  82. * <p>Defaults to <tt><em>DOMAIN_BASE_DIR</em>/tmp</tt> .
  83. */
  84. public static final String DOMAIN_TEMP_DIR = "jboss.domain.temp.dir";
  85. private final Map<String, String> hostSystemProperties;
  86. private final String processName;
  87. private final InetAddress processControllerAddress;
  88. private final Integer processControllerPort;
  89. private final InetAddress hostControllerAddress;
  90. private final Integer hostControllerPort;
  91. private final File homeDir;
  92. private final File modulesDir;
  93. private final File domainBaseDir;
  94. private final File domainConfigurationDir;
  95. private final ConfigurationFile hostConfigurationFile;
  96. private final ConfigurationFile domainConfigurationFile;
  97. private final File domainDeploymentDir;
  98. private final File domainDataDir;
  99. private final File domainLogDir;
  100. private final File domainServersDir;
  101. private final File domainTempDir;
  102. private final File defaultJVM;
  103. private final boolean isRestart;
  104. private final boolean backupDomainFiles;
  105. private final boolean useCachedDc;
  106. private final InputStream stdin;
  107. private final PrintStream stdout;
  108. private final PrintStream stderr;
  109. public HostControllerEnvironment(Map<String, String> hostSystemProperties, boolean isRestart, InputStream stdin, PrintStream stdout, PrintStream stderr,
  110. String processName, InetAddress processControllerAddress, Integer processControllerPort, InetAddress hostControllerAddress,
  111. Integer hostControllerPort, String defaultJVM, String domainConfig, String hostConfig, boolean backupDomainFiles, boolean useCachedDc) {
  112. if (hostSystemProperties == null) {
  113. throw new IllegalArgumentException("hostSystemProperties is null");
  114. }
  115. this.hostSystemProperties = Collections.unmodifiableMap(hostSystemProperties);
  116. if (stdin == null) {
  117. throw new IllegalArgumentException("stdin is null");
  118. }
  119. this.stdin = stdin;
  120. if (stdout == null) {
  121. throw new IllegalArgumentException("stdout is null");
  122. }
  123. this.stdout = stdout;
  124. if (stderr == null) {
  125. throw new IllegalArgumentException("stderr is null");
  126. }
  127. this.stderr = stderr;
  128. if (processName == null) {
  129. throw new IllegalArgumentException("processName is null");
  130. }
  131. if (processControllerAddress == null) {
  132. throw new IllegalArgumentException("processControllerAddress is null");
  133. }
  134. if (processControllerPort == null) {
  135. throw new IllegalArgumentException("processControllerPort is null");
  136. }
  137. if (hostControllerAddress == null) {
  138. throw new IllegalArgumentException("hostControllerAddress is null");
  139. }
  140. if (hostControllerPort == null) {
  141. throw new IllegalArgumentException("hostControllerPort is null");
  142. }
  143. this.processName = processName;
  144. this.processControllerPort = processControllerPort;
  145. this.processControllerAddress = processControllerAddress;
  146. this.hostControllerAddress = hostControllerAddress;
  147. this.hostControllerPort = hostControllerPort;
  148. this.isRestart = isRestart;
  149. File home = getFileFromProperty(HOME_DIR);
  150. if (home == null) {
  151. home = new File(System.getProperty("user.dir"));
  152. }
  153. this.homeDir = home;
  154. System.setProperty(HOME_DIR, homeDir.getAbsolutePath());
  155. File tmp = getFileFromProperty(MODULES_DIR);
  156. if (tmp == null) {
  157. tmp = new File(this.homeDir, "modules");
  158. }
  159. this.modulesDir = tmp;
  160. System.setProperty(MODULES_DIR, this.modulesDir.getAbsolutePath());
  161. tmp = getFileFromProperty(DOMAIN_BASE_DIR);
  162. if (tmp == null) {
  163. tmp = new File(this.homeDir, "domain");
  164. }
  165. this.domainBaseDir = tmp;
  166. System.setProperty(DOMAIN_BASE_DIR, this.domainBaseDir.getAbsolutePath());
  167. tmp = getFileFromProperty(DOMAIN_CONFIG_DIR);
  168. if (tmp == null) {
  169. tmp = new File(this.domainBaseDir, "configuration");
  170. }
  171. this.domainConfigurationDir = tmp;
  172. System.setProperty(DOMAIN_CONFIG_DIR, this.domainConfigurationDir.getAbsolutePath());
  173. hostConfigurationFile = new ConfigurationFile(domainConfigurationDir, "host.xml", hostConfig);
  174. domainConfigurationFile = new ConfigurationFile(domainConfigurationDir, "domain.xml", domainConfig);
  175. tmp = getFileFromProperty(DOMAIN_DEPLOYMENT_DIR);
  176. if (tmp == null) {
  177. tmp = new File(this.domainBaseDir, "content");
  178. }
  179. this.domainDeploymentDir = tmp;
  180. System.setProperty(DOMAIN_DEPLOYMENT_DIR, this.domainDeploymentDir.getAbsolutePath());
  181. tmp = getFileFromProperty(DOMAIN_DATA_DIR);
  182. if (tmp == null) {
  183. tmp = new File(this.domainBaseDir, "data");
  184. }
  185. this.domainDataDir = tmp;
  186. System.setProperty(DOMAIN_DATA_DIR, this.domainDataDir.getAbsolutePath());
  187. tmp = getFileFromProperty(DOMAIN_LOG_DIR);
  188. if (tmp == null) {
  189. tmp = new File(this.domainBaseDir, "log");
  190. }
  191. this.domainLogDir = tmp;
  192. System.setProperty(DOMAIN_LOG_DIR, this.domainLogDir.getAbsolutePath());
  193. tmp = getFileFromProperty(DOMAIN_SERVERS_DIR);
  194. if (tmp == null) {
  195. tmp = new File(this.domainBaseDir, "servers");
  196. }
  197. this.domainServersDir = tmp;
  198. System.setProperty(DOMAIN_SERVERS_DIR, this.domainServersDir.getAbsolutePath());
  199. tmp = getFileFromProperty(DOMAIN_TEMP_DIR);
  200. if (tmp == null) {
  201. tmp = new File(this.domainBaseDir, "tmp");
  202. }
  203. this.domainTempDir = tmp;
  204. System.setProperty(DOMAIN_TEMP_DIR, this.domainTempDir.getAbsolutePath());
  205. if(defaultJVM != null) {
  206. if (defaultJVM.equals("java")) {
  207. defaultJVM = DefaultJvmUtils.findJavaExecutable(DefaultJvmUtils.getCurrentJvmHome());
  208. }
  209. this.defaultJVM = new File(defaultJVM);
  210. } else {
  211. this.defaultJVM = null;
  212. }
  213. this.backupDomainFiles = backupDomainFiles;
  214. this.useCachedDc = useCachedDc;
  215. }
  216. /**
  217. * Gets the original System.in for this process. This should only
  218. * be used for communication with the process controller that spawned this process.
  219. *
  220. * @return stdin
  221. */
  222. public InputStream getStdin() {
  223. return stdin;
  224. }
  225. /**
  226. * Gets the original System.out for this process. This should only
  227. * be used for communication with the process controller that spawned this process.
  228. *
  229. * @return stdout
  230. */
  231. public PrintStream getStdout() {
  232. return stdout;
  233. }
  234. /**
  235. * Gets the original System.err for this process. This should only
  236. * be used for communication with the process controller that spawned this process.
  237. *
  238. * @return stderr
  239. */
  240. public PrintStream getStderr() {
  241. return stderr;
  242. }
  243. /**
  244. * Get the process name of this process, needed to inform the process controller we have started
  245. *
  246. * @return the process name
  247. */
  248. public String getProcessName() {
  249. return processName;
  250. }
  251. /**
  252. * Gets the address the process controller passed to this process
  253. * to use in communicating with it.
  254. *
  255. * @return the process controller's address
  256. */
  257. public InetAddress getProcessControllerAddress() {
  258. return processControllerAddress;
  259. }
  260. /**
  261. * Gets the port number the process controller passed to this process
  262. * to use in communicating with it.
  263. *
  264. * @return the process controller's port
  265. */
  266. public Integer getProcessControllerPort() {
  267. return processControllerPort;
  268. }
  269. /**
  270. * Gets the address the process controller told us to listen for communication from the servers.
  271. *
  272. * @return the host controller's address
  273. */
  274. public InetAddress getHostControllerAddress() {
  275. return hostControllerAddress;
  276. }
  277. /**
  278. * Gets the port the process controller told us to listen for communication from the servers.
  279. *
  280. * @return the host controller's port
  281. */
  282. public Integer getHostControllerPort() {
  283. return hostControllerPort;
  284. }
  285. /**
  286. * Gets whether this was a restarted host controller.
  287. *
  288. * @return if it was restarted
  289. */
  290. public boolean isRestart() {
  291. return isRestart;
  292. }
  293. /**
  294. * Whether we should grab a copy of the master Domain Controller's files on startup.
  295. * This only has an effect if we are in slave mode
  296. *
  297. * @return <code>true</code> if we should grab the files
  298. */
  299. public boolean isBackupDomainFiles() {
  300. return backupDomainFiles;
  301. }
  302. /**
  303. * Whether we should try to start up with our copy of the domain controller.
  304. * This only has an effect if we are in slave mode
  305. *
  306. * @return <code>true</code> if we should grab the files
  307. */
  308. public boolean isUseCachedDc() {
  309. return useCachedDc;
  310. }
  311. public File getHomeDir() {
  312. return homeDir;
  313. }
  314. public File getModulesDir() {
  315. return modulesDir;
  316. }
  317. public File getDomainBaseDir() {
  318. return domainBaseDir;
  319. }
  320. public File getDomainConfigurationDir() {
  321. return domainConfigurationDir;
  322. }
  323. public File getDomainDeploymentDir() {
  324. return domainDeploymentDir;
  325. }
  326. public File getDomainDataDir() {
  327. return domainDataDir;
  328. }
  329. public File getDomainLogDir() {
  330. return domainLogDir;
  331. }
  332. public File getDomainServersDir() {
  333. return domainServersDir;
  334. }
  335. public File getDomainTempDir() {
  336. return domainTempDir;
  337. }
  338. public File getDefaultJVM() {
  339. return defaultJVM;
  340. }
  341. public ConfigurationFile getHostConfigurationFile() {
  342. return hostConfigurationFile;
  343. }
  344. public ConfigurationFile getDomainConfigurationFile() {
  345. return domainConfigurationFile;
  346. }
  347. public Map<String, String> getHostSystemProperties() {
  348. return hostSystemProperties;
  349. }
  350. /**
  351. * Get a File from configuration.
  352. * @return the CanonicalFile form for the given name.
  353. */
  354. private File getFileFromProperty(final String name) {
  355. String value = hostSystemProperties.get(name);
  356. if (value != null) {
  357. File f = new File(value);
  358. return f;
  359. }
  360. return null;
  361. }
  362. }