}
// Create the LDAP connection options object, which will be used to
// customize the way that we connect to the server and specify a set of
// basic defaults.
LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions();
connectionOptions.setVersionNumber(3);
// See if we should use SSL or StartTLS when establishing the connection.
// If so, then make sure only one of them was specified.
if (args.useSSLArg.isPresent())
{
if (args.useStartTLSArg.isPresent())
{
Message message = ERR_LDAP_CONN_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
args.useSSLArg.getLongIdentifier(),
args.useSSLArg.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
else
{
connectionOptions.setUseSSL(true);
}
}
else if (args.useStartTLSArg.isPresent())
{
connectionOptions.setStartTLS(true);
}
// If we should blindly trust any certificate, then install the appropriate
// SSL connection factory.
if (args.useSSLArg.isPresent() || args.useStartTLSArg.isPresent())
{
try
{
String clientAlias;
if (args.certNicknameArg.isPresent())
{
clientAlias = args.certNicknameArg.getValue();
}
else
{
clientAlias = null;
}
SSLConnectionFactory sslConnectionFactory = new SSLConnectionFactory();
sslConnectionFactory.init(args.trustAllArg.isPresent(),
args.keyStorePathArg.getValue(),
args.keyStorePasswordArg.getValue(),
clientAlias,
args.trustStorePathArg.getValue(),
args.trustStorePasswordArg.getValue());
connectionOptions.setSSLConnectionFactory(sslConnectionFactory);
}
catch (SSLConnectionException sce)
{
Message message =
ERR_LDAP_CONN_CANNOT_INITIALIZE_SSL.get(sce.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
}
}
// If one or more SASL options were provided, then make sure that one of
// them was "mech" and specified a valid SASL mechanism.
if (args.saslOptionArg.isPresent())
{
String mechanism = null;
LinkedList<String> options = new LinkedList<String>();
for (String s : args.saslOptionArg.getValues())
{
int equalPos = s.indexOf('=');
if (equalPos <= 0)
{
Message message = ERR_LDAP_CONN_CANNOT_PARSE_SASL_OPTION.get(s);
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
else
{
String name = s.substring(0, equalPos);
if (name.equalsIgnoreCase("mech"))
{
mechanism = s;
}
else
{
options.add(s);
}
}
}
if (mechanism == null)
{
Message message = ERR_LDAP_CONN_NO_SASL_MECHANISM.get();
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
connectionOptions.setSASLMechanism(mechanism);
for (String option : options)
{
connectionOptions.addSASLProperty(option);
}
}
int timeout = args.connectTimeoutArg.getIntValue();