use of com.sun.enterprise.admin.servermgmt.stringsubs.impl.AttributePreprocessorImpl in project Payara by payara.
the class DomainBuilder method run.
/**
* Performs all the domain configurations which includes security, configuration processing,
* substitution of parameters... etc.
*
* @throws DomainException If any exception occurs in configuration.
*/
public void run() throws RepositoryException, DomainException {
// Create domain directories.
File domainDir = FileUtils.safeGetCanonicalFile(new File(_domainConfig.getRepositoryRoot(), _domainConfig.getDomainName()));
createDirectory(domainDir);
try {
// Extract other jar entries
byte[] buffer = new byte[10000];
for (Enumeration<JarEntry> entry = _templateJar.entries(); entry.hasMoreElements(); ) {
JarEntry jarEntry = (JarEntry) entry.nextElement();
String entryName = jarEntry.getName();
if (entryName.startsWith(META_DIR_NAME)) {
// Skipping the extraction of jar meta data.
continue;
}
if (_extractedEntries.contains(entryName)) {
continue;
}
if (jarEntry.isDirectory()) {
File dir = new File(domainDir, jarEntry.getName());
if (!dir.exists()) {
if (!dir.mkdir()) {
_logger.log(Level.WARNING, SLogger.DIR_CREATION_ERROR, dir.getName());
}
}
continue;
}
InputStream in = null;
BufferedOutputStream outputStream = null;
try {
in = _templateJar.getInputStream(jarEntry);
outputStream = new BufferedOutputStream(new FileOutputStream(new File(domainDir.getAbsolutePath(), jarEntry.getName())));
int i = 0;
while ((i = in.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
} finally {
if (in != null) {
try {
in.close();
} catch (Exception io) {
/**
* ignore
*/
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception io) {
/**
* ignore
*/
}
}
}
}
File configDir = new File(domainDir, DomainConstants.CONFIG_DIR);
String user = (String) _domainConfig.get(DomainConfig.K_USER);
String password = (String) _domainConfig.get(DomainConfig.K_PASSWORD);
String[] adminUserGroups = ((String) _domainConfig.get(DomainConfig.K_INITIAL_ADMIN_USER_GROUPS)).split(",");
String masterPassword = (String) _domainConfig.get(DomainConfig.K_MASTER_PASSWORD);
Boolean saveMasterPassword = (Boolean) _domainConfig.get(DomainConfig.K_SAVE_MASTER_PASSWORD);
// Process domain security.
DomainSecurity domainSecurity = new DomainSecurity();
domainSecurity.processAdminKeyFile(new File(configDir, DomainConstants.ADMIN_KEY_FILE), user, password, adminUserGroups);
try {
domainSecurity.createSSLCertificateDatabase(configDir, _domainConfig, masterPassword);
} catch (Exception e) {
String msg = _strings.getString("SomeProblemWithKeytool", e.getMessage());
System.err.println(msg);
FileOutputStream fos = null;
try {
File keystoreFile = new File(configDir, DomainConstants.KEYSTORE_FILE);
fos = new FileOutputStream(keystoreFile);
fos.write(_keystoreBytes);
} catch (Exception ex) {
getLogger().log(Level.SEVERE, UNHANDLED_EXCEPTION, ex);
} finally {
if (fos != null)
fos.close();
}
}
domainSecurity.changeMasterPasswordInMasterPasswordFile(new File(configDir, DomainConstants.MASTERPASSWORD_FILE), masterPassword, saveMasterPassword);
domainSecurity.createPasswordAliasKeystore(new File(configDir, DomainConstants.DOMAIN_PASSWORD_FILE), masterPassword);
// Add customized tokens in domain.xml.
CustomTokenClient tokenClient = new CustomTokenClient(_domainConfig);
Map<String, String> generatedTokens = tokenClient.getSubstitutableTokens();
// Perform string substitution.
if (_domainTempalte.hasStringsubs()) {
StringSubstitutor substitutor = _domainTempalte.getStringSubs();
Map<String, String> lookUpMap = SubstitutableTokens.getSubstitutableTokens(_domainConfig);
lookUpMap.putAll(generatedTokens);
substitutor.setAttributePreprocessor(new AttributePreprocessorImpl(lookUpMap));
substitutor.substituteAll();
}
// Change the permission for bin & config directories.
try {
File binDir = new File(domainDir, DomainConstants.BIN_DIR);
if (binDir.exists() && binDir.isDirectory()) {
domainSecurity.changeMode("-R u+x ", binDir);
}
domainSecurity.changeMode("-R g-rwx,o-rwx ", configDir);
} catch (Exception e) {
throw new DomainException(_strings.get("setPermissionError"), e);
}
// Generate domain-info.xml
DomainInfoManager domainInfoManager = new DomainInfoManager();
domainInfoManager.process(_domainTempalte, domainDir);
} catch (DomainException de) {
// roll-back
FileUtils.liquidate(domainDir);
throw de;
} catch (Exception ex) {
// roll-back
FileUtils.liquidate(domainDir);
throw new DomainException(ex);
}
}
use of com.sun.enterprise.admin.servermgmt.stringsubs.impl.AttributePreprocessorImpl in project Payara by payara.
the class TestStringSubstitutionFactory method testStringSubstitutorValidStream.
/**
* Test String substitution for valid stream.
*/
@Test
public void testStringSubstitutorValidStream() {
InputStream invalidStream = TestStringSubstitutionFactory.class.getClassLoader().getResourceAsStream(_stringSubsPath);
try {
StringSubstitutor substitutor = StringSubstitutionFactory.createStringSubstitutor(invalidStream);
substitutor.setAttributePreprocessor(new AttributePreprocessorImpl(_substitutionMap));
backUpTestFile();
substitutor.substituteAll();
for (Group group : substitutor.getStringSubsDefinition().getGroup()) {
if (group.getId().equals(VALID_GROUP_ID)) {
validateSubstitutedArchiveEntries(group);
for (FileEntry fileEntry : group.getFileEntry()) {
if (fileEntry.getName().equalsIgnoreCase(_testFileName) && !validateTestFile(new File(fileEntry.getName()))) {
Assert.fail("Substitution failed in the test file.");
break;
}
}
}
}
restoreTestFile();
} catch (StringSubstitutionException e) {
Assert.fail("Exception occurred during string substitution process.", e);
}
}
Aggregations