Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, 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.host.controller;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.GROUP;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.JVM;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.PROFILE;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_CONFIG;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_GROUP;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.SYSTEM_PROPERTY;
importstatic org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.jboss.as.controller.ExpressionResolver;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.domain.controller.resources.ServerGroupResourceDefinition;
import org.jboss.as.host.controller.model.host.HostResourceDefinition;
import org.jboss.as.host.controller.model.jvm.JvmElement;
import org.jboss.as.host.controller.model.jvm.JvmOptionsBuilderFactory;
import org.jboss.as.process.DefaultJvmUtils;
import org.jboss.as.process.ProcessControllerClient;
import org.jboss.as.server.ServerEnvironment;
import org.jboss.as.server.controller.resources.SystemPropertyResourceDefinition;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.Property;
import org.wildfly.security.manager.WildFlySecurityManager;
/**
* Combines the relevant parts of the domain-level and host-level models to
* determine the jvm launch command needed to start an application server instance.
*
* @authorKabir Khan
* @authorJames R. Perkins
*/publicclassManagedServerBootCmdFactoryimplementsManagedServerBootConfiguration {privatestaticfinal String HOST_CONTROLLER_PROCESS_NAME_PROP = "[" + ProcessControllerClient.HOST_CONTROLLER_PROCESS_NAME + "]";
privatestaticfinal ModelNode EMPTY = new ModelNode();
static {
EMPTY.setEmptyList();
EMPTY.protect();
}
privatefinal String serverName;
privatefinal ModelNode domainModel;
privatefinal ModelNode hostModel;
privatefinal ModelNode serverModel;
privatefinal ModelNode serverGroup;
privatefinal JvmElement jvmElement;
privatefinal HostControllerEnvironment environment;
privatefinalboolean managementSubsystemEndpoint;
privatefinal ModelNode endpointConfig = new ModelNode();
privatefinal ExpressionResolver expressionResolver;
privatefinal DirectoryGrouping directoryGrouping;
public ManagedServerBootCmdFactory(final String serverName, final ModelNode domainModel, final ModelNode hostModel, final HostControllerEnvironment environment, final ExpressionResolver expressionResolver) {
this.serverName = serverName;
this.domainModel = domainModel;
this.hostModel = hostModel;
this.environment = environment;
this.expressionResolver = expressionResolver;
this.serverModel = resolveExpressions(hostModel.require(SERVER_CONFIG).require(serverName));
this.directoryGrouping = resolveDirectoryGrouping(hostModel, expressionResolver);
final String serverGroupName = serverModel.require(GROUP).asString();
this.serverGroup = resolveExpressions(domainModel.require(SERVER_GROUP).require(serverGroupName));
String serverVMName = null;
ModelNode serverVM = null;
if(serverModel.hasDefined(JVM)) {
for (final String jvm : serverModel.get(JVM).keys()) {
serverVMName = jvm;
serverVM = serverModel.get(JVM, jvm);
break;
}
}
String groupVMName = null;
ModelNode groupVM = null;
if(serverGroup.hasDefined(JVM)) {
for(final String jvm : serverGroup.get(JVM).keys()) {
groupVMName = jvm;
groupVM = serverGroup.get(JVM, jvm);
break;
}
}
// Use the subsystem endpoint// TODO by default use the subsystem endpointthis.managementSubsystemEndpoint = serverGroup.get(ServerGroupResourceDefinition.MANAGEMENT_SUBSYSTEM_ENDPOINT.getName()).asBoolean(false);
// Get the endpoint configurationif(managementSubsystemEndpoint) {
final String profileName = serverGroup.get(PROFILE).asString();
final ModelNode profile = domainModel.get(PROFILE, profileName);
if(profile.hasDefined(SUBSYSTEM) && profile.hasDefined("remoting")) {
endpointConfig.set(profile.get(SUBSYSTEM, "remoting"));
}
}
final String jvmName = serverVMName != null ? serverVMName : groupVMName;
final ModelNode hostVM = jvmName != null ? hostModel.get(JVM, jvmName) : null;
this.jvmElement = new JvmElement(jvmName,
resolveExpressions(hostVM),
resolveExpressions(groupVM),
resolveExpressions(serverVM));
}
/**
* Resolve expressions in the given model (if there are any)
*/private ModelNode resolveExpressions(final ModelNode unresolved) {
if (unresolved == null) {
returnnull;
}
try {
return expressionResolver.resolveExpressions(unresolved.clone());
} catch (OperationFailedException e) {
// Failthrownew IllegalStateException(e.getMessage(), e);
}
}
/**
* Returns the value of found in the model.
*
* @param model the model that contains the key and value.
* @param expressionResolver the expression resolver to use to resolve expressions
*
* @return the directory grouping found in the model.
*
* @throws IllegalArgumentException if the {@link org.jboss.as.controller.descriptions.ModelDescriptionConstants#DIRECTORY_GROUPING directory grouping}
* was not found in the model.
*/privatestatic DirectoryGrouping resolveDirectoryGrouping(final ModelNode model, final ExpressionResolver expressionResolver) {
try {
return DirectoryGrouping.forName(HostResourceDefinition.DIRECTORY_GROUPING.resolveModelAttribute(expressionResolver, model).asString());
} catch (OperationFailedException e) {
thrownew IllegalStateException(e);
}
}
/**
* Create and verify the configuration before trying to start the process.
*
* @return the process boot configuration
*/public ManagedServerBootConfiguration createConfiguration() {
returnthis;
}
/** {@inheritDoc} */@Overridepublic HostControllerEnvironment getHostControllerEnvironment() {
return environment;
}
/** {@inheritDoc} */@Overridepublic List getServerLaunchCommand() {
final List command = new ArrayList();
command.add(getJavaCommand());
command.add("-D[" + ManagedServer.getServerProcessName(serverName) + "]");
JvmOptionsBuilderFactory.getInstance().addOptions(jvmElement, command);
Map bootTimeProperties = getAllSystemProperties(true);
// Add in properties passed in to the ProcessController command linefor (Map.EntryhostProp : environment.getHostSystemProperties().entrySet()) {
if (!bootTimeProperties.containsKey(hostProp.getKey())) {
bootTimeProperties.put(hostProp.getKey(), hostProp.getValue());
}
}
for (Entryentry : bootTimeProperties.entrySet()) {
String property = entry.getKey();
String value = entry.getValue();
if (!"org.jboss.boot.log.file".equals(property) && !"logging.configuration".equals(property)
&& !HOST_CONTROLLER_PROCESS_NAME_PROP.equals(property)) {
final StringBuilder sb = new StringBuilder("-D");
sb.append(property);
if (value != null) {
sb.append('=');
sb.append(value);
}
command.add(sb.toString());
}
}
// Use the directory grouping type to set props controlling the server data/log/tmp dirs
String serverDirProp = bootTimeProperties.get(ServerEnvironment.SERVER_BASE_DIR);
File serverDir = serverDirProp == null ? new File(environment.getDomainServersDir(), serverName) : new File(serverDirProp);
final String logDir = addPathProperty(command, "log", ServerEnvironment.SERVER_LOG_DIR, bootTimeProperties,
directoryGrouping, environment.getDomainLogDir(), serverDir);
addPathProperty(command, "tmp", ServerEnvironment.SERVER_TEMP_DIR, bootTimeProperties,
directoryGrouping, environment.getDomainTempDir(), serverDir);
final String dataDir = addPathProperty(command, "data", ServerEnvironment.SERVER_DATA_DIR, bootTimeProperties,
directoryGrouping, environment.getDomainDataDir(), serverDir);
final File loggingConfig = new File(dataDir, "logging.properties");
final String path;
if (loggingConfig.exists()) {
path = "file:" + loggingConfig.getAbsolutePath();
} else {
// Sets the initial log file to use
command.add("-Dorg.jboss.boot.log.file=" + getAbsolutePath(new File(logDir), "server.log"));
// The default host controller and process controller configuration filefinal String domainConfigFile = "file:" + getAbsolutePath(environment.getDomainConfigurationDir(), "logging.properties");
// The configuration file from the system property, could the default domain/configuration/logging.properties filefinal String systemPropConfigFile = WildFlySecurityManager.getPropertyPrivileged("logging.configuration", null);
// The default configuration file to use if nothing is setfinal File defaultConfigFile = getAbsoluteFile(environment.getDomainConfigurationDir(), "default-server-logging.properties");
// Ignore the system property value if domain/configuration/logging.properties is usedif (domainConfigFile.equals(systemPropConfigFile) && defaultConfigFile.exists()) {
path = "file:" + defaultConfigFile.getAbsolutePath();
} elseif (systemPropConfigFile != null) {
path = systemPropConfigFile;
} else {
// Default to the domain/configuration/logging.properties if nothing else found
path = domainConfigFile;
}
}
command.add(String.format("-Dlogging.configuration=%s", path));
command.add("-jar");
command.add(getAbsolutePath(environment.getHomeDir(), "jboss-modules.jar"));
command.add("-mp");
command.add(environment.getModulePath());
command.add("org.jboss.as.server");
return command;
}
@Overridepublicboolean isManagementSubsystemEndpoint() {
return managementSubsystemEndpoint;
}
@Overridepublic ModelNode getSubsystemEndpointConfiguration() {
return endpointConfig;
}
private String getJavaCommand() {
String javaHome = jvmElement.getJavaHome();
if (javaHome == null) {
if(environment.getDefaultJVM() != null) {
String defaultJvm = environment.getDefaultJVM().getAbsolutePath();
if (!defaultJvm.equals("java") || (defaultJvm.equals("java") && System.getenv("JAVA_HOME") != null)) {
return defaultJvm;
}
}
javaHome = DefaultJvmUtils.getCurrentJvmHome();
}
return DefaultJvmUtils.findJavaExecutable(javaHome);
}
/** {@inheritDoc} */@Overridepublic Map getServerLaunchEnvironment() {
final Map env = new HashMap();
for(final Entryproperty : jvmElement.getEnvironmentVariables().entrySet()) {
env.put(property.getKey(), property.getValue());
}
return env;
}
private Map getAllSystemProperties(boolean boottimeOnly){
Map props = new TreeMap();
addSystemProperties(domainModel, props, boottimeOnly);
addSystemProperties(serverGroup, props, boottimeOnly);
addSystemProperties(hostModel, props, boottimeOnly);
addSystemProperties(serverModel, props, boottimeOnly);
return props;
}
privatevoid addSystemProperties(final ModelNode source, final Map props, boolean boottimeOnly) {
if (source.hasDefined(SYSTEM_PROPERTY)) {
for (Property prop : source.get(SYSTEM_PROPERTY).asPropertyList()) {
ModelNode propResource = prop.getValue();
try {
if (boottimeOnly && !SystemPropertyResourceDefinition.BOOT_TIME.resolveModelAttribute(expressionResolver, propResource).asBoolean()) {
continue;
}
} catch (OperationFailedException e) {
thrownew IllegalStateException(e);
}
String val = propResource.hasDefined(VALUE) ? propResource.get(VALUE).asString() : null;
props.put(prop.getName(), val);
}
}
}
/**
* Adds the absolute path to command.
*
* @param command the command to add the arguments to.
* @param typeName the type of directory.
* @param propertyName the name of the property.
* @param properties the properties where the path may already be defined.
* @param directoryGrouping the directory group type.
* @param typeDir the domain level directory for the given directory type; to be used for by-type grouping
* @param serverDir the root directory for the server, to be used for 'by-server' grouping
* @return the absolute path that was added.
*/private String addPathProperty(final List command, final String typeName, final String propertyName, final Map properties, final DirectoryGrouping directoryGrouping,
final File typeDir, File serverDir) {
final String result;
final String value = properties.get(propertyName);
if (value == null) {
switch (directoryGrouping) {
caseBY_TYPE:
result = getAbsolutePath(typeDir, "servers", serverName);
break;
caseBY_SERVER: default:
result = getAbsolutePath(serverDir, typeName);
break;
}
properties.put(propertyName, result);
} else {
final File dir = new File(value);
switch (directoryGrouping) {
caseBY_TYPE:
result = getAbsolutePath(dir, "servers", serverName);
break;
caseBY_SERVER: default:
result = dir.getAbsolutePath();
break;
}
}
command.add(String.format("-D%s=%s", propertyName, result));
return result;
}
static String getAbsolutePath(final File root, final String... paths) {
return getAbsoluteFile(root, paths).getAbsolutePath();
}
static File getAbsoluteFile(final File root, final String... paths) {
File path = root;
for(String segment : paths) {
path = new File(path, segment);
}
return path.getAbsoluteFile();
}
}