Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-7618] refactor executors to expose raw data streams in order…
… to correctly read 'p4 -G' formatted data
  • Loading branch information
Rob Petti committed Jun 24, 2011
1 parent 6f25b91 commit bc63a27
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -491,11 +492,11 @@ protected byte[] getRawPerforceResponseBytes(String cmd[]) throws PerforceExcept

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)));
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/tek42/perforce/process/CmdLineExecutor.java
Expand Up @@ -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.");
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/tek42/perforce/process/Executor.java
@@ -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();}
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/hudson/plugins/perforce/HudsonP4DefaultExecutor.java
Expand Up @@ -28,6 +28,9 @@ public class HudsonP4DefaultExecutor implements HudsonP4Executor {
private BufferedReader reader;
private BufferedWriter writer;

private InputStream input;
private OutputStream output;

private Launcher hudsonLauncher;
private String[] env;
private FilePath filePath;
Expand All @@ -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
}
Expand All @@ -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();

Expand Down Expand Up @@ -108,21 +111,35 @@ private String[] convertEnvMaptoArray(Map<String, String> envMap) {
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.");
}

}
6 changes: 4 additions & 2 deletions src/main/java/hudson/plugins/perforce/HudsonP4Executor.java
Expand Up @@ -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;

/**
*
Expand All @@ -20,8 +22,8 @@ public interface HudsonP4Executor extends Executor {

void exec(String[] cmd) throws PerforceException;

BufferedReader getReader();
InputStream getInputStream();

BufferedWriter getWriter();
OutputStream getOutputStream();

}
35 changes: 26 additions & 9 deletions src/main/java/hudson/plugins/perforce/HudsonP4RemoteExecutor.java
Expand Up @@ -53,6 +53,9 @@ public class HudsonP4RemoteExecutor implements HudsonP4Executor {
private BufferedReader reader;
private BufferedWriter writer;

private InputStream input;
private OutputStream output;

private Launcher hudsonLauncher;
private String[] env;
private FilePath filePath;
Expand All @@ -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
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -179,20 +196,20 @@ private String[] convertEnvMaptoArray(Map<String, String> envMap) {
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) {};
}

Expand Down

0 comments on commit bc63a27

Please sign in to comment.