Search in sources :

Example 6 with Substitutable

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

the class FileEntryFactory method getFileElements.

/**
 * Create the {@link List} of {@link FileSubstitutionHandler} by processing the file path.
 * The path can point to a single file\directory  or can contain pattern or wild
 * card characters. A {@link FileSubstitutionHandler} point to a individual file eligible
 * for String substitution process.
 *
 * @param fileEntry
 * @return List of matching substitutable entries.
 */
@SuppressWarnings("unchecked")
List<Substitutable> getFileElements(FileEntry fileEntry) {
    // TODO: Name attribute of file entry can not contain comma separated files.
    String[] pathEntries = fileEntry.getName().split(",");
    List<Substitutable> substituables = null;
    List<File> retrievedFiles = null;
    for (String pathEntry : pathEntries) {
        String isRegex = fileEntry.getRegex();
        if (Boolean.getBoolean(isRegex) || "yes".equalsIgnoreCase(isRegex)) {
            File file = new File(pathEntry);
            File parentDir = file.getParentFile();
            if (parentDir == null || !parentDir.exists()) {
                continue;
            }
            retrievedFiles = new ArrayList<File>();
            String expression = file.getName();
            String[] fileList = parentDir.list();
            Pattern pattern = Pattern.compile(expression);
            for (String fileName : fileList) {
                Matcher matcher = pattern.matcher(fileName);
                if (matcher.matches()) {
                    File matchingFile = new File(parentDir, fileName);
                    if (matchingFile.exists() && matchingFile.canRead() && matchingFile.canWrite()) {
                        retrievedFiles.add(matchingFile);
                    } else {
                        if (_logger.isLoggable(Level.FINER)) {
                            _logger.log(Level.FINER, _strings.get("skipFileFromSubstitution", matchingFile.getAbsolutePath()));
                        }
                    }
                }
            }
        } else {
            FileLister fileLocator = new FileLister();
            retrievedFiles = fileLocator.getFiles(fileEntry.getName());
        }
        if (retrievedFiles.isEmpty()) {
            if (_logger.isLoggable(Level.FINER)) {
                _logger.log(Level.FINER, _strings.get("noMatchedFile", pathEntry));
            }
            continue;
        }
        if (substituables == null) {
            substituables = new ArrayList<Substitutable>(retrievedFiles.size());
        }
        for (File retrievedFile : retrievedFiles) {
            if (retrievedFile.exists()) {
                try {
                    FileSubstitutionHandler substituable = retrievedFile.length() > SubstitutionFileUtil.getInMemorySubstitutionFileSizeInBytes() ? new LargeFileSubstitutionHandler(retrievedFile) : new SmallFileSubstitutionHandler(retrievedFile);
                    substituables.add(substituable);
                } catch (FileNotFoundException e) {
                    LogHelper.log(_logger, Level.WARNING, SLogger.INVALID_FILE_LOCATION, e, retrievedFile);
                }
            }
        }
    }
    return substituables == null ? Collections.EMPTY_LIST : substituables;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileNotFoundException(java.io.FileNotFoundException) Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) File(java.io.File)

Example 7 with Substitutable

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

the class TestFileEntryFactory method testGetFileFromDir.

/**
 * Test get file by mentioning the path of an directory.
 */
@Test
public void testGetFileFromDir() {
    FileEntry fileEntry = new FileEntry();
    fileEntry.setName(_classFile.getParentFile().getAbsolutePath());
    List<Substitutable> substitutables = _factory.getFileElements(fileEntry);
    Assert.assertTrue(!substitutables.isEmpty());
    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 8 with Substitutable

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

the class TestFileEntryFactory method testGetFilesUsingWildCard.

/**
 * Test get file by using wild card.
 */
@Test
public void testGetFilesUsingWildCard() {
    FileEntry fileEntry = new FileEntry();
    fileEntry.setName(_classFile.getParentFile().getAbsolutePath() + File.separator + "Test*");
    List<Substitutable> substitutables = _factory.getFileElements(fileEntry);
    Assert.assertTrue(!substitutables.isEmpty());
    boolean validResult = true;
    for (Substitutable substitutable : substitutables) {
        if (!(new File(substitutable.getName())).getName().startsWith("Test")) {
            validResult = false;
            break;
        }
    }
    Assert.assertTrue(validResult);
}
Also used : Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) File(java.io.File) Test(org.testng.annotations.Test)

Example 9 with Substitutable

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

the class TestFileEntryFactory method testGetFilesUsingWildCardBetweenPath.

/**
 * Test get file by using wild card in between file path.
 */
@Test
public void testGetFilesUsingWildCardBetweenPath() {
    FileEntry fileEntry = new FileEntry();
    File parentFile = _classFile.getParentFile();
    File grandParentFile = parentFile.getParentFile();
    if (grandParentFile == null || !grandParentFile.exists()) {
        return;
    }
    String className = this.getClass().getSimpleName() + ".class";
    fileEntry.setName(grandParentFile.getAbsolutePath() + File.separator + "*" + File.separator + className);
    List<Substitutable> substitutables = _factory.getFileElements(fileEntry);
    Assert.assertTrue(!substitutables.isEmpty());
    Assert.assertTrue(substitutables.size() == 1);
    Assert.assertTrue((new File(substitutables.get(0).getName())).getName().equals(className));
}
Also used : Substitutable(com.sun.enterprise.admin.servermgmt.stringsubs.Substitutable) FileEntry(com.sun.enterprise.admin.servermgmt.xml.stringsubs.FileEntry) File(java.io.File) Test(org.testng.annotations.Test)

Example 10 with Substitutable

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

the class AbstractSubstitutionAlgo method testLargeTextFileSubstitution.

/**
 * Test substitution for large text file.
 */
// @Test
// TODO: Test case failing on hudson, Test case execution create temporary file
// to perform substitution.
public void testLargeTextFileSubstitution() {
    createTextFile();
    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(_testFileName))));
    } catch (FileNotFoundException e) {
        Assert.fail("Not able to locate test file : " + _testFileName, e);
    }
    String afterSubstitutionLine = null;
    try {
        int i = 0;
        while ((afterSubstitutionLine = reader.readLine()) != null) {
            switch(i++) {
                case 0:
                    Assert.assertEquals(afterSubstitutionLine, "First replacedLine in testFile repeat First replacedLine in testFile");
                    break;
                case 1:
                    Assert.assertEquals(afterSubstitutionLine, "Second replacedLine in testFile");
                    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