You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2010/04/21 12:53:27 UTC

svn commit: r936252 - in /directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server: Service.java UberjarMain.java

Author: kayyagari
Date: Wed Apr 21 10:53:27 2010
New Revision: 936252

URL: http://svn.apache.org/viewvc?rev=936252&view=rev
Log:
o modified the code to start all the servers and services from the LDIF based config partition
o removed the dependency on the ApacheDS class and spring's contextloader

Modified:
    directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/Service.java
    directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/UberjarMain.java

Modified: directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/Service.java
URL: http://svn.apache.org/viewvc/directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/Service.java?rev=936252&r1=936251&r2=936252&view=diff
==============================================================================
--- directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/Service.java (original)
+++ directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/Service.java Wed Apr 21 10:53:27 2010
@@ -21,19 +21,28 @@ package org.apache.directory.server;
 
 
 import java.io.File;
+import java.util.List;
 
 import org.apache.directory.daemon.DaemonApplication;
 import org.apache.directory.daemon.InstallationLayout;
 import org.apache.directory.server.changepw.ChangePasswordServer;
-import org.apache.directory.server.configuration.ApacheDS;
+import org.apache.directory.server.config.ConfigPartitionReader;
+import org.apache.directory.server.config.LdifConfigExtractor;
 import org.apache.directory.server.core.DirectoryService;
-import org.apache.directory.server.core.factory.DefaultDirectoryServiceFactory;
+import org.apache.directory.server.core.partition.ldif.LdifPartition;
+import org.apache.directory.server.core.schema.SchemaPartition;
+import org.apache.directory.server.i18n.I18n;
 import org.apache.directory.server.integration.http.HttpServer;
 import org.apache.directory.server.kerberos.kdc.KdcServer;
 import org.apache.directory.server.ldap.LdapServer;
 import org.apache.directory.server.ntp.NtpServer;
-import org.apache.directory.server.protocol.shared.transport.TcpTransport;
-import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
+import org.apache.directory.shared.ldap.schema.SchemaManager;
+import org.apache.directory.shared.ldap.schema.ldif.extractor.SchemaLdifExtractor;
+import org.apache.directory.shared.ldap.schema.ldif.extractor.impl.DefaultSchemaLdifExtractor;
+import org.apache.directory.shared.ldap.schema.loader.ldif.LdifSchemaLoader;
+import org.apache.directory.shared.ldap.schema.manager.impl.DefaultSchemaManager;
+import org.apache.directory.shared.ldap.schema.registries.SchemaLoader;
+import org.apache.directory.shared.ldap.util.ExceptionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,15 +73,38 @@ public class Service implements DaemonAp
     /** The Kerberos server instance */
     private KdcServer kdcServer;
 
-    private ApacheDS apacheDS;
-
     private HttpServer httpServer;
 
