directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r894466 - in /directory/apacheds/branches/apacheds-schema: core-annotations/src/main/java/org/apache/directory/server/core/factory/ core-annotations/src/test/java/org/apache/directory/server/core/factory/ test-framework/src/main/java/org/ap...
Date Tue, 29 Dec 2009 19:17:28 GMT
Author: elecharny
Date: Tue Dec 29 19:17:27 2009
New Revision: 894466

URL: http://svn.apache.org/viewvc?rev=894466&view=rev
Log:
o Renamed the DSBuilderAnnotationProcessor
o Added a method to infer the @ using reflection
o Injected a test with Partition (not working atm)

Added:
    directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
      - copied, changed from r894422, directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
Removed:
    directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
Modified:
    directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DefaultDirectoryServiceFactory.java
    directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
    directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
    directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkSuite.java

Copied: directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
(from r894422, directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java)
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java?p2=directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java&p1=directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java&r1=894422&r2=894466&rev=894466&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSBuilderAnnotationProcessor.java
(original)
+++ directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
Tue Dec 29 19:17:27 2009
@@ -1,6 +1,24 @@
-
+/*
+ * 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.core.factory;
 
+import java.lang.reflect.Method;
 import java.util.List;
 
 import javax.naming.NamingException;
@@ -8,19 +26,38 @@
 import org.apache.directory.server.core.DirectoryService;
 import org.apache.directory.server.core.annotations.ApplyLdifFiles;
 import org.apache.directory.server.core.annotations.ApplyLdifs;
+import org.apache.directory.server.core.annotations.ContextEntry;
 import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreateIndex;
+import org.apache.directory.server.core.annotations.CreatePartition;
 import org.apache.directory.server.core.entry.DefaultServerEntry;
+import org.apache.directory.server.core.entry.ServerEntry;
+import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmIndex;
+import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
+import org.apache.directory.server.xdbm.Index;
 import org.apache.directory.shared.ldap.ldif.LdifEntry;
 import org.apache.directory.shared.ldap.ldif.LdifReader;
 import org.junit.runner.Description;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DSBuilderAnnotationProcessor
+/**
+ * A Helper class used to create a DS from the annotations
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class DSAnnotationProcessor
 {
     /** A logger for this class */
-    private static final Logger LOG = LoggerFactory.getLogger( DSBuilderAnnotationProcessor.class
);
+    private static final Logger LOG = LoggerFactory.getLogger( DSAnnotationProcessor.class
);
 
+    /**
+     * Create a DirectoryService from a Unit test annotation
+     *
+     * @param description The annotations containing the info from which we will create the
DS
+     * @return A valid DS
+     */
     public static DirectoryService getDirectoryService( Description description )
     {
         try
@@ -54,6 +91,110 @@
     }
     
     
