Skip to content
Permalink
Browse files

[JENKINS-7618] refactor executors to expose raw data streams in order…

… to correctly read 'p4 -G' formatted data
  • Loading branch information...
Rob Petti
Rob Petti committed Jun 24, 2011
1 parent 6f25b91 commit bc63a279f47f357d9bf12f9ebca26bbc8bcb203f
@@ -43,6 +43,7 @@
import com.tek42.perforce.Depot;
import com.tek42.perforce.PerforceException;
import com.tek42.perforce.process.Executor;
import java.io.InputStream;
import org.slf4j.LoggerFactory;

/**
@@ -491,11 +492,11 @@ protected StringBuilder getPerforceResponse(String cmd[], ResponseFilter filter)

try
{
char[] cbuf = new char[1024];
BufferedReader reader = p4.getReader();
byte[] cbuf = new byte[1024];
InputStream input = p4.getInputStream();
p4.getWriter().close();
int readCount = -1;
while((readCount = reader.read(cbuf, 0, 1024)) != -1) {
while((readCount = input.read(cbuf, 0, 1024)) != -1) {
for(int i=0; i<readCount; i++){
bytes.add(new Byte((byte)(cbuf[i]&0xff)));
}
@@ -141,4 +141,12 @@ public Process getProcess() {
return currentProcess;
}

public OutputStream getOutputStream() {
throw new UnsupportedOperationException("Not supported yet.");
}

public InputStream getInputStream() {
throw new UnsupportedOperationException("Not supported yet.");
}

}
@@ -1 +1 @@
/* * P4Java - java integration with Perforce SCM * Copyright (C) 2007-, Mike Wille, Tek42 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * You can contact the author at: * * Web: http://tek42.com * Email: mike@tek42.com * Mail: 755 W Big Beaver Road * Suite 1110 * Troy, MI 48084 */package com.tek42.perforce.process;import java.io.*;import com.tek42.perforce.*;/** * A simplified interface for interacting with another process. * * @author Mike Wille */public interface Executor { /** * Execute the specified command and its arguments * * @param args * @throws PerforceException */ public void exec(String args[]) throws PerforceException; /** * Returns a BufferedWriter for writing to the stdin of this process * * @return */ public BufferedWriter getWriter(); /** * Returns a BufferedReader for reading from the stdout/stderr of this process * * @return */ public BufferedReader getReader(); /** * Close down all open resources */ public void close();}
/* * P4Java - java integration with Perforce SCM * Copyright (C) 2007-, Mike Wille, Tek42 * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * You can contact the author at: * * Web: http://tek42.com * Email: mike@tek42.com * Mail: 755 W Big Beaver Road * Suite 1110 * Troy, MI 48084 */package com.tek42.perforce.process;import java.io.*;import com.tek42.perforce.*;/** * A simplified interface for interacting with another process. * * @author Mike Wille */public interface Executor { /** * Execute the specified command and its arguments * * @param args * @throws PerforceException */ public void exec(String args[]) throws PerforceException; /** * Returns a BufferedWriter for writing to the stdin of this process * * @return */ public BufferedWriter getWriter(); public OutputStream getOutputStream(); /** * Returns a BufferedReader for reading from the stdout/stderr of this process * * @return */ public BufferedReader getReader(); public InputStream getInputStream(); /** * Close down all open resources */ public void close();}
@@ -28,6 +28,9 @@
private BufferedReader reader;
private BufferedWriter writer;

private InputStream input;
private OutputStream output;

