Search in sources :

Example 1 with AttributePreprocessorImpl

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);
    }
}
Also used : DomainException(com.sun.enterprise.admin.servermgmt.DomainException) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry) DomainException(com.sun.enterprise.admin.servermgmt.DomainException) RepositoryException(com.sun.enterprise.admin.servermgmt.RepositoryException) StringSubstitutor(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutor) AttributePreprocessorImpl(com.sun.enterprise.admin.servermgmt.stringsubs.impl.AttributePreprocessorImpl) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with AttributePreprocessorImpl

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);
    }
}
Also used : Group(com.sun.enterprise.admin.servermgmt.xml.stringsubs.Group) AttributePreprocessorImpl(com.sun.enterprise.admin.servermgmt.stringsubs.impl.AttributePreprocessorImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

AttributePreprocessorImpl (com.sun.enterprise.admin.servermgmt.stringsubs.impl.AttributePreprocessorImpl)2 File (java.io.File)2 InputStream (java.io.InputStream)2 DomainException (com.sun.enterprise.admin.servermgmt.DomainException)1 RepositoryException (com.sun.enterprise.admin.servermgmt.RepositoryException)1 StringSubstitutor (com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutor)1 FileEntry (com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry)1 Group (com.sun.enterprise.admin.servermgmt.xml.stringsubs.Group)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 Test (org.testng.annotations.Test)1