package com.perforce.p4java;
import java.net.URISyntaxException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.perforce.p4java.exception.ConfigException;
import com.perforce.p4java.exception.ConnectionException;
import com.perforce.p4java.exception.NoSuchObjectException;
import com.perforce.p4java.exception.ResourceException;
import com.perforce.p4java.impl.mapbased.rpc.RpcPropertyDefs;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.ServerFactory;
public class RshConnection {
private static Logger logger = LoggerFactory.getLogger(RshConnection.class);
public static IOptionsServer getRshConnection(String p4root) {
// Get a server connection
String p4d = getP4D();
String rsh = "rsh:" + p4d + " -r " + p4root + " -i";
String p4javaUri = getP4JavaUri(rsh);
return getIOptionsServer(p4javaUri);
}
private static IOptionsServer getIOptionsServer(String p4javaUri) {
IOptionsServer newIServer = null;
Properties props = System.getProperties();
// Identify ourselves in server log files.
props.put(PropertyDefs.PROG_NAME_KEY, "jrsh");
props.put(PropertyDefs.PROG_VERSION_KEY, "1.0");
props.put(RpcPropertyDefs.RPC_SOCKET_POOL_SIZE_NICK, "1");
props.put(RpcPropertyDefs.RPC_RELAX_CMD_NAME_CHECKS_NICK, "true");
props.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "0");
try {
newIServer = ServerFactory.getOptionsServer(p4javaUri, props, null);
} catch (ConnectionException | NoSuchObjectException | ConfigException
| ResourceException | URISyntaxException e) {
StringBuffer sb = new StringBuffer();
sb.append("Unable to get servert: ");
sb.append(p4javaUri);
logger.error(sb.toString());
}
return newIServer;
}
private static String getP4D() {
String p4dPath = "src/test/resources/";
String p4dVersion = "r15.1";
String os = System.getProperty("os.name").toLowerCase();
String p4d = p4dPath + p4dVersion + "/";
if (os.contains("win")) {
p4d += "bin.ntx64/p4d.exe";
}
if (os.contains("mac")) {
p4d += "bin.darwin90x86_64/p4d";
}
if (os.contains("nix") || os.contains("nux")) {
p4d += "bin.linux26x86_64/p4d";
}
return p4d;
}
private static String getP4JavaUri(String port) {
if (port.startsWith("ssl:")) {
String trim = port.substring(4, port.length());
return "p4javassl://" + trim;
}
if (port.startsWith("rsh:")) {
String trim = port.substring(4, port.length());
return "p4jrsh://" + trim + " --java";
}
return "p4java://" + port;
}
}