PageRenderTime 25ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/jenkins/plugins/shiningpanda/utils/LauncherUtil.java

https://github.com/jenkinsci/shiningpanda-plugin
Java | 101 lines | 33 code | 5 blank | 63 comment | 2 complexity | deb41d6e4d2cff55c6d2a85ddaa483b6 MD5 | raw file
  1. /*
  2. * ShiningPanda plug-in for Jenkins
  3. * Copyright (C) 2011-2015 ShiningPanda S.A.S.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of its license which incorporates the terms and
  7. * conditions of version 3 of the GNU Affero General Public License,
  8. * supplemented by the additional permissions under the GNU Affero GPL
  9. * version 3 section 7: if you modify this program, or any covered work,
  10. * by linking or combining it with other code, such other code is not
  11. * for that reason alone subject to any of the requirements of the GNU
  12. * Affero GPL version 3.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * license for more details.
  18. *
  19. * You should have received a copy of the license along with this program.
  20. * If not, see <https://raw.github.com/jenkinsci/shiningpanda-plugin/master/LICENSE.txt>.
  21. */
  22. package jenkins.plugins.shiningpanda.utils;
  23. import java.io.IOException;
  24. import hudson.EnvVars;
  25. import hudson.FilePath;
  26. import hudson.Launcher;
  27. import hudson.Util;
  28. import hudson.model.TaskListener;
  29. import hudson.tasks.Messages;
  30. import hudson.util.ArgumentListBuilder;
  31. public class LauncherUtil {
  32. /**
  33. * Launch a process.
  34. *
  35. * @param launcher
  36. * The launcher
  37. * @param listener
  38. * The build listener
  39. * @param pwd
  40. * The working directory
  41. * @param environment
  42. * The environment
  43. * @param args
  44. * The arguments
  45. * @return true if was successful, else false
  46. * @throws InterruptedException
  47. */
  48. public static boolean launch(Launcher launcher, TaskListener listener, FilePath pwd, EnvVars environment,
  49. ArgumentListBuilder args) throws InterruptedException {
  50. // Be able to display error
  51. try {
  52. // Launch the process
  53. return 0 == launcher.launch().cmds(FilePathUtil.isUnix(pwd) ? args : args.toWindowsCommand())
  54. .envs(environment).stdout(listener).pwd(pwd).join();
  55. } catch (IOException e) {
  56. // Something went wrong, display error
  57. Util.displayIOException(e, listener);
  58. // Log error message
  59. e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_CommandFailed()));
  60. // Return an error
  61. return false;
  62. }
  63. }
  64. /**
  65. * Create a symbolic link.
  66. *
  67. * @param launcher
  68. * The launcher
  69. * @param listener
  70. * The listener
  71. * @param target
  72. * The target file
  73. * @param link
  74. * The link file
  75. * @return true if successful, else false
  76. * @throws InterruptedException
  77. * @throws IOException
  78. */
  79. public static boolean createSymlink(Launcher launcher, TaskListener listener, FilePath target, FilePath link)
  80. throws InterruptedException, IOException {
  81. // Get the arguments
  82. ArgumentListBuilder args = new ArgumentListBuilder("ln", "-s", target.getRemote(), link.getRemote());
  83. // Be able to display error
  84. try {
  85. // Launch the process
  86. return 0 == launcher.launch().cmds(args).stdout(listener).join();
  87. } catch (IOException e) {
  88. // Something went wrong, display error
  89. Util.displayIOException(e, listener);
  90. // Log error message
  91. e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_CommandFailed()));
  92. // Return an error
  93. return false;
  94. }
  95. }
  96. }