/hudson-core/src/main/java/hudson/tools/InstallerTranslator.java

http://github.com/hudson/hudson · Java · 70 lines · 37 code · 6 blank · 27 comment · 8 complexity · 734e1385fe9a3bca23a1b5ac9730f113 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2009, Sun Microsystems, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.tools;
  25. import hudson.Extension;
  26. import hudson.model.Node;
  27. import hudson.model.TaskListener;
  28. import java.io.IOException;
  29. import java.util.Map;
  30. import java.util.WeakHashMap;
  31. import java.util.concurrent.Semaphore;
  32. /**
  33. * Actually runs installations.
  34. * @since 1.305
  35. */
  36. @Extension
  37. public class InstallerTranslator extends ToolLocationTranslator {
  38. private static final Map<Node,Map<ToolInstallation,Semaphore>> mutexByNode = new WeakHashMap<Node,Map<ToolInstallation,Semaphore>>();
  39. public String getToolHome(Node node, ToolInstallation tool, TaskListener log) throws IOException, InterruptedException {
  40. InstallSourceProperty isp = tool.getProperties().get(InstallSourceProperty.class);
  41. if (isp == null) {
  42. return null;
  43. }
  44. for (ToolInstaller installer : isp.installers) {
  45. if (installer.appliesTo(node)) {
  46. Map<ToolInstallation, Semaphore> mutexByTool = mutexByNode.get(node);
  47. if (mutexByTool == null) {
  48. mutexByNode.put(node, mutexByTool = new WeakHashMap<ToolInstallation, Semaphore>());
  49. }
  50. Semaphore semaphore = mutexByTool.get(tool);
  51. if (semaphore == null) {
  52. mutexByTool.put(tool, semaphore = new Semaphore(1));
  53. }
  54. semaphore.acquire();
  55. try {
  56. return installer.performInstallation(tool, node, log).getRemote();
  57. } finally {
  58. semaphore.release();
  59. }
  60. }
  61. }
  62. return null;
  63. }
  64. }