directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r894530 - in /directory/apacheds/branches/apacheds-schema: server-annotations/src/main/java/org/apache/directory/server/factory/ server-annotations/src/test/java/org/apache/directory/server/factory/ test-framework/src/main/java/org/apache/d...
Date Wed, 30 Dec 2009 01:30:53 GMT
Author: elecharny
Date: Wed Dec 30 01:30:51 2009
New Revision: 894530

URL: http://svn.apache.org/viewvc?rev=894530&view=rev
Log:
o Added the ServerAnnotationProcessor, used to create the LdapServer
o Added a test to check that we are correctly starting the server
o Improved the FrameworkSuite by calling the ServerAnnotationProcessor methods

Added:
    directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
Modified:
    directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
    directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java

Added: directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java?rev=894530&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
(added)
+++ directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
Wed Dec 30 01:30:51 2009
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.factory;
+
+import java.lang.reflect.Method;
+
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.ldap.LdapServer;
+import org.apache.directory.server.protocol.shared.transport.TcpTransport;
+import org.apache.directory.server.protocol.shared.transport.Transport;
+import org.apache.mina.util.AvailablePortFinder;
+import org.junit.runner.Description;
+
+/**
+ * 
+ * TODO ServerAnnotationProcessor.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ServerAnnotationProcessor
+{
+    private static void createTransports( LdapServer ldapServer, CreateTransport[] transportBuilders
)
+    {
+        if ( transportBuilders.length != 0 )
+        {
+            int createdPort = 1024;
+            
+            for ( CreateTransport transportBuilder : transportBuilders )
+            {
+                String protocol = transportBuilder.protocol();
+                int port = transportBuilder.port();
+                int nbThreads = transportBuilder.nbThreads();
+                int backlog = transportBuilder.backlog();
+                String address = transportBuilder.address();
+                
+                if ( port == -1 )
+                {
+                    port = AvailablePortFinder.getNextAvailable( createdPort );
+                    createdPort = port + 1;
+                }
+                
+                if ( protocol.equalsIgnoreCase( "LDAP" ) )
+                {
+                    Transport ldap = new TcpTransport( address, port, nbThreads, backlog
);
+                    ldapServer.addTransports( ldap );
+                }
+                else if ( protocol.equalsIgnoreCase( "LDAPS" ) )
+                {
+                    Transport ldaps = new TcpTransport( address, port, nbThreads, backlog
);
+                    ldaps.setEnableSSL( true );
+                    ldapServer.addTransports( ldaps );
+                }
+            }
+        }
+        else
+        {
+            // Create default LDAP and LDAPS transports
+            int port = AvailablePortFinder.getNextAvailable( 1024 );
+            Transport ldap = new TcpTransport( port );
+            ldapServer.addTransports( ldap );
+            
+            port = AvailablePortFinder.getNextAvailable( port );
+            Transport ldaps = new TcpTransport( port );
+            ldaps.setEnableSSL( true );
+            ldapServer.addTransports( ldaps );
+        }
+    }
+    
+    
+    private static LdapServer createLdapServer( CreateLdapServer createLdapServer, DirectoryService
directoryService )
+    {
+        if ( createLdapServer != null )
+        {
+            LdapServer ldapServer = new LdapServer();
+            
+            ldapServer.setServiceName( createLdapServer.name() );
+            
+            // Read the transports
+            createTransports( ldapServer, createLdapServer.transports() );
+            
+            // Associate the DS to this LdapServer
+            ldapServer.setDirectoryService( directoryService );
+            
+            // Launch the server
+            try
+            {
+                ldapServer.start();
+            }
+            catch ( Exception e )
+            {
+                e.printStackTrace();
+            }
+            
+            return ldapServer;
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    
+    public static LdapServer getLdapServer( DirectoryService directoryService ) throws Exception
+    {
+        CreateLdapServer createLdapServer = null;
+        
+        // Get the caller by inspecting the stackTrace
+        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
+        
+        // Get the enclosing class
+        Class<?> classCaller = Class.forName( stackTrace[2].getClassName() );
+        
+        // Get the current method
+        String methodCaller = stackTrace[2].getMethodName();
+        
+        // Check if we have any annotation associated with the method
+        Method[] methods = classCaller.getMethods();
+        
+        for ( Method method : methods )
+        {
+            if ( methodCaller.equals( method.getName() ) )
+            {
+                createLdapServer = method.getAnnotation( CreateLdapServer.class );
+                
+                if ( createLdapServer != null )
+                {
+                    break;
+                }
+            }
+        }
+
+        // No : look at the class level
+        if ( createLdapServer == null )
+        {
+            createLdapServer = classCaller.getAnnotation( CreateLdapServer.class );
+        }
+        
+        // Ok, we have found a CreateLdapServer annotation. Process it now.
+        return createLdapServer( createLdapServer, directoryService );
+    }
+
+
+    public static LdapServer getLdapServer( Description description, DirectoryService directoryService
) throws Exception
+    {
+        CreateLdapServer createLdapServer = description.getAnnotation( CreateLdapServer.class
);
+
+        // Ok, we have found a CreateLdapServer annotation. Process it now.
+        return createLdapServer( createLdapServer, directoryService );
+    }
+}

Modified: directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java?rev=894530&r1=894529&r2=894530&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
(original)
+++ directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
Wed Dec 30 01:30:51 2009
@@ -24,9 +24,21 @@
 import static org.junit.Assert.assertTrue;
 
 import java.util.HashSet;
+import java.util.Hashtable;
 import java.util.Set;
 
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import javax.naming.ldap.Control;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
 import org.apache.commons.io.FileUtils;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.constants.ServerDNConstants;
 import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.annotations.ContextEntry;
 import org.apache.directory.server.core.annotations.CreateDS;
@@ -34,6 +46,7 @@
 import org.apache.directory.server.core.annotations.CreatePartition;
 import org.apache.directory.server.core.factory.DSAnnotationProcessor;
 import org.apache.directory.server.core.partition.Partition;
+import org.apache.directory.server.ldap.LdapServer;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.junit.Test;
 
@@ -47,6 +60,27 @@
 @CreateDS( name = "classDS" )
 public class DirectoryServiceAnnotationTest
 {
+    /**
+     * Creates a JNDI LdapContext with a connection over the wire using the 
+     * SUN LDAP provider.  The connection is made using the administrative 
+     * user as the principalDN.  The context is to the rootDSE.
+     *
+     * @param ldapServer the LDAP server to get the connection to
+     * @return an LdapContext as the administrative user to the RootDSE
+     * @throws Exception if there are problems creating the context
+     */
+    private LdapContext getWiredContext( LdapServer ldapServer, Control[] controls ) throws
Exception
+    {
+        Hashtable<String, String> env = new Hashtable<String, String>();
+        env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
+        env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() );
+        env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN );
+        env.put( Context.SECURITY_CREDENTIALS, "secret" );
+        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
+        return new InitialLdapContext( env, controls );
+    }
+    
+
     @Test
     public void testCreateDS() throws Exception
     {
@@ -130,4 +164,102 @@
         service.shutdown();
         FileUtils.deleteDirectory( service.getWorkingDirectory() );
     }