-    private FileSystemXmlApplicationContext factory;
+    private LdifPartition schemaLdifPartition;
+
+    private SchemaManager schemaManager;
+
+    private LdifPartition configPartition;
+
+    private ConfigPartitionReader cpReader;
 
 
     public void init( InstallationLayout layout, String[] args ) throws Exception
     {
+        if ( args == null )
+        {
+            args = new String[1];
+            args[0] = System.getProperty( "java.io.tmpdir" ) + File.separator + "server-work";
+        }
+
+        File partitionsDir = new File( args[0] );
+        if ( !partitionsDir.exists() )
+        {
+            LOG.info( "partition directory doesn't exist, creating {}", partitionsDir.getAbsolutePath() );
+            partitionsDir.mkdirs();
+        }
+
+        LOG.info( "using partition dir {}", partitionsDir.getAbsolutePath() );
+        initSchemaLdifPartition( partitionsDir );
+        initConfigPartition( partitionsDir );
+
+        cpReader = new ConfigPartitionReader( configPartition );
+
         // Initialize the LDAP server
         initLdap( layout, args );
 
@@ -97,44 +129,109 @@ public class Service implements DaemonAp
 
 
     /**
-     * Initialize the LDAP server
+     * initialize the schema partition by loading the schema LDIF files
+     * 
+     * @throws Exception in case of any problems while extracting and writing the schema files
      */
-    private void initLdap( InstallationLayout layout, String[] args ) throws Exception
+    private void initSchemaLdifPartition( File partitionsDir ) throws Exception
     {
-        LOG.info( "Starting the LDAP server" );
+        // Init the LdifPartition
+        schemaLdifPartition = new LdifPartition();
+        schemaLdifPartition.setWorkingDirectory( partitionsDir.getPath() + "/schema" );
 
-        printBanner( BANNER_LDAP );
-        long startTime = System.currentTimeMillis();
+        // Extract the schema on disk (a brand new one) and load the registries
+        File schemaRepository = new File( partitionsDir, "schema" );
 
-        if ( ( args != null ) && ( args.length > 0 ) && new File( args[0] ).exists() ) // hack that takes server.xml file argument
+        if ( schemaRepository.exists() )
         {
-            LOG.info( "server: loading settings from ", args[0] );
-            factory = new FileSystemXmlApplicationContext( new File( args[0] ).toURI().toURL().toString() );
-            ldapServer = ( LdapServer ) factory.getBean( "ldapServer" );
-            apacheDS = ( ApacheDS ) factory.getBean( "apacheDS" );
+            LOG.info( "schema partition already exists, skipping schema extraction" );
         }
         else
         {
-            LOG.info( "server: using default settings ..." );
-            DefaultDirectoryServiceFactory.DEFAULT.init( "default" );
-            DirectoryService directoryService = DefaultDirectoryServiceFactory.DEFAULT.getDirectoryService();
-            directoryService.startup();
-            ldapServer = new LdapServer();
-            ldapServer.setDirectoryService( directoryService );
-            TcpTransport tcpTransportSsl = new TcpTransport( 10636 );
-            tcpTransportSsl.enableSSL( true );
-            ldapServer.setTransports( new TcpTransport( 10389 ), tcpTransportSsl );
-            apacheDS = new ApacheDS( ldapServer );
+            SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor( partitionsDir );
+            extractor.extractOrCopy();
         }
 
-        if ( layout != null )
+        SchemaLoader loader = new LdifSchemaLoader( schemaRepository );
+        schemaManager = new DefaultSchemaManager( loader );
+
+        // We have to load the schema now, otherwise we won't be able
+        // to initialize the Partitions, as we won't be able to parse 
+        // and normalize their suffix DN
+        schemaManager.loadAllEnabled();
+
+        List<Throwable> errors = schemaManager.getErrors();
+
+        if ( errors.size() != 0 )
         {
-            ldapServer.getDirectoryService().setWorkingDirectory( layout.getPartitionsDirectory() );
+            throw new Exception( I18n.err( I18n.ERR_317, ExceptionUtils.printErrors( errors ) ) );
         }
+    }
 
-        // And start the server now
-        apacheDS.startup();
 
+    /**
+     * 
+     * initializes a LDIF partition for configuration
+     * 
+     * @param partitionsDir the directory where all the partitions' data is stored
+     * @throws Exception in case of any issues while extracting the schema
+     */
+    private void initConfigPartition( File partitionsDir ) throws Exception
+    {
+
+        File configRepository = new File( partitionsDir, "config" );
+
+        if ( configRepository.exists() )
+        {
+            LOG.info( "config partition already exists, skipping default config extraction" );
+        }
+        else
+        {
+            LdifConfigExtractor.extract( partitionsDir, true );
+        }
+
+        configPartition = new LdifPartition();
+        configPartition.setId( "config" );
+        configPartition.setSuffix( "ou=config" );
+        configPartition.setSchemaManager( schemaManager );
+        configPartition.setWorkingDirectory( partitionsDir.getPath() + "/config" );
+        configPartition.setPartitionDir( new File( configPartition.getWorkingDirectory() ) );
+
+        configPartition.initialize();
+    }
+
+
+    /**
+     * Initialize the LDAP server
+     */
+    private void initLdap( InstallationLayout layout, String[] args ) throws Exception
+    {
+        LOG.info( "Starting the LDAP server" );
+
+        printBanner( BANNER_LDAP );
+        long startTime = System.currentTimeMillis();
+
+        DirectoryService directoryService = cpReader.getDirectoryService();
+        directoryService.setSchemaManager( schemaManager );
+
+        SchemaPartition schemaPartition = directoryService.getSchemaService().getSchemaPartition();
+        schemaPartition.setWrappedPartition( schemaLdifPartition );
+        schemaPartition.setSchemaManager( schemaManager );
+
+        directoryService.addPartition( configPartition );
+
+        // this is a chicken-egg issue to have the working directory in the configuration so it should always be passed as
+        // a command line arg
+        directoryService.setWorkingDirectory( new File( args[0] ) );
+
+        ldapServer = cpReader.getLdapServer();
+        ldapServer.setDirectoryService( directoryService );
+
+        directoryService.startup();
+
+        // And start the server now
+        start();
+        
         if ( LOG.isInfoEnabled() )
         {
             LOG.info( "LDAP server: started in {} milliseconds", ( System.currentTimeMillis() - startTime ) + "" );
@@ -147,19 +244,11 @@ public class Service implements DaemonAp
      */
     private void initNtp( InstallationLayout layout, String[] args ) throws Exception
     {
-        if ( factory == null )
-        {
-            return;
-        }
-
-        try
-        {
-            ntpServer = ( NtpServer ) factory.getBean( "ntpServer" );
-        }
-        catch ( Exception e )
+        ntpServer = cpReader.getNtpServer();
+        if ( ntpServer == null )
         {
             LOG
-                .info( "Cannot find any reference to the NTP Server in the server.xml file : the server won't be started" );
+                .info( "Cannot find any reference to the NTP Server in the configuration : the server won't be started" );
             return;
         }
 
@@ -195,7 +284,7 @@ public class Service implements DaemonAp
     //        }
     //        catch ( Exception e )
     //        {
-    //            LOG.info( "Cannot find any reference to the DNS Server in the server.xml file : the server won't be started" );
+    //            LOG.info( "Cannot find any reference to the DNS Server in the configuration : the server won't be started" );
     //            return;
     //        }
     //        
@@ -219,19 +308,12 @@ public class Service implements DaemonAp
      */
     private void initKerberos( InstallationLayout layout, String[] args ) throws Exception
     {
-        if ( factory == null )
-        {
-            return;
-        }
 
-        try
-        {
-            kdcServer = ( KdcServer ) factory.getBean( "kdcServer" );
-        }
-        catch ( Exception e )
+        kdcServer = cpReader.getKdcServer();
+        if( kdcServer == null )
         {
             LOG
-                .info( "Cannot find any reference to the Kerberos Server in the server.xml file : the server won't be started" );
+            .info( "Cannot find any reference to the Kerberos Server in the configuration : the server won't be started" );
             return;
         }
 
@@ -257,19 +339,12 @@ public class Service implements DaemonAp
      */
     private void initChangePwd( InstallationLayout layout, String[] args ) throws Exception
     {
-        if ( factory == null )
-        {
-            return;
-        }
 
-        try
-        {
-            changePwdServer = ( ChangePasswordServer ) factory.getBean( "changePasswordServer" );
-        }
-        catch ( Exception e )
+        changePwdServer = cpReader.getChangePwdServer();
+        if ( changePwdServer == null )
         {
             LOG
-                .info( "Cannot find any reference to the Change Password Server in the server.xml file : the server won't be started" );
+                .info( "Cannot find any reference to the Change Password Server in the configuration : the server won't be started" );
             return;
         }
 
@@ -292,19 +367,12 @@ public class Service implements DaemonAp
 
     private void initHttpServer() throws Exception
     {
-        if ( factory == null )
-        {
-            return;
-        }
 
-        try
-        {
-            httpServer = ( HttpServer ) factory.getBean( "httpServer" );
-        }
-        catch ( Exception e )
+        httpServer = cpReader.getHttpServer();
+        if ( httpServer == null )
         {
             LOG
-                .info( "Cannot find any reference to the HTTP Server in the server.xml file : the server won't be started" );
+                .info( "Cannot find any reference to the HTTP Server in the configuration : the server won't be started" );
             return;
         }
 
@@ -343,11 +411,6 @@ public class Service implements DaemonAp
     public void stop( String[] args ) throws Exception
     {
 
-        if ( factory != null )
-        {
-            factory.close();
-        }
-
         // Stops the server
         ldapServer.stop();
 

Modified: directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/UberjarMain.java
URL: http://svn.apache.org/viewvc/directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/UberjarMain.java?rev=936252&r1=936251&r2=936252&view=diff
==============================================================================
--- directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/UberjarMain.java (original)
+++ directory/installers/trunk/apacheds-noarch/src/main/java/org/apache/directory/server/UberjarMain.java Wed Apr 21 10:53:27 2010
@@ -44,16 +44,24 @@ public class UberjarMain
     {
         Service service = new Service();
 
-        if ( args.length > 0 && new File( args[0] ).isDirectory() )
+        if ( args.length > 0 )
         {
             InstallationLayout layout = new InstallationLayout( args[0] );
-            String confFile = layout.getConfigurationFile().toURI().toURL().toString();
-            service.init( layout, new String[]
-                { confFile } );
-        }
-        else if ( args.length > 0 && new File( args[0] ).isFile() )
-        {
-            service.init( null, args );
+            
+            // assign the given directory path first
+            String partitionDir = args[0];
+            try
+            {
+                // check the validity of the installationlayout, if correct set it's partition dir to partitionDir
+                layout.verifyInstallation();
+                partitionDir = layout.getPartitionsDirectory().getAbsolutePath();
+            }
+            catch( Exception e )
+            {
+                // nothing to do
+            }
+            
+            service.init( layout, new String[]{ partitionDir } );
         }
         else
         {