KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > remoting > ChannelRunner


1 package hudson.remoting;
2
3 import junit.framework.Assert;
4
5 import java.io.IOException JavaDoc;
6 import java.io.PipedInputStream JavaDoc;
7 import java.io.PipedOutputStream JavaDoc;
8 import java.util.concurrent.ExecutorService JavaDoc;
9 import java.util.concurrent.Executors JavaDoc;
10
11 /**
12  * Hides the logic of starting/stopping a channel for test.
13  *
14  * @author Kohsuke Kawaguchi
15  */

16 interface ChannelRunner {
17     Channel start() throws Exception JavaDoc;
18     void stop(Channel channel) throws Exception JavaDoc;
19     String JavaDoc getName();
20
21     Class JavaDoc<? extends ChannelRunner>[] LIST = new Class JavaDoc[] {
22         InProcess.class,
23         Fork.class
24     };
25
26
27     /**
28      * Runs a channel in the same JVM.
29      */

30     static class InProcess implements ChannelRunner {
31         private ExecutorService JavaDoc executor;
32         /**
33          * failure occurred in the other {@link Channel}.
34          */

35         private Exception JavaDoc failure;
36
37         public Channel start() throws Exception JavaDoc {
38             final PipedInputStream JavaDoc in1 = new PipedInputStream JavaDoc();
39             final PipedOutputStream JavaDoc out1 = new PipedOutputStream JavaDoc(in1);
40
41             final PipedInputStream JavaDoc in2 = new PipedInputStream JavaDoc();
42             final PipedOutputStream JavaDoc out2 = new PipedOutputStream JavaDoc(in2);
43
44             executor = Executors.newCachedThreadPool();
45
46             Thread JavaDoc t = new Thread JavaDoc("south bridge runner") {
47                 public void run() {
48                     try {
49                         Channel s = new Channel("south", executor, in2, out1);
50                         s.join();
51                         System.out.println("south completed");
52                     } catch (IOException JavaDoc e) {
53                         e.printStackTrace();
54                         failure = e;
55                     } catch (InterruptedException JavaDoc e) {
56                         e.printStackTrace();
57                         failure = e;
58                     }
59                 }
60             };
61             t.start();
62
63             return new Channel("north", executor, in1, out2);
64         }
65
66         public void stop(Channel channel) throws Exception JavaDoc {
67             channel.close();
68
69             System.out.println("north completed");
70
71             executor.shutdown();
72
73             if(failure!=null)
74                 throw failure; // report a failure in the south side
75
}
76
77         public String JavaDoc getName() {
78             return "local";
79         }
80     }
81
82     /**
83      * Runs a channel in a separate JVM by launching a new JVM.
84      */

85     static class Fork implements ChannelRunner {
86         private Process JavaDoc proc;
87         private ExecutorService JavaDoc executor;
88         private Copier copier;
89
90         public Channel start() throws Exception JavaDoc {
91             System.out.println("forking a new process");
92             // proc = Runtime.getRuntime().exec("java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 hudson.remoting.Launcher");
93
proc = Runtime.getRuntime().exec("java hudson.remoting.Launcher");
94
95             copier = new Copier("copier",proc.getErrorStream(),System.err);
96             copier.start();
97
98             executor = Executors.newCachedThreadPool();
99             return new Channel("north", executor, proc.getInputStream(), proc.getOutputStream());
100         }
101
102         public void stop(Channel channel) throws Exception JavaDoc {
103             channel.close();
104
105             System.out.println("north completed");
106
107             executor.shutdown();
108
109             copier.join();
110             int r = proc.waitFor();
111             System.out.println("south completed");
112
113             Assert.assertEquals("exit code should have been 0",0,r);
114         }
115
116         public String JavaDoc getName() {
117             return "fork";
118         }
119     }
120 }
121
Popular Tags