Author: akarasulu Date: Wed Feb 2 00:05:21 2005 New Revision: 149496 URL: http://svn.apache.org/viewcvs?view=rev&rev=149496 Log: changes ...
o committing Emmanuel's changes from following JIRA o as far as I can see he cleaned up the code o all avalon deps have been removed issues ... o http://issues.apache.org/jira/browse/DIRLDAP-28 Modified: incubator/directory/ldap/trunk/clients/project.xml incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/AddOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/BindOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/DelOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapAdd.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapBind.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapDel.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapModify.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapSearch.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapUnbind.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ModifyOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/Opts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/SearchOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/InputParser.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClient.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapInputParser.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapSearchParameters.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/LdapTestCase.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/LdapTestSession.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/LdapTester.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/SessionAddUI.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/SessionListUI.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/TestCaseAddUI.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/TestCaseListUI.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/ldaptest/TestConfiguration.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/AddOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/BindOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/DelOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/LdapAdd.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/LdapBind.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/LdapDelete.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/LdapModify.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/ModifyOpts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/Opts.java incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/standalone/SearchOpts.java Modified: incubator/directory/ldap/trunk/clients/project.xml URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/project.xml?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/project.xml (original) +++ incubator/directory/ldap/trunk/clients/project.xml Wed Feb 2 00:05:21 2005 @@ -26,6 +26,18 @@ <url>http://jakarta.apache.org/commons/collections.html</url> </dependency> <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>1.0</version> + <url>http://jakarta.apache.org/commons/cli.html</url> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.8</version> + <url>http://logging.apache.org/log4j/docs/</url> + </dependency> + <dependency> <groupId>regexp</groupId> <artifactId>regexp</artifactId> <version>1.2</version> Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/AddOpts.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/AddOpts.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/AddOpts.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/AddOpts.java Wed Feb 2 00:05:21 2005 @@ -205,17 +205,15 @@ package org.apache.ldap.clients ; -import java.util.List ; - -import org.apache.avalon.excalibur.cli.CLArgsParser ; -import org.apache.avalon.excalibur.cli.CLOption ; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; /** * Add operation command line options bean. * * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols</a> * @author $Author: jmachols $ - * @version $Revision: 1.10 $ + * @version $Revision$ */ public class AddOpts @@ -224,22 +222,22 @@ /** * The Distinguished Name of the entry we are adding */ - private String m_AddDn ; + private String addedDn ; /** * The LDIF file name, if any, passed in from the command * line arguments */ - private String m_ldifFile = null ; + private String ldifFile ; /** * Return the Distinguished Name of the entry we are adding * - * @return m_AddDn DN of entry add operation is being performed on + * @return m_addedDn DN of entry add operation is being performed on */ - public String getAddDn() + public String getAddedDn() { - return m_AddDn ; + return addedDn ; } /** @@ -250,7 +248,7 @@ */ public boolean usesLdifFile () { - return ( m_ldifFile != null ) ; + return ( ldifFile != null ) ; } /** @@ -260,7 +258,7 @@ */ public String getLdifFile () { - return m_ldifFile ; + return ldifFile ; } // ------------------------------------------------------------------------ @@ -270,74 +268,53 @@ /** * Overrides BindOpts.process to be able to get the ADD entry's Dn. * - * @param an_argv the variable argument list from the command line. + * @param argv the variable argument list from the command line. + * @throws ParseException if any of the command line option is incorrect */ - public void process( String [] an_argv ) + public void process( String [] argv ) throws ParseException { // -------------------------------------------------------------------- // Process Command Line Arguements // -------------------------------------------------------------------- - - CLArgsParser l_parser = getParser( an_argv ) ; + // Setting the bind options + options.addOption(hostOption); + options.addOption(portOption); + options.addOption(bindDnOption); + options.addOption(nothingOption); + options.addOption(protocolOption); + options.addOption(passwordOption); + options.addOption(verboseOption); + options.addOption(fileOption); // Get a list of parsed options - List l_argList = l_parser.getArguments() ; - int l_size = l_argList.size() ; - - for ( int l_ii = 0; l_ii < l_size; l_ii++ ) + CommandLine cl = getParser(argv, options); + + host = cl.getOptionValue(HOST_OPT, DEFAULT_HOST); + port = Integer.parseInt(cl.getOptionValue(PORT_OPT, DEFAULT_PORT)); + bindDn = cl.getOptionValue(BINDDN_OPT, ""); + isDryRun = cl.hasOption(NOTHING_OPT); + isVersion3 = Integer.parseInt(cl.getOptionValue(PROTOCOL_OPT, "3")) == 3; + password = cl.getOptionValue(PASSWORD_OPT, ""); + isVerbose = cl.hasOption(VERBOSE_OPT); + + // LdifFile must not be null + ldifFile = cl.getOptionValue(FILE_OPT, null); + + if (ldifFile == null) { - CLOption l_option = ( CLOption ) l_argList.get( l_ii ) ; - - switch ( l_option.getId() ) - { - case CLOption.TEXT_ARGUMENT: - m_AddDn = l_option.getArgument() ; - break ; - - case Opts.HOST_OPT: - super.setHost( l_option.getArgument() ) ; - break ; - - case Opts.PORT_OPT: - super.setPort( Integer.parseInt( - l_option.getArgument() ) ) ; - break ; - - case Opts.BINDDN_OPT: - super.setBindDn( l_option.getArgument() ) ; - break ; - - case Opts.NOTHING_OPT: - super.setIsDryRun( true ) ; - break ; - - case Opts.PROTOCOL_OPT: - super.setIsVersion3( - Integer.parseInt( l_option.getArgument() ) == 3 ) ; - break ; - - case Opts.PASSWORD_OPT: - String l_pwd = l_option.getArgument() ; - if ( l_pwd == null ) - { - super.setPassword( "" ) ; - } - else - { - super.setPassword( l_pwd ) ; - } - break ; - - case Opts.VERBOSE_OPT: - super.setIsVerbose( true ) ; - break ; - - - case Opts.FILE_OPT: - m_ldifFile = l_option.getArgument() ; - break ; - } + throw new ParseException("No ldif file given"); } + + // Added DN must not be null + String[] leftArgs = cl.getArgs(); + + if ((leftArgs == null) || (leftArgs.length == 0)) + { + throw new ParseException("A Distinguised Name (DN) must be given"); + } + + addedDn = leftArgs[0]; + super.setProcessed() ; } } Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/BindOpts.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/BindOpts.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/BindOpts.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/BindOpts.java Wed Feb 2 00:05:21 2005 @@ -205,68 +205,86 @@ package org.apache.ldap.clients ; -import java.util.List ; - -import org.apache.avalon.excalibur.cli.CLOption ; -import org.apache.avalon.excalibur.cli.CLArgsParser ; - +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; /** + * @TODO : using a default configuration file could be an option? * * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols</a> * @author $Author: jmachols $ - * @version $Revision: 1.5 $ + * @version $Revision$ */ public class BindOpts extends Opts { /** + * The options used to Bind + */ + Options options = new Options(); + + /** * The default port to use if none are specified on * command line */ - public static final int DEFAULT_PORT = 1396 ; + public static final String DEFAULT_PORT = "389"; /** * The default host to use if it is not specified on * command lin */ public static final String DEFAULT_HOST = "localhost" ; + + /** + * The default Max Entries is set to 1000 entries. + * It's a better solution than defaulting to 0, + * which means no limit... + */ + public static final String DEFAULT_MAX_ENTRIES = "1000"; + + /** + * The default Max Time is set to 1000 seconds. + * It's a better solution than defaulting to 0, + * which means no limit... + */ + public static final String DEFAULT_MAX_TIME = "1000"; /** * The port used to connect to the LDAP server */ - private int m_port = DEFAULT_PORT ; + protected int port; /** * Toggle switch: false will not send request to the server, * true is the "normal" operating mode */ - private boolean m_isDryRun = false ; + protected boolean isDryRun; /** * Verbose mode, default is off */ - private boolean m_isVerbose = false ; + protected boolean isVerbose; /** * Is the LDIF format version 3 compliant */ - private boolean m_isVersion3 = true ; + protected boolean isVersion3; /** * LDAP server host name or IP */ - private String m_host = DEFAULT_HOST ; + protected String host; /** * DN used to bind to the server */ - private String m_bindDn = "" ; + protected String bindDn; /** * Password of the DN entry used to bind to the server */ - private String m_password = "" ; + protected String password; //------------------------------------------------------------------------- @@ -276,71 +294,71 @@ /** * Set the hostname of the LDAP Server * - * @param a_host Hostname of the LDAP Server + * @param host Hostname of the LDAP Server */ - public void setHost( String a_host ) + public void setHost( String host ) { - m_host = a_host ; + this.host = host ; } /** * Set the port number to connect to server with * - * @param a_port Port number to use to connect to the server + * @param port Port number to use to connect to the server */ - public void setPort( int a_port ) + public void setPort( int port ) { - m_port = a_port ; + this.port = port ; } /** * Set the DN of the user to bind to server with * - * @param a_bindDn DN of the user to bind to server with + * @param bindDn DN of the user to bind to server with */ - public void setBindDn( String a_bindDn ) + public void setBindDn( String bindDn ) { - m_bindDn = a_bindDn ; + this.bindDn = bindDn ; } /** * Password for the DN entry being used to bind * - * @param a_password Password for the DN entry being used to bind + * @param password Password for the DN entry being used to bind */ - public void setPassword( String a_password ) + public void setPassword( String password ) { - m_password = a_password ; + this.password = password ; } /** * Set value of version3 compatibility * - * @param a_isVersion3 True if LDIF is version3 + * @param isVersion3 True if LDIF is version3 */ - public void setIsVersion3( boolean a_isVersion3 ) + public void setIsVersion3( boolean isVersion3 ) { - m_isVersion3 = a_isVersion3 ; + this.isVersion3 = isVersion3 ; } /** * Set verbose mode * - * @param a_isVerbose True if verbose mode is turned on + * @param isVerbose True if verbose mode is turned on */ - public void setIsVerbose( boolean a_isVerbose ) + public void setIsVerbose( boolean isVerbose ) { - m_isVerbose = a_isVerbose ; + this.isVerbose = isVerbose ; } /** * Set the value of the dry run mode * - * @param a_isDryRun True if the dry run mode is turned on + * @param isDryRun True if the dry run mode is turned on */ - public void setIsDryRun( boolean a_isDryRun ) + public void setIsDryRun( boolean isDryRun ) { - m_isDryRun = a_isDryRun ; + this.isDryRun = isDryRun ; } // ------------------------------------------------------------------------ @@ -354,7 +372,7 @@ */ public String getHost() { - return m_host ; + return host ; } /** @@ -364,7 +382,7 @@ */ public int getPort() { - return m_port ; + return port ; } /** @@ -374,7 +392,7 @@ */ public String getBindDn() { - return m_bindDn ; + return bindDn ; } /** @@ -384,7 +402,7 @@ */ public String getPassword() { - return m_password ; + return password ; } /** @@ -394,7 +412,7 @@ */ public boolean isVersion3() { - return m_isVersion3 ; + return isVersion3 ; } /** @@ -404,7 +422,7 @@ */ public boolean isVerbose() { - return m_isVerbose ; + return isVerbose ; } /** @@ -414,9 +432,18 @@ */ public boolean isDryRun() { - return m_isDryRun ; + return isDryRun ; } + /** + * Options used by the LdapBind client + * + * @return An collection of available options for this client + */ + public Options getOptions() + { + return options; + } // ------------------------------------------------------------------------ // Opt Abstract Method Implementations @@ -425,67 +452,35 @@ /** * Process the command line arguments and load the options * - * @param an_argv The command line string + * @param argv The command line string + * @throws ParseException if any of the command line option is incorrect */ - public void process( String [] an_argv ) + public void process(String[] argv) throws ParseException { // -------------------------------------------------------------------- // Process Command Line Arguements // -------------------------------------------------------------------- - - CLArgsParser l_parser = getParser( an_argv ) ; + // Setting the bind options + options.addOption(hostOption); + options.addOption(portOption); + options.addOption(bindDnOption); + options.addOption(nothingOption); + options.addOption(protocolOption); + options.addOption(passwordOption); + options.addOption(verboseOption); // Get a list of parsed options - List l_argList = l_parser.getArguments() ; - int l_size = l_argList.size() ; + CommandLine cl = getParser(argv, options); + + // Commons options + host = cl.getOptionValue(HOST_OPT, DEFAULT_HOST); + port = Integer.parseInt(cl.getOptionValue(PORT_OPT, DEFAULT_PORT)); + bindDn = cl.getOptionValue(BINDDN_OPT, ""); + isDryRun = cl.hasOption(NOTHING_OPT); + isVersion3 = Integer.parseInt(cl.getOptionValue(PROTOCOL_OPT, "3")) == 3; + password = cl.getOptionValue(PASSWORD_OPT, ""); + isVerbose = cl.hasOption(VERBOSE_OPT); - for ( int l_ii = 0; l_ii < l_size; l_ii++ ) - { - CLOption l_option = ( CLOption ) l_argList.get( l_ii ) ; - - switch ( l_option.getId() ) - { - case CLOption.TEXT_ARGUMENT: - System.err.println( "Unknown arg: " - + l_option.getArgument() ) ; - break ; - - case Opts.HOST_OPT: - m_host = l_option.getArgument() ; - break ; - - case Opts.PORT_OPT: - m_port = Integer.parseInt( l_option.getArgument() ) ; - break ; - - case Opts.BINDDN_OPT: - m_bindDn = l_option.getArgument() ; - break ; - - case Opts.NOTHING_OPT: - m_isDryRun = true ; - break ; - - case Opts.PROTOCOL_OPT: - if ( Integer.parseInt( l_option.getArgument() ) != 3 ) - { - m_isVersion3 = false ; - } - break ; - - case Opts.PASSWORD_OPT: - String l_pwd = l_option.getArgument() ; - if ( l_pwd != null ) - { - m_password = l_pwd ; - } - break ; - - case Opts.VERBOSE_OPT: - m_isVerbose = true ; - break ; - } - } super.setProcessed() ; } } Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/DelOpts.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/DelOpts.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/DelOpts.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/DelOpts.java Wed Feb 2 00:05:21 2005 @@ -205,17 +205,15 @@ package org.apache.ldap.clients ; -import java.util.List ; - -import org.apache.avalon.excalibur.cli.CLArgsParser ; -import org.apache.avalon.excalibur.cli.CLOption ; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.ParseException; /** * Delete operation command line options bean. * * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols</a> * @author $Author: jmachols $ - * @version $Revision: 1.3 $ + * @version $Revision$ */ public class DelOpts @@ -224,7 +222,11 @@ /** * DN of the entry to be deleted */ - private String m_deletedDn ; + private String deletedDn ; + + // ------------------------------------------------------------------------ + // Option Accessors + // ------------------------------------------------------------------------ /** * Return the DN of the entry to be deleted @@ -233,16 +235,20 @@ */ public String getDeletedDn() { - return m_deletedDn ; + return deletedDn ; } + //------------------------------------------------------------------------- + // Option Setters + //------------------------------------------------------------------------- + /** * Set the DN of the entry to be deleted - * @param a_deletedDn DN of the entry to be deleted + * @param deletedDn DN of the entry to be deleted */ - public void setDeletedDN( String a_deletedDn ) + public void setDeletedDN( String deletedDn ) { - m_deletedDn = a_deletedDn ; + this.deletedDn = deletedDn ; } // ------------------------------------------------------------------------ // Opt Abstract Method Implementations @@ -252,68 +258,46 @@ /** * Overrides BindOpts.process to be able to get the deleted entry's Dn. * - * @param an_argv the variable argument list from the command line. + * @param argv the variable argument list from the command line. + * @throws ParseException if any of the command line option is incorrect */ - public void process( String [] an_argv ) + public void process( String [] argv ) throws ParseException { // -------------------------------------------------------------------- // Process Command Line Arguements // -------------------------------------------------------------------- - - CLArgsParser l_parser = getParser( an_argv ) ; + // Setting the bind options + options.addOption(hostOption); + options.addOption(portOption); + options.addOption(bindDnOption); + options.addOption(nothingOption); + options.addOption(protocolOption); + options.addOption(passwordOption); + options.addOption(verboseOption); + options.addOption(fileOption); // Get a list of parsed options - List l_argList = l_parser.getArguments() ; - int l_size = l_argList.size() ; - - for ( int l_ii = 0; l_ii < l_size; l_ii++ ) + CommandLine cl = getParser(argv, options); + + // Commons options + host = cl.getOptionValue(HOST_OPT, DEFAULT_HOST); + port = Integer.parseInt(cl.getOptionValue(PORT_OPT, DEFAULT_PORT)); + bindDn = cl.getOptionValue(BINDDN_OPT, ""); + isDryRun = cl.hasOption(NOTHING_OPT); + isVersion3 = Integer.parseInt(cl.getOptionValue(PROTOCOL_OPT, "3")) == 3; + password = cl.getOptionValue(PASSWORD_OPT, ""); + isVerbose = cl.hasOption(VERBOSE_OPT); + + // Deleted DN must not be null + String[] leftArgs = cl.getArgs(); + + if ((leftArgs == null) || (leftArgs.length == 0)) { - CLOption l_option = ( CLOption ) l_argList.get( l_ii ) ; - - switch ( l_option.getId() ) - { - case CLOption.TEXT_ARGUMENT: - m_deletedDn = l_option.getArgument() ; - break ; - - case Opts.HOST_OPT: - super.setHost( l_option.getArgument() ) ; - break ; - - case Opts.PORT_OPT: - super.setPort( Integer.parseInt( l_option.getArgument() ) ); - break ; - - case Opts.BINDDN_OPT: - super.setBindDn( l_option.getArgument() ) ; - break ; - - case Opts.NOTHING_OPT: - super.setIsDryRun( true ) ; - break ; - - case Opts.PROTOCOL_OPT: - super.setIsVersion3( - Integer.parseInt( l_option.getArgument() ) == 3 ) ; - break ; - - case Opts.PASSWORD_OPT: - String l_pwd = l_option.getArgument() ; - if ( l_pwd == null ) - { - super.setPassword( "" ) ; - } - else - { - super.setPassword( l_pwd ) ; - } - break ; - - case Opts.VERBOSE_OPT: - super.setIsVerbose( true ) ; - break ; - } + throw new ParseException("A Distinguised Name (DN) must be given"); } + + deletedDn = leftArgs[0]; + super.setProcessed() ; } } Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapAdd.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapAdd.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapAdd.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapAdd.java Wed Feb 2 00:05:21 2005 @@ -203,7 +203,7 @@ * */ -package org.apache.ldap.clients ; +package org.apache.ldap.clients; import java.io.BufferedReader; import java.io.FileInputStream; @@ -229,220 +229,220 @@ /** * Ldap client for the ADD operation - * - * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols</a> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols </a> * @author $Author: jmachols $ * @version $Revision$ */ -public class LdapAdd - extends LdapBind +public class LdapAdd extends LdapBind { + private String module = "LdapAdd"; /** - * Create the instance of the add client and parse the - * command line arguments + * Create the instance of the add client and parse the command line + * arguments * - * @param a_args a_args Command line arguments + * @param args + * Command line arguments */ - public LdapAdd( String [] a_args ) + public LdapAdd( String[] args ) { - super( new AddOpts(), a_args ) ; + super( args ); } /** - * Get the inputStream to read the LDIF entries from. This will - * either be a file or STDIN based in the command line arguments + * Get the inputStream to read the LDIF entries from. This will either be a + * file or STDIN based in the command line arguments * * @return returns The inputStream to read LDIF entries from */ - public InputStream getStream( ) + public InputStream getStream() { - AddOpts l_opts = ( AddOpts ) super.getOpts() ; - InputStream l_stream = null ; - if ( l_opts.usesLdifFile() ) + AddOpts opts = (AddOpts) super.getOpts(); + InputStream stream = null; + + if( opts.usesLdifFile() ) { try { - l_stream = new FileInputStream ( l_opts.getLdifFile() ) ; + stream = new FileInputStream( opts.getLdifFile() ); } - catch ( FileNotFoundException e ) + catch( FileNotFoundException e ) { - System.out.println( "Could not open LDIF file " - + l_opts.getLdifFile() ) ; - System.exit( -1 ) ; + System.out.println( "Could not open LDIF file " + opts.getLdifFile() ); + System.exit( -1 ); } } else { - l_stream = System.in ; + stream = System.in; } - return l_stream ; + + return stream; } /** - * Parse the input from the InputStream. For each LDIF entry found - * pass it to the modify method for processing. This will only - * segragate different entries, it won't breakdown the individual - * entries. There is a separate LDIF parsing class for that. - * - * @param a_stream The input stream to read the LDIF entries from - */ - public void parseInput ( InputStream a_stream ) + * Parse the input from the InputStream. For each LDIF entry found pass it + * to the modify method for processing. This will only segragate different + * entries, it won't breakdown the individual entries. There is a separate + * LDIF parsing class for that. + * + * @param stream + * The input stream to read the LDIF entries from + */ + public void parseInput( InputStream inputStream ) { - String l_line = new String () ; - StringBuffer l_buf = new StringBuffer() ; - int l_messageNum = 2; - BufferedReader l_stream = new BufferedReader ( new InputStreamReader - ( a_stream ) ) ; + String line = new String(); + StringBuffer buf = new StringBuffer(); + int messageNum = 2; + BufferedReader stream = new BufferedReader( new InputStreamReader( inputStream ) ); try { - while ( ( l_line = l_stream.readLine() ) != null ) + while( (line = stream.readLine()) != null ) { - if ( l_line.startsWith ( "#" ) ) + if( line.startsWith( "#" ) ) { - continue ; + continue; } - if ( l_line.trim().equals( "" ) ) + + if( line.trim().equals( "" ) ) { - add( l_buf, l_messageNum ) ; - l_messageNum++; - l_buf.setLength( 0 ) ; - continue ; + add( buf, messageNum ); + messageNum++; + buf.setLength( 0 ); + continue; } - l_buf.append( l_line ) ; - l_buf.append( '\n' ) ; + + buf.append( line ); + buf.append( '\n' ); } } - catch ( IOException e ) + catch( IOException e ) { - System.out.println( "error reading from input " + e.toString() ) ; - System.exit( -1 ) ; + getLog().error( "error reading from input " + e.toString() ); + System.exit( -1 ); } - add ( l_buf, l_messageNum ) ; + add( buf, messageNum ); } /** - * Prepare the LDIF entry and send it to the encoder, then - * wait for a reponse from the LDAP server on the results - * of the operation. - * - * @param a_buf The string buffer that contain the LDIF entry - * @param msgId message id number - */ - public void add( StringBuffer a_buf, int msgId ) + * Prepare the LDIF entry and send it to the encoder, then wait for a + * reponse from the LDAP server on the results of the operation. + * + * @param a_buf + * The string buffer that contain the LDIF entry + * @param msgId + * message id number + */ + public void add( StringBuffer buf, int msgId ) { - AddRequest l_request = new AddRequestImpl( msgId ) ; + AddRequest request = new AddRequestImpl( msgId ); try { // ------------------------------------------ // We need to change this to lockableAttribute // but that impl needs some work first //------------------------------------------- - String l_dn = new String () ; - LdifParser l_parser = new LdifParserImpl () ; - Attributes l_attributes = new BasicAttributes () ; - l_parser.parse( l_attributes, a_buf.toString() ) ; - Attribute l_attr = l_attributes.get( "dn" ) ; - if ( l_attr == null ) + String dn = new String(); + LdifParser parser = new LdifParserImpl(); + Attributes attributes = new BasicAttributes(); + parser.parse( attributes, buf.toString() ); + Attribute attr = attributes.get( "dn" ); + + if( attr == null ) { - return ; + return; } - l_dn = ( String ) l_attr.get() ; - l_request.setName( l_dn ) ; - l_attributes.remove( "dn" ) ; - l_request.setEntry( l_attributes ) ; - } - catch ( NamingException ne ) - { - System.out.println( "Error parsing file " + ne.toString() ) ; - System.exit( -1 ) ; - } - super.getEncoder().encodeBlocking( null, super.getOut(), l_request ); ; - AddResponse l_addResponse = - ( AddResponse ) super.getDecoder().decode( - null, super.getIn() ) ; - LdapResult l_result = l_addResponse.getLdapResult() ; - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) + + dn = (String) attr.get(); + request.setName( dn ); + attributes.remove( "dn" ); + request.setEntry( attributes ); + } + catch( NamingException ne ) { - System.out.println( "Add of Entry " + l_request.getName() - + " was successful" ) ; + System.out.println( "Error parsing file " + ne.toString() ); + System.exit( -1 ); + } + + super.getEncoder().encodeBlocking( null, super.getOut(), request ); + AddResponse l_addResponse = (AddResponse) super.getDecoder().decode( null, super.getIn() ); + LdapResult l_result = l_addResponse.getLdapResult(); + + if( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) + { + getLog().info( "Add of Entry " + request.getName() + " was successful" ); } else { - System.out.println( "Add of entry " + l_request.getName() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; + getLog().error( + "Add of entry " + request.getName() + " failed for the following reasons provided by the server:\n" + + new String( l_result.getErrorMessage() ) ); } } /** - * The main method of the Modify client. - * - * @param a_args Command line arguments - */ - public static void main( String [] a_args ) + * The main method of the Modify client. + * + * @param a_args + * Command line arguments + */ + public static void main( String[] args ) { - LdapAdd l_client = new LdapAdd( a_args ) ; - BindResponse l_bindResponse = null ; + LdapAdd client = new LdapAdd( args ); + + BindResponse bindResponse = null; try { - l_bindResponse = l_client.bind( 1 ) ; + bindResponse = client.bind( 1 ); } - catch ( MessageException e ) + catch( MessageException e ) { - l_client.getLog().fatalError( - "Failed to bind to server due to message composition failure", - e ) ; - System.exit( 1 ) ; + System.out.println( "Failed to bind to server due to message composition failure" + e.getMessage() ); + System.exit( 1 ); } - catch ( IOException e ) + catch( IOException e ) { - l_client.getLog().fatalError( "Failed to bind to server", e ) ; - System.exit( 1 ) ; + System.out.println( "Failed to bind to server " + e.getMessage() ); + System.exit( 1 ); } // -------------------------------------------------------------------- // Report the results from the response. // -------------------------------------------------------------------- - l_client.getLog().debug( - "Got back BindResponse from the server:\n" ) ; - LdapResult l_result = l_bindResponse.getLdapResult() ; - - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) - { - System.out.println( "Authentication for " - + l_client.getOpts().getBindDn() - + " was successful" ) ; + client.getLog().debug( "Got back BindResponse from the server:\n" ); + LdapResult result = bindResponse.getLdapResult(); + + if( result.getResultCode() == ResultCodeEnum.SUCCESS ) + { + System.out.println( "Authentication for " + client.getOpts().getBindDn() + " was successful" ); } else { - System.out.println( "Authentication for " - + l_client.getOpts().getBindDn() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; - System.exit( -1 ) ; + System.out.println( "Authentication for " + client.getOpts().getBindDn() + + " failed for the following reasons provided by the server:\n" + new String( result.getErrorMessage() ) ); + System.exit( -1 ); } // -------------------------------------------------------------------- - // Get the input method then parse each entry. The parsing method + // Get the input method then parse each entry. The parsing method // will submit the entry to the add routine // -------------------------------------------------------------------- - InputStream l_inStream = l_client.getStream () ; - l_client.parseInput( l_inStream ) ; - + InputStream inStream = client.getStream(); + client.parseInput( inStream ); + // -------------------------------------------------------------------- // We are done, free up the socket connection // -------------------------------------------------------------------- try { - l_client.disconnect() ; + client.disconnect(); } - catch ( IOException e ) + catch( IOException e ) { - l_client.getLog().error( - "Failed to close client connection!", e ) ; + client.getLog().error( "Failed to close client connection!", e ); } } -} +} \ No newline at end of file Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapBind.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapBind.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapBind.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapBind.java Wed Feb 2 00:05:21 2005 @@ -220,10 +220,12 @@ import org.apache.ldap.common.message.MessageEncoder; import org.apache.ldap.common.message.ResultCodeEnum; -import org.apache.avalon.framework.logger.LogKitLogger; -import org.apache.avalon.framework.logger.Logger; -import org.apache.log.Hierarchy; -import org.apache.log.Priority; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.ParseException; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; /** * Ldap client for the ADD operation @@ -234,224 +236,78 @@ */ public class LdapBind { + /** + * Instance of the logger + */ + private static Logger log = Logger.getLogger(LdapBind.class); + + static { + BasicConfigurator.configure(); + } + + /** + * The module's name + */ + private String module = "LdapBind"; /** * Stream used to talk to the server */ - private InputStream m_in = null; + private InputStream in = null; /** * Stream used to read from the server */ - private OutputStream m_out = null; + private OutputStream out = null; /** * ASN1 Encoder class used to send messages to the LDAP server */ - private MessageDecoder m_decoder = null; + private MessageDecoder decoder = null; /** * ASN1 Decoder class used to translate messages from the LDAP server */ - private MessageEncoder m_encoder = null; + private MessageEncoder encoder = null; /** * Command line options */ - private BindOpts m_opts = null; - - /** - * Instance of the logger - */ - private Logger m_log = null; + private static BindOpts opts = null; /** * Socket used to connect to the server */ - private Socket m_socket = null; - - /** - * Take the command line string and pass it into the argument parser, - * which will set our option flags. - * @param a_opts The options to be populated - * @param a_argv The command line string - */ - public LdapBind( BindOpts a_opts, String[] a_argv ) - { - m_opts = a_opts; - m_opts.process( a_argv ); - - if ( m_opts.isVerbose() ) - { - enableLogging( Priority.DEBUG ); - } - else - { - enableLogging( Priority.INFO ); - } - } + private Socket socket = null; /** - * Connect to the LDAP server through a socket and establish the - * Input and Output Streams. All the required information for the - * connection should be in the options from the command line, - * or the default values. + * Get the module's name * - * @throws UnknownHostException The hostname or the Address of - * server could not be found - * @throws IOException There was a error opening or establishing the socket - */ - public void connect() throws UnknownHostException, IOException - { - InetAddress l_address = InetAddress.getByName( m_opts.getHost() ); - m_socket = new Socket( l_address, m_opts.getPort() ); - - // Set up the output stream to write to the server - m_out = new BufferedOutputStream( m_socket.getOutputStream() ); - m_encoder = new MessageEncoder(); - - // Setup the input stream for reading from the server - m_in = m_socket.getInputStream(); - m_decoder = new MessageDecoder(); - } - - /** - * Send the bind request to the LDAP server. - * - * @param id Message number of this request, If this is a simple - * bind, the ID will be one. - * @return The decoder response from the bind request - * @throws UnknownHostException The hostname or the Address of - * server could not be found - * @throws IOException There was a error opening or establishing the socket - */ - public BindResponse bind( int id ) - throws UnknownHostException, IOException - { - BindRequestImpl l_request = new BindRequestImpl( id ); - - // -------------------------------------------------------------------- - // Build BindRequest - // -------------------------------------------------------------------- - - l_request.setName( m_opts.getBindDn() ); - l_request.setSimple( true ); - l_request.setCredentials( m_opts.getPassword().getBytes() ); - l_request.setVersion3( m_opts.isVersion3() ); - - // -------------------------------------------------------------------- - // Connect and deliver request to the server - // -------------------------------------------------------------------- - - if ( m_socket == null ) - { - connect(); - } - - // Send encoded request to server - m_encoder.encodeBlocking( null, m_out, l_request ); - - // Retrieve the response back from server to my last request. - return ( BindResponse ) m_decoder.decode( null, m_in ); - } - - /** - * Close the socket - * - * @throws IOException Could not close the socket + * @return Returns the module's name. */ - public void disconnect() throws IOException + public String getModule() { - if ( m_socket != null ) - { - m_socket.close(); - } + return module; } - + /** * Set the logging priority of the logger object * - * @param a_priority The priority to set the logging to + * @param level The level to set the logging to */ - public void enableLogging( Priority a_priority ) + public void enableLogging( Level level ) { - org.apache.log.Logger l_logger = - Hierarchy.getDefaultHierarchy().getLoggerFor( - getClass().toString() ) ; - l_logger.setPriority( a_priority ) ; - l_logger.debug( "Initializing logger at " + a_priority ) ; - m_log = new LogKitLogger( l_logger ) ; + log.setLevel(level); } /** - * Main method for the Bind client. All this will do is call the bind, - * read the response, close the socket and report the response - * of the bind operation - * - * @param a_args Command line string - */ - public static void main( String[] a_args ) - { - LdapBind l_client = new LdapBind( new BindOpts(), a_args ) ; - BindResponse l_response = null ; - - try - { - l_response = l_client.bind( 1 ) ; - } - catch ( IOException e ) - { - l_client.getLog().fatalError( - "Failed to transmit pdu to server", e ) ; - System.exit( 1 ) ; - } - finally - { - try - { - l_client.disconnect(); - } - catch ( Throwable t ) - { - l_client.getLog().error( - "Error while closing socket connection", t ) ; - } - } - - // -------------------------------------------------------------------- - // Report the results from the response. - // -------------------------------------------------------------------- - - l_client.getLog().debug( - "Got back BindResponse from the server:\n" ) ; - LdapResult l_result = l_response.getLdapResult(); - - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) - { - System.out.println( - "Authentication for " - + l_client.getOpts().getBindDn() - + " was successful" ) ; - } - else - { - System.out.println( - "Authentication for " - + l_client.getOpts().getBindDn() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; - System.exit( -1 ); - } - } - - /** * Get the message decoder used to tranform the ASN1 from the server * * @return Message decoder used to tranform the ASN1 from the server */ public MessageDecoder getDecoder() { - return m_decoder; + return decoder; } /** @@ -463,7 +319,7 @@ */ public MessageEncoder getEncoder() { - return m_encoder; + return encoder; } /** @@ -473,7 +329,7 @@ */ public InputStream getIn() { - return m_in; + return in; } /** @@ -483,7 +339,7 @@ */ public Logger getLog() { - return m_log; + return log; } /** @@ -493,7 +349,7 @@ */ public BindOpts getOpts() { - return m_opts; + return opts; } /** @@ -503,7 +359,7 @@ */ public OutputStream getOut() { - return m_out; + return out; } /** @@ -513,71 +369,80 @@ */ public Socket getSocket() { - return m_socket; + return socket; } /** + * Set the module's name + * + * @param module The module name to set. + */ + public void setModule( String module ) + { + this.module = module; + } + /** * Set the Message decoder used to tranform the ASN1 from the server * - * @param a_decoder Message decoder used to tranform the ASN1 from + * @param decoder Message decoder used to tranform the ASN1 from * server */ - public void setDecoder( MessageDecoder a_decoder ) + public void setDecoder( MessageDecoder decoder ) { - m_decoder = a_decoder; + this.decoder = decoder; } /** * Set the Message encoder used to transform the message into ASN1 * nototation being sent to the server * - * @param a_encoder Message encoder used to transform the message into ASN1 + * @param encoder Message encoder used to transform the message into ASN1 * nototation being sent to the server */ - public void setEncoder( MessageEncoder a_encoder ) + public void setEncoder( MessageEncoder encoder ) { - m_encoder = a_encoder; + this.encoder = encoder; } /** * Set the input stream used to send messages to the server * - * @param a_stream Set the input stream used to send messages to the server + * @param stream Set the input stream used to send messages to the server */ - public void setIn( InputStream a_stream ) + public void setIn( InputStream stream ) { - m_in = a_stream; + in = stream; } /** * Set the instance of the logger * - * @param a_logger logger object + * @param logger logger object */ - public void setLog( Logger a_logger ) + public void setLog( Logger logger ) { - m_log = a_logger; + log = logger; } /** * Set the command line options from the clinet * - * @param a_opts Command line options from the clinet + * @param opts Command line options from the clinet */ - public void setOpts( BindOpts a_opts ) + public void setOpts( BindOpts opts ) { - m_opts = a_opts; + LdapBind.opts = opts; } /** * Set the output stream used to receive messages from the server * - * @param a_stream Output stream used to receive messages from the server + * @param stream Output stream used to receive messages from the server */ - public void setOut( OutputStream a_stream ) + public void setOut( OutputStream stream ) { - m_out = a_stream; + out = stream; } /** @@ -585,9 +450,178 @@ * * @param a_socket Socket used to connect to the server machine */ - public void setSocket( Socket a_socket ) + public void setSocket( Socket socket ) + { + this.socket = socket; + } + + /** + * Take the command line string and pass it into the argument parser, + * which will set our option flags. + * @param argv The command line string + */ + public LdapBind(String[] argv) { + try { + opts = new BindOpts(); + opts.process(argv); + } catch (ParseException pe) { + System.out.println("Error while parsing command line arguments : " + pe.getMessage()); + + StringBuffer sb = new StringBuffer(); + + for (int i=0; i<argv.length; i++) { + sb.append(argv[i]).append(' '); + } + + System.out.println("Command line arguments : " + sb.toString()); + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp( module, opts.getOptions()); + System.exit(1); + } + + if (opts.isVerbose()) { + enableLogging(Level.DEBUG); + } else { + enableLogging(Level.INFO); + } + } + + /** + * Connect to the LDAP server through a socket and establish the + * Input and Output Streams. All the required information for the + * connection should be in the options from the command line, + * or the default values. + * + * @throws UnknownHostException The hostname or the Address of + * server could not be found + * @throws IOException There was a error opening or establishing the socket + */ + public void connect() throws UnknownHostException, IOException + { + InetAddress l_address = InetAddress.getByName( opts.getHost() ); + socket = new Socket( l_address, opts.getPort() ); + + // Set up the output stream to write to the server + out = new BufferedOutputStream( socket.getOutputStream() ); + encoder = new MessageEncoder(); + + // Setup the input stream for reading from the server + in = socket.getInputStream(); + decoder = new MessageDecoder(); + } + + /** + * Send the bind request to the LDAP server. + * + * @param id Message number of this request, If this is a simple + * bind, the ID will be one. + * @return The decoder response from the bind request + * @throws UnknownHostException The hostname or the Address of + * server could not be found + * @throws IOException There was a error opening or establishing the socket + */ + public BindResponse bind( int id ) + throws UnknownHostException, IOException + { + BindRequestImpl request = new BindRequestImpl( id ); + + // -------------------------------------------------------------------- + // Build BindRequest + // -------------------------------------------------------------------- + + request.setName( opts.getBindDn() ); + request.setSimple( true ); + request.setCredentials( opts.getPassword().getBytes() ); + request.setVersion3( opts.isVersion3() ); + + // -------------------------------------------------------------------- + // Connect and deliver request to the server + // -------------------------------------------------------------------- + + if ( socket == null ) + { + connect(); + } + + // Send encoded request to server + encoder.encodeBlocking( null, out, request ); + + // Retrieve the response back from server to my last request. + return ( BindResponse ) decoder.decode( null, in ); + } + + /** + * Close the socket + * + * @throws IOException Could not close the socket + */ + public void disconnect() throws IOException { - m_socket = a_socket; + if ( socket != null ) + { + socket.close(); + } } + /** + * Main method for the Bind client. All this will do is call the bind, + * read the response, close the socket and report the response + * of the bind operation + * + * @param args Command line string + */ + public static void main( String[] args ) + { + LdapBind client = new LdapBind(args) ; + + BindResponse response = null ; + + try + { + response = client.bind( 1 ) ; + } + catch ( IOException e ) + { + client.getLog().fatal( + "Failed to transmit pdu to server", e ) ; + System.exit( 1 ) ; + } + finally + { + try + { + client.disconnect(); + } + catch ( Throwable t ) + { + client.getLog().error( + "Error while closing socket connection", t ) ; + } + } + + // -------------------------------------------------------------------- + // Report the results from the response. + // -------------------------------------------------------------------- + + client.getLog().debug( + "Got back BindResponse from the server:\n" ) ; + LdapResult result = response.getLdapResult(); + + if ( result.getResultCode() == ResultCodeEnum.SUCCESS ) + { + System.out.println( + "Authentication for " + + client.getOpts().getBindDn() + + " was successful" ) ; + } + else + { + System.out.println( + "Authentication for " + + client.getOpts().getBindDn() + + " failed for the following reasons provided by the server:\n" + + new String( result.getErrorMessage() ) ) ; + System.exit( -1 ); + } + } } Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapDel.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapDel.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapDel.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapDel.java Wed Feb 2 00:05:21 2005 @@ -203,150 +203,138 @@ * */ -package org.apache.ldap.clients ; +package org.apache.ldap.clients; +import java.io.IOException; -import java.io.IOException ; - -import org.apache.ldap.common.message.LdapResult ; -import org.apache.ldap.common.message.BindResponse ; -import org.apache.ldap.common.message.ResultCodeEnum ; -import org.apache.ldap.common.message.MessageException ; +import org.apache.ldap.common.message.LdapResult; +import org.apache.ldap.common.message.BindResponse; +import org.apache.ldap.common.message.ResultCodeEnum; +import org.apache.ldap.common.message.MessageException; import org.apache.ldap.common.message.DeleteRequestImpl; import org.apache.ldap.common.message.DeleteResponse; import org.apache.ldap.common.message.DeleteRequest; - /** * Ldap client for the DELETE operation - * - * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols</a> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols </a> * @author $Author: jmachols $ * @version $Revision$ */ -public class LdapDel - extends LdapBind +public class LdapDel extends LdapBind { + private String module = "LdapDel"; /** - * Create an instance of the delete client and parse the command - * line arguments + * Create an instance of the delete client and parse the command line + * arguments * - * @param a_args Command line arguments + * @param args + * Command line arguments */ - public LdapDel( String [] a_args ) + public LdapDel( String[] args ) { - super( new DelOpts(), a_args ) ; + super( args ); } - /** * Encode the message for the delete of the entry and send the encoded - * message to the serever. Then wait for the serever to respond and - * decode that message + * message to the serever. Then wait for the serever to respond and decode + * that message * - * @param id Message number being sent to the server in this - * transaction. Since bind will be the fist, start with 2. + * @param id + * Message number being sent to the server in this transaction. + * Since bind will be the fist, start with 2. * * @return The LDAP response to the operation from the server */ public DeleteResponse delete( int id ) { - DelOpts l_opts = ( DelOpts ) super.getOpts() ; - DeleteRequest l_request = new DeleteRequestImpl( id ) ; - l_request.setName( l_opts.getDeletedDn() ) ; + DelOpts opts = (DelOpts) super.getOpts(); + DeleteRequest request = new DeleteRequestImpl( id ); + request.setName( opts.getDeletedDn() ); // Send encoded request to server - super.getEncoder().encodeBlocking( null, super.getOut(), l_request ) ; + super.getEncoder().encodeBlocking( null, super.getOut(), request ); // Retrieve the response back from server to my last request. - return ( DeleteResponse ) super.getDecoder().decode( - null, super.getIn() ) ; + return (DeleteResponse) super.getDecoder().decode( null, super.getIn() ); } - /** - * The main will call trhe bind operation first. If that is successful, - * call the delete and write the reponse from the server to the console + * The main will call trhe bind operation first. If that is successful, call + * the delete and write the reponse from the server to the console * - * @param a_args Command line string + * @param args + * Command line string */ - public static void main( String [] a_args ) + public static void main( String[] args ) { - LdapDel l_client = new LdapDel( a_args ) ; - BindResponse l_bindResponse = null ; + LdapDel client = new LdapDel( args ); + + BindResponse bindResponse = null; try { - l_bindResponse = l_client.bind( 1 ) ; + bindResponse = client.bind( 1 ); } - catch ( MessageException e ) + catch( MessageException e ) { - l_client.getLog().fatalError( - "Failed to bind to server due to message composition failure", - e ) ; - System.exit( 1 ) ; + System.out.println( "Failed to bind to server due to message composition failure" + e.getMessage() ); + System.exit( 1 ); } - catch ( IOException e ) + catch( IOException e ) { - l_client.getLog().fatalError( "Failed to bind to server", e ) ; - System.exit( 1 ) ; + System.out.println( "Failed to bind to server" + e.getMessage() ); + System.exit( 1 ); } - // -------------------------------------------------------------------- // Report the results from the response. // -------------------------------------------------------------------- - l_client.getLog().debug( - "Got back BindResponse from the server:\n" ) ; - LdapResult l_result = l_bindResponse.getLdapResult() ; + client.getLog().debug( "Got back BindResponse from the server:\n" ); + LdapResult result = bindResponse.getLdapResult(); - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) + if( result.getResultCode() == ResultCodeEnum.SUCCESS ) { - System.out.println( "Authentication for " - + l_client.getOpts().getBindDn() + " was successful" ) ; + client.getLog().info( "Authentication for " + client.getOpts().getBindDn() + " was successful" ); } else { - System.out.println( "Authentication for " - + l_client.getOpts().getBindDn() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; - System.exit( -1 ) ; + client.getLog().error( + "Authentication for " + client.getOpts().getBindDn() + + " failed for the following reasons provided by the server:\n" + + new String( result.getErrorMessage() ) ); + System.exit( -1 ); } - // -------------------------------------------------------------------- // Perform delete operation and report results // -------------------------------------------------------------------- - DeleteResponse l_delResponse = l_client.delete( 2 ) ; - l_result = l_delResponse.getLdapResult() ; + DeleteResponse l_delResponse = client.delete( 2 ); + result = l_delResponse.getLdapResult(); - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) + if( result.getResultCode() == ResultCodeEnum.SUCCESS ) { - System.out.println( "Delete of entry " - + ( ( DelOpts ) l_client.getOpts() ).getDeletedDn() - + " was successful" ) ; + System.out.println( "Delete of entry " + ((DelOpts) client.getOpts()).getDeletedDn() + " was successful" ); } else { - System.out.println( "Delete of entry " - + ( ( DelOpts ) l_client.getOpts() ).getDeletedDn() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; - System.exit( -1 ) ; + System.out.println( "Delete of entry " + ((DelOpts) client.getOpts()).getDeletedDn() + + " failed for the following reasons provided by the server:\n" + new String( result.getErrorMessage() ) ); + System.exit( -1 ); } try { - l_client.disconnect() ; + client.disconnect(); } - catch ( IOException e ) + catch( IOException e ) { - l_client.getLog().error( "Failed to close client connection!", - e ) ; + client.getLog().error( "Failed to close client connection!", e ); } } -} +} \ No newline at end of file Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapModify.java URL: http://svn.apache.org/viewcvs/incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapModify.java?view=diff&r1=149495&r2=149496 ============================================================================== --- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapModify.java (original) +++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/LdapModify.java Wed Feb 2 00:05:21 2005 @@ -203,8 +203,7 @@ * */ -package org.apache.ldap.clients ; - +package org.apache.ldap.clients; import java.io.BufferedReader; import java.io.FileInputStream; @@ -228,221 +227,213 @@ import org.apache.ldap.common.message.ModifyResponse; import org.apache.ldap.common.message.ResultCodeEnum; - /** - * Ldap Modify Client + * Ldap Modify Client * - * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Machols </a> * @author $$Author: jmachols $$ * @version $$Revision$$ */ -public class LdapModify - extends LdapBind +public class LdapModify extends LdapBind { + private String module = "LdapModify"; - /** - * Create the client instance and loads the options from - * the command line arguments - * - * @param a_args Command line arguments - */ - public LdapModify( String [] a_args ) - { - super( new ModifyOpts(), a_args ) ; - } - - /** - * Get the inputStream to read the LDIF entries from. This will - * either be a file or STDIN based in the command line arguments - * - * @return returns The inputStream to read LDIF entries from - */ - public InputStream getStream( ) - { - ModifyOpts l_opts = ( ModifyOpts ) super.getOpts() ; - InputStream l_stream = null ; - if ( l_opts.usesLdifFile() ) - { - try - { - l_stream = new FileInputStream ( l_opts.getLdifFile() ) ; - } - catch ( FileNotFoundException e ) - { - System.out.println( "Could not open LDIF file " - + l_opts.getLdifFile() ) ; - System.exit( -1 ) ; - } - } - else - { - l_stream = System.in ; + /** + * Create the client instance and loads the options from the command line + * arguments + * + * @param args + * Command line arguments + */ + public LdapModify( String[] args ) + { + super( args ); + } + + /** + * Get the inputStream to read the LDIF entries from. This will either be a + * file or STDIN based in the command line arguments + * + * @return returns The inputStream to read LDIF entries from + */ + public InputStream getStream() + { + ModifyOpts opts = (ModifyOpts) super.getOpts(); + InputStream stream = null; + if( opts.usesLdifFile() ) + { + try + { + stream = new FileInputStream( opts.getLdifFile() ); + } + catch( FileNotFoundException e ) + { + System.out.println( "Could not open LDIF file " + opts.getLdifFile() ); + System.exit( -1 ); + } + } + else + { + stream = System.in; + } + return stream; } - return l_stream ; - } - /** - * Parse the input from the InputStream. For each LDIF entry found - * pass it to the modify method for processing. This will only - * segragate different entries, it won't breakdown the individual - * entries. There is a separate LDIF parsing class for that. - * - * @param a_stream The input stream to read the LDIF entries from - */ - public void parseInput ( InputStream a_stream ) - { - // Setup the request - String l_line = new String () ; - StringBuffer l_buf = new StringBuffer() ; - int l_messageNum = 2; - BufferedReader l_stream = new BufferedReader ( new InputStreamReader - ( a_stream ) ) ; - try - { - while ( ( l_line = l_stream.readLine() ) != null ) - { - if ( l_line.startsWith ( "#" ) ) - { - continue ; - } - if ( l_line.trim().equals( "" ) ) - { - modify( l_buf, l_messageNum ) ; - l_messageNum++; - l_buf.setLength( 0 ) ; - continue ; - } - l_buf.append( l_line ) ; - l_buf.append( '\n' ) ; - } - } - catch ( IOException e ) - { - System.out.println( "error reading from input " + e.toString() ) ; - System.exit( -1 ) ; - } - modify( l_buf, l_messageNum ) ; - } - - /** - * Prepare the LDIF entry and send it to the encoder, then - * wait for a reponse from the LDAP server on the results - * of the operation. - * - * @param a_buf The string buffer that contain the LDIF entry - * @param id the message id for the session - */ - public void modify( StringBuffer a_buf, int id ) - { - ModifyRequest l_request = new ModifyRequestImpl( id ) ; - try - { - // ------------------------------------------ - // We need to change this to lockableAttribute - // but that impl needs some work first - //------------------------------------------- - LdifParser l_parser = new LdifParserImpl () ; - LdifEntry l_ldifEntry = null ; - l_ldifEntry = l_parser.parse( a_buf.toString() ) ; - - l_request.setName( l_ldifEntry.getDn() ) ; - LinkedList l_items = l_ldifEntry.getModificationItems() ; - for ( int l_index = 0; l_index < l_items.size(); l_index ++ ) - { - l_request.addModification( - ( ModificationItem ) l_items.get( l_index ) ) ; - } - } - catch ( NamingException ne ) - { - System.out.println( "Error parsing file " + ne.toString() ) ; - System.exit( -1 ) ; + /** + * Parse the input from the InputStream. For each LDIF entry found pass it + * to the modify method for processing. This will only segragate different + * entries, it won't breakdown the individual entries. There is a separate + * LDIF parsing class for that. + * + * @param a_stream + * The input stream to read the LDIF entries from + */ + public void parseInput( InputStream a_stream ) + { + // Setup the request + String line = new String(); + StringBuffer buf = new StringBuffer(); + int messageNum = 2; + BufferedReader stream = new BufferedReader( new InputStreamReader( a_stream ) ); + try + { + while( (line = stream.readLine()) != null ) + { + if( line.startsWith( "#" ) ) + { + continue; + } + if( line.trim().equals( "" ) ) + { + modify( buf, messageNum ); + messageNum++; + buf.setLength( 0 ); + continue; + } + buf.append( line ); + buf.append( '\n' ); + } + } + catch( IOException e ) + { + System.out.println( "error reading from input " + e.toString() ); + System.exit( -1 ); + } + modify( buf, messageNum ); } - super.getEncoder().encodeBlocking( null, super.getOut(), l_request ); ; - ModifyResponse l_modifyResponse = - ( ModifyResponse ) super.getDecoder().decode( null, super.getIn() ) ; - LdapResult l_result = l_modifyResponse.getLdapResult() ; - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) + /** + * Prepare the LDIF entry and send it to the encoder, then wait for a + * reponse from the LDAP server on the results of the operation. + * + * @param a_buf + * The string buffer that contain the LDIF entry + * @param id + * the message id for the session + */ + public void modify( StringBuffer a_buf, int id ) { - System.out.println( "Modify of Entry " + l_request.getName() - + " was successful" ) ; + ModifyRequest request = new ModifyRequestImpl( id ); + try + { + // ------------------------------------------ + // We need to change this to lockableAttribute + // but that impl needs some work first + //------------------------------------------- + LdifParser parser = new LdifParserImpl(); + LdifEntry ldifEntry = null; + ldifEntry = parser.parse( a_buf.toString() ); + + request.setName( ldifEntry.getDn() ); + LinkedList items = ldifEntry.getModificationItems(); + for( int index = 0; index < items.size(); index++ ) + { + request.addModification( (ModificationItem) items.get( index ) ); + } + } + catch( NamingException ne ) + { + System.out.println( "Error parsing file " + ne.toString() ); + System.exit( -1 ); + } + + super.getEncoder().encodeBlocking( null, super.getOut(), request ); + + ModifyResponse modifyResponse = (ModifyResponse) super.getDecoder().decode( null, super.getIn() ); + LdapResult result = modifyResponse.getLdapResult(); + + if( result.getResultCode() == ResultCodeEnum.SUCCESS ) + { + System.out.println( "Modify of Entry " + request.getName() + " was successful" ); + } + else + { + System.out.println( "Modify of entry " + request.getName() + + " failed for the following reasons provided by the server:\n" + new String( result.getErrorMessage() ) ); + } } - else + + /** + * The main method of the Modify client. + * + * @param a_args + * Command line arguments + */ + public static void main( String[] args ) { - System.out.println( "Modify of entry " + l_request.getName() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; - } - } + LdapModify client = new LdapModify( args ); - /** - * The main method of the Modify client. - * - * @param a_args Command line arguments - */ - public static void main ( String [] a_args ) - { - LdapModify l_client = new LdapModify( a_args ) ; - BindResponse l_bindResponse = null ; + BindResponse bindResponse = null; - try - { - l_bindResponse = l_client.bind( 1 ) ; - } - catch ( MessageException e ) - { - l_client.getLog().fatalError( - "Failed to bind to server due to message composition failure", - e ) ; - System.exit( 1 ) ; - } - catch ( IOException e ) - { - l_client.getLog().fatalError( "Failed to bind to server", e ) ; - System.exit( 1 ) ; - } + try + { + bindResponse = client.bind( 1 ); + } + catch( MessageException e ) + { + System.out.println( "Failed to bind to server due to message composition failure" + e.getMessage() ); + System.exit( 1 ); + } + catch( IOException e ) + { + System.out.println( "Failed to bind to server" + e.getMessage() ); + System.exit( 1 ); + } - // -------------------------------------------------------------------- - // Report the results from the response. - // -------------------------------------------------------------------- - l_client.getLog().debug( - "Got back BindResponse from the server:\n" ) ; - LdapResult l_result = l_bindResponse.getLdapResult() ; + // -------------------------------------------------------------------- + // Report the results from the response. + // -------------------------------------------------------------------- + client.getLog().debug( "Got back BindResponse from the server:\n" ); + LdapResult result = bindResponse.getLdapResult(); - if ( l_result.getResultCode() == ResultCodeEnum.SUCCESS ) - { - System.out.println( "Authentication for " - + l_client.getOpts().getBindDn() - + " was successful" ) ; - } - else - { - System.out.println( "Authentication for " - + l_client.getOpts().getBindDn() - + " failed for the following reasons provided by the server:\n" - + new String( l_result.getErrorMessage() ) ) ; - System.exit( -1 ) ; - } + if( result.getResultCode() == ResultCodeEnum.SUCCESS ) + { + System.out.println( "Authentication for " + client.getOpts().getBindDn() + " was successful" ); + } + else + { + System.out.println( "Authentication for " + client.getOpts().getBindDn() + + " failed for the following reasons provided by the server:\n" + new String( result.getErrorMessage() ) ); + System.exit( -1 ); + } - // -------------------------------------------------------------------- - // Get the input method then parse each entry. The parsing method - // will submit the entry to the add routine - // -------------------------------------------------------------------- - InputStream l_inStream = l_client.getStream () ; - l_client.parseInput( l_inStream ) ; - - // -------------------------------------------------------------------- - // We are done, free up the socket connection - // -------------------------------------------------------------------- - try - { - l_client.disconnect() ; - } - catch ( IOException e ) - { - l_client.getLog().error( "Failed to close client connection!", e ) ; - } - } + // -------------------------------------------------------------------- + // Get the input method then parse each entry. The parsing method + // will submit the entry to the add routine + // -------------------------------------------------------------------- + InputStream inStream = client.getStream(); + client.parseInput( inStream ); -} + // -------------------------------------------------------------------- + // We are done, free up the socket connection + // -------------------------------------------------------------------- + try + { + client.disconnect(); + } + catch( IOException e ) + { + client.getLog().error( "Failed to close client connection!", e ); + } + } +} \ No newline at end of file
