package com.perforce.bumble.connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.perforce.bumble.project.Project;
import com.perforce.p4java.client.IClient;
import com.perforce.p4java.core.file.FileSpecOpStatus;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.mapbased.server.Server;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.callback.ICommandCallback;
public class ConnectionService {
private static Logger logger = LoggerFactory.getLogger(ConnectionService.class);
private final Project project;
private IOptionsServer p4;
public ConnectionService(Project project) {
this.project = project;
}
public void connect() throws P4JavaException {
String uri = project.getURI();
p4 = ConnectionFactory.getConnection(uri);
p4.connect();
// register command logging
ICommandCallback logging = new ConnectionLogging();
p4.registerCallback(logging);
// Set Username
String userName = project.getConfig("P4USER");
p4.setUserName(userName);
// Set Charset if server is unicode
if (p4.getServerInfo().isUnicodeEnabled()) {
String charsetName = project.getConfig("P4CHARSET");
p4.setCharsetName(charsetName);
}
// Get Environment
String ignore = ".p4ignore";
String ticket = ".p4tickets";
String home = System.getProperty("user.home");
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
ignore = "p4ignore.txt";
ticket = "p4tickets.txt";
home += "\\";
} else {
home += "/";
}
// Set Tickets location
p4.setTicketsFilePath(home + ticket);
// Set p4ignore file
Server server = (Server) p4;
server.setIgnoreFileName(ignore);
// Set Workspace
String clientName = project.getConfig("P4CLIENT");
IClient client = p4.getClient(clientName);
p4.setCurrentClient(client);
}
public void disconnect() throws P4JavaException {
p4.disconnect();
}
public boolean login(String password) throws P4JavaException {
p4.login(password);
return isLogin();
}
public boolean isLogin() throws P4JavaException {
String status = p4.getLoginStatus();
if (status.contains("not necessary")) {
return true;
}
if (status.contains("ticket expires in")) {
return true;
}
// If there is a broker or something else that swallows the message
if (status.isEmpty()) {
return true;
}
return false;
}
public Project getProject() {
return project;
}
public IOptionsServer getConnection() {
return p4;
}
/**
* Look for a message in the returned FileSpec from operation.
*
* @param fileSpecs
* @param ignore
* @return
* @throws ConverterException
*/
public static void validate(List<IFileSpec> fileSpecs, String... ignore) throws P4JavaException {
validate(fileSpecs, true, ignore);
}
public static boolean validate(List<IFileSpec> fileSpecs, boolean quiet, String... ignore) throws P4JavaException {
boolean success = true;
boolean abort = false;
ArrayList<String> ignoreList = new ArrayList<String>();
ignoreList.addAll(Arrays.asList(ignore));
for (IFileSpec fileSpec : fileSpecs) {
FileSpecOpStatus status = fileSpec.getOpStatus();
if (status != FileSpecOpStatus.VALID) {
String msg = fileSpec.getStatusMessage();
// superfluous p4java message
boolean unknownMsg = true;
for (String istring : ignoreList) {
if (msg.contains(istring)) {
// its a known message
unknownMsg = false;
break;
}
}
// check and report unknown message
if (unknownMsg) {
if (!quiet) {
msg = "P4JAVA: " + msg;
logger.warn(msg);
if (status == FileSpecOpStatus.ERROR || status == FileSpecOpStatus.CLIENT_ERROR) {
abort = true;
}
}
success = false;
}
}
}
if (abort) {
String msg = "P4JAVA: Error(s)";
throw new P4JavaException(msg);
}
return success;
}
}