directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r373960 - in /directory/trunks/apacheds: simple/installers/ simple/main/src/main/resources/ tools/ tools/src/main/java/org/apache/directory/server/tools/ tools/src/main/manifest/ tools/src/main/resources/ tools/src/main/resources/org/ tools...
Date Wed, 01 Feb 2006 01:40:03 GMT
Author: akarasulu
Date: Tue Jan 31 17:39:47 2006
New Revision: 373960

URL: http://svn.apache.org/viewcvs?rev=373960&view=rev
Log:
Added apacheds tools executable with multiple commands:

[akarasulu@newton tools]$ java -jar /usr/local/apacheds-1.0-RC1-SNAPSHOT/bin/apacheds-tools.jar  help
       _                     _          ____  ____    _____           _
      / \   _ __   __ _  ___| |__   ___|  _ \/ ___|  |_   _|__   ___ | |___
     / _ \ | '_ \ / _` |/ __| '_ \ / _ \ | | \___ \    | |/ _ \ / _ \| / __|
    / ___ \| |_) | (_| | (__| | | |  __/ |_| |___) |   | | (_) | (_) | \__ \
   /_/   \_\ .__/ \__,_|\___|_| |_|\___|____/|____/    |_|\___/ \___/|_|___/
           |_|

usage: apacheds-tools <command> [options]

Global options:
 -c,--configuration   force loading the server.xml (requires -i)
 -i,--install-path    path to apacheds installation directory
 -b,--banner          suppress banner print outs
 -d,--debug           toggle debug mode
 -q,--quiet           keep the noise down to a minimum
 -v,--verbose         toggle verbose debugging
 -version             print the version information and exit

Type "apacheds-tools help <command>" for help on a command.

Avalable commands:
        help
        notifications
        dump
        graceful
        diagnostic

These apacheds-tools are used to manage the Apache Directory Server.
For additional information, see http://directory.apache.org/

The commands do the following:
   - help ... you know what this is
   - notifications ... demonstrates NoD and graceful disconnect and in the near
     future will have interfaces for launching external processes for delivering
     notifications
   - dump ... dumps the contents of partitions as LDIF see options of command
   - graceful ... sends a graceful shutdown extended request to the server
   - diagnostic ... sends an extended request to launch a diagnostic UI for
     troubleshooting the server

Things that may be added:
   - import-ldap ... imports an LDIF into the server via LDAP as a user
   - import-jdbm ... imports an LDIF as is into a partition
   - changes ... interface to detecting and responding to changes to entries
     within the server using psearch
   - sync ... forces backing store synchronization
   - index ... builds, and rebuilds indices

NOTE: the executable jar is designed to sit within the ./bin directory of the 
installation layout so it has access to all needed jars in ../lib.  It can 
read the apacheds configuration to have commands operate on the server while
the server is up or when it is down.


Added:
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ApachedsTools.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DiagnosticCommand.java
      - copied, changed from r373767, directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/LaunchDiagnosticUi.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DisconnectNotificationCommand.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpCommand.java
      - copied, changed from r373767, directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpTool.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdownCommand.java
      - copied, changed from r373767, directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdown.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ToolCommand.java
    directory/trunks/apacheds/tools/src/main/manifest/
    directory/trunks/apacheds/tools/src/main/manifest/MANIFEST.MF
    directory/trunks/apacheds/tools/src/main/resources/
    directory/trunks/apacheds/tools/src/main/resources/org/
    directory/trunks/apacheds/tools/src/main/resources/org/apache/
    directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/
    directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/
    directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/
    directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/ApachedsTools.properties   (with props)
Removed:
    directory/trunks/apacheds/simple/main/src/main/resources/log4j.properties
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpTool.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdown.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/LaunchDiagnosticUi.java
    directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/Notifications.java
Modified:
    directory/trunks/apacheds/simple/installers/pom.xml
    directory/trunks/apacheds/tools/pom.xml

Modified: directory/trunks/apacheds/simple/installers/pom.xml
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/simple/installers/pom.xml?rev=373960&r1=373959&r2=373960&view=diff
==============================================================================
--- directory/trunks/apacheds/simple/installers/pom.xml (original)
+++ directory/trunks/apacheds/simple/installers/pom.xml Tue Jan 31 17:39:47 2006
@@ -40,6 +40,11 @@
       <version>1.0.1</version>
     </dependency>
     <dependency>
+      <groupId>commons-cli</groupId>
+      <artifactId>commons-cli</artifactId>
+      <version>1.0</version>
+    </dependency>
+    <dependency>
       <groupId>springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>1.2.1</version>

Modified: directory/trunks/apacheds/tools/pom.xml
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/pom.xml?rev=373960&r1=373959&r2=373960&view=diff
==============================================================================
--- directory/trunks/apacheds/tools/pom.xml (original)
+++ directory/trunks/apacheds/tools/pom.xml Tue Jan 31 17:39:47 2006
@@ -15,20 +15,47 @@
   <packaging>jar</packaging>  
   <dependencies>
     <dependency>
-      <groupId>org.apache.directory.server</groupId>
-      <artifactId>org.apache.ldap.server.core</artifactId>
+      <groupId>org.apache.directory.server.standalone.simple</groupId>
+      <artifactId>org.apache.ldap.server.standalone.simple.main</artifactId>
       <version>0.9.4-SNAPSHOT</version>
     </dependency>
     <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>nlog4j</artifactId>
-      <version>1.2.19</version>
+      <groupId>org.apache.directory.daemon</groupId>
+      <artifactId>daemon-bootstrappers</artifactId>
+      <version>0.9.4-SNAPSHOT</version>
     </dependency>
     <dependency>
       <groupId>commons-cli</groupId>
       <artifactId>commons-cli</artifactId>
       <version>1.0</version>
     </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.1</version>
+    </dependency>
   </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <configuration>
+          <archive>
+            <manifestFile>src/main/manifest/MANIFEST.MF</manifestFile>
+            <manifest>
+              <mainClass>org.apache.directory.server.tools.ApachedsTools</mainClass>
+            </manifest>
+          </archive>
+        </configuration>
+      </plugin>
+    </plugins>
+    <resources>
+      <resource>
+        <directory>src/main/resources</directory>
+        <filtering>true</filtering>
+      </resource>
+    </resources>
+  </build>
 </project>
 

Added: directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ApachedsTools.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ApachedsTools.java?rev=373960&view=auto
==============================================================================
--- directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ApachedsTools.java (added)
+++ directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ApachedsTools.java Tue Jan 31 17:39:47 2006
@@ -0,0 +1,293 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.server.tools;
+
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.apache.ldap.server.configuration.ServerStartupConfiguration;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.FileSystemXmlApplicationContext;
+
+
+/**
+ * The main() application which executes command targets.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ApachedsTools
+{
+    private final static Map commands = new HashMap();
+    private final static Options global = new Options();
+    private final static Properties props = new Properties();
+    private final static String version;
+    
+    static
+    {
+        ToolCommand command;
+        command = new DumpCommand();
+        commands.put( command.getName(), command );
+        command = new GracefulShutdownCommand();
+        commands.put( command.getName(), command );
+        command = new DiagnosticCommand();
+        commands.put( command.getName(), command );
+        command = new DisconnectNotificationCommand();
+        commands.put( command.getName(), command );
+
+        Option op = new Option( "i", "install-path", true, "path to apacheds installation directory" );
+        global.addOption( op );
+        op = new Option( "b", "banner", false, "suppress banner print outs" );
+        global.addOption( op );
+        op = new Option( "d", "debug", false, "toggle debug mode" );
+        global.addOption( op );
+        op = new Option( "v", "verbose", false, "toggle verbose debugging" );
+        global.addOption( op );
+        op = new Option( "q", "quiet", false, "keep the noise down to a minimum" );
+        global.addOption( op );
+        op = new Option( "c", "configuration", false, "force loading the server.xml (requires -i)" );
+        global.addOption( op );
+        op = new Option( "version", false, "print the version information and exit" );
+        global.addOption( op );
+        
+        try
+        {
+            props.load( ApachedsTools.class.getResourceAsStream( "ApachedsTools.properties" ) );
+        }
+        catch ( IOException e )
+        {
+            e.printStackTrace();
+        }
+        
+        version = props.getProperty( "apacheds.tools.version" );
+    }
+    
+    
+    private static boolean hasBannerOption( String[] args )
+    {
+        for ( int ii = 0; ii < args.length; ii++ )
+        {
+            if ( args[ii].equals( "-b" ) || args[ii].equals( "-banner" ))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+
+    public static void main( String [] args ) throws Exception
+    {
+        if ( ! hasBannerOption( args ) )
+        {
+            printBanner();
+        }
+
+        if ( args.length == 0 )
+        {
+            System.err.println( "Type apacheds-tools help for usage." );
+            System.exit( 1 );
+        }
+        
+        // help is a special command 
+        String command = args[0].toLowerCase();
+        if ( "help".equals( command ) )
+        {
+            CommandLine cmdline = getCommandLine( command, args );
+            if ( cmdline.getArgs().length > 1 )
+            {
+                helpOnCommand( cmdline.getArgs()[1] );
+                System.exit( 0 );
+            }
+            else
+            {
+                printUsage();
+                System.exit( 0 );
+            }
+        }
+        else if ( command.equals( "-version" ) ) 
+        {
+            System.out.println( "apacheds-tools version " + version );
+            System.exit( 0 );
+        }
+        
+        ToolCommand cmd = ( ToolCommand ) commands.get( command );
+        if ( cmd == null )
+        {
+            System.err.println( "Unknown command: " + args[0] );
+            System.err.println( "Type apacheds-tools help for usage." );
+            System.exit( 1 );
+        }        
+        
+        CommandLine cmdline = getCommandLine( command, args );
+        if ( cmdline.hasOption( 'd' ) )
+        {
+            cmd.setDebugEnabled( true );
+            dumpArgs( "raw command line arguments: ", args );
+            dumpArgs( "parsed arguments: ", cmdline.getArgs() );
+        }
+        
+        cmd.setQuietEnabled( cmdline.hasOption( 'q' ) );
+        cmd.setDebugEnabled( cmdline.hasOption( 'd' ) );
+        cmd.setVerboseEnabled( cmdline.hasOption( 'v' ) );
+        cmd.setVersion( version );
+        if ( cmdline.getOptionValue( 'i' ) != null )
+        {
+            cmd.setLayout( cmdline.getOptionValue( 'i' ) );
+            if ( ! cmd.isQuietEnabled() )
+            {
+                System.out.println( "loading settings from: " + cmd.getLayout().getConfigurationFile() );
+            }
+            ApplicationContext factory = null;
+            URL configUrl = configUrl = cmd.getLayout().getConfigurationFile().toURL();
+            factory = new FileSystemXmlApplicationContext( configUrl.toString() );
+            cmd.setConfiguration( ( ServerStartupConfiguration ) factory.getBean( "configuration" ) );
+        }
+        else if ( cmdline.hasOption( 'c' ) )
+        {
+            System.err.println( "forced configuration load (-c) requires the -i option" );
+            System.exit( 1 );
+        }
+        
+        cmd.execute( cmdline );
+    }
+    
+
+    private static CommandLine getCommandLine( String command, String[] args )
+    {
+        Options all = allOptions( command );
+        CommandLineParser parser = new PosixParser();
+        CommandLine cmdline = null;
+        try
+        {
+            cmdline = parser.parse( all, args );
+        }
+        catch( ParseException e )
+        {
+            System.err.println( "Command line parsing failed for " + command 
+                + ".  Reason: " + e.getMessage() );
+            System.exit( 1 );
+        }
+        return cmdline;
+    }
+    
+    
+    private static Options allOptions( String command )
+    {
+        if ( command.equals( "help" ) )
+        {
+            return global;
+        }
+        
+        Options all = new Options();
+        ToolCommand cmd = ( ToolCommand ) commands.get( command );
+        for ( Iterator ii = global.getOptions().iterator(); ii.hasNext(); )
+        {
+            all.addOption( ( Option ) ii.next() );
+        }
+        
+        for ( Iterator ii = cmd.getOptions().getOptions().iterator(); ii.hasNext(); )
+        {
+            all.addOption( ( Option ) ii.next() );
+        }
+        return all;
+    }
+
+    
+    private static void dumpArgs( String msg, String[] args )
+    {
+        if ( args.length == 0 )
+        {
+            System.out.println( msg );
+            System.out.println( "\t NONE" );
+            return;
+        }
+        
+        StringBuffer buf = new StringBuffer();
+        buf.append( msg ).append( "\n" );
+        for ( int ii = 0; ii < args.length; ii++ )
+        {
+            buf.append( "\targs[" + ii + "] = " ).append( args[ii] ).append( "\n" );
+        }
+        System.out.println( buf );
+    }
+    
+    
+    private static void helpOnCommand( String command )
+    {
+        if ( command.equals( "help" ) )
+        {
+            printUsage();
+            System.exit( 0 );
+        }
+        if ( commands.containsKey( command ) )
+        {
+            ToolCommand cmd = ( ToolCommand ) commands.get( command );
+            HelpFormatter formatter = new HelpFormatter();
+            formatter.printHelp( "apacheds-tools "+cmd+" [options]", cmd.getOptions() );
+        }
+        else
+        {
+            System.err.println( command + ": unknown command" );
+            System.exit( 1 );
+        }
+    }
+
+
+    private static void printUsage()
+    {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp( "apacheds-tools <command> [options]", 
+            "\nGlobal options:", global, "\nType \"apacheds-tools help <command>\" for help on a command." );
+        System.out.println( "\nAvalable commands:" );
+        Iterator it = commands.values().iterator();
+        System.out.println( "\thelp" );
+        while( it.hasNext() )
+        {
+            System.out.println( "\t" + it.next() );
+        }
+        
+        System.out.println( "\nThese apacheds-tools are used to manage the Apache Directory Server." );
+        System.out.println( "For additional information, see http://directory.apache.org/" );
+    }
+    
+
+    public static final String BANNER = 
+        "       _                     _          ____  ____    _____           _      \n" +
+        "      / \\   _ __   __ _  ___| |__   ___|  _ \\/ ___|  |_   _|__   ___ | |___  \n" +
+        "     / _ \\ | '_ \\ / _` |/ __| '_ \\ / _ \\ | | \\___ \\    | |/ _ \\ / _ \\| / __| \n" +
+        "    / ___ \\| |_) | (_| | (__| | | |  __/ |_| |___) |   | | (_) | (_) | \\__ \\ \n" +
+        "   /_/   \\_\\ .__/ \\__,_|\\___|_| |_|\\___|____/|____/    |_|\\___/ \\___/|_|___/ \n" +
+        "           |_|                                                               \n";
+
+    public static void printBanner()
+    {
+        System.out.println( BANNER );
+    }
+}

Copied: directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DiagnosticCommand.java (from r373767, directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/LaunchDiagnosticUi.java)
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DiagnosticCommand.java?p2=directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DiagnosticCommand.java&p1=directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/LaunchDiagnosticUi.java&r1=373767&r2=373960&rev=373960&view=diff
==============================================================================
--- directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/LaunchDiagnosticUi.java (original)
+++ directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DiagnosticCommand.java Tue Jan 31 17:39:47 2006
@@ -18,27 +18,54 @@
 
 
 import java.util.Hashtable;
-import javax.naming.NamingException;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.directory.daemon.AvailablePortFinder;
 import org.apache.ldap.common.message.extended.LaunchDiagnosticUiRequest;
 
 
 /**
+ * A command to send an extened request which launches a diagnostic UI 
+ * on the server's console.  This may not work unless the display is set 
+ * and access is granted to the display (via xhost +).  This is especially
+ * the case when running the server in daemon mode.  Usually when running
+ * the server in debug mode is when you want the diagnostics turned on.
  * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public class LaunchDiagnosticUi
+public class DiagnosticCommand extends ToolCommand
 {
+    public static final String PORT_RANGE = "(" + AvailablePortFinder.MIN_PORT_NUMBER + ", " 
+    + AvailablePortFinder.MAX_PORT_NUMBER + ")";
+
     private int port = 10389;
     private String host = "localhost";
     private String password = "secret";
 
     
-    public void execute() throws NamingException
+    protected DiagnosticCommand()
+    {
+        super( "diagnostic" );
+    }
+
+
+    public void execute( CommandLine cmd ) throws Exception
     {
+        processOptions( cmd );
+        
+        if ( isDebugEnabled() )
+        {
+            System.out.println( "Parameters for LaunchDiagnosticUI extended request:" );
+            System.out.println( "port = " + port );
+            System.out.println( "host = " + host );
+            System.out.println( "password = " + password );
+        }
+
         Hashtable env = new Hashtable();
         env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
         env.put( "java.naming.provider.url", "ldap://" + host + ":" + port );
@@ -47,14 +74,125 @@
         env.put( "java.naming.security.authentication", "simple" );
 
         LdapContext ctx = new InitialLdapContext( env, null );
+        if ( isDebugEnabled() )
+        {
+            System.out.println( "Connection to the server established.\n"+
+                "Sending extended request ... " );
+        }
         ctx.extendedOperation( new LaunchDiagnosticUiRequest( 3 ) );
         ctx.close();
     }
 
 
-    public static void main( String[] args ) throws Exception
+    private void processOptions( CommandLine cmd )
+    {
+        if ( isDebugEnabled() )
+        {
+            System.out.println( "Processing options for launching diagnostic UI ..." );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out and error check the port value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'p' ) )   // - user provided port w/ -p takes precedence
+        {
+            String val = cmd.getOptionValue( 'p' );
+            try
+            {
+                port = Integer.parseInt( val );
+            }
+            catch ( NumberFormatException e )
+            {
+                System.err.println( "port value of '" + val + "' is not a number" );
+                System.exit( 1 );
+            }
+            
+            if ( port > AvailablePortFinder.MAX_PORT_NUMBER )
+            {
+                System.err.println( "port value of '" + val + "' is larger than max port number: " 
+                    + AvailablePortFinder.MAX_PORT_NUMBER );
+                System.exit( 1 );
+            }
+            else if ( port < AvailablePortFinder.MIN_PORT_NUMBER )
+            {
+                System.err.println( "port value of '" + val + "' is smaller than the minimum port number: " 
+                    + AvailablePortFinder.MIN_PORT_NUMBER );
+                System.exit( 1 );
+            }
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "port overriden by -p option: " + port );
+            }
+        }
+        else if ( getConfiguration() != null )
+        {
+            port = getConfiguration().getLdapPort();
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "port overriden by server.xml configuration: " + port );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "port set to default: " + port );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the host value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'h' ) )
+        {
+            host = cmd.getOptionValue( 'h' );
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "host overriden by -h option: " + host );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "host set to default: " + host );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the password value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'w' ) )
+        {
+            password = cmd.getOptionValue( 'w' );
+
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "password overriden by -w option: " + password );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "password set to default: " + password );
+        }
+    }
+
+
+    public Options getOptions()
     {
-        LaunchDiagnosticUi command = new LaunchDiagnosticUi();
-        command.execute();
+        Options opts = new Options();
+        Option op = new Option( "h", "host", true, "server host: defaults to localhost" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option(  "p", "port", true, "server port: defaults to 10389 or server.xml specified port" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "w", "password", true, "the apacheds administrator's password: defaults to secret" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "i", "install-path", true, "path to apacheds installation directory" );
+        op.setRequired( false );
+        opts.addOption( op );
+        return opts;
     }
 }

Added: directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DisconnectNotificationCommand.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DisconnectNotificationCommand.java?rev=373960&view=auto
==============================================================================
--- directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DisconnectNotificationCommand.java (added)
+++ directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DisconnectNotificationCommand.java Tue Jan 31 17:39:47 2006
@@ -0,0 +1,296 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.server.tools;
+
+
+import java.util.Hashtable;
+
+import javax.naming.directory.SearchControls;
+import javax.naming.event.EventContext;
+import javax.naming.event.NamingExceptionEvent;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.UnsolicitedNotification;
+import javax.naming.ldap.UnsolicitedNotificationListener;
+import javax.naming.ldap.UnsolicitedNotificationEvent;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.directory.daemon.AvailablePortFinder;
+import org.apache.ldap.common.message.extended.GracefulDisconnect;
+import org.apache.ldap.common.message.extended.NoticeOfDisconnect;
+
+
+/**
+ * Responds to unsolicited notifications by launching an external process.  Also 
+ * reconnects to the server an launches another process to notify that the server
+ * is back up.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class DisconnectNotificationCommand extends ToolCommand implements UnsolicitedNotificationListener
+{
+    UnsolicitedNotification notification;
+    boolean canceled = false;
+    private String host = "localhost";
+    private int port = 10389;
+    private String bindDn = "uid=admin,ou=system";
+    private String password = "secret";
+//    private String shutdownCommand = "echo"; 
+//    private String[] shutdownCommandArgs = new String[] { 
+//        "server $HOST:$PORT will shutdown for $OFFLINE minutes in $DELAY seconds" };
+
+    
+    protected DisconnectNotificationCommand()
+    {
+        super( "notifications" );
+    }
+
+
+    public void notificationReceived( UnsolicitedNotificationEvent evt )
+    {
+        notification = evt.getNotification();
+        
+        if ( notification.getID().equals( NoticeOfDisconnect.EXTENSION_OID ) )
+        {
+            System.out.println( "\nRecieved NoticeOfDisconnect: " + NoticeOfDisconnect.EXTENSION_OID );
+            System.out.println( "Expect to loose this connection without further information." );
+            canceled = true;
+        }
+        else if ( notification.getID().equals( GracefulDisconnect.EXTENSION_OID ) )
+        {
+            System.out.println( "Recieved GracefulDisconnect: " + GracefulDisconnect.EXTENSION_OID );
+            GracefulDisconnect gd = new GracefulDisconnect( notification.getEncodedValue() );
+            System.out.println( "LDAP server will shutdown in " + gd.getDelay() + " seconds." );
+            System.out.println( "LDAP server will be back online in " + gd.getTimeOffline() + " minutes." );
+            
+            if ( gd.getDelay() > 0 )
+            {
+                Thread t = new Thread( new Counter( gd.getDelay() ) );
+                t.start();
+            }
+        }
+        else 
+        {
+            System.out.println( "Unknown event recieved with OID: " + evt.getNotification().getID() );
+        }
+    }
+
+
+    public void namingExceptionThrown( NamingExceptionEvent evt )
+    {
+        canceled = true;
+        System.out.println( "Got an excption event: " + evt.getException().getMessage() );
+        System.out.println( "Process shutting down abruptly." );
+        System.exit( 1 );
+    }
+    
+    
+    class Counter implements Runnable
+    {
+        int delay;
+        
+        Counter( int delay )
+        {
+            this.delay = delay;
+        }
+        
+        public void run()
+        {
+            System.out.println( "Starting countdown until server shutdown:" );
+            System.out.print( "[" ); 
+            long delayMillis = delay * 1000 - 1000; // 1000 is for setup costs
+            long startTime = System.currentTimeMillis();
+            while ( System.currentTimeMillis() - startTime < delayMillis && !canceled )
+            {
+                try{ Thread.sleep( 1000 ); }catch ( InterruptedException e ){}
+                System.out.print( "." );
+            }
+            
+            if ( canceled )
+            {
+                System.out.println( " -- countdown canceled -- " );
+            }
+            else
+            {
+                System.out.println( "]" );
+                System.out.println( "Client shutting down gracefully." );
+                System.exit( 0 );
+            }
+        }
+    }
+
+
+    public void execute( CommandLine cmd ) throws Exception
+    {
+        processOptions( cmd );
+        
+        Hashtable env = new Hashtable();
+        env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
+        env.put("java.naming.provider.url", "ldap://" + host + ":" + port ); 
+        env.put("java.naming.security.principal", bindDn ); 
+        env.put("java.naming.security.credentials", password );
+        env.put("java.naming.security.authentication", "simple" );
+
+        LdapContext ctx = new InitialLdapContext( env, null );
+        ctx = ctx.newInstance( null );
+        UnsolicitedNotificationListener listener = new DisconnectNotificationCommand();
+        ( ( EventContext ) ctx ).addNamingListener( "", SearchControls.SUBTREE_SCOPE, listener );
+        
+        System.out.println( "Listening for notifications." );
+        System.out.println( "Press any key to terminate." );
+        System.in.read();
+        ctx.close();
+        System.out.println( "Process terminated!!!" );
+    }
+
+
+    private void processOptions( CommandLine cmd )
+    {
+        if ( isDebugEnabled() )
+        {
+            System.out.println( "Processing options for disconnect notifications ..." );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out and error check the port value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'p' ) )   // - user provided port w/ -p takes precedence
+        {
+            String val = cmd.getOptionValue( 'p' );
+            try
+            {
+                port = Integer.parseInt( val );
+            }
+            catch ( NumberFormatException e )
+            {
+                System.err.println( "port value of '" + val + "' is not a number" );
+                System.exit( 1 );
+            }
+            
+            if ( port > AvailablePortFinder.MAX_PORT_NUMBER )
+            {
+                System.err.println( "port value of '" + val + "' is larger than max port number: " 
+                    + AvailablePortFinder.MAX_PORT_NUMBER );
+                System.exit( 1 );
+            }
+            else if ( port < AvailablePortFinder.MIN_PORT_NUMBER )
+            {
+                System.err.println( "port value of '" + val + "' is smaller than the minimum port number: " 
+                    + AvailablePortFinder.MIN_PORT_NUMBER );
+                System.exit( 1 );
+            }
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "port overriden by -p option: " + port );
+            }
+        }
+        else if ( getConfiguration() != null )
+        {
+            port = getConfiguration().getLdapPort();
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "port overriden by server.xml configuration: " + port );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "port set to default: " + port );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the host value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'h' ) )
+        {
+            host = cmd.getOptionValue( 'h' );
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "host overriden by -h option: " + host );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "host set to default: " + host );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the password value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'w' ) )
+        {
+            password = cmd.getOptionValue( 'w' );
+
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "password overriden by -w option: " + password );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "password set to default: " + password );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the binddn value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'u' ) )
+        {
+            bindDn = cmd.getOptionValue( 'u' );
+
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "binddn overriden by -u option: " + bindDn );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "binddn set to default: " + bindDn );
+        }
+    }
+
+
+    public Options getOptions()
+    {
+        Options opts = new Options();
+        Option op = new Option( "h", "host", true, "server host: defaults to localhost" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option(  "p", "port", true, "server port: defaults to 10389 or server.xml specified port" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "w", "password", true, "the apacheds administrator's password: defaults to secret" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "u", "binddn", true, "an apacheds user's dn: defaults to " + bindDn );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "i", "install-path", true, "path to apacheds installation directory" );
+        op.setRequired( false );
+        opts.addOption( op );
+        return opts;
+    }
+}

Copied: directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpCommand.java (from r373767, directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpTool.java)
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpCommand.java?p2=directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpCommand.java&p1=directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpTool.java&r1=373767&r2=373960&rev=373960&view=diff
==============================================================================
--- directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpTool.java (original)
+++ directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/DumpCommand.java Tue Jan 31 17:39:47 2006
@@ -1,9 +1,30 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
 package org.apache.directory.server.tools;
 
 
 import java.io.File;
-import java.io.IOException;
+import java.io.FileWriter;
+import java.io.PrintWriter;
 import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
@@ -14,71 +35,114 @@
 import jdbm.recman.BaseRecordManager;
 import jdbm.recman.CacheRecordManager;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
 
 import org.apache.ldap.common.message.LockableAttributeImpl;
 import org.apache.ldap.common.message.LockableAttributesImpl;
 import org.apache.ldap.common.schema.AttributeType;
-import org.apache.ldap.server.configuration.StartupConfiguration;
+import org.apache.ldap.common.schema.UsageEnum;
 import org.apache.ldap.server.partition.impl.btree.Tuple;
 import org.apache.ldap.server.partition.impl.btree.jdbm.JdbmIndex;
 import org.apache.ldap.server.partition.impl.btree.jdbm.JdbmMasterTable;
+import org.apache.ldap.server.schema.AttributeTypeRegistry;
 import org.apache.ldap.server.schema.bootstrap.BootstrapRegistries;
 import org.apache.ldap.server.schema.bootstrap.BootstrapSchemaLoader;
 
 
 /**
  * Simple tool used to dump the contents of a jdbm based partition.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
  */
-public class DumpTool
+public class DumpCommand extends ToolCommand
 {
-    /**
-     * Just give it the path to the working directory where all the db files
-     * are for a partition like the system partition.  Right now this will print 
-     * stuff out to the console. 
-     * 
-     * @param args
-     * @throws Exception
-     */
-    public static void main( String[] args ) throws Exception
+    private BootstrapRegistries bootstrapRegistries = new BootstrapRegistries();
+    private BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
+    private Set exclusions = new HashSet();
+    private boolean includeOperational = false;
+    
+    
+    public DumpCommand()
+    {
+        super( "dump" );
+    }
+    
+    
+    public void execute( CommandLine cmdline ) throws Exception
     {
-        File workingDirectory = new File( args[0] );
-        System.out.println( "# ========================================================================");
-        System.out.println( "# Dumptool Version: 0.1" );
-        System.out.println( "# Partition Working Directory: " + workingDirectory );
-        System.out.println( "# ========================================================================\n\n");
-        CacheRecordManager recMan;
-        JdbmMasterTable master;
+        getLayout().verifyInstallation();
+        loader.load( getConfiguration().getBootstrapSchemas(), bootstrapRegistries );
+
+        includeOperational = cmdline.hasOption( 'o' );
+        String[] partitions = cmdline.getOptionValues( 'p' );
+        String outputFile = cmdline.getOptionValue( 'f' );
+        PrintWriter out = null;
+        
+        String[] excludedAttributes = cmdline.getOptionValues( 'e' );
+        if ( excludedAttributes != null )
+        {
+            AttributeTypeRegistry registry = bootstrapRegistries.getAttributeTypeRegistry();
+            for ( int ii = 0; ii < excludedAttributes.length; ii++ )
+            {
+                AttributeType type = registry.lookup( excludedAttributes[ii] );
+                exclusions.add( type.getName() );
+            }
+        }
         
-        try 
+        if ( outputFile == null )
+        {
+            out = new PrintWriter( System.out );
+        }
+        else
         {
-            String path = workingDirectory.getPath() + File.separator + "master";
-            BaseRecordManager base = new BaseRecordManager( path );
-            base.disableTransactions();
-            recMan = new CacheRecordManager( base, new MRU( 1000 ) );
-        } 
-        catch ( IOException e )
-        {
-            NamingException ne = new NamingException( 
-                "Could not initialize RecordManager" );
-            ne.setRootCause( e );
-            throw ne;
-        }
-
-        master = new JdbmMasterTable( recMan );
-        BootstrapRegistries bootstrapRegistries = new BootstrapRegistries();
-        BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
-        StartupConfiguration startupConfiguration = new StartupConfiguration();
-        loader.load( startupConfiguration.getBootstrapSchemas(), bootstrapRegistries );
+            out = new PrintWriter( new FileWriter( outputFile ) );
+        }
+        
+        for ( int ii = 0; ii < partitions.length; ii++ )
+        {
+            File partitionDirectory = new File( getLayout().getPartitionsDirectory(), partitions[ii] );
+            out.println( "\n\n" );
+            dump( partitionDirectory, out );
+        }
+    }
+    
+    
+    private void dump( File partitionDirectory, PrintWriter out ) throws Exception
+    {
+        if ( ! partitionDirectory.exists() )
+        {
+            System.err.println( "Partition directory " + partitionDirectory + " does not exist!" );
+            System.exit( 1 );
+        }
+
+        out.println( "# ========================================================================");
+        out.println( "# ApacheDS Tools Version: " + getVersion() );
+        out.println( "# Partition Directory: " + partitionDirectory );
+        out.println( "# ========================================================================\n\n");
+
+        String path = partitionDirectory.getPath() + File.separator + "master";
+        BaseRecordManager base = new BaseRecordManager( path );
+        base.disableTransactions();
+        CacheRecordManager recMan = new CacheRecordManager( base, new MRU( 1000 ) );
+
+        JdbmMasterTable master = new JdbmMasterTable( recMan );
         AttributeType attributeType = bootstrapRegistries.getAttributeTypeRegistry().lookup( "apacheUpdn" );
-        JdbmIndex idIndex = new JdbmIndex( attributeType, workingDirectory );
+        JdbmIndex idIndex = new JdbmIndex( attributeType, partitionDirectory );
 
-        System.out.println( "#---------------------" );
+        out.println( "#---------------------" );
         NamingEnumeration list = master.listTuples();
+        StringBuffer buf = new StringBuffer();
         while ( list.hasMore() )
         {
             Tuple tuple = ( Tuple ) list.next();
             BigInteger id = ( BigInteger ) tuple.getKey();
+            String dn = ( String ) idIndex.reverseLookup( id );
             Attributes entry = ( Attributes ) tuple.getValue();
+
+            filterAttributes( dn, entry );
             
             if ( ! ( entry instanceof LockableAttributesImpl ) )
             {
@@ -97,16 +161,76 @@
                 }
             }
             
-            String dn = ( String ) idIndex.reverseLookup( id );
+            buf.append( "# Entry: " ).append( id ).append( "\n#---------------------\n\n" );
+            buf.append( "dn: " ).append( dn ).append( "\n" ).append( entry );
             if ( list.hasMore() )
             {
-                System.out.println( "# Entry: " + id + "\n#---------------------\n\n" + "dn: " + dn + "\n" + entry 
-                    + "\n\n#---------------------" );
+                buf.append( "\n\n#---------------------\n" );
+            }
+            out.print( buf.toString() );
+            out.flush();
+            buf.setLength( 0 );
+        }
+    }
+
+
+    private void filterAttributes( String dn, Attributes entry ) throws NamingException
+    {
+        List toRemove = new ArrayList();
+        AttributeTypeRegistry registry = bootstrapRegistries.getAttributeTypeRegistry();
+        NamingEnumeration attrs = entry.getAll();
+        while ( attrs.hasMore() )
+        {
+            Attribute attr = ( Attribute ) attrs.next();
+            if ( ! registry.hasAttributeType( attr.getID() ) )
+            {
+                if ( ! isQuietEnabled() )
+                {
+                    System.out.println( "# Cannot properly filter unrecognized attribute " 
+                        + attr.getID() + " in " + dn );
+                }
+                continue;
             }
-            else
+            
+            AttributeType type = registry.lookup( attr.getID() );
+            boolean isOperational = type.getUsage() != UsageEnum.USERAPPLICATIONS;
+            if ( exclusions.contains( attr.getID() ) || ( isOperational && ( ! includeOperational ) ) )
             {
-                System.out.println( "# Entry: " + id + "\n#---------------------\n\n" + "dn: " + dn + "\n" + entry );
+                toRemove.add( attr.getID() );
             }
         }
+        for ( int ii = 0; ii < toRemove.size(); ii++ )
+        {
+            String id = ( String ) toRemove.get( ii );
+            entry.remove( id );
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "# Excluding attribute " + id + " in " + dn );
+            }
+        }
+    }
+    
+    
+    public Options getOptions()
+    {
+        Options opts = new Options();
+        Option op = new Option( "f", "file", true, "file to output the dump to" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option(  "p", "partitions", true, "the partitions to dump" );
+        op.setRequired( true );
+        op.setValueSeparator( File.pathSeparatorChar );
+        opts.addOption( op );
+        op = new Option( "e", "excluded-attributes", true, "the attributes to exclude" );
+        op.setRequired( false );
+        op.setValueSeparator( File.pathSeparatorChar );
+        opts.addOption( op );
+        op = new Option( "o", "include-operational", false, "include operational attributes: defaults to false" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "i", "install-path", true, "path to apacheds installation directory" );
+        op.setRequired( true );
+        opts.addOption( op );
+        return opts;
     }
 }

