Search in sources :

Example 1 with Substitutable

use of com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable in project Payara by payara.

the class StringSubstitutionEngine method doSubstitution.

/**
 * Perform's string substitution for a given group.
 *
 * @param groups Groups for which the string substitution
 *  has to be performed.
 * @throws StringSubstitutionException If any error occurs during
 *  substitution.
 */
private void doSubstitution(Group group) throws StringSubstitutionException {
    List<? extends FileEntry> fileList = group.getFileEntry();
    List<? extends Archive> archiveList = group.getArchive();
    if (!isValid(fileList) && !isValid(archiveList)) {
        if (_logger.isLoggable(Level.FINER)) {
            _logger.log(Level.FINER, _strings.get("noSubstitutableGroupEntry", group.getId()));
        }
        return;
    }
    List<? extends ChangePairRef> refList = group.getChangePairRef();
    if (!isValid(refList)) {
        if (_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE, _strings.get("noChangePairForGroup", group.getId()));
        }
        return;
    }
    String groupMode = null;
    ModeType modeType = group.getMode();
    if (modeType != null) {
        groupMode = modeType.value();
    }
    buildChangePairsMap();
    Map<String, String> substitutionMap = new HashMap<String, String>();
    for (ChangePairRef ref : refList) {
        String name = ref.getName();
        String localMode = ref.getMode();
        // then inherit the mode of the group
        if (localMode == null || localMode.length() == 0) {
            localMode = groupMode;
        }
        Pair pair = _changePairsMap.get(name);
        if (pair == null) {
            _logger.log(Level.INFO, SLogger.MISSING_CHANGE_PAIR, new Object[] { name, group.getId() });
            continue;
        }
        String beforeString = pair.getBefore();
        String afterString = pair.getAfter();
        if (localMode == null || localMode.length() == 0) {
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINEST, _strings.get("noModeValue", group.getId()));
            }
        } else {
            try {
                afterString = ModeProcessor.processModeType(ModeType.fromValue(localMode), afterString);
            } catch (Exception e) {
                _logger.log(Level.WARNING, SLogger.INVALID_MODE_TYPE, localMode);
            }
        }
        substitutionMap.put(beforeString, afterString);
    }
    SubstitutionAlgorithm algorithm = new SubstitutionAlgorithmFactory().getAlgorithm(substitutionMap);
    for (FileEntry fileEntry : fileList) {
        fileEntry.setName(_attrPreprocessor.substitutePath(fileEntry.getName()));
        List<? extends Substitutable> substituables = _substitutableFactory.getFileEntrySubstituables(fileEntry);
        for (Substitutable substituable : substituables) {
            algorithm.substitute(substituable);
            substituable.finish();
        }
    }
    for (Archive archive : archiveList) {
        if (archive == null || archive.getName().isEmpty()) {
            continue;
        }
        try {
            archive.setName(_attrPreprocessor.substitutePath(archive.getName()));
            List<? extends Substitutable> substituables = _substitutableFactory.getArchiveEntrySubstitutable(archive);
            if (!isValid(substituables)) {
                continue;
            }
            for (Substitutable substituable : substituables) {
                algorithm.substitute(substituable);
                substituable.finish();
            }
        } catch (Exception e) {
            LogHelper.log(_logger, Level.WARNING, SLogger.ERR_ARCHIVE_SUBSTITUTION, e, archive.getName());
        }
    }
}
Also used : Archive(com.sun.enterprise.admin.servermgmt.xml.stringsubs.Archive) HashMap(java.util.HashMap) ChangePairRef(com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePairRef) StringSubstitutionException(com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException) SubstitutionAlgorithm(com.sun.enterprise.admin.servermgmt.stringsubs.SubstitutionAlgorithm) ModeType(com.sun.enterprise.admin.servermgmt.xml.stringsubs.ModeType) Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) ChangePair(com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePair)

Example 2 with Substitutable

use of com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable in project Payara by payara.

the class TestFileEntryFactory method testGetFileInvalidInput.

/**
 * Test get files for invalid file name.
 */
@Test
public void testGetFileInvalidInput() {
    FileEntry fileEntry = new FileEntry();
    fileEntry.setName(_classFile.getAbsolutePath() + File.separator + "zzzzzzzzz.class");
    List<Substitutable> substitutables = _factory.getFileElements(fileEntry);
    Assert.assertTrue(substitutables.isEmpty());
}
Also used : Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) Test(org.testng.annotations.Test)

