Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Enhance the forked tomcat shutdown mojo to ensure it waits for tomcat… #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/googlecode/t7mp/AbstractT7BaseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public abstract class AbstractT7BaseMojo extends AbstractMojo {

public static final String CONTEXT_PATH_ROOT = "ROOT";

public static final int DEFAULT_TOMCAT_SHUTDOWN_TIMEOUT = 10; // in seconds

/**
* @parameter expression="${project}"
* @required
Expand Down Expand Up @@ -289,6 +291,12 @@ public abstract class AbstractT7BaseMojo extends AbstractMojo {
*/
protected int instanceCount = 1;

/**
* @parameter expression="${t7.tomcatShutdownTimeout}" default-value="10"
*/
protected int tomcatShutdownTimeout = DEFAULT_TOMCAT_SHUTDOWN_TIMEOUT;


public boolean isWebProject() {
return this.packaging.equals("war");
}
Expand Down Expand Up @@ -587,4 +595,11 @@ public void setInstanceCount(int instanceCount) {
this.instanceCount = instanceCount;
}

public int getTomcatShutdownTimeout() {
return tomcatShutdownTimeout;
}

public void setTomcatShutdownTimeout(int timeout) {
this.tomcatShutdownTimeout = timeout;
}
}
17 changes: 16 additions & 1 deletion src/main/java/com/googlecode/t7mp/BaseConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class BaseConfiguration implements T7Configuration {
public static final int DEFAULT_TOMCAT_HTTP_PORT = 8080;
public static final int DEFAULT_TOMCAT_SHUTDOWN_PORT = 8005;
public static final String DEFAULT_CONTEXT_PATH_ROOT = "ROOT";
public static final int DEFAULT_TOMCAT_SHUTDOWN_TIMEOUT = 10; // in seconds
protected TomcatArtifact tomcatArtifact = new TomcatArtifact();

/**
Expand Down Expand Up @@ -201,6 +202,11 @@ public class BaseConfiguration implements T7Configuration {
*/
protected int instanceCount = 1;

/**
* @parameter expression="${t7.tomcatShutdownTimeout}" default-value="10"
*/
protected int tomcatShutdownTimeout = DEFAULT_TOMCAT_SHUTDOWN_TIMEOUT;

/* (non-Javadoc)
* @see com.googlecode.t7mp.PluginConfiguration#isTomcatSetAwait()
*/
Expand Down Expand Up @@ -595,7 +601,16 @@ public int getInstanceCount() {
return instanceCount;
}

public void setInstanceCount(final int instanceCount) {
public void setTomcatShutdownTimeout(int timeout) {
this.tomcatShutdownTimeout = timeout;
}

@Override
public int getTomcatShutdownTimeout() {
return tomcatShutdownTimeout;
}

public void setInstanceCount(final int instanceCount) {
this.instanceCount = instanceCount;
}
}
37 changes: 33 additions & 4 deletions src/main/java/com/googlecode/t7mp/ForkedInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ public class ForkedInstance implements Runnable {
private Process p;
private PluginLog log;
private T7Configuration configuration;
private int id;

/**
* @param id instance id
*/
public ForkedInstance(int id) {
this.id = id;
}

/**
* Set up the instance.
*
* @param mavenPluginContext
Expand All @@ -53,12 +61,17 @@ public void configureInstance(MavenPluginContext mavenPluginContext) {

@Override
public void run() {
startTomcat();
try {
startTomcat();
} catch(IOException e) {
log.error("Failed to start tomcat instance.");
}
}

private void startTomcat() {
private void startTomcat() throws IOException {
File lockFile = createLockFile();

setStartScriptPermissions(TomcatUtil.getBinDirectory(configuration.getCatalinaBase()));
setStartScriptPermissions(TomcatUtil.getBinDirectory(configuration.getCatalinaBase()));

ProcessBuilder processBuilder = new ProcessBuilder(getStartSkriptCommand());
processBuilder.directory(TomcatUtil.getBinDirectory(configuration.getCatalinaBase()));
Expand Down Expand Up @@ -90,9 +103,25 @@ private void startTomcat() {
log.error(e.getMessage(), e);
}
log.info("Exit-Value " + exitValue);

deleteLockFile(lockFile);
}

private File createLockFile() throws IOException {
File lockFile = new File(new File(System.getProperty("java.io.tmpdir")), "maven-t7-forked-mojo-" + id + ".tmp");
if(!lockFile.createNewFile()) {
log.error("Failed to create lock file for tomcat instance " + id);
}
return lockFile;
}

private void deleteLockFile(File lockFile) {
if(!lockFile.delete()) {
log.error("Failed to delete lock file for tomcat instance " + id);
}
}

private String getNextLine(BufferedReader br) {
private String getNextLine(BufferedReader br) {
String line;
try {
line = br.readLine();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/googlecode/t7mp/RunForkedMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
printInstancesToStart(configurations);

for (InstanceConfiguration configuration : configurations) {
getLog().info("Starting Tomcat ...");
try {
getLog().info("Starting Tomcat instance " + configuration.getId() + " ...");
try {
mavenPluginContext.getConfiguration().setTomcatHttpPort(configuration.getHttpPort());
mavenPluginContext.getConfiguration().setTomcatShutdownPort(configuration.getShutdownPort());
mavenPluginContext.getConfiguration().setCatalinaBase(new File(configuration.getCatalinaBasePath()));
ForkedInstance p = new ForkedInstance();
ForkedInstance p = new ForkedInstance(configuration.getId());
p.configureInstance(mavenPluginContext);
Thread t = new Thread(p);
t.start();
Expand Down
70 changes: 44 additions & 26 deletions src/main/java/com/googlecode/t7mp/StopForkedMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
*/
public class StopForkedMojo extends AbstractT7TomcatMojo {

private static final long SLEEPTIME = 3000;

private PluginLog log;
private Context context;

Expand All @@ -56,33 +54,53 @@ public void execute() throws MojoExecutionException, MojoFailureException {
List<InstanceConfiguration> configurations = InstanceConfigurationUtil.createInstanceConfigurations(mavenPluginContext);

for (InstanceConfiguration configuration : configurations) {
setStartScriptPermissions(TomcatUtil.getBinDirectory(new File(configuration.getCatalinaBasePath())));
ProcessBuilder processBuilder = new ProcessBuilder(getStopSkriptCommand());
int exitValue = -1;
try {
File binDirectory = TomcatUtil.getBinDirectory(new File(configuration.getCatalinaBasePath()));
Process p = processBuilder.directory(binDirectory).start();
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
log.info(line);
}
exitValue = p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(SLEEPTIME);
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
log.info("Exit-Value ForkedTomcatShutdownHook " + exitValue);
setStartScriptPermissions(TomcatUtil.getBinDirectory(new File(configuration.getCatalinaBasePath())));
ProcessBuilder processBuilder = new ProcessBuilder(getStopSkriptCommand());
int exitValue = -1;
try {
File binDirectory = TomcatUtil.getBinDirectory(new File(configuration.getCatalinaBasePath()));
Process p = processBuilder.directory(binDirectory).start();
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
log.info(line);
}
exitValue = p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

waitForTomcatShutdown(configuration.getId(), mavenPluginContext);
log.info("Exit-Value ForkedTomcatShutdownHook " + exitValue);
}
}

private void waitForTomcatShutdown(int id, DefaultMavenPluginContext mavenPluginContext) {
int shutdownTimeout = mavenPluginContext.getConfiguration().getTomcatShutdownTimeout();
try {
log.info("Waiting up to " + shutdownTimeout + " seconds for tomcat instance " + id + " to stop.");
File lockFile = new File(new File(System.getProperty("java.io.tmpdir")), "maven-t7-forked-mojo-" + id + ".tmp");
int elapsedSeconds = 0;
while (lockFile.exists() && elapsedSeconds < shutdownTimeout) {
Thread.sleep(1000);
elapsedSeconds++;
}
if(lockFile.exists()) {
log.warn("Timed out waiting for tomcat to stop.");
if(!lockFile.delete()) {
log.error("Failed to delete lock file for tomcat instance " + id);
}
} else {
log.info("Tomcat has stopped in " + elapsedSeconds + " seconds.");
}
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
}

protected String[] getStopSkriptCommand() {
if (SystemUtil.isWindowsSystem()) {
return new String[] {"cmd", "/c", "catalina.bat", "stop"};
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/googlecode/t7mp/T7Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ public interface T7Configuration {

int getInstanceCount();

int getTomcatShutdownTimeout();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public LocalMavenRepositoryArtifactResolver(String localMavenRepositoryPath) {
}

private File createLocalMavenRepository() {
File userHomeDirectory = new File(System.getProperty("user.home"));
File repositoryDirectory = new File(userHomeDirectory, "/.m2/repository");
return createLocalMavenRepository(repositoryDirectory);
String mavenRepositoryPath = System.getProperty("maven.repo.home");
if(StringUtils.isNotBlank(mavenRepositoryPath)) {
return createLocalMavenRepository(new File(mavenRepositoryPath));
} else {
return createLocalMavenRepository(new File(new File(System.getProperty("user.home")), "/.m2/repository"));
}
}

private File createLocalMavenRepository(File file) {
Expand Down