/src/main/java/hudson/plugins/jboss/CommandsUtils.java

https://github.com/fasol2002/jboss-plugin · Java · 159 lines · 112 code · 22 blank · 25 comment · 18 complexity · c500c938ce4499d4669d3762e10fc0ab MD5 · raw file

  1. package hudson.plugins.jboss;
  2. import hudson.EnvVars;
  3. import hudson.Launcher;
  4. import hudson.Util;
  5. import hudson.model.AbstractBuild;
  6. import hudson.model.BuildListener;
  7. import hudson.plugins.jboss.JBossBuilder.ServerBean;
  8. import hudson.util.ArgumentListBuilder;
  9. import hudson.util.VariableResolver;
  10. import java.io.IOException;
  11. import org.apache.commons.io.output.NullOutputStream;
  12. /**
  13. * Utility class for dealing with jboss command on OS system level.
  14. *
  15. * @author Juliusz Brzostek
  16. *
  17. */
  18. public class CommandsUtils {
  19. private CommandsUtils() {
  20. // utility class cannot be instantiated
  21. }
  22. /**
  23. * Starts given server.
  24. * Method is not waiting.
  25. *
  26. * @param server server to start
  27. * @param extraProperties extra properties for run command
  28. * @param launcher system command luncher
  29. * @param listener {@link BuildListener} for logging purpose
  30. * @return true if everything gone fine, false if any error occurred
  31. */
  32. @SuppressWarnings("unchecked")
  33. public static boolean start(ServerBean server, String extraProperties,
  34. AbstractBuild build, Launcher launcher,
  35. BuildListener listener) throws IOException, InterruptedException {
  36. int kindOfServer = server.getKind();
  37. String startCommand;
  38. if(kindOfServer == 1){//remote case
  39. startCommand = server.getCmdToStart();
  40. }
  41. else{//local case
  42. startCommand = server.getHomeDir() + "/bin/"
  43. + (launcher.isUnix() ? "run.sh" : "run.bat");
  44. }
  45. ArgumentListBuilder args = new ArgumentListBuilder();
  46. args.add(startCommand);
  47. if(kindOfServer == 0)//local case
  48. args.add("-c", server.getServerName());
  49. if(kindOfServer == 0)//local case
  50. args.add("-b", server.getAddress());
  51. if(!launcher.isUnix()) {
  52. args = args.toWindowsCommand();
  53. }
  54. EnvVars env = build.getEnvironment(listener);
  55. VariableResolver<String> vr = build.getBuildVariableResolver();
  56. String properties = env.expand(extraProperties);
  57. args.addKeyValuePairsFromPropertyString("-D",properties,vr);
  58. try {
  59. if(kindOfServer == 1){//remote case
  60. launcher.launch()
  61. .stderr(listener.getLogger())
  62. .stdout(new NullOutputStream())
  63. .cmds(args)
  64. .start();
  65. return true;
  66. }
  67. else{//local case
  68. launcher.launch()
  69. .stderr(listener.getLogger())
  70. .stdout(new NullOutputStream())
  71. .cmds(args)
  72. .pwd(server.getHomeDir() + "/bin")
  73. .start();
  74. return true;
  75. }
  76. } catch (Exception e) {
  77. if (e instanceof IOException) {
  78. Util.displayIOException((IOException)e,listener);
  79. }
  80. e.printStackTrace( listener.fatalError("Error during execution.") );
  81. return false;
  82. }
  83. }
  84. /**
  85. * Stops given server.
  86. *
  87. * @param server {@link ServerBean} to be stopped
  88. * @param launcher system command luncher
  89. * @param listener {@link BuildListener} for logging purpose
  90. * @return true if everything gone fine, false if any error occurred
  91. */
  92. public static boolean stop(ServerBean server, Launcher launcher, BuildListener listener) {
  93. int kindOfServer = server.getKind();
  94. String stopCommand;
  95. if(kindOfServer == 1){//remote case
  96. stopCommand = server.getCmdToShutdown();
  97. }
  98. else{//local case
  99. stopCommand = server.getHomeDir() + "/bin/"
  100. + (launcher.isUnix() ? "shutdown.sh" : "shutdown.bat");
  101. }
  102. ArgumentListBuilder args = new ArgumentListBuilder();
  103. args.add(stopCommand);
  104. if(kindOfServer == 0){//local case
  105. String jndiUrl = "jnp://"+server.getAddress()+":" + server.getJndiPort(); //jnp://127.0.0.1:1099
  106. args.add("-s", jndiUrl, "-S");
  107. }
  108. if(!launcher.isUnix()) {
  109. args = args.toWindowsCommand();
  110. }
  111. try {
  112. if(kindOfServer == 1){//remote case
  113. launcher.launch()
  114. .stderr(listener.getLogger())
  115. .stdout(new NullOutputStream())
  116. .cmds(args)
  117. .join();
  118. return true;
  119. }
  120. else{
  121. launcher.launch()
  122. .stderr(listener.getLogger())
  123. .stdout(new NullOutputStream())
  124. .cmds(args)
  125. .pwd(server.getHomeDir() + "/bin")
  126. .join();
  127. return true;
  128. }
  129. } catch (Exception e) {
  130. if (e instanceof IOException) {
  131. Util.displayIOException((IOException)e,listener);
  132. }
  133. e.printStackTrace( listener.fatalError("Error during execution.") );
  134. return false;
  135. }
  136. }
  137. }