Example 3 with Substitutable

use of com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable in project Payara by payara.

the class TestFileEntryFactory method testGetFilesUsingRegex.

/**
 * Test get file by using regex pattern.
 */
@Test
public void testGetFilesUsingRegex() {
    FileEntry fileEntry = new FileEntry();
    if (!_classFile.exists()) {
        Assert.fail("Not able to locate Test class :" + TestFileEntryFactory.class.getSimpleName());
    }
    fileEntry.setName(_classFile.getParentFile().getAbsolutePath() + File.separator + "(.*+)");
    fileEntry.setRegex("yes");
    List<Substitutable> substitutables = _factory.getFileElements(fileEntry);
    boolean fileFound = false;
    for (Substitutable substitutable : substitutables) {
        if (substitutable.getName().endsWith(_classFile.getAbsolutePath())) {
            fileFound = true;
            break;
        }
    }
    Assert.assertTrue(fileFound);
}
Also used : Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) Test(org.testng.annotations.Test)

Example 4 with Substitutable

use of com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable in project Payara by payara.

the class TestFileEntryFactory method testGetFile.

/**
 * Test get file by mentioning the absolute path of an file.
 */
@Test
public void testGetFile() {
    FileEntry fileEntry = new FileEntry();
    fileEntry.setName(_classFile.getAbsolutePath());
    List<Substitutable> substitutables = _factory.getFileElements(fileEntry);
    Assert.assertTrue(!substitutables.isEmpty());
    Assert.assertTrue(substitutables.size() == 1);
    Assert.assertTrue(substitutables.get(0).getName().equals(_classFile.getAbsolutePath()));
}
Also used : Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) Test(org.testng.annotations.Test)

Example 5 with Substitutable

use of com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable in project Payara by payara.

the class AbstractSubstitutionAlgo method testLargeXMLFileSubstitution.

/**
 * Test substitution for large XML file.
 */
// @Test
// TODO: Test case failing on hudson, Test case execution create temporary file
// to perform substitution.
public void testLargeXMLFileSubstitution() {
    String fileName = _testFileName.replace(".txt", ".xml");
    createXMLFile(fileName);
    Substitutable resolver = null;
    try {
        resolver = new LargeFileSubstitutionHandler(_testFile);
        _algorithm.substitute(resolver);
        resolver.finish();
    } catch (Exception e) {
        Assert.fail("Test case failed : " + e.getMessage());
    }
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
    } catch (FileNotFoundException e) {
        Assert.fail("Test case failed : " + e.getMessage());
    }
    String afterSubstitutionLine = null;
    try {
        int i = 0;
        while ((afterSubstitutionLine = reader.readLine()) != null) {
            switch(i++) {
                case 1:
                    Assert.assertEquals(afterSubstitutionLine, "<port name=\"http\" value=\"8080\"></port>");
                    break;
                case 2:
                    Assert.assertEquals(afterSubstitutionLine, "<port name=\"https\" value=\"8443\"></port>");
                    break;
                default:
                    break;
            }
        }
        reader.close();
    } catch (IOException e) {
        Assert.fail("Not able to read test file");
    } finally {
        _testFile.delete();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LargeFileSubstitutionHandler(com.sun.enterprise.admin.servermgmt.stringsubs.impl.LargeFileSubstitutionHandler) Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Aggregations

Substitutable (com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable)12 Test (org.testng.annotations.Test)8 FileEntry (com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry)7 File (java.io.File)6 FileNotFoundException (java.io.FileNotFoundException)5 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 LargeFileSubstitutionHandler (com.sun.enterprise.admin.servermgmt.stringsubs.impl.LargeFileSubstitutionHandler)2 SmallFileSubstitutionHandler (com.sun.enterprise.admin.servermgmt.stringsubs.impl.SmallFileSubstitutionHandler)2 AfterTest (org.testng.annotations.AfterTest)2 StringSubstitutionException (com.sun.enterprise.admin.servermgmt.stringsubs.StringSubstitutionException)1 SubstitutionAlgorithm (com.sun.enterprise.admin.servermgmt.stringsubs.SubstitutionAlgorithm)1 Archive (com.sun.enterprise.admin.servermgmt.xml.stringsubs.Archive)1 ChangePair (com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePair)1 ChangePairRef (com.sun.enterprise.admin.servermgmt.xml.stringsubs.ChangePairRef)1 ModeType (com.sun.enterprise.admin.servermgmt.xml.stringsubs.ModeType)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1