File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\maven-plugin\src\main\java\hudson\maven\MavenComputerListener.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Stephen Connolly
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.maven;
25
26 import hudson.Extension;
27 import hudson.FilePath;
28 import hudson.maven.agent.AbortException;
29 import hudson.maven.agent.Main;
30 import hudson.maven.agent.Maven21Interceptor;
31 import hudson.model.Computer;
32 import hudson.model.TaskListener;
33 import hudson.remoting.Channel;
34 import hudson.remoting.Which;
35 import hudson.slaves.ComputerListener;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.io.PrintStream;
40
41 import org.apache.tools.ant.Project;
42 import org.apache.tools.ant.taskdefs.Zip;
43 import org.codehaus.plexus.classworlds.ClassWorld;
44 import org.jvnet.hudson.maven3.agent.Maven3Main;
45 import org.jvnet.hudson.maven3.launcher.Maven3Launcher;
46
47 /**
48 * When a slave is connected, copy <tt>maven-agent.jar</tt> and <tt>maven-intercepter.jar</tt>
49 *
50 * @author Kohsuke Kawaguchi
51 */
52 @Extension
53 public class MavenComputerListener extends ComputerListener {
54 @Override
55 public void preOnline(Computer c, Channel channel,FilePath root, TaskListener listener) throws IOException, InterruptedException {
56 PrintStream logger = listener.getLogger();
57 copyJar(logger, root, Main.class, "maven-agent");
58 copyJar(logger, root, Maven3Main.class, "maven3-agent");
59 copyJar(logger, root, Maven3Launcher.class, "maven3-interceptor");
60 copyJar(logger, root, AbortException.class, "maven-interceptor");
61 copyJar(logger, root, Maven21Interceptor.class, "maven2.1-interceptor");
62 copyJar(logger, root, ClassWorld.class, "plexus-classworld");
63
64 // copy classworlds 1.1 for maven2 builds
65 // if this line fails during the unit test from IDE, it means you need to "mvn compile" maven-plugin
66 root.child( "classworlds.jar" ).copyFrom(getClass().getClassLoader().getResource("classworlds.jar"));
67 logger.println("Copied classworlds.jar");
68 }
69
70 /**
71 * Copies a jar file from the master to slave.
72 */
73 private void copyJar(PrintStream log, FilePath dst, Class<?> representative, String seedName) throws IOException, InterruptedException {
74 // in normal execution environment, the master should be loading 'representative' from this jar, so
75 // in that way we can find it.
76 File jar = Which.jarFile(representative);
77
78 if(jar.isDirectory()) {
79 // but during the development and unit test environment, we may be picking the class up from the classes dir
80 Zip zip = new Zip();
81 zip.setBasedir(jar);
82 File t = File.createTempFile(seedName, "jar");
83 t.delete();
84 zip.setDestFile(t);
85 zip.setProject(new Project());
86 zip.execute();
87 jar = t;
88 }
89
90 new FilePath(jar).copyTo(dst.child(seedName +".jar"));
91 log.println("Copied "+seedName+".jar");
92 }
93 }
94