+    /**
+     * Create a DirectoryService from an annotation. The @CreateDS annotation must
+     * be associated with either the method or the encapsulating class. We will first
+     * try to get the annotation from the method, and if there is none, then we try
+     * at the class level. 
+     *
+     * @return A valid DS
+     */
+    public static DirectoryService getDirectoryService() throws Exception
+    {
+        CreateDS dsBuilder = 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() ) )
+            {
+                dsBuilder = method.getAnnotation( CreateDS.class );
+                
+                if ( dsBuilder != null )
+                {
+                    break;
+                }
+            }
+        }
+
+        // No : look at the class level
+        if ( dsBuilder == null )
+        {
+            dsBuilder = classCaller.getAnnotation( CreateDS.class );
+        }
+        
+        // Ok, we have found a CreateDS annotation. Process it now.
+        try
+        {
+            LOG.debug( "Starting DS {}...", dsBuilder.name() );
+            Class<?> factory = dsBuilder.factory();
+            DirectoryServiceFactory dsf = ( DirectoryServiceFactory ) factory.newInstance();
+            
+            DirectoryService service = dsf.getDirectoryService();
+            service.setAccessControlEnabled( dsBuilder.enableAccessControl() );
+            service.setAllowAnonymousAccess( dsBuilder.allowAnonAccess() );
+            
+            dsf.init( dsBuilder.name() );
+            
+            // Process the Partition, if any.
+            for ( CreatePartition createPartition : dsBuilder.partitions() )
+            {
+                // Create the partition
+                JdbmPartition partition = new JdbmPartition();
+                partition.setId( createPartition.name() );
+                partition.setSuffix( createPartition.suffix() );
+                partition.setCacheSize( createPartition.cacheSize() );
+                
+                // Process the indexes if any
+                CreateIndex[] indexes = createPartition.indexes();
+
+                for ( CreateIndex createIndex : indexes )
+                {
+                    Index<String, ServerEntry> index = new JdbmIndex<String, ServerEntry>(
createIndex.attribute() );
+                    index.setCacheSize( createIndex.cacheSize() );
+                    
+                    partition.addIndexedAttributes( index );
+                }
+                
+                // Inject the partition into the DirectoryService
+                service.addPartition( partition );
+                
+                // Last, process the context entry
+                ContextEntry contextEntry = createPartition.contextEntry();
+                
+                if ( contextEntry != null )
+                {
+                    injectEntries( service, contextEntry.entryLdif() );
+                }
+            }
+            
+            return service;
+        }
+        catch ( Exception e )
+        {
+            return null;
+        }
+    }
+    
+    
+    /**
+     * Create a DirectoryService using the class annotation
+     * TODO getClassDirectoryService.
+     *
+     * @param clazz
+     * @return
+     */
     public static DirectoryService getClassDirectoryService( Class<?> clazz )
     {
         try

Modified: directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DefaultDirectoryServiceFactory.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DefaultDirectoryServiceFactory.java?rev=894466&r1=894465&r2=894466&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DefaultDirectoryServiceFactory.java
(original)
+++ directory/apacheds/branches/apacheds-schema/core-annotations/src/main/java/org/apache/directory/server/core/factory/DefaultDirectoryServiceFactory.java
Tue Dec 29 19:17:27 2009
@@ -196,7 +196,7 @@
         File workingDirectory = new File( workingDirectoryPath );
         FileUtils.deleteDirectory( workingDirectory );
         
-        // Init the sevice now
+        // Init the service now
         initSchema();
         initChangeLog();
         initSystemPartition();

Modified: directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java?rev=894466&r1=894465&r2=894466&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
(original)
+++ directory/apacheds/branches/apacheds-schema/core-annotations/src/test/java/org/apache/directory/server/core/factory/DirectoryServiceAnnotationTest.java
Tue Dec 29 19:17:27 2009
@@ -20,11 +20,15 @@
 
 package org.apache.directory.server.core.factory;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.annotations.ContextEntry;
 import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreateIndex;
+import org.apache.directory.server.core.annotations.CreatePartition;
 import org.junit.Test;
 
 
@@ -34,17 +38,67 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-@CreateDS( name="test" )
+@CreateDS( name = "classDS" )
 public class DirectoryServiceAnnotationTest
 {
     @Test
     public void testCreateDS() throws Exception
     {
-        DirectoryService service = DSBuilderAnnotationProcessor.getClassDirectoryService(
this.getClass() );
+        DirectoryService service = DSAnnotationProcessor.getDirectoryService();
         
         assertTrue( service.isStarted() );
+        assertEquals( "classDS", service.getInstanceId() );
         
         service.shutdown();
         FileUtils.deleteDirectory( service.getWorkingDirectory() );
     }
+
+
+    @Test
+    @CreateDS( name = "methodDS" )
+    public void testCreateMethodDS() throws Exception
+    {
+        DirectoryService service = DSAnnotationProcessor.getDirectoryService();
+        
+        assertTrue( service.isStarted() );
+        assertEquals( "methodDS", service.getInstanceId() );
+        
+        service.shutdown();
+        FileUtils.deleteDirectory( service.getWorkingDirectory() );
+    }
+    
+    
+    @Test
+    @CreateDS( 
+        name = "MethodDSWithPartition",
+        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" ),
+                } )
+        } )
+    public void testCreateMethodDSWithPartition() throws Exception
+    {
+        /*
+        DirectoryService service = DSAnnotationProcessor.getDirectoryService();
+        
+        assertTrue( service.isStarted() );
+        assertEquals( "methodDS", service.getInstanceId() );
+        
+        service.shutdown();
+        FileUtils.deleteDirectory( service.getWorkingDirectory() );
+        */
+    }
 }

Modified: directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java?rev=894466&r1=894465&r2=894466&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
(original)
+++ directory/apacheds/branches/apacheds-schema/test-framework/src/main/java/org/apache/directory/server/core/integ/FrameworkRunner.java
Tue Dec 29 19:17:27 2009
@@ -26,7 +26,7 @@
 import org.apache.directory.server.DefaultLdapServerFactory;
 import org.apache.directory.server.annotations.CreateLdapServer;
 import org.apache.directory.server.core.DirectoryService;
-import org.apache.directory.server.core.factory.DSBuilderAnnotationProcessor;
+import org.apache.directory.server.core.factory.DSAnnotationProcessor;
 import org.apache.directory.server.core.factory.DefaultDirectoryServiceFactory;
 import org.apache.directory.server.core.factory.DirectoryServiceFactory;
 import org.junit.Ignore;
