package com.perforce.p4splunk;
import java.util.Properties;
import com.perforce.p4java.PropertyDefs;
import com.perforce.p4java.impl.mapbased.rpc.RpcPropertyDefs;
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.ServerFactory;
public class ConnectionFactory {
/**
* Creates a server connection; provides a connection to the Perforce
* Server, initially client is undefined.
*
* @return
* @throws Exception
*/
public static IOptionsServer getConnection(ConnectionConfig config)
throws Exception {
IOptionsServer iserver = getRawConnection(config);
// Add trust for SSL connections
if (config.isSsl()) {
iserver.addTrust(config.getTrust());
}
// Connect and update current P4 connection
iserver.connect();
return iserver;
}
public static String testConnection(ConnectionConfig config) {
// Test for SSL connections
try {
IOptionsServer iserver = getRawConnection(config);
if (config.isSsl()) {
String serverTrust = iserver.getTrust();
if (!serverTrust.equalsIgnoreCase(config.getTrust())) {
return "Trust missmatch! Server fingerprint: "
+ serverTrust;
}
}
} catch (Exception e) {
StringBuffer sb = new StringBuffer();
sb.append("Unable to connect to: ");
sb.append(config.getServerUri());
sb.append("\n");
sb.append(e.getMessage());
return sb.toString();
}
return null;
}
private static IOptionsServer getRawConnection(ConnectionConfig config)
throws Exception {
Properties props = System.getProperties();
// Identify ourselves in server log files.
Identifier id = new Identifier();
props.put(PropertyDefs.PROG_NAME_KEY, id.getProduct());
props.put(PropertyDefs.PROG_VERSION_KEY, id.getVersion());
// Allow p4 admin commands.
props.put(RpcPropertyDefs.RPC_RELAX_CMD_NAME_CHECKS_NICK, "true");
// disable timeout for slow servers / large db lock times
props.put(RpcPropertyDefs.RPC_SOCKET_SO_TIMEOUT_NICK, "0");
// Get a server connection
String serverUri = config.getServerUri();
IOptionsServer iserver;
iserver = ServerFactory.getOptionsServer(serverUri, props);
return iserver;
}
}