directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1153095 - in /directory/apacheds/trunk: core-annotations/src/main/java/org/apache/directory/server/core/factory/ core-api/src/main/java/org/apache/directory/server/core/annotations/ server-annotations/src/main/java/org/apache/directory/ser...
Date Tue, 02 Aug 2011 11:14:41 GMT
Author: elecharny
Date: Tue Aug  2 11:14:39 2011
New Revision: 1153095

URL: http://svn.apache.org/viewvc?rev=1153095&view=rev
Log:
o Created a AnnotationUtils class
o Moved a method from DSAnnotationProcessor to AnnotationUtils
o Used this helper class in tests

Added:
    directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/annotations/
    directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/annotations/AnnotationUtils.java
Modified:
    directory/apacheds/trunk/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
    directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
    directory/apacheds/trunk/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
    directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientInitialRefreshIT.java
    directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientServerReplicationIT.java

Modified: directory/apacheds/trunk/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java?rev=1153095&r1=1153094&r2=1153095&view=diff
==============================================================================
--- directory/apacheds/trunk/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
(original)
+++ directory/apacheds/trunk/core-annotations/src/main/java/org/apache/directory/server/core/factory/DSAnnotationProcessor.java
Tue Aug  2 11:14:39 2011
@@ -22,14 +22,13 @@ package org.apache.directory.server.core
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
-import java.lang.annotation.Annotation;
 import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
 import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.annotations.AnnotationUtils;
 import org.apache.directory.server.core.annotations.ApplyLdifFiles;
 import org.apache.directory.server.core.annotations.ApplyLdifs;
 import org.apache.directory.server.core.annotations.ContextEntry;
@@ -234,56 +233,6 @@ public class DSAnnotationProcessor
     
     
     /**
-     * Get an instance of a class extracted from the @nnotation found in the method
-     * or the class.
-     * 
-     * @param clazz The Annotation we want to get an instance for
-     * @return The instance or null if no annotation is found
-     * @throws ClassNotFoundException If we can't find a class
-     */
-    public static Object getInstance( Class<? extends Annotation> clazz ) throws ClassNotFoundException
-    {
-        Object instance = null;
-        
-        // Get the caller by inspecting the stackTrace
-        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
-
-        // In Java5 the 0th stacktrace element is:
-        // java.lang.Thread.dumpThreads(Native Method)
-        int index = stackTrace[0].getMethodName().equals( "dumpThreads" ) ? 4 : 3;
-
-        // Get the enclosing class
-        Class<?> classCaller = Class.forName( stackTrace[index].getClassName() );
-
-        // Get the current method
-        String methodCaller = stackTrace[index].getMethodName();
-
-        // Check if we have any annotation associated with the method
-        Method[] methods = classCaller.getMethods();
-
-        for ( Method method : methods )
-        {
-            if ( methodCaller.equals( method.getName() ) )
-            {
-                instance = method.getAnnotation( clazz );
-
-                if ( instance != null )
-                {
-                    break;
-                }
-            }
-        }
-        
-        if ( instance == null )
-        {
-            instance = classCaller.getAnnotation( clazz );
-        }
-        
-        return instance;
-    }
-
-
-    /**
      * 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
@@ -293,7 +242,7 @@ public class DSAnnotationProcessor
      */
     public static DirectoryService getDirectoryService() throws Exception
     {
-        Object instance = getInstance( CreateDS.class );
+        Object instance = AnnotationUtils.getInstance( CreateDS.class );
         CreateDS dsBuilder = null;
         
         if ( instance != null )

Added: directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/annotations/AnnotationUtils.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/annotations/AnnotationUtils.java?rev=1153095&view=auto
==============================================================================
--- directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/annotations/AnnotationUtils.java
(added)
+++ directory/apacheds/trunk/core-api/src/main/java/org/apache/directory/server/core/annotations/AnnotationUtils.java
Tue Aug  2 11:14:39 2011
@@ -0,0 +1,95 @@
+/*
+ *  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.annotations;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
+/**
+ * An helper class used to find annotations in methods and classes
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AnnotationUtils
+{
+    /**
+     * Get an instance of a class extracted from the annotation found in the method
+     * or the class. We iterate on the stack trace until we find the desired annotation.
+     * 
+     * @param clazz The Annotation we want to get an instance for
+     * @return The instance or null if no annotation is found
+     * @throws ClassNotFoundException If we can't find a class
+     */
+    public static Object getInstance( Class<? extends Annotation> clazz ) throws ClassNotFoundException
+    {
+        Object instance = null;
+        
+        // Get the caller by inspecting the stackTrace
+        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
+
+        // Iterate on the stack trace.
+        for ( int i = stackTrace.length - 1; i >= 0  ; i-- )
+        {
+            Class<?> classCaller = null;
+            
+            // Get the current class
+            try
+            {
+                classCaller = Class.forName( stackTrace[i].getClassName() );
+            }
+            catch ( ClassNotFoundException cnfe )
+            {
+                // Corner case : we just have to go higher in the stack in this case.
+                continue;
+            }
+    
+            // Get the current method
+            String methodCaller = stackTrace[i].getMethodName();
+    
+            // Check if we have any annotation associated with the method
+            Method[] methods = classCaller.getMethods();
+    
+            for ( Method method : methods )
+            {
+                if ( methodCaller.equals( method.getName() ) )
+                {
+                    instance = method.getAnnotation( clazz );
+    
+                    if ( instance != null )
+                    {
+                        break;
+                    }
+                }
+            }
+            
+            if ( instance == null )
+            {
+                instance = classCaller.getAnnotation( clazz );
+            }
+            
+            if ( instance != null )
+            {
+                break;
+            }
+        }
+        
+        return instance;
+    }
+}

