/**
*
*/
//package com.perforce.p4javademo;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.lang3.NotImplementedException;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.FileSpecOpStatus;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.exception.RequestException;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.callback.IProgressCallback;
import com.perforce.p4java.option.server.GetDepotFilesOptions;
import com.perforce.p4java.option.server.GetFileContentsOptions;
/**
* Simple P4Java file list and progress callback sample demo.<p>
*
* This example demonstrates a typical pattern used in P4Java
* to use and retrieve lists of IFileSpec objects; it also
* demonstrates a very simple progress callback implementation
* and associated usage.
*/
public class ListFilesDemo extends P4JavaDemo {
private static final boolean DEBUG = false;
public static void main(String[] args) {
try {
IOptionsServer server = getOptionsServer(null, null);
server.registerProgressCallback(new DemoProgressCallback());
server.setUserName(userName);
// server.login(password);
System.out.println("Depot files on Perforce server at URI '" + serverUri + "':");
List<IFileSpec> fileList = server.getDepotFiles(
//Add the '-e' option.
FileSpecBuilder.makeFileSpecList("//depot/..."), new GetDepotFilesOptions("-e"));
if (fileList != null) {
for (IFileSpec fileSpec : fileList) {
if (fileSpec != null) {
fetchFile(fileSpec, false);
if (fileSpec.getOpStatus() == FileSpecOpStatus.VALID) {
System.out.println(formatFileSpec(fileSpec));
} else {
System.err.println(fileSpec.getStatusMessage());
}
}
}
}
server.setCurrentClient(server.getClient(clientName));
} catch (RequestException rexc) {
System.err.println(rexc.getDisplayString());
rexc.printStackTrace();
} catch (Exception exc) {
System.err.println(exc.getLocalizedMessage());
exc.printStackTrace();
}
}
protected static String formatFileSpec(IFileSpec fileSpec) {
return fileSpec.getDepotPathString();
}
/**
* A simple demo P4Java progress callback implementation. Real
* implementations would probably correlate the key arguments
* and associated output, but this version simply puts whatever
* it's passed onto standard output with a dash prepended.
*/
protected static class DemoProgressCallback implements IProgressCallback {
public void start(int key) {
System.out.println("Starting command " + key);
}
public void stop(int key) {
System.out.println("Stopping command " + key);
}
public boolean tick(int key, String tickMarker) {
if (tickMarker != null) {
System.out.println(key + " - " + tickMarker);
}
return true;
}
}
private static void fetchFile(IFileSpec f, boolean keep) throws P4JavaException, IOException {
if (keep) throw new NotImplementedException("not imple"); // TODO: keep files
GetFileContentsOptions opts = new GetFileContentsOptions();
opts.setAllrevs(false);
opts.setNoHeaderLine(true);
InputStream fs = f.getContents(opts);
if (fs == null) {
if (DEBUG)
System.out.println(String.format("Could not get file contents. Path: %s", f.getDepotPathString()));
return;
}
OutputStream os = new NullOutputStream();
int l = IOUtils.copy(fs, os);
if (DEBUG)
System.out.println(String.format("Downloaded file %s (%d bytes)", f.getDepotPathString(), l));
}
}
# |
Change |
User |
Description |
Committed |
|
#2
|
25911 |
Jen Bottom |
Adding correct version of build.xml |
|
|
#1
|
25898 |
Jen Bottom |
Integrating the p4java demo code to my dev area |
|
|
//guest/perforce_software/p4java/main/samples/basic/src/com/perforce/p4javademo/ListFilesDemo.java |
#1
|
12851 |
Matt Attaway |
Reorganize content to match Workshop branch naming standards |
|
|
//guest/perforce_software/p4java/samples/basic/src/com/perforce/p4javademo/ListFilesDemo.java |
#2
|
7778 |
jkovisto |
Updated for 2010.1 Release |
|
|
#1
|
7534 |
hreid |
Added P4Java basic demo samples to public depot for 9.2 beta release. |
|
|