1 package hudson.remoting; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.ObjectInputStream ; 6 import java.io.ObjectOutputStream ; 7 import java.io.OutputStream ; 8 import java.io.PipedInputStream ; 9 import java.io.PipedOutputStream ; 10 import java.io.Serializable ; 11 import java.util.logging.Level ; 12 import java.util.logging.Logger ; 13 14 61 public final class Pipe implements Serializable { 62 private InputStream in; 63 private OutputStream out; 64 65 private Pipe(InputStream in, OutputStream out) { 66 this.in = in; 67 this.out = out; 68 } 69 70 73 public InputStream getIn() { 74 return in; 75 } 76 77 80 public OutputStream getOut() { 81 return out; 82 } 83 84 87 public static Pipe createRemoteToLocal() { 88 return new Pipe(new PipedInputStream (),null); 90 } 91 92 95 public static Pipe createLocalToRemote() { 96 return new Pipe(null,new ProxyOutputStream()); 97 } 98 99 private void writeObject(ObjectOutputStream oos) throws IOException { 100 if(in!=null && out==null) { 101 PipedOutputStream pos = new PipedOutputStream ((PipedInputStream )in); 103 int oid = Channel.current().export(pos); 104 105 oos.writeBoolean(true); oos.writeInt(oid); 107 } else { 108 int oid = Channel.current().export(out); 110 111 oos.writeBoolean(false); 112 oos.writeInt(oid); 113 } 114 } 115 116 private void readObject(ObjectInputStream ois) throws IOException , ClassNotFoundException { 117 final Channel channel = Channel.current(); 118 assert channel !=null; 119 120 if(ois.readBoolean()) { 121 in = null; 123 out = new ProxyOutputStream(channel, ois.readInt()); 124 } else { 125 128 final int oidRos = ois.readInt(); 130 131 PipedOutputStream pos = new PipedOutputStream (); 133 final int oidPos = channel.export(pos); 134 135 channel.send(new ConnectCommand(oidRos, oidPos)); 137 138 out = null; 139 in = new PipedInputStream (pos); 140 } 141 } 142 143 private static final long serialVersionUID = 1L; 144 145 private static final Logger logger = Logger.getLogger(Pipe.class.getName()); 146 147 private static class ConnectCommand extends Command { 148 private final int oidRos; 149 private final int oidPos; 150 151 public ConnectCommand(int oidRos, int oidPos) { 152 this.oidRos = oidRos; 153 this.oidPos = oidPos; 154 } 155 156 protected void execute(Channel channel) { 157 try { 158 ProxyOutputStream ros = (ProxyOutputStream) channel.getExportedObject(oidRos); 159 channel.unexport(oidRos); 160 ros.connect(channel, oidPos); 161 } catch (IOException e) { 162 logger.log(Level.SEVERE,"Failed to connect to pipe",e); 163 } 164 } 165 } 166 } 167 | Popular Tags |