Modified: directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java?rev=1153095&r1=1153094&r2=1153095&view=diff
==============================================================================
--- directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
(original)
+++ directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java
Tue Aug  2 11:14:39 2011
@@ -29,7 +29,7 @@ import org.apache.directory.server.annot
 import org.apache.directory.server.annotations.CreateTransport;
 import org.apache.directory.server.annotations.SaslMechanism;
 import org.apache.directory.server.core.DirectoryService;
-import org.apache.directory.server.core.factory.DSAnnotationProcessor;
+import org.apache.directory.server.core.annotations.AnnotationUtils;
 import org.apache.directory.server.i18n.I18n;
 import org.apache.directory.server.kerberos.kdc.KdcServer;
 import org.apache.directory.server.ldap.ExtendedOperationHandler;
@@ -199,14 +199,14 @@ public class ServerAnnotationProcessor
     
     
     /**
-     * creates an LdapServer and starts before returning the instance, infering
+     * Returns an LdapServer instance and starts it before returning the instance, infering
      * the configuration from the Stack trace
      *  
      * @return a running LdapServer instance
      */
-    public static LdapServer createLdapServer( DirectoryService directoryService ) throws
ClassNotFoundException
+    public static LdapServer getLdapServer( DirectoryService directoryService ) throws ClassNotFoundException
     {
-        Object instance = DSAnnotationProcessor.getInstance( CreateLdapServer.class );
+        Object instance = AnnotationUtils.getInstance( CreateLdapServer.class );
         LdapServer ldapServer = null;
         
         if ( instance != null )
@@ -220,6 +220,9 @@ public class ServerAnnotationProcessor
     }
     
     
+    /**
+     * Create a replication consumer
+     */
     private static ReplicationConsumer createConsumer( CreateConsumer createConsumer )
     {
         ReplicationConsumer consumer = new SyncReplConsumer();
@@ -247,7 +250,7 @@ public class ServerAnnotationProcessor
      */
     public static ReplicationConsumer createConsumer() throws ClassNotFoundException
     {
-        Object instance = DSAnnotationProcessor.getInstance( CreateConsumer.class );
+        Object instance = AnnotationUtils.getInstance( CreateConsumer.class );
         ReplicationConsumer consumer = null;
         
         if ( instance != null )

Modified: directory/apacheds/trunk/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java?rev=1153095&r1=1153094&r2=1153095&view=diff
==============================================================================
--- directory/apacheds/trunk/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
(original)
+++ directory/apacheds/trunk/server-annotations/src/test/java/org/apache/directory/server/factory/DirectoryServiceAnnotationTest.java
Tue Aug  2 11:14:39 2011
@@ -231,7 +231,7 @@ public class DirectoryServiceAnnotationT
         assertTrue( service.getAdminSession().exists( new Dn( "dc=example,dc=com" ) ) );
 
         // Now, get the server
-        LdapServer ldapServer = ServerAnnotationProcessor.createLdapServer( service );
+        LdapServer ldapServer = ServerAnnotationProcessor.getLdapServer( service );
 
         // Check that the server is running
         assertTrue( ldapServer.isStarted() );

Modified: directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientInitialRefreshIT.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientInitialRefreshIT.java?rev=1153095&r1=1153094&r2=1153095&view=diff
==============================================================================
--- directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientInitialRefreshIT.java
(original)
+++ directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientInitialRefreshIT.java
Tue Aug  2 11:14:39 2011
@@ -160,7 +160,7 @@ public class ClientInitialRefreshIT
     {
         DirectoryService provDirService = DSAnnotationProcessor.getDirectoryService();
 
-        providerServer = ServerAnnotationProcessor.createLdapServer( provDirService );
+        providerServer = ServerAnnotationProcessor.getLdapServer( provDirService );
         
         providerServer.setReplicationReqHandler( new SyncReplRequestHandler() );
         providerServer.startReplicationProducer();

Modified: directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientServerReplicationIT.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientServerReplicationIT.java?rev=1153095&r1=1153094&r2=1153095&view=diff
==============================================================================
--- directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientServerReplicationIT.java
(original)
+++ directory/apacheds/trunk/server-integ/src/test/java/org/apache/directory/server/replication/ClientServerReplicationIT.java
Tue Aug  2 11:14:39 2011
@@ -342,7 +342,7 @@ public class ClientServerReplicationIT
     {
         DirectoryService provDirService = DSAnnotationProcessor.getDirectoryService();
 
-        providerServer = ServerAnnotationProcessor.createLdapServer( provDirService );
+        providerServer = ServerAnnotationProcessor.getLdapServer( provDirService );
         providerServer.setReplicationReqHandler( new SyncReplRequestHandler() );
         providerServer.startReplicationProducer();
         
@@ -402,7 +402,7 @@ public class ClientServerReplicationIT
     public static void startConsumer() throws Exception
     {
         DirectoryService provDirService = DSAnnotationProcessor.getDirectoryService();
-        consumerServer = ServerAnnotationProcessor.createLdapServer( provDirService );
+        consumerServer = ServerAnnotationProcessor.getLdapServer( provDirService );
         
         final SyncReplConsumer consumer = (SyncReplConsumer)ServerAnnotationProcessor.createConsumer();
         



Mime
View raw message