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;
}
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);
}
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);
}
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));
}
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();
}
}
Aggregations