}
// Create the password temp file
File temp = createTemporaryAsadminPasswordFile(password);
CommandResult result = null;
try {
// Construct the command line
List<String> asAdminCommandLine = new ArrayList<>();
asAdminCommandLine.add(asAdminCommand);
asAdminCommandLine.add("--user");
asAdminCommandLine.add(user);
asAdminCommandLine.add("--passwordfile");
asAdminCommandLine.add(temp.getAbsolutePath());
asAdminCommandLine.add("create-domain");
if (portBase > 0) {
asAdminCommandLine.add("--portbase");
asAdminCommandLine.add(Integer.toString(portBase));
}
if (!checkPorts) {
asAdminCommandLine.add("--checkports");
asAdminCommandLine.add("false");
}
// Check if we have a valid template
if (template != null) {
if (!template.exists()) {
throw new IllegalArgumentException("Domain template '" + template + "' does not exist");
}
// Create the domain from the template
asAdminCommandLine.add("--template");
if (template.isDirectory()) {
// Find domain xml template file
File domainXmlTemplate = template.toPath().resolve(DOMAIN_XML_FILE).toFile();
if (!domainXmlTemplate.isFile()) {
throw new IllegalArgumentException("Domain template file not found in '" + domainXmlTemplate.getAbsolutePath() + "'!");
}
asAdminCommandLine.add(domainXmlTemplate.getAbsolutePath());
} else {
asAdminCommandLine.add(template.getAbsolutePath());
}
}
// Add the desired domain name
asAdminCommandLine.add(domainName);
// Create the domain
logger.log(Level.CONFIG, "Creating domain with: {0}", asAdminCommandLine);
result = SystemCommandExecutor.execute(asAdminCommandLine);
} finally {
try {
temp.delete();
} catch (Exception ex) {
logger.log(Level.WARNING, "Could not delete temporary password file '" + temp.getAbsolutePath() + "'! ", ex);
}
}
if (result != null && result.isOk()) {
if (template != null && template.isDirectory()) {
try {
logger.log(Level.CONFIG, "Copying domain template files from {0} to {1}", new Object[]{template.getAbsolutePath(), getDomainPath()});
// Copy additional files to destination directories
copyDomainTemplateFiles(template);
} catch (IOException ex) {
logger.log(Level.SEVERE, "Could not copy template files to domain directory! {0}", ex);
result.setExitCode(1);
result.setExitCodeInfo("Could not copy template files to domain directory!");
result.setThrowable(ex);
}
if (result.isOk()) {
setupDomain(template);
}
}
}