Skip to content

Commit

Permalink
Merge pull request #284 from jGauravGupta/FISH-8152
Browse files Browse the repository at this point in the history
FISH-8152 Micro Maven - Devmode - Store session state
  • Loading branch information
jGauravGupta authored Jan 23, 2024
2 parents 2410115 + edd15c8 commit 908cef3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -305,6 +306,7 @@ private void executeBuildReloadTask(List<String> goalsList, boolean rebootRequir
} else {
updateTitle("Reloading");
ReloadMojo reloadMojo = new ReloadMojo(project, log);
reloadMojo.setKeepState(start.keepState);
if (start.hotDeploy) {
Path rootPath = project.getBasedir().toPath();
List<String> sourcesChanged = new ArrayList<>();
Expand Down Expand Up @@ -344,11 +346,9 @@ public void deleteBuildDir(String filePath) {
log.error("Error occurred while deleting the file: ", e);
}
}

private void updateTitle(String state) {
if (start.getDriver() != null) {
((JavascriptExecutor) start.getDriver()).executeScript(String.format("document.title = '%s %s';", state, project.getName()));
}
WebDriverFactory.executeScript(String.format("document.title = '%s %s';", state, project.getName()), start.getDriver(), log);
}

class DeleteFileVisitor extends SimpleFileVisitor<Path> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Run mojo that executes payara-micro in dev mode
Expand All @@ -51,12 +52,19 @@ public class DevMojo extends StartMojo {

@Override
public void execute() throws MojoExecutionException {
liveReload = true;
deployWar = true;
exploded = true;
autoDeploy = true;
if (autoDeploy == null) {
autoDeploy = true;
}
if (liveReload == null) {
liveReload = true;
}
if (keepState == null) {
keepState = true;
}
if (trimLog == null) {
trimLog = !getLog().isDebugEnabled();
trimLog = true;
}
super.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class ReloadMojo extends BasePayaraMojo {

@Parameter(property = "metadataChanged")
private boolean metadataChanged;

@Parameter(property = "keepState", defaultValue = "false")
protected boolean keepState;

public ReloadMojo(MavenProject mavenProject, Log log) {
this.mavenProject = mavenProject;
Expand All @@ -95,14 +98,19 @@ public void execute() throws MojoExecutionException {
}
File reloadFile = new File(explodedDir, RELOAD_FILE);
getLog().info("Reloading " + explodedDir);
if (hotDeploy) {
if (hotDeploy || keepState) {
Properties props = new Properties();
props.setProperty("hotdeploy", "true");
if (metadataChanged) {
props.setProperty("metadatachanged", "true");
if (keepState) {
props.setProperty("keepState", Boolean.TRUE.toString());
}
if (sourcesChanged != null && !sourcesChanged.isEmpty()) {
props.setProperty("sourceschanged", sourcesChanged);
if (hotDeploy) {
props.setProperty("hotdeploy", Boolean.TRUE.toString());
if (metadataChanged) {
props.setProperty("metadatachanged", Boolean.TRUE.toString());
}
if (sourcesChanged != null && !sourcesChanged.isEmpty()) {
props.setProperty("sourceschanged", sourcesChanged);
}
}
try (FileOutputStream outputStrem = new FileOutputStream(reloadFile)) {
props.store(outputStrem, null);
Expand Down Expand Up @@ -133,6 +141,14 @@ public void setHotDeploy(boolean hotDeploy) {
this.hotDeploy = hotDeploy;
}

public boolean isKeepState() {
return keepState;
}

public void setKeepState(boolean keepState) {
this.keepState = keepState;
}

public String getSourcesChanged() {
return sourcesChanged;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,14 @@ public class StartMojo extends BasePayaraMojo {
@Parameter(property = "exploded", defaultValue = "false")
protected boolean exploded;

@Parameter(property = "autoDeploy", defaultValue = "false")
protected boolean autoDeploy;
@Parameter(property = "autoDeploy")
protected Boolean autoDeploy;

@Parameter(property = "keepState")
protected Boolean keepState;

@Parameter(property = "liveReload", defaultValue = "false")
protected boolean liveReload;
@Parameter(property = "liveReload")
protected Boolean liveReload;

@Parameter(property = "browser")
protected String browser;
Expand Down Expand Up @@ -181,6 +184,15 @@ public void execute() throws MojoExecutionException {
if(trimLog == null) {
trimLog = false;
}
if (autoDeploy == null) {
autoDeploy = false;
}
if (liveReload == null) {
liveReload = false;
}
if (keepState == null) {
keepState = false;
}
if (autoDeploy && autoDeployHandler == null) {
autoDeployHandler = new AutoDeployHandler(this, webappDirectory);
Thread devModeThread = new Thread(autoDeployHandler);
Expand Down Expand Up @@ -518,13 +530,13 @@ private void redirectStreamToGivenOutputStream(final InputStream inputStream, fi
driver = WebDriverFactory.createWebDriver(browser);
driver.get(PropertiesUtils.getProperty(payaraMicroURL, payaraMicroURL));
} catch (Exception ex) {
getLog().error("Error in running WebDriver:" + ex.getMessage());
getLog().error("Error in running WebDriver" , ex);
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI(payaraMicroURL));
}
} catch (IOException | URISyntaxException e) {
getLog().error("Error in running Desktop browse:" + e.getMessage());
getLog().error("Error in running Desktop browse", e);
} finally {
driver = null;
}
Expand All @@ -538,7 +550,7 @@ private void redirectStreamToGivenOutputStream(final InputStream inputStream, fi
try {
driver.navigate().refresh();
} catch (Exception ex) {
getLog().error("Error in refreshing with WebDriver:" + ex.getMessage());
getLog().debug("Error in refreshing with WebDriver", ex);
}
} else if (autoDeploy
&& line.contains(INOTIFY_USER_LIMIT_REACHED_MESSAGE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
import org.openqa.selenium.safari.SafariDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.File;
import org.apache.maven.plugin.logging.Log;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

Expand Down Expand Up @@ -165,4 +168,14 @@ public static boolean isChromeBrowserInstalled() {
return false;
}
}

public static void executeScript(String script, WebDriver driver, Log log) {
if (driver != null && driver instanceof JavascriptExecutor) {
try {
((JavascriptExecutor) driver).executeScript(script);
} catch(WebDriverException ex) {
log.debug(ex);
}
}
}
}

0 comments on commit 908cef3

Please sign in to comment.