Skip to content

Commit

Permalink
Merge pull request #102 from WASdev/devMode
Browse files Browse the repository at this point in the history
Dev mode
  • Loading branch information
ericglau committed Jul 18, 2019
2 parents 8837b9d + 6f6954f commit 1deb2ea
Showing 1 changed file with 100 additions and 28 deletions.
128 changes: 100 additions & 28 deletions src/main/java/net/wasdev/wlp/ant/AbstractTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2014.
* (C) Copyright IBM Corporation 2014, 2019.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -293,31 +293,26 @@ public void doJoin() throws InterruptedException {
*/
public String waitForStringInLog(String regexp, long timeout,
File outputFile) {
int waited = 0;
final int waitIncrement = 500;
long waited = 0;
final long waitIncrement = 500;

log(MessageFormat.format(messages.getString("info.search.string"), regexp, outputFile.getAbsolutePath(), timeout / 1000));

try {
while (waited <= timeout) {
String string = findStringInFile(regexp, outputFile);
if (string == null) {
try {
Thread.sleep(waitIncrement);
} catch (InterruptedException e) {
// Ignore and carry on
}
waited += waitIncrement;
} else {
return string;
while (waited <= timeout) {
String string = findStringInFile(regexp, outputFile);
if (string == null) {
try {
Thread.sleep(waitIncrement);
} catch (InterruptedException e) {
// Ignore and carry on
}
waited += waitIncrement;
} else {
return string;
}
log(MessageFormat.format(messages.getString("error.serch.string.timeout"), regexp, outputFile.getAbsolutePath()));
} catch (Exception e) {
// I think we can assume if we can't read the file it doesn't
// contain our string
throw new BuildException(e);
}
log(MessageFormat.format(messages.getString("error.serch.string.timeout"), regexp, outputFile.getAbsolutePath()));

return null;

}
Expand All @@ -331,9 +326,8 @@ public String waitForStringInLog(String regexp, long timeout,
* the file to search
* @return The first line which includes the pattern, or null if the pattern
* isn't found or if the file doesn't exist
* @throws Exception
*/
protected String findStringInFile(String regexp, File fileToSearch) throws Exception {
protected String findStringInFile(String regexp, File fileToSearch) {
String foundString = null;
List<String> matches = findStringsInFileCommon(regexp, true, -1, fileToSearch);

Expand All @@ -352,20 +346,24 @@ protected String findStringInFile(String regexp, File fileToSearch) throws Excep
* a regular expression (or just a text snippet) to search for
* @param fileToSearch
* the file to search
* @param msgLevel the log level for the match number message
* @return List of Strings which match the pattern. No match results in an
* empty list.
* @throws Exception
*/
protected List<String> findStringsInFileCommon(String regexp,
boolean stopOnFirst, int searchLimit, File fileToSearch)
throws Exception {
private List<String> findStringsInFileCommon(String regexp,
boolean stopOnFirst, int searchLimit, File fileToSearch, int msgLevel) {
if (fileToSearch == null) {
log(messages.getString("info.file.validated"));
return null;
}

if (!fileToSearch.exists()) {
log(MessageFormat.format(messages.getString("info.file.validate.noexist"), fileToSearch.getCanonicalPath()));
try {
log(MessageFormat.format(messages.getString("info.file.validate.noexist"), fileToSearch.getCanonicalPath()));
} catch (IOException e) {
// file doesn't exist anyways, so just print the absolute path for the message
log(MessageFormat.format(messages.getString("info.file.validate.noexist"), fileToSearch.getAbsolutePath()));
}
return null;
}

Expand Down Expand Up @@ -398,7 +396,7 @@ protected List<String> findStringsInFileCommon(String regexp,
if (pattern.matcher(line).find()) {
foundString = line;
matches.add(line);
log(MessageFormat.format(messages.getString("info.match.string"), matches.size(), line));
log(MessageFormat.format(messages.getString("info.match.string"), matches.size(), line), msgLevel);
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -427,6 +425,80 @@ protected List<String> findStringsInFileCommon(String regexp,
return matches;
}

/**
* Searches the given file for the given regular expression.
*
* @param regexp
* a regular expression (or just a text snippet) to search for
* @param fileToSearch
* the file to search
* @return List of Strings which match the pattern. No match results in an
* empty list.
*/
protected List<String> findStringsInFileCommon(String regexp,
boolean stopOnFirst, int searchLimit, File fileToSearch) {
return findStringsInFileCommon(regexp, stopOnFirst, searchLimit, fileToSearch, Project.MSG_INFO);
}

/**
* Searches the given file for the given regular expression, and returns the number of times it occurs.
*
* @param regexp
* a regular expression (or just a text snippet) to search for
* @param fileToSearch
* the file to search
* @return the number of occurrences of the regular expression
*/
public int countStringOccurrencesInFile(String regexp, File fileToSearch) {
List<String> lines = findStringsInFileCommon(regexp, false, -1, fileToSearch, Project.MSG_VERBOSE);

if (lines == null) {
return 0;
}
return lines.size();
}

/**
* Wait until the number of occurrences of the regular expression in the file increases
* above the given number of previous occurrences.
*
* @param regexp
* a regular expression to search for
* @param timeout
* a timeout, in milliseconds
* @param outputFile
* file to check
* @param the previous number of occurrences of the regular expression
* @return updated line that matched the regexp
*/
public String waitForUpdatedStringInLog(String regexp, long timeout,
File outputFile, int previousOccurrences) {
long waited = 0;
final long waitIncrement = 500;

log(MessageFormat.format(messages.getString("info.search.string"), regexp, outputFile.getAbsolutePath(), timeout / 1000));

while (waited <= timeout) {
List<String> matches = findStringsInFileCommon(regexp, false, -1, outputFile, Project.MSG_VERBOSE);
if (matches == null || matches.size() <= previousOccurrences) {
try {
Thread.sleep(waitIncrement);
} catch (InterruptedException e) {
// Ignore and carry on
}
waited += waitIncrement;
} else {
String line = matches.get(matches.size() - 1);
log(MessageFormat.format(messages.getString("info.match.string"), matches.size(), line), Project.MSG_INFO);
return line;
}
}
log(MessageFormat.format(messages.getString("error.serch.string.timeout"), regexp, outputFile.getAbsolutePath()));

return null;

}

/*
* Returns file name without the extension.
*/
Expand Down

0 comments on commit 1deb2ea

Please sign in to comment.