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

1 package hudson.os.windows;
2
3 import hudson.FilePath;
4 import hudson.Launcher;
5 import hudson.Proc;
6 import hudson.Util;
7 import hudson.model.Computer;
8 import hudson.model.TaskListener;
9 import hudson.remoting.Channel;
10 import hudson.util.IOException2;
11 import hudson.util.StreamCopyThread;
12 import org.jinterop.dcom.common.JIException;
13 import org.jvnet.hudson.remcom.WindowsRemoteProcessLauncher;
14
15 import java.io.BufferedOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.util.Map;
20
21 import static java.util.Arrays.asList;
22
23 /**
24 * Pseudo-{@link Launcher} implementation that uses {@link WindowsRemoteProcessLauncher}
25 *
26 * @author Kohsuke Kawaguchi
27 */
28 public class WindowsRemoteLauncher extends Launcher {
29 private final WindowsRemoteProcessLauncher launcher;
30
31 public WindowsRemoteLauncher(TaskListener listener, WindowsRemoteProcessLauncher launcher) {
32 super(listener,null);
33 this.launcher = launcher;
34 }
35
36 private String buildCommandLine(ProcStarter ps) {
37 StringBuilder b = new StringBuilder();
38 for (String cmd : ps.cmds()) {
39 if (b.length()>0) b.append(' ');
40 if (cmd.indexOf(' ')>=0)
41 b.append('"').append(cmd).append('"');
42 else
43 b.append(cmd);
44 }
45 return b.toString();
46 }
47
48 public Proc launch(ProcStarter ps) throws IOException {
49 maskedPrintCommandLine(ps.cmds(), ps.masks(), ps.pwd());
50
51 // TODO: environment variable handling
52
53 String name = ps.cmds().toString();
54
55 final Process proc;
56 try {
57 proc = launcher.launch(buildCommandLine(ps), ps.pwd().getRemote());
58 } catch (JIException e) {
59 throw new IOException2(e);
60 } catch (InterruptedException e) {
61 throw new IOException2(e);
62 }
63 final Thread t1 = new StreamCopyThread("stdout copier: "+name, proc.getInputStream(), ps.stdout(),false);
64 t1.start();
65 final Thread t2 = new StreamCopyThread("stdin copier: "+name,ps.stdin(), proc.getOutputStream(),true);
66 t2.start();
67
68 return new Proc() {
69 public boolean isAlive() throws IOException, InterruptedException {
70 try {
71 proc.exitValue();
72 return false;
73 } catch (IllegalThreadStateException e) {
74 return true;
75 }
76 }
77
78 public void kill() throws IOException, InterruptedException {
79 t1.interrupt();
80 t2.interrupt();
81 proc.destroy();
82 }
83
84 public int join() throws IOException, InterruptedException {
85 try {
86 t1.join();
87 t2.join();
88 return proc.waitFor();
89 } finally {
90 proc.destroy();
91 }
92 }
93
94 @Override
95 public InputStream getStdout() {
96 throw new UnsupportedOperationException();
97 }
98
99 @Override
100 public InputStream getStderr() {
101 throw new UnsupportedOperationException();
102 }
103
104 @Override
105 public OutputStream getStdin() {
106 throw new UnsupportedOperationException();
107 }
108 };
109 }
110
111 public Channel launchChannel(String[] cmd, OutputStream out, FilePath _workDir, Map<String, String> envVars) throws IOException, InterruptedException {
112 printCommandLine(cmd, _workDir);
113
114 try {
115 Process proc = launcher.launch(Util.join(asList(cmd), " "), _workDir.getRemote());
116
117 return new Channel("channel over named pipe to "+launcher.getHostName(),
118 Computer.threadPoolForRemoting, proc.getInputStream(), new BufferedOutputStream(proc.getOutputStream()));
119 } catch (JIException e) {
120 throw new IOException2(e);
121 }
122 }
123
124 public void kill(Map<String, String> modelEnvVars) throws IOException, InterruptedException {
125 // no way to do this
126 }
127 }
128