import java.awt.BorderLayout;
import java.awt.Frame;
import java.security.Security;
import java.text.ParseException;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.*;
import com.novell.ldap.client.Debug;
import schema.*;
import arguments.*;
public class ListSchema
{
public final int BIND_TIMEOUT = 20000;
public static void main( String[] args )
{
System.exit( new ListSchema().runSchema(args));
}
private int runSchema( String[] args)
{
LDAPConnection lc;
Options options = null;
try {
options = new Options();
options.parse(args);
} catch( ParseException e) {
String errorMessage = e.getMessage();
if( errorMessage == null) {
errorMessage = e.toString();
}
errorMessage = options.usage(errorMessage);
displayArguments( options, args);
System.err.println( errorMessage);
return 1;
} catch( Exception e) {
System.err.println("Exception: " + e.toString());
e.printStackTrace();
return 1;
}
if( options.debug()) {
Debug.setTraceStream( System.err);
Debug.setTrace( "TraceAll", true);
}
String keyPath = null;
String type = options.getConnectionType();
if( (keyPath = options.getKeystore()) != null) {
LDAPSocketFactory ssf;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
if( options.getPrintVerbose()) {
System.err.println("Java key store is \"" + keyPath + "\"");
}
System.setProperty("javax.net.ssl.trustStore", keyPath);
if( type.equalsIgnoreCase("TLS")) {
if( options.getPrintVerbose()) {
System.err.println("Setting factory for a TLS connection");
}
ssf = new LDAPJSSEStartTLSFactory();
} else {
if( options.getPrintVerbose()) {
System.err.println("Setting factory for a SSL connection");
}
ssf = new LDAPJSSESecureSocketFactory();
}
LDAPConnection.setSocketFactory(ssf);
}
Frame app;
ListAttributeSchema attrList;
ListObjectClassSchema objList;
lc = new LDAPConnection();
try {
if( options.getPrintVerbose()) {
System.err.println("Connecting to host \"" + options.getHostPort() + "\"");
}
lc.connect(options.getHostPort(), 0);
if( (keyPath != null) && (type.equalsIgnoreCase("TLS")) ) {
lc.startTLS();
}
String loginDN = options.getLoginDN();
if( loginDN.length() != 0) {
LDAPConstraints cons = new LDAPConstraints();
cons.setTimeLimit( BIND_TIMEOUT);
lc.bind(3, loginDN,
options.getLoginPasswd().getBytes("UTF8"), cons);
}
if ( options.attributeSchema() ) {
app = new Frame("Display an attribute's properties");
attrList = new ListAttributeSchema( lc );
attrList.init();
app.add( attrList, BorderLayout.WEST );
app.setSize( 835, 505);
app.addWindowListener(new CloseWindowAndExit());
app.setVisible(true);
} else {
app = new Frame("Display an object class' properties");
objList = new ListObjectClassSchema( lc );
objList.init();
app.add( objList, BorderLayout.WEST );
app.setSize( 835, 505);
app.addWindowListener(new CloseWindowAndExit());
app.setVisible(true);
}
lc.disconnect();
while( true ) {
Thread.sleep(1000);
}
}
catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
return 1;
}
catch( Exception e ) {
System.out.println( "\n Error: " + e.toString() );
return 1;
}
}
private
void displayArguments( Options options, String[] args)
{
if( options.getPrintVerbose()) {
System.err.println("\nCommand line arguments");
for( int i=0; i < args.length; i++) {
System.err.println(" " + i + ": " + args[i]);
}
}
return;
}
private
class Options
{
private ApplicationArguments options;
private Options()
throws Exception
{
super();
options = new ApplicationArguments("java ListSchema", 25);
options.add( new Argument(
'a',
"list attribute schema. The absence of this\n" +
" option means list object class schema",
Argument.SINGLE_VALUED));
options.add( new Argument(
'd',
"enable API debug output",
Argument.SINGLE_VALUED));
options.add( new Argument(
'D',
"bind DN",
"the DN of the object used to bind"
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'e',
"keystore",
"Path to a Java Keystore. A valid certificate in the keystore enables\n" +
" an encrypted TLS connection. See also the -Z option.",
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'h',
"host",
"host name or IP address. A port can be specified with the\n" +
" host name as hostname:port, i.e. myhost:389. See also \n" +
" the -p option",
"localhost",
Argument.SINGLE_VALUED));
options.add( new Argument(
'p',
"port",
"host IP port number. See also the -h option",
LDAPConnection.DEFAULT_PORT,
Argument.SINGLE_VALUED));
options.add( new Argument(
'w',
"password",
"the password value used for authentication",
"",
Argument.SINGLE_VALUED));
options.add( new Argument(
'v',
"enable verbose output",
false));
options.add( new Argument(
'Z',
"encrypted connection",
"sets the type of encrypted connection. A Keystore must be specified\n" +
" with the -e option to enable an encrypted connection.\n" +
" SSL - Establishes an encrypted connection using \n" +
" SSL. The default port is 636\n" +
" TLS - Establishes an encrypted connection using \n" +
" TLS. The default port is 389",
"TLS",
Argument.SINGLE_VALUED));
return;
}
void parse( String[] args)
throws Exception
{
options.parse(args);
return;
}
String getHostPort()
{
String host;
int port;
try {
host=(String)(options.getArgument('h').getValue());
Argument arg = options.getArgument('p');
int cnt = arg.getValueCount();
if( cnt == 0) {
port=((Integer)arg.getValue()).intValue();
String type = getConnectionType();
if( type.equalsIgnoreCase("SSL")) {
port = LDAPConnection.DEFAULT_SSL_PORT;
}
} else {
port=((Integer)arg.getValue()).intValue();
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
if( host.indexOf(":") == -1) {
host = host + ":" + port;
}
return host;
}
String getConnectionType()
{
String type = null;
try {
type = (String)(options.getArgument('Z').getValue());
if( type.equalsIgnoreCase("SSL") ) {
;
} else
if( type.equalsIgnoreCase("TLS") ) {
;
} else {
throw new NoSuchFieldException(
"Invalid connection type specified: " + type);
}
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
return type;
}
String getKeystore()
{
try {
Argument arg = options.getArgument('e');
int size = arg.getValueCount();
if( size == 0)
return null;
return (String)arg.getValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getLoginDN()
{
try {
return (String)(options.getArgument('D').getValue());
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
void clearLoginDN()
{
try {
options.getArgument('D').clearValues();
return;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String getLoginPasswd()
{
try {
return (String)(options.getArgument('w').getValue());
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
void clearLoginPassword()
{
try {
options.getArgument('w').clearValues();
return;
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean getPrintVerbose()
{
try {
return ((Boolean)
(options.getArgument('v').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean debug()
{
try {
return ((Boolean)
(options.getArgument('d').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
boolean attributeSchema()
{
try {
return ((Boolean)
(options.getArgument('a').getValue())).booleanValue();
} catch( NoSuchFieldException e) {
throw new RuntimeException(e.toString());
}
}
String usage( String msg)
{
return options.usage( msg);
}
}
}