@@ -88,7 +88,7 @@
 
         try
         {
-            classDS = DSBuilderAnnotationProcessor.getDirectoryService( getDescription()
);
+            classDS = DSAnnotationProcessor.getDirectoryService( getDescription() );
             long revision = 0L;
             DirectoryService directoryService = null;
 
@@ -101,10 +101,10 @@
                 // Get the applyLdifs for each level and apply them
                 if ( suite != null )
                 {
-                    DSBuilderAnnotationProcessor.applyLdifs( suite.getDescription(), classDS
);
+                    DSAnnotationProcessor.applyLdifs( suite.getDescription(), classDS );
                 }
                 
-                DSBuilderAnnotationProcessor.applyLdifs( getDescription(), classDS );
+                DSAnnotationProcessor.applyLdifs( getDescription(), classDS );
             }
             else
             {
@@ -121,7 +121,7 @@
                         LOG.debug( "Create revision {}", revision );
 
                         // apply the class LDIFs
-                        DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
+                        DSAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
                     }
                     else
                     {
@@ -134,13 +134,13 @@
                         suite.setDirectoryService( directoryService );
                         
                         // Apply the suite LDIF first
-                        DSBuilderAnnotationProcessor.applyLdifs( suite.getDescription(),
directoryService );
+                        DSAnnotationProcessor.applyLdifs( suite.getDescription(), directoryService
);
                         
                         // Then tag for reversion and apply the class LDIFs
                         revision = directoryService.getChangeLog().getCurrentRevision();
                         LOG.debug( "Create revision {}", revision );
                         
-                        DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
+                        DSAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
                     }
                 }
                 else
@@ -154,7 +154,7 @@
                     classDS = directoryService;
 
                     // Apply the class LDIFs
-                    DSBuilderAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
+                    DSAnnotationProcessor.applyLdifs( getDescription(), directoryService
);
                 }
             }
 
@@ -223,14 +223,14 @@
             long revision = 0L;
 
             // Check if this method has a dedicated DSBuilder
-            DirectoryService methodDS = DSBuilderAnnotationProcessor.getDirectoryService(
methodDescription );
+            DirectoryService methodDS = DSAnnotationProcessor.getDirectoryService( methodDescription
);
 
             if ( methodDS != null )
             {
                 // Apply all the LDIFs
-                DSBuilderAnnotationProcessor.applyLdifs( suiteDescription, methodDS );
-                DSBuilderAnnotationProcessor.applyLdifs( classDescription, methodDS );
-                DSBuilderAnnotationProcessor.applyLdifs( methodDescription, methodDS );
+                DSAnnotationProcessor.applyLdifs( suiteDescription, methodDS );
+                DSAnnotationProcessor.applyLdifs( classDescription, methodDS );
+                DSAnnotationProcessor.applyLdifs( methodDescription, methodDS );
                 
                 directoryService = methodDS;
             }
@@ -242,7 +242,7 @@
                 revision = directoryService.getChangeLog().getCurrentRevision();
                 LOG.debug( "Create revision {}", revision );
                 
-                DSBuilderAnnotationProcessor.applyLdifs( methodDescription, directoryService
);
+                DSAnnotationProcessor.applyLdifs( methodDescription, directoryService );
             }
             else if ( suite != null )
             {
@@ -252,7 +252,7 @@
                 revision = directoryService.getChangeLog().getCurrentRevision();
                 LOG.debug( "Create revision {}", revision );
                 
-                DSBuilderAnnotationProcessor.applyLdifs( methodDescription, directoryService
);
+                DSAnnotationProcessor.applyLdifs( methodDescription, directoryService );
             }
 
             // At this point, we know which service to use.

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=894466&r1=894465&r2=894466&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
Tue Dec 29 19:17:27 2009
@@ -24,7 +24,7 @@
 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.DSBuilderAnnotationProcessor;
+import org.apache.directory.server.core.factory.DSAnnotationProcessor;
 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;
@@ -75,14 +75,14 @@
     private void startDS( Description description )
     {
         // Initialize and start the DS before running any test, if we have a DS annotation
-        directoryService = DSBuilderAnnotationProcessor.getDirectoryService( description
);
+        directoryService = DSAnnotationProcessor.getDirectoryService( description );
         
         // and inject LDIFs if needed
         if ( directoryService != null )
         {
             try
             {
-                DSBuilderAnnotationProcessor.applyLdifs( description, directoryService );
+                DSAnnotationProcessor.applyLdifs( description, directoryService );
             }
             catch ( Exception e )
             {



Mime
View raw message