/src/main/java/com/polopoly/jboss/mojos/JBossStopMojo.java

https://github.com/polopoly/embedded-jboss-maven-plugin · Java · 142 lines · 109 code · 23 blank · 10 comment · 20 complexity · e1ac40236096404b26235aa934006070 MD5 · raw file

  1. package com.polopoly.jboss.mojos;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.maven.plugin.MojoExecutionException;
  5. import org.apache.maven.plugin.MojoFailureException;
  6. import com.polopoly.jboss.JBossOperations;
  7. /**
  8. * Will stop the JBoss server started with jboss:start
  9. * @goal stop
  10. * @aggregator
  11. */
  12. public class JBossStopMojo extends JBossStartMojo {
  13. /**
  14. * Wait for locks to disappear when stopping
  15. *
  16. * @parameter default-value="false" expression="${jboss.waitLock}"
  17. */
  18. protected boolean jbossWaitLock;
  19. public void execute() throws MojoExecutionException, MojoFailureException {
  20. stoppingJBoss();
  21. stoppingAdm();
  22. }
  23. private void stoppingAdm() throws MojoExecutionException {
  24. if (!shouldStartAdm()) {
  25. return;
  26. }
  27. info("Shutting down ADM Content Services " + isAdmPortRunning());
  28. if (!admLock.exists()) {
  29. info("lockFile does not exists");
  30. if (!isAdmPortRunning()) {
  31. info("ADM Content Services are already stopped");
  32. return;
  33. }
  34. }
  35. if (isAdmPortRunning()) {
  36. List<String> startOpts = new ArrayList<>();
  37. if (admDistributionFile == null) {
  38. if (admDistribution == null) {
  39. throw new MojoExecutionException("Configure admDistribution");
  40. }
  41. admDistributionFile = resolveArtifact(admDistribution).getFile();
  42. }
  43. startOpts.add("-p");
  44. startOpts.add(admPort);
  45. startOpts.add("--stop");
  46. final String[] params = isWindows()
  47. ? createWindowsCommand(ADM_STARTUP_COMMAND, startOpts)
  48. : createUnixCommand(ADM_STARTUP_COMMAND, startOpts);
  49. info("Stop With\n" + arrayToString(params));
  50. ProcessBuilder pb = new ProcessBuilder(params);
  51. pb.directory(admHome);
  52. setupEnvironments(admEnvironments, pb);
  53. try {
  54. Process proc = pb.start();
  55. new ADMLogger(proc.getInputStream(), "out", logToConsole).start();
  56. new ADMLogger(proc.getErrorStream(), "err", logToConsole).start();
  57. proc.waitFor();
  58. } catch (Exception ioe) {
  59. throw new MojoExecutionException("Unable to stop ADM Content Services!", ioe);
  60. }
  61. }
  62. int maxRetry = retry;
  63. while (isAdmPortRunning()) {
  64. if (maxRetry-- <= 0) {
  65. throw new MojoExecutionException("timeout waiting for ADM Content Services to stop");
  66. }
  67. sleep("Interrupted while waiting for ADM Content Services to stop");
  68. }
  69. while (admLock.exists()) {
  70. if (maxRetry-- <= 0) {
  71. throw new MojoExecutionException("timeout waiting for ADM Content Services to stop");
  72. }
  73. sleep("Interrupted while waiting for ADM Content Services to stop");
  74. }
  75. info("ADM Content Services stopped!");
  76. }
  77. private void stoppingJBoss() throws MojoExecutionException {
  78. info("Shutting down JBoss");
  79. JBossOperations operations = new JBossOperations(connect(false));
  80. if (!isNamingPortFree() || isStarted(operations)) {
  81. operations.shutDown();
  82. try {
  83. info("Waiting for JBoss to shutdown");
  84. while (isStarted(operations)) {
  85. sleep("Interrupted while waiting for JBoss to stop");
  86. }
  87. } catch (RuntimeException ignore) {
  88. }
  89. } else {
  90. info("JBoss seems to be already down");
  91. }
  92. while(!isNamingPortFree()) {
  93. sleep("Interrupted while waiting for JBoss to stop");
  94. }
  95. debug("jbossWaitLock -> " + jbossWaitLock);
  96. if (jbossWaitLock) {
  97. if (jbossLock.exists()) {
  98. info("Waiting for " + jbossLock + " to disappear");
  99. }
  100. int maxRetry = retry;
  101. debug("jbossLock " + jbossLock.getAbsolutePath() + " exists: " + jbossLock.exists());
  102. while (jbossLock.exists()) {
  103. if (maxRetry-- <= 0) {
  104. throw new MojoExecutionException("timeout waiting for JBOSS to stop");
  105. }
  106. sleep("Interrupted while waiting for JBOSS to stop");
  107. debug("exists: " + jbossLock.exists());
  108. }
  109. }
  110. info("JBOSS stopped!");
  111. }
  112. private boolean isStarted(final JBossOperations operations) {
  113. try {
  114. return operations.isStarted();
  115. } catch (Exception e) {
  116. return false;
  117. }
  118. }
  119. }