-
Notifications
You must be signed in to change notification settings - Fork 97
/
ServerEnvironment.java
586 lines (505 loc) · 20.6 KB
/
ServerEnvironment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.server;
import java.io.File;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import org.jboss.as.controller.ControllerMessages;
import org.jboss.as.controller.ProcessType;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.persistence.ConfigurationFile;
import org.jboss.as.version.ProductConfig;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.modules.ModuleLoader;
/**
* Encapsulates the runtime environment for a server.
*
* @author Brian Stansberry
* @author Mike M. Clark
*/
public class ServerEnvironment implements Serializable {
private static final long serialVersionUID = -349976376447122910L;
public static enum LaunchType {
DOMAIN,
STANDALONE,
EMBEDDED,
APPCLIENT;
public ProcessType getProcessType() {
switch (this) {
case DOMAIN:
return ProcessType.DOMAIN_SERVER;
case STANDALONE:
return ProcessType.STANDALONE_SERVER;
case EMBEDDED:
return ProcessType.EMBEDDED_SERVER;
case APPCLIENT:
return ProcessType.APPLICATION_CLIENT;
default:
// programming error
throw new RuntimeException("unimplemented LaunchType");
}
}
}
// ///////////////////////////////////////////////////////////////////////
// Configuration Value Identifiers //
// ///////////////////////////////////////////////////////////////////////
/**
* Constant that holds the name of the system property for specifying the JDK extension directory paths.
*/
public static final String JAVA_EXT_DIRS = "java.ext.dirs";
/**
* Constant that holds the name of the environment property for specifying the home directory for JBoss.
*/
public static final String HOME_DIR = "jboss.home.dir";
/**
* Constant that holds the name of the environment property for specifying the directory from which JBoss will read modules.
*
* <p>
* Defaults to <tt><em>HOME_DIR</em>/modules</tt>/
*/
public static final String MODULES_DIR = "jboss.modules.dir";
/**
* Constant that holds the name of the environment property for specifying the directory from which JBoss will read OSGi bundles.
*
* <p>
* Defaults to <tt><em>HOME_DIR</em>/bundles</tt>/
*/
public static final String BUNDLES_DIR = "jboss.bundles.dir";
/**
* VFS module identifier
*/
public static final String VFS_MODULE_IDENTIFIER = "org.jboss.vfs";
/**
* Constant that holds the name of the environment property for specifying the base directory for server content.
*
* <p>
* Defaults to <tt><em>HOME_DIR</em>/standalone</tt>.
*/
public static final String SERVER_BASE_DIR = "jboss.server.base.dir";
/**
* Constant that holds the name of the environment property for specifying the server configuration URL.
*
* <p>
* Defaults to <tt><em>SERVER_BASE_DIR</em>/configuration</tt> .
*/
public static final String SERVER_CONFIG_DIR = "jboss.server.config.dir";
/**
* Constant that holds the name of the environment property for specifying the directory which JBoss will use for persistent
* data file storage.
*
* <p>
* Defaults to <tt><em>SERVER_BASE_DIR</em>/data</tt>.
*/
public static final String SERVER_DATA_DIR = "jboss.server.data.dir";
/**
* Constant that holds the name of the environment property for specifying the directory which JBoss will use for
* deployments.
*
* <p>
* Defaults to <tt><em>SERVER_DATA_DIR</em>/content</tt>.
*/
public static final String SERVER_DEPLOY_DIR = "jboss.server.deploy.dir";
/**
* Constant that holds the name of the environment property for specifying the server log directory for JBoss.
*
* <p>
* Defaults to <tt><em>SERVER_BASE_DIR</em>/<em>log</em></tt>.
*/
public static final String SERVER_LOG_DIR = "jboss.server.log.dir";
/**
* Constant that holds the name of the environment property for specifying the directory which JBoss will use for temporary
* file storage.
*
* <p>
* Defaults to <tt><em>SERVER_BASE_DIR</em>/tmp</tt> .
*/
public static final String SERVER_TEMP_DIR = "jboss.server.temp.dir";
/**
* Common alias between domain and standalone mode. Uses jboss.domain.temp.dir on domain mode,
* and jboss.server.temp.dir on standalone server mode.
*/
public static final String CONTROLLER_TEMP_DIR = "jboss.controller.temp.dir";
/**
* Constant that holds the name of the system property for specifying the node name within a cluster.
*/
public static final String NODE_NAME = "jboss.node.name";
/**
* Constant that holds the name of the system property for specifying the name of this server instance.
*/
public static final String SERVER_NAME = "jboss.server.name";
/**
* Constant that holds the name of the system property for specifying the local part of the name of the host that this
* server is running on.
*/
public static final String HOST_NAME = "jboss.host.name";
/**
* Constant that holds the name of the system property for specifying the fully-qualified name of the host that this server
* is running on.
*/
public static final String QUALIFIED_HOST_NAME = "jboss.qualified.host.name";
/**
* Constant that holds the name of the system property for specifying the max threads used by the bootstrap ServiceContainer.
*/
public static final String BOOTSTRAP_MAX_THREADS = "org.jboss.server.bootstrap.maxThreads";
/**
* The default system property used to store bind address information from the command-line (-b).
*/
public static final String JBOSS_BIND_ADDRESS = "jboss.bind.address";
/**
* Prefix for the system property used to store qualified bind address information from the command-line (-bxxx).
*/
public static final String JBOSS_BIND_ADDRESS_PREFIX = JBOSS_BIND_ADDRESS + ".";
/**
* The default system property used to store bind address information from the command-line (-b).
*/
public static final String JBOSS_DEFAULT_MULTICAST_ADDRESS = "jboss.default.multicast.address";
private final LaunchType launchType;
private final String qualifiedHostName;
private final String hostName;
private final String hostControllerName;
private final String serverName;
private final String nodeName;
private final File[] javaExtDirs;
private final File homeDir;
private final File modulesDir;
private final File bundlesDir;
private final File serverBaseDir;
private final File serverConfigurationDir;
private final ConfigurationFile serverConfigurationFile;
private final File serverDataDir;
private final File serverDeployDir;
private final File serverLogDir;
private final File serverTempDir;
private final File controllerTempDir;
private final boolean standalone;
private final boolean allowModelControllerExecutor;
private final RunningMode initialRunningMode;
private final ProductConfig productConfig;
public ServerEnvironment(final String hostControllerName, final Properties props, final Map<String, String> env, final String serverConfig,
final LaunchType launchType, final RunningMode initialRunningMode, ProductConfig productConfig) {
if (props == null) {
throw ControllerMessages.MESSAGES.nullVar("props");
}
this.launchType = launchType;
this.standalone = launchType != LaunchType.DOMAIN;
this.initialRunningMode = initialRunningMode == null ? RunningMode.NORMAL : initialRunningMode;
this.hostControllerName = hostControllerName;
if (standalone && hostControllerName != null) {
throw ServerMessages.MESSAGES.hostControllerNameNonNullInStandalone();
}
if (!standalone && hostControllerName == null) {
throw ServerMessages.MESSAGES.hostControllerNameNullInDomain();
}
// Calculate host and default server name
String hostName = props.getProperty(HOST_NAME);
String qualifiedHostName = props.getProperty(QUALIFIED_HOST_NAME);
if (qualifiedHostName == null) {
// if host name is specified, don't pick a qualified host name that isn't related to it
qualifiedHostName = hostName;
if (qualifiedHostName == null) {
// POSIX-like OSes including Mac should have this set
qualifiedHostName = env.get("HOSTNAME");
}
if (qualifiedHostName == null) {
// Certain versions of Windows
qualifiedHostName = env.get("COMPUTERNAME");
}
if (qualifiedHostName == null) {
try {
qualifiedHostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
qualifiedHostName = null;
}
}
if (qualifiedHostName != null && qualifiedHostName.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$|:")) {
// IP address is not acceptable
qualifiedHostName = null;
}
if (qualifiedHostName == null) {
// Give up
qualifiedHostName = "unknown-host.unknown-domain";
}
qualifiedHostName = qualifiedHostName.trim().toLowerCase();
}
this.qualifiedHostName = qualifiedHostName;
if (hostName == null) {
// Use the host part of the qualified host name
final int idx = qualifiedHostName.indexOf('.');
hostName = idx == -1 ? qualifiedHostName : qualifiedHostName.substring(0, idx);
}
this.hostName = hostName;
// Set up the server name for management purposes
String serverName = props.getProperty(SERVER_NAME);
if (serverName == null) {
serverName = hostName;
}
this.serverName = serverName;
// Set up the clustering node name
String nodeName = props.getProperty(NODE_NAME);
if (nodeName == null) {
nodeName = serverName;
}
this.nodeName = nodeName;
// Java system-wide extension dirs
javaExtDirs = getFilesFromProperty(JAVA_EXT_DIRS, props);
// Must have HOME_DIR
homeDir = getFileFromProperty(HOME_DIR, props);
if (homeDir == null)
throw ServerMessages.MESSAGES.missingHomeDirConfiguration(HOME_DIR);
File tmp = getFileFromProperty(MODULES_DIR, props);
if (tmp == null) {
tmp = new File(homeDir, "modules");
}
modulesDir = tmp;
tmp = getFileFromProperty(BUNDLES_DIR, props);
if (tmp == null) {
tmp = new File(homeDir, "bundles");
}
bundlesDir = tmp;
tmp = getFileFromProperty(SERVER_BASE_DIR, props);
if (tmp == null) {
tmp = new File(homeDir, standalone ? "standalone" : "domain/servers/" + serverName);
}
serverBaseDir = tmp;
tmp = getFileFromProperty(SERVER_CONFIG_DIR, props);
if (tmp == null) {
tmp = new File(serverBaseDir, "configuration");
}
serverConfigurationDir = tmp;
serverConfigurationFile = standalone ? new ConfigurationFile(serverConfigurationDir, "standalone.xml", serverConfig) : null;
tmp = getFileFromProperty(SERVER_DATA_DIR, props);
if (tmp == null) {
tmp = new File(serverBaseDir, "data");
}
serverDataDir = tmp;
tmp = getFileFromProperty(SERVER_DEPLOY_DIR, props);
if (tmp == null) {
tmp = new File(serverDataDir, "content");
}
serverDeployDir = tmp;
tmp = getFileFromProperty(SERVER_LOG_DIR, props);
if (tmp == null) {
tmp = new File(serverBaseDir, "log");
}
serverLogDir = tmp;
tmp = getFileFromProperty(SERVER_TEMP_DIR, props);
if (tmp == null) {
tmp = new File(serverBaseDir, "tmp");
}
serverTempDir = tmp;
tmp = getFileFromProperty(CONTROLLER_TEMP_DIR, props);
if (tmp == null) {
tmp = new File(serverBaseDir, "tmp");
}
controllerTempDir = tmp;
boolean allowExecutor = true;
String maxThreads = SecurityActions.getSystemProperty(BOOTSTRAP_MAX_THREADS);
if (maxThreads != null && maxThreads.length() > 0) {
try {
Integer.decode(maxThreads);
// Property was set to a valid value; user wishes to control core service threads
allowExecutor = false;
} catch(NumberFormatException ex) {
ServerLogger.ROOT_LOGGER.failedToParseCommandLineInteger(BOOTSTRAP_MAX_THREADS, maxThreads);
}
}
allowModelControllerExecutor = allowExecutor;
this.productConfig = productConfig;
}
void install() {
SecurityActions.setSystemProperty(QUALIFIED_HOST_NAME, qualifiedHostName);
SecurityActions.setSystemProperty(HOST_NAME, hostName);
SecurityActions.setSystemProperty(SERVER_NAME, serverName);
SecurityActions.setSystemProperty(NODE_NAME, nodeName);
SecurityActions.setSystemProperty(HOME_DIR, homeDir.getAbsolutePath());
SecurityActions.setSystemProperty(MODULES_DIR, modulesDir.getAbsolutePath());
SecurityActions.setSystemProperty(SERVER_BASE_DIR, serverBaseDir.getAbsolutePath());
SecurityActions.setSystemProperty(SERVER_CONFIG_DIR, serverConfigurationDir.getAbsolutePath());
SecurityActions.setSystemProperty(SERVER_DATA_DIR, serverDataDir.getAbsolutePath());
SecurityActions.setSystemProperty(SERVER_DEPLOY_DIR, serverDeployDir.getAbsolutePath());
SecurityActions.setSystemProperty(SERVER_LOG_DIR, serverLogDir.getAbsolutePath());
SecurityActions.setSystemProperty(SERVER_TEMP_DIR, serverTempDir.getAbsolutePath());
// Register the vfs module as URLStreamHandlerFactory
try {
ModuleLoader bootLoader = Module.getBootModuleLoader();
Module vfsModule = bootLoader.loadModule(ModuleIdentifier.create(VFS_MODULE_IDENTIFIER));
Module.registerURLStreamHandlerFactoryModule(vfsModule);
} catch (Exception ex) {
ServerLogger.ROOT_LOGGER.cannotAddURLStreamHandlerFactory(ex, VFS_MODULE_IDENTIFIER);
}
}
/**
* Get the name of this server's host controller. For domain-mode servers, this is the name given in the domain configuration. For
* standalone servers, which do not utilize a host controller, the value should be <code>null</code>.
*
* @return server's host controller name if the instance is running in domain mode, or <code>null</code> if running in standalone
* mode
*/
public String getHostControllerName() {
return hostControllerName;
}
/**
* Get the name of this server instance. For domain-mode servers, this is the name given in the domain configuration. For
* standalone servers, this is the name either provided in the server configuration, or, if not given, the name specified
* via system property, or auto-detected based on host name.
*
* @return the server name
*/
public String getServerName() {
return serverName;
}
/**
* Get the fully-qualified host name detected at server startup.
*
* @return the qualified host name
*/
public String getQualifiedHostName() {
return qualifiedHostName;
}
/**
* Get the local host name detected at server startup.
*
* @return the local host name
*/
public String getHostName() {
return hostName;
}
/**
* Get the node name used for clustering purposes.
*
* @return the node name
*/
public String getNodeName() {
return nodeName;
}
public File[] getJavaExtDirs() {
return javaExtDirs.clone();
}
public File getHomeDir() {
return homeDir;
}
public File getModulesDir() {
return modulesDir;
}
public File getBundlesDir() {
return bundlesDir;
}
public File getServerBaseDir() {
return serverBaseDir;
}
public File getServerConfigurationDir() {
return serverConfigurationDir;
}
public ConfigurationFile getServerConfigurationFile() {
return serverConfigurationFile;
}
public File getServerDataDir() {
return serverDataDir;
}
public File getServerDeployDir() {
return serverDeployDir;
}
public File getServerLogDir() {
return serverLogDir;
}
public File getServerTempDir() {
return serverTempDir;
}
public File getControllerTempDir() {
return controllerTempDir;
}
public LaunchType getLaunchType() {
return launchType;
}
public boolean isStandalone() {
return standalone;
}
public RunningMode getInitialRunningMode() {
return initialRunningMode;
}
// package protected for now as this is not a stable API
boolean isAllowModelControllerExecutor() {
return allowModelControllerExecutor;
}
public ProductConfig getProductConfig() {
return productConfig;
}
/**
* Determine the number of threads to use for the bootstrap service container. This reads
* the {@link #BOOTSTRAP_MAX_THREADS} system property and if not set, defaults to 2*cpus.
* @see Runtime#availableProcessors()
* @return the maximum number of threads to use for the bootstrap service container.
*/
public static int getBootstrapMaxThreads() {
// Base the bootstrap thread on proc count if not specified
int cpuCount = Runtime.getRuntime().availableProcessors();
int defaultThreads = cpuCount * 2;
String maxThreads = SecurityActions.getSystemProperty(BOOTSTRAP_MAX_THREADS);
if (maxThreads != null && maxThreads.length() > 0) {
try {
int max = Integer.decode(maxThreads);
defaultThreads = Math.max(max, 1);
} catch(NumberFormatException ex) {
ServerLogger.ROOT_LOGGER.failedToParseCommandLineInteger(BOOTSTRAP_MAX_THREADS, maxThreads);
}
}
return defaultThreads;
}
/**
* Get a File from configuration.
*
* @return the CanonicalFile form for the given name.
*/
private File getFileFromProperty(final String name, final Properties props) {
String value = props.getProperty(name, null);
if (value != null) {
File f = new File(value);
return f;
}
return null;
}
private static final File[] NO_FILES = new File[0];
/**
* Get a File path list from configuration.
*
* @return the CanonicalFile form for the given name.
*/
private File[] getFilesFromProperty(final String name, final Properties props) {
String sep = props.getProperty("path.separator");
String value = props.getProperty(name, null);
if (value != null) {
final String[] paths = value.split(Pattern.quote(sep));
final int len = paths.length;
final File[] files = new File[len];
for (int i = 0; i < len; i++) {
files[i] = new File(paths[i]);
}
return files;
}
return NO_FILES;
}
}