+    
+    
+    @Test
+    @CreateDS( 
+        name = "MethodDSWithPartitionAndServer",
+        partitions =
+        {
+            @CreatePartition(
+                name = "example",
+                suffix = "dc=example,dc=com",
+                contextEntry = @ContextEntry( 
+                    entryLdif =
+                        "dn: dc=example,dc=com\n" +
+                        "dc: example\n" +
+                        "objectClass: top\n" +
+                        "objectClass: domain\n\n" ),
+                indexes = 
+                {
+                    @CreateIndex( attribute = "objectClass" ),
+                    @CreateIndex( attribute = "dc" ),
+                    @CreateIndex( attribute = "ou" ),
+                } )
+        } )
+    @CreateLdapServer ( 
+        transports = 
+        {
+            @CreateTransport( protocol = "LDAP" ), 
+            @CreateTransport( protocol = "LDAPS" ) 
+        })
+    public void testCreateLdapServer() throws Exception
+    {
+        // First, get the service
+        DirectoryService service = DSAnnotationProcessor.getDirectoryService();
+        
+        // Check that the service is running
+        assertTrue( service.isStarted() );
+        assertEquals( "MethodDSWithPartitionAndServer", service.getInstanceId() );
+        
+        
+        Set<String> expectedNames = new HashSet<String>();
+        
+        expectedNames.add( "example" );
+        expectedNames.add( "schema" );
+        
+        assertEquals( 2, service.getPartitions().size() );
+        
+        for ( Partition partition : service.getPartitions() )
+        {
+            assertTrue( expectedNames.contains( partition.getId() ) );
+            
+            if ( "example".equalsIgnoreCase( partition.getId() ) )
+            {
+                assertTrue( partition.isInitialized() );
+                assertEquals( "dc=example,dc=com", partition.getSuffixDn().getUpName() );
+            }
+            else if ( "schema".equalsIgnoreCase( partition.getId() ) )
+            {
+                assertTrue( partition.isInitialized() );
+                assertEquals( "ou=schema", partition.getSuffixDn().getUpName() );
+            }
+        }
+        
+        assertTrue( service.getAdminSession().exists( new LdapDN( "dc=example,dc=com" ) )
);
+
+        // Now, get the server
+        LdapServer ldapServer = ServerAnnotationProcessor.getLdapServer( service );
+
+        // Check that the server is running
+        assertTrue( ldapServer.isStarted() );
+        
+        // Try to read an entry in the server
+        LdapContext ctx = ( LdapContext ) getWiredContext( ldapServer, null ).lookup( "dc=example,dc=com"
);
+        SearchControls controls = new SearchControls();
+        controls.setSearchScope( SearchControls.OBJECT_SCOPE );
+        controls.setReturningAttributes( new String[]{"*"} );
+        
+        NamingEnumeration<SearchResult> enumeration = ctx.search( "", "(objectClass=*)",
controls );
+        
+        // collect all results 
+        HashSet<String> results = new HashSet<String>();
+        
+        while ( enumeration.hasMore() )
+        {
+            SearchResult result = enumeration.next();
+            results.add( result.getNameInNamespace() );
+        }
+        
+        assertEquals( 1, results.size() );
+        assertTrue( results.contains( "dc=example,dc=com" ) );
+
+        enumeration.close();
+        ctx.close();
+        ldapServer.stop();
+        service.shutdown();
+        
+        FileUtils.deleteDirectory( service.getWorkingDirectory() );
+    }
+
 }

