String url = args[0];
try
{
Environment env = new Environment();
env.setProviderUrl(url);
// If we've been given an explicit username and password, use
// them. Otherwise, the JNDI connection will attempt to default
// to guest/guest.
String user = null;
for (int arg = 1; arg < args.length; arg++)
{
if (args[arg].equals("-user") && arg + 1 < args.length)
{
env.setSecurityPrincipal(user = args[++arg]);
}
else if (args[arg].equals("-pass") && arg + 1 < args.length)
{
env.setSecurityCredentials(args[++arg]);
}
else if (args[arg].equals("-sslCert") && arg + 1 < args.length)
{
// If we've been asked to make a secure T3 connection and we
// have enough arguments, we populate the SSL client
// certificate so that we can attempt two-way SSL authentication.
// In order to do this, we must make an array of at least two
// InputStream objects; the first is the client's private key,
// and the rest are the certificate chain, starting with the
// client's certificate (which must be present) and ending with
// the root CA's certificate.
InputStream[] certs = readCerts(args[++arg]);
if (url.startsWith("t3s") || url.startsWith("https"))
{
env.setSSLClientCertificate(certs);
} else {
fatal("the URL doesn't specify use of SSL");
}
}
else if (args[arg].equals("-cert") && arg + 1 < args.length)
{
if (user == null)
{
fatal("user name must be specified before certificate chain");
}
InputStream[] certs = readCerts(args[++arg]);
X509[] x509 = new X509[certs.length];
for (int i = 0; i < certs.length; i++)
{
x509[i] = new X509(certs[i]);
}
env.setSecurityCredentials(new DefaultUserInfoImpl(user, x509));
} else {
usage();
}
}
ctx = env.getInitialContext();
Frobable f = (Frobable) ctx.lookup("frobtarget");
f.frob();
System.out.println("Frobbed successfully");
}