private Launcher hudsonLauncher;
private String[] env;
private FilePath filePath;
@@ -50,7 +53,7 @@ public void close() {
// Need to close writer
// (reader gets closed in HudsonPipedOutputStream)
try {
writer.close();
output.close();
} catch(IOException e) {
// Do nothing
}
@@ -66,12 +69,12 @@ public void exec(String[] cmd) throws PerforceException {
// hudsonOut->p4in->reader
HudsonPipedOutputStream hudsonOut = new HudsonPipedOutputStream();
FastPipedInputStream p4in = new FastPipedInputStream(hudsonOut);
reader = new BufferedReader(new InputStreamReader(p4in));
input = p4in;

// hudsonIn<-p4Out<-writer
FastPipedInputStream hudsonIn = new FastPipedInputStream();
FastPipedOutputStream p4out = new FastPipedOutputStream(hudsonIn);
writer = new BufferedWriter(new OutputStreamWriter(p4out));
output = p4out;

Proc process = hudsonLauncher.launch().cmds(cmd).envs(env).stdin(hudsonIn).stdout(hudsonOut).pwd(filePath).start();

@@ -108,21 +111,35 @@ public void exec(String[] cmd) throws PerforceException {
return result;
}

public BufferedReader getReader() {
return reader;
}

public BufferedWriter getWriter() {
if(writer==null){
writer = new BufferedWriter(new OutputStreamWriter(output));
}
return writer;
}

public BufferedReader getReader() {
if(reader==null){
reader = new BufferedReader(new InputStreamReader(input));
}
return reader;
}

private void closeBuffers(){
try {
reader.close();
input.close();
} catch(IOException ignoredException) {};
try {
writer.close();
output.close();
} catch(IOException ignoredException) {};
}

public InputStream getInputStream() {
throw new UnsupportedOperationException("Not supported yet.");
}

public OutputStream getOutputStream() {
throw new UnsupportedOperationException("Not supported yet.");
}

}
@@ -9,6 +9,8 @@
import com.tek42.perforce.process.Executor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStream;

/**
*
@@ -20,8 +22,8 @@

void exec(String[] cmd) throws PerforceException;

BufferedReader getReader();
InputStream getInputStream();

BufferedWriter getWriter();
OutputStream getOutputStream();

}
@@ -53,6 +53,9 @@
private BufferedReader reader;
private BufferedWriter writer;

private InputStream input;
private OutputStream output;

private Launcher hudsonLauncher;
private String[] env;
private FilePath filePath;
@@ -75,7 +78,7 @@ public void close() {
// Need to close writer
// (reader gets closed by remote process)
try {
writer.close();
output.close();
} catch(IOException e) {
// Do nothing
}
@@ -92,12 +95,12 @@ public void exec(String[] cmd) throws PerforceException {
// hudsonOut->p4in->reader
FastPipedOutputStream hudsonOut = new FastPipedOutputStream();
FastPipedInputStream p4in = new FastPipedInputStream(hudsonOut);
reader = new BufferedReader(new InputStreamReader(p4in));
input = p4in;

// hudsonIn<-p4Out<-writer
FastPipedInputStream hudsonIn = new FastPipedInputStream();
FastPipedOutputStream p4out = new FastPipedOutputStream(hudsonIn);
writer = new BufferedWriter(new OutputStreamWriter(p4out));
output = p4out;

final OutputStream out = hudsonOut == null ? null : new RemoteOutputStream(hudsonOut);
final InputStream in = hudsonIn ==null ? null : new RemoteInputStream(hudsonIn);
@@ -119,6 +122,20 @@ public void exec(String[] cmd) throws PerforceException {
}
}

public BufferedWriter getWriter() {
if(writer==null){
writer = new BufferedWriter(new OutputStreamWriter(output));
}
return writer;
}

public BufferedReader getReader() {
if(reader==null){
reader = new BufferedReader(new InputStreamReader(input));
}
return reader;
}

private static class RemoteCall implements Callable<Integer,IOException> {
private final List<String> cmd;
private final String[] env;
@@ -179,20 +196,20 @@ public Integer call() throws IOException {
return result;
}

public BufferedReader getReader() {
return reader;
public InputStream getInputStream() {
return input;
}

public BufferedWriter getWriter() {
return writer;
public OutputStream getOutputStream() {
return output;
}

private void closeBuffers(){
try {
reader.close();
input.close();
} catch(IOException ignoredException) {};
try {
writer.close();
output.close();
} catch(IOException ignoredException) {};
}

0 comments on commit bc63a27

Please sign in to comment.
You can’t perform that action at this time.