Modified: directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java?rev=894530&r1=894529&r2=894530&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
(original)
+++ directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java
Wed Dec 30 01:30:51 2009
@@ -21,14 +21,11 @@
 
 import org.apache.commons.io.FileUtils;
 import org.apache.directory.server.annotations.CreateLdapServer;
-import org.apache.directory.server.annotations.CreateTransport;
 import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.annotations.CreatePartition;
 import org.apache.directory.server.core.factory.DSAnnotationProcessor;
+import org.apache.directory.server.factory.ServerAnnotationProcessor;
 import org.apache.directory.server.ldap.LdapServer;
-import org.apache.directory.server.protocol.shared.transport.TcpTransport;
-import org.apache.directory.server.protocol.shared.transport.Transport;
-import org.apache.mina.util.AvailablePortFinder;
 import org.junit.runner.Description;
 import org.junit.runner.Runner;
 import org.junit.runner.notification.RunNotifier;
@@ -113,54 +110,6 @@
     }
     
     
-    private void createTransports( LdapServer ldapServer, CreateTransport[] transportBuilders
)
-    {
-        if ( transportBuilders.length != 0 )
-        {
-            int createdPort = 1024;
-            
-            for ( CreateTransport transportBuilder : transportBuilders )
-            {
-                String protocol = transportBuilder.protocol();
-                int port = transportBuilder.port();
-                int nbThreads = transportBuilder.nbThreads();
-                int backlog = transportBuilder.backlog();
-                String address = transportBuilder.address();
-                
-                if ( port == -1 )
-                {
-                    port = AvailablePortFinder.getNextAvailable( createdPort );
-                    createdPort = port + 1;
-                }
-                
-                if ( protocol.equalsIgnoreCase( "LDAP" ) )
-                {
-                    Transport ldap = new TcpTransport( address, port, nbThreads, backlog
);
-                    ldapServer.addTransports( ldap );
-                }
-                else if ( protocol.equalsIgnoreCase( "LDAPS" ) )
-                {
-                    Transport ldaps = new TcpTransport( address, port, nbThreads, backlog
);
-                    ldaps.setEnableSSL( true );
-                    ldapServer.addTransports( ldaps );
-                }
-            }
-        }
-        else
-        {
-            // Create default LDAP and LDAPS transports
-            int port = AvailablePortFinder.getNextAvailable( 1024 );
-            Transport ldap = new TcpTransport( port );
-            ldapServer.addTransports( ldap );
-            
-            port = AvailablePortFinder.getNextAvailable( port );
-            Transport ldaps = new TcpTransport( port );
-            ldaps.setEnableSSL( true );
-            ldapServer.addTransports( ldaps );
-        }
-    }
-
-    
     private void addPartitions( Description description )
     {
         CreatePartition createPartition = description.getAnnotation( CreatePartition.class
);
@@ -174,29 +123,13 @@
     
     private void startLdapServer( Description description )
     {
-        ldapServerBuilder = description.getAnnotation( CreateLdapServer.class );
-
-        if ( ldapServerBuilder != null )
+        try
         {
-            LdapServer ldapServer = new LdapServer();
-            
-            ldapServer.setServiceName( ldapServerBuilder.name() );
-            
-            // Read the transports
-            createTransports( ldapServer, ldapServerBuilder.transports() );
-            
-            // Associate the DS to this LdapServer
-            ldapServer.setDirectoryService( directoryService );
-            
-            // Launch the server
-            try
-            {
-                ldapServer.start();
-            }
-            catch ( Exception e )
-            {
-                e.printStackTrace();
-            }
+            ldapServer = ServerAnnotationProcessor.getLdapServer( description, directoryService
);
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
         }
     }
     



Mime
View raw message