directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1752395 - /directory/apacheds/trunk/wrapper/src/main/java/org/apache/directory/server/wrapper/ApacheDsTanukiWrapper.java
Date Wed, 13 Jul 2016 11:34:50 GMT
Author: elecharny
Date: Wed Jul 13 11:34:49 2016
New Revision: 1752395

URL: http://svn.apache.org/viewvc?rev=1752395&view=rev
Log:
Handling the repair command in the wrapper

Modified:
    directory/apacheds/trunk/wrapper/src/main/java/org/apache/directory/server/wrapper/ApacheDsTanukiWrapper.java

Modified: directory/apacheds/trunk/wrapper/src/main/java/org/apache/directory/server/wrapper/ApacheDsTanukiWrapper.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/wrapper/src/main/java/org/apache/directory/server/wrapper/ApacheDsTanukiWrapper.java?rev=1752395&r1=1752394&r2=1752395&view=diff
==============================================================================
--- directory/apacheds/trunk/wrapper/src/main/java/org/apache/directory/server/wrapper/ApacheDsTanukiWrapper.java
(original)
+++ directory/apacheds/trunk/wrapper/src/main/java/org/apache/directory/server/wrapper/ApacheDsTanukiWrapper.java
Wed Jul 13 11:34:49 2016
@@ -21,6 +21,15 @@
 package org.apache.directory.server.wrapper;
 
 
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import org.apache.directory.api.util.Network;
+import org.apache.directory.api.util.Strings;
 import org.apache.directory.server.ApacheDsService;
 import org.apache.directory.server.core.api.InstanceLayout;
 import org.slf4j.Logger;
@@ -43,6 +52,9 @@ public final class ApacheDsTanukiWrapper
     private ApacheDsService service;
 
 
+    /**
+     * Creates a new instance of ApacheDsTanukiWrapper.
+     */
     private ApacheDsTanukiWrapper()
     {
     }
@@ -54,33 +66,157 @@ public final class ApacheDsTanukiWrapper
     }
 
 
-    public Integer start( String[] args )
+    
+    private static int readShutdownPort( InstanceLayout layout ) throws IOException 
     {
-        LOG.info( "Starting the service..." );
+        return Integer.parseInt( new String( Files.readAllBytes( 
+                Paths.get( layout.getRunDirectory().getAbsolutePath(), ".shutdown.port" )
),
+                Charset.forName( "utf-8" ) ) );
+    }
+    
+
+    private static String readShutdownPassword( InstanceLayout layout ) throws IOException

+    {
+        return new String( Files.readAllBytes( 
+                Paths.get( layout.getRunDirectory().getAbsolutePath(), ".shutdown.pwd" )
),
+                Charset.forName( "utf-8" ) );
+    }
 
-        if ( ( args != null ) && ( args.length == 1 ) )
+    
+    /**
+     * Try to repair the databases
+     *
+     * @param instanceDirectory The directory containing the server instance 
+     */
+    public void repair( String instanceDirectory )
+    {
+        System.out.println( "Trying to repair the following data :" + instanceDirectory );
+        InstanceLayout layout = new InstanceLayout( instanceDirectory );
+        
+        // Creating ApacheDS service
+        service = new ApacheDsService();
+        
+        try
+        {
+            System.out.println( "Starting the service." );
+            service.start( layout );
+            System.out.println( "Service started." );
+        }
+        catch ( Exception e )
+        {
+            return;
+        }
+
+        // Initializing the service
+        try
+        {
+            System.out.println( "Repairing the database." );
+            service.repair( layout );
+            System.out.println( "Database repaired." );
+        }
+        catch ( Exception e )
         {
-            // Creating ApacheDS service
-            service = new ApacheDsService();
+            LOG.error( "Failed to start the service.", e );
+            System.exit( 1 );
+        }
+    }
 
-            // Creating instance layouts from the argument
-            InstanceLayout instanceLayout = new InstanceLayout( args[0] );
 
-            // Starting the service
-            try
-            {
-                service.start( instanceLayout );
-            }
-            catch ( Exception e )
+    /**
+     * Implemented the start() method from the WrapperListener class.
+     * 
+     * The possible arguments are the instance layout directory and the command, one of :
+     * <ul>
+     *   <li>START (default) : starts the server</li>
+     *   <li>STOP : stops the server</li>
+     *   <li>REPAIR : repairs the index</li>
+     * </ul>
+     */
+    public Integer start( String[] args )
+    {
+        LOG.info( "Starting the service..." );
+
+        if ( args != null )
+        {
+            int argNb = 0;
+            
+            for ( String arg : args )
             {
-                LOG.error( "Failed to start the service.", e );
-                System.exit( ExitCodes.START );
+                LOG.info( "Args[{}] : {}", argNb, arg );
+                argNb++;
             }
         }
-        else
+
+        if ( args != null )
         {
-            throw new IllegalArgumentException(
-                "Program must be launched with 1 arguement, the path to the instance directory."
);
+            // the default action
+            String action = "START";
+            String instanceDirectory = args[0];
+
+            switch ( args.length )
+            {
+                case 2 :
+                    action = args[1];
+                    /* Passthrough...*/
+                    
+                case 1 :
+                    // Creating ApacheDS service
+                    service = new ApacheDsService();
+
+                    // Creating instance layouts from the argument
+                    InstanceLayout instanceLayout = new InstanceLayout( instanceDirectory
);
+                    
+                    // Process the action
+                    switch ( Strings.toLowerCaseAscii( action ) )
+                    {
+                        case "stop" :
+                            // Stops the server
+                            LOG.debug( "Stopping runtime" );
+                            InstanceLayout layout = new InstanceLayout( instanceDirectory
);
+                            
+                            try ( Socket socket = new Socket( Network.LOOPBACK, readShutdownPort(
layout ) );
+                                    PrintWriter writer = new PrintWriter( socket.getOutputStream()
) )
+                            {
+                                writer.print( readShutdownPassword( layout ) );
+                            }
+                            catch ( IOException e )
+                            {
+                                // TODO Auto-generated catch block
+                                e.printStackTrace();
+                            }
+                            
+                            break;
+
+                        case "repair" :
+                            // Try to fix the JDBM database
+                            LOG.debug( "Fixing the database runtime" );
+                            repair( instanceDirectory );
+                            
+                            break;
+                            
+                        default :
+                            // Starts the server
+                            LOG.debug( "Starting runtime" );
+
+                            try
+                            {
+                                service.start( instanceLayout );
+                            }
+                            catch ( Exception e )
+                            {
+                                LOG.error( "Failed to start the service.", e );
+                                System.exit( ExitCodes.START );
+                            }
+                            
+                            break;
+                    }
+                    
+                    break;
+                    
+                default :
+                    throw new IllegalArgumentException(
+                        "Program must be launched with at least 1 argument, the path to the
instance directory." );
+            }
         }
 
         return null;



Mime
View raw message