Copied: directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdownCommand.java (from r373767, directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdown.java)
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdownCommand.java?p2=directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdownCommand.java&p1=directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdown.java&r1=373767&r2=373960&rev=373960&view=diff
==============================================================================
--- directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdown.java (original)
+++ directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/GracefulShutdownCommand.java Tue Jan 31 17:39:47 2006
@@ -18,27 +18,64 @@
 
 
 import java.util.Hashtable;
-import javax.naming.NamingException;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.directory.daemon.AvailablePortFinder;
 import org.apache.ldap.common.message.extended.GracefulShutdownRequest;
 
 
 /**
+ * A command used to send a graceful disconnect to established clients 
+ * while allowing them time to complete operations already in progress.
  * 
+ * @see <a href="http://docs.safehaus.org/display/APACHEDS/LDAP+Extensions+for+Graceful+Shutdown">
+ * Graceful Shutdown</a>
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public class GracefulShutdown
+public class GracefulShutdownCommand extends ToolCommand
 {
+    public static final String PORT_RANGE = "(" + AvailablePortFinder.MIN_PORT_NUMBER + ", " 
+        + AvailablePortFinder.MAX_PORT_NUMBER + ")";
+
+    private static final int DELAY_MAX = 86400;
+
+    private static final int TIME_OFFLINE_MAX = 720;
+
     private int port = 10389;
     private String host = "localhost";
     private String password = "secret";
+    private int delay;
+    private int timeOffline;
 
     
-    public void execute() throws NamingException
+    protected GracefulShutdownCommand()
+    {
+        super( "graceful" );
+    }
+
+    private boolean isWaiting;
+    private boolean isSuccess = false;
+    private Thread executeThread = null;
+    public void execute( CommandLine cmd ) throws Exception
     {
+        executeThread = Thread.currentThread();
+        processOptions( cmd );
+        
+        if ( isDebugEnabled() )
+        {
+            System.out.println( "Parameters for GracefulShutdown extended request:" );
+            System.out.println( "port = " + port );
+            System.out.println( "host = " + host );
+            System.out.println( "password = " + password );
+            System.out.println( "delay = " + delay );
+            System.out.println( "timeOffline = " + timeOffline );
+        }
+        
         Hashtable env = new Hashtable();
         env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
         env.put( "java.naming.provider.url", "ldap://" + host + ":" + port );
@@ -47,14 +84,274 @@
         env.put( "java.naming.security.authentication", "simple" );
 
         LdapContext ctx = new InitialLdapContext( env, null );
-        ctx.extendedOperation( new GracefulShutdownRequest( 0, 10, 10 ) );
+        if ( ! isQuietEnabled() )
+        {
+            System.out.println( "Connection to the server established.\n"+
+                "Sending extended request and blocking for shutdown:" );
+            isWaiting = true;
+            Thread t = new Thread( new Ticker() );
+            t.start();
+        }
+        try
+        {
+            ctx.extendedOperation( new GracefulShutdownRequest( 0, timeOffline, delay ) );
+            isSuccess = true;
+        }
+        catch( Throwable t )
+        {
+            isSuccess = false;
+            System.err.print( "failed with error: " + t.getMessage() );
+        }
+        isWaiting = false;
         ctx.close();
     }
+    
+    
+    class Ticker implements Runnable
+    {
+        public void run()
+        {
+            if ( ! isQuietEnabled() ) System.out.print( "[waiting for shutdown] " );
+            while( isWaiting )
+            {
+                try
+                {
+                    Thread.sleep( 1000 );
+                }
+                catch ( InterruptedException e )
+                {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+                if ( ! isQuietEnabled() ) System.out.print( "." );
+            }
+            if ( isSuccess )
+            {
+                if ( ! isQuietEnabled() ) System.out.println( "\n[shutdown complete]" );
+                try
+                {
+                    executeThread.join( 1000 );
+                }
+                catch ( InterruptedException e )
+                {
+                    e.printStackTrace();
+                }
+                System.exit( 0 );
+            }
+            else
+            {
+                if ( ! isQuietEnabled() ) System.out.println( "\n[shutdown failed]" );
+                try
+                {
+                    executeThread.join( 1000 );
+                }
+                catch ( InterruptedException e )
+                {
+                    e.printStackTrace();
+                }
+                System.exit( 1 );
+            }
+        }
+    }
+
+
+    private void processOptions( CommandLine cmd )
+    {
+        if ( isDebugEnabled() )
+        {
+            System.out.println( "Processing options for graceful shutdown ..." );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out and error check the port value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'p' ) )   // - user provided port w/ -p takes precedence
+        {
+            String val = cmd.getOptionValue( 'p' );
+            try
+            {
+                port = Integer.parseInt( val );
+            }
+            catch ( NumberFormatException e )
+            {
+                System.err.println( "port value of '" + val + "' is not a number" );
+                System.exit( 1 );
+            }
+            
+            if ( port > AvailablePortFinder.MAX_PORT_NUMBER )
+            {
+                System.err.println( "port value of '" + val + "' is larger than max port number: " 
+                    + AvailablePortFinder.MAX_PORT_NUMBER );
+                System.exit( 1 );
+            }
+            else if ( port < AvailablePortFinder.MIN_PORT_NUMBER )
+            {
+                System.err.println( "port value of '" + val + "' is smaller than the minimum port number: " 
+                    + AvailablePortFinder.MIN_PORT_NUMBER );
+                System.exit( 1 );
+            }
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "port overriden by -p option: " + port );
+            }
+        }
+        else if ( getConfiguration() != null )
+        {
+            port = getConfiguration().getLdapPort();
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "port overriden by server.xml configuration: " + port );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "port set to default: " + port );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the host value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'h' ) )
+        {
+            host = cmd.getOptionValue( 'h' );
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "host overriden by -h option: " + host );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "host set to default: " + host );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the password value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'w' ) )
+        {
+            password = cmd.getOptionValue( 'w' );
+
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "password overriden by -w option: " + password );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "password set to default: " + password );
+        }
+        
+        // -------------------------------------------------------------------
+        // figure out the delay value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 'e' ) )
+        {
+            String val = cmd.getOptionValue( 'e' );
+            try
+            {
+                delay = Integer.parseInt( val );
+            }
+            catch ( NumberFormatException e )
+            {
+                System.err.println( "delay value of '" + val + "' is not a number" );
+                System.exit( 1 );
+            }
+            
+            if ( delay > DELAY_MAX )
+            {
+                System.err.println( "delay value of '" + val 
+                    + "' is larger than max delay (seconds) allowed: " 
+                    + DELAY_MAX );
+                System.exit( 1 );
+            }
+            else if ( delay < 0 )
+            {
+                System.err.println( "delay value of '" + val + "' is less than zero and makes no sense" );
+                System.exit( 1 );
+            }
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "delay seconds overriden by -e option: " + delay );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "Using default delay value of " + delay );
+        }
+
+    
+        // -------------------------------------------------------------------
+        // figure out the timeOffline value
+        // -------------------------------------------------------------------
+
+        if ( cmd.hasOption( 't' ) )
+        {
+            String val = cmd.getOptionValue( 't' );
+            try
+            {
+                timeOffline = Integer.parseInt( val );
+            }
+            catch ( NumberFormatException e )
+            {
+                System.err.println( "timeOffline value of '" + val + "' is not a number" );
+                System.exit( 1 );
+            }
+            
+            if ( timeOffline > TIME_OFFLINE_MAX )
+            {
+                System.err.println( "timeOffline value of '" + val 
+                    + "' is larger than max timeOffline (minutes) allowed: " 
+                    + TIME_OFFLINE_MAX );
+                System.exit( 1 );
+            }
+            else if ( timeOffline < 0 )
+            {
+                System.err.println( "timeOffline value of '" + val + "' is less than zero and makes no sense" );
+                System.exit( 1 );
+            }
+            
+            if ( isDebugEnabled() )
+            {
+                System.out.println( "timeOffline seconds overriden by -t option: " + timeOffline );
+            }
+        }
+        else if ( isDebugEnabled() )
+        {
+            System.out.println( "Using default timeOffline value of " + delay );
+        }
+    }
 
 
-    public static void main( String[] args ) throws Exception
+    public Options getOptions()
     {
-        GracefulShutdown command = new GracefulShutdown();
-        command.execute();
+        Options opts = new Options();
+        Option op = new Option( "h", "host", true, "server host: defaults to localhost" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option(  "p", "port", true, "server port: defaults to 10389 or server.xml specified port" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "e", "delay", true, "delay (seconds) before shutdown: defaults to 0" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "w", "password", true, "the apacheds administrator's password: defaults to secret" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "t", "time-offline", true, 
+            "server offline time (minutes): defaults to 0 (indefinate)" );
+        op.setRequired( false );
+        opts.addOption( op );
+        op = new Option( "i", "install-path", true, "path to apacheds installation directory" );
+        op.setRequired( false );
+        opts.addOption( op );
+        return opts;
     }
 }

Added: directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ToolCommand.java
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ToolCommand.java?rev=373960&view=auto
==============================================================================
--- directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ToolCommand.java (added)
+++ directory/trunks/apacheds/tools/src/main/java/org/apache/directory/server/tools/ToolCommand.java Tue Jan 31 17:39:47 2006
@@ -0,0 +1,149 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.directory.server.tools;
+
+
+import java.io.File;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+import org.apache.directory.daemon.InstallationLayout;
+import org.apache.ldap.server.configuration.ServerStartupConfiguration;
+
+
+/**
+ * Simple base class for tool commands.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public abstract class ToolCommand
+{
+    private final String name;
+    private boolean debugEnabled = false;
+    private boolean verboseEnabled = false;
+    private boolean quietEnabled = false;
+    private String version;
+    private InstallationLayout layout;
+    private ServerStartupConfiguration configuration;
+    
+
+    protected ToolCommand( String name )
+    {
+        this.name = name;
+    }
+    
+    
+    public abstract void execute( CommandLine cmd ) throws Exception;
+    public abstract Options getOptions();
+
+    
+    public String getName()
+    {
+        return this.name;
+    }
+    
+    
+    public void setLayout( File installationDirectory )
+    {
+        this.layout = new InstallationLayout( installationDirectory );
+    }
+
+
+    public void setLayout( String installationPath )
+    {
+        this.layout = new InstallationLayout( installationPath );
+    }
+
+
+    public void setLayout( InstallationLayout layout )
+    {
+        this.layout = layout;
+    }
+
+
+    public InstallationLayout getLayout()
+    {
+        return layout;
+    }
+
+
+    public void setConfiguration( ServerStartupConfiguration configuration )
+    {
+        this.configuration = configuration;
+    }
+
+
+    public ServerStartupConfiguration getConfiguration()
+    {
+        return configuration;
+    }
+
+
+    public void setVersion( String version )
+    {
+        this.version = version;
+    }
+
+
+    public String getVersion()
+    {
+        return version;
+    }
+
+
+    public String toString()
+    {
+        return getName();
+    }
+
+
+    public void setDebugEnabled( boolean debugEnabled )
+    {
+        this.debugEnabled = debugEnabled;
+    }
+
+
+    public boolean isDebugEnabled()
+    {
+        return debugEnabled;
+    }
+
+
+    public void setVerboseEnabled( boolean verboseEnabled )
+    {
+        this.verboseEnabled = verboseEnabled;
+    }
+
+
+    public boolean isVerboseEnabled()
+    {
+        return verboseEnabled;
+    }
+
+
+    public void setQuietEnabled( boolean quietEnabled )
+    {
+        this.quietEnabled = quietEnabled;
+    }
+
+
+    public boolean isQuietEnabled()
+    {
+        return quietEnabled;
+    }
+}

Added: directory/trunks/apacheds/tools/src/main/manifest/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/manifest/MANIFEST.MF?rev=373960&view=auto
==============================================================================
--- directory/trunks/apacheds/tools/src/main/manifest/MANIFEST.MF (added)
+++ directory/trunks/apacheds/tools/src/main/manifest/MANIFEST.MF Tue Jan 31 17:39:47 2006
@@ -0,0 +1,6 @@
+Manifest-Version: 1.0
+Main-Class: org.apache.directory.server.tools.ApachedsTools
+Class-Path: logger.jar daemon.jar bootstrapper.jar ../lib/antlr-2.7.2.jar ../lib/commons-collections-3.1.jar ../lib/commons-lang-2.0.jar ../lib/commons-logging-1.0.4.jar ../lib/directory-asn1-0.3.4-SNAPSHOT.jar ../lib/jdbm-1.0.jar ../lib/lcrypto-jdk14-131.jar ../lib/mina-core-0.9.1-SNAPSHOT.jar ../lib/mina-filter-codec-asn1-0.9.1-SNAPSHOT.jar ../lib/mina-filter-ssl-0.9.1-SNAPSHOT.jar ../lib/org.apache.directory.kerberos.common-0.7-SNAPSHOT.jar ../lib/org.apache.directory.protocol.changepw-0.7-SNAPSHOT.jar ../lib/org.apache.directory.protocol.common-0.7.1-SNAPSHOT.jar ../lib/org.apache.directory.protocol.kerberos-0.7-SNAPSHOT.jar ../lib/org.apache.directory.protocol.ldap-0.9.4-SNAPSHOT.jar ../lib/org.apache.directory.protocol.ntp-0.5-SNAPSHOT.jar ../lib/org.apache.ldap.common-0.9.4-SNAPSHOT.jar ../lib/org.apache.ldap.server.core-0.9.4-SNAPSHOT.jar ../lib/org.apache.ldap.server.shared-0.9.4-SNAPSHOT.jar ../lib/org.apache.ldap.server.standalone.simple.jndi-0.9.4-SNAPSHOT.jar ..
 /lib/org.apache.ldap.server.standalone.simple.main-0.9.4-SNAPSHOT.jar ../lib/oro-2.0.8.jar ../lib/spring-beans-1.2.1.jar ../lib/spring-context-1.2.1.jar ../lib/spring-core-1.2.1.jar ../lib/commons-cli-1.0.jar
+Specification-Title: apacheds-tools
+Specification-Version: 1.0
+

Added: directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/ApachedsTools.properties
URL: http://svn.apache.org/viewcvs/directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/ApachedsTools.properties?rev=373960&view=auto
==============================================================================
--- directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/ApachedsTools.properties (added)
+++ directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/ApachedsTools.properties Tue Jan 31 17:39:47 2006
@@ -0,0 +1 @@
+apacheds.tools.version=${pom.version}

Propchange: directory/trunks/apacheds/tools/src/main/resources/org/apache/directory/server/tools/ApachedsTools.properties
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message