PageRenderTime 38ms CodeModel.GetById 33ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
25package hudson.tools;
26
27import hudson.Extension;
28import hudson.model.Node;
29import hudson.model.TaskListener;
30import java.io.IOException;
31import java.util.Map;
32import java.util.WeakHashMap;
33import java.util.concurrent.Semaphore;
34
35/**
36 * Actually runs installations.
37 * @since 1.305
38 */
39@Extension
40public class InstallerTranslator extends ToolLocationTranslator {
41
42    private static final Map<Node,Map<ToolInstallation,Semaphore>> mutexByNode = new WeakHashMap<Node,Map<ToolInstallation,Semaphore>>();
43
44    public String getToolHome(Node node, ToolInstallation tool, TaskListener log) throws IOException, InterruptedException {
45        InstallSourceProperty isp = tool.getProperties().get(InstallSourceProperty.class);
46        if (isp == null) {
47            return null;
48        }
49        for (ToolInstaller installer : isp.installers) {
50            if (installer.appliesTo(node)) {
51                Map<ToolInstallation, Semaphore> mutexByTool = mutexByNode.get(node);
52                if (mutexByTool == null) {
53                    mutexByNode.put(node, mutexByTool = new WeakHashMap<ToolInstallation, Semaphore>());
54                }
55                Semaphore semaphore = mutexByTool.get(tool);
56                if (semaphore == null) {
57                    mutexByTool.put(tool, semaphore = new Semaphore(1));
58                }
59                semaphore.acquire();
60                try {
61                    return installer.performInstallation(tool, node, log).getRemote();
62                } finally {
63                    semaphore.release();
64                }
65            }
66        }
67        return null;
68    }
69
70}