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/01/01 16:40:44 UTC

svn commit: r895027 - in /directory/apacheds/branches/apacheds-schema/server-annotations: ./ src/main/java/org/apache/directory/server/annotations/ src/main/java/org/apache/directory/server/factory/ src/test/java/org/apache/directory/server/factory/

Author: kayyagari
Date: Fri Jan  1 15:40:43 2010
New Revision: 895027

URL: http://svn.apache.org/viewvc?rev=895027&view=rev
Log:
o added support for creating KdcServer using annotation
o added dependency on apacheds-protocol-kerberos
o added a test class for testing CreateKdcServer annotation

Added:
    directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateKdcServer.java
    directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/CreateKdcServerAnnotationTest.java
Modified:
    directory/apacheds/branches/apacheds-schema/server-annotations/pom.xml
    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/pom.xml
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/server-annotations/pom.xml?rev=895027&r1=895026&r2=895027&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-annotations/pom.xml (original)
+++ directory/apacheds/branches/apacheds-schema/server-annotations/pom.xml Fri Jan  1 15:40:43 2010
@@ -58,5 +58,11 @@
       <artifactId>apacheds-protocol-ldap</artifactId>
       <version>${pom.version}</version>
     </dependency>  
+
+    <dependency>
+      <groupId>org.apache.directory.server</groupId>
+      <artifactId>apacheds-protocol-kerberos</artifactId>
+      <version>${pom.version}</version>
+    </dependency>  
   </dependencies>
 </project>

Added: directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateKdcServer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateKdcServer.java?rev=895027&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateKdcServer.java (added)
+++ directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateKdcServer.java Fri Jan  1 15:40:43 2010
@@ -0,0 +1,65 @@
+/*
+ *  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.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * A annotation used to define a LdapServer configuration. Many elements can be configured :
+ * <ul>
+ * <li> The server ID (or name)</li>
+ * <li>primary realm</li>
+ * <li>service principal</li>
+ * <li>maximum ticket lifetime</li>
+ * <li>maximum renewable lifetime</li>
+ * </ul>
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+@Documented
+@Inherited
+@Retention ( RetentionPolicy.RUNTIME )
+@Target ( { ElementType.METHOD, ElementType.TYPE } )
+public @interface CreateKdcServer
+{
+    /** The instance name */
+    String name() default "DefaultKrbServer";
+    
+    /** The transports to use, default to LDAP */
+    CreateTransport[] transports() default {};
+    
+    /** The default kdc realm */
+    String primaryRealm() default "EXAMPLE.COM";
+    
+    /** The default kdc service principal */
+    String kdcPrincipal() default "krbtgt/EXAMPLE.COM@EXAMPLE.COM";
+    
+    /** The maximum ticket lifetime. */
+    long maxTicketLifetime() default 60000 * 1440;
+    
+    /** The maximum renewable lifetime. */
+    long maxRenewableLifetime() default 60000 * 10080;
+}
\ No newline at end of file

Modified: 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=895027&r1=895026&r2=895027&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java (original)
+++ directory/apacheds/branches/apacheds-schema/server-annotations/src/main/java/org/apache/directory/server/factory/ServerAnnotationProcessor.java Fri Jan  1 15:40:43 2010
@@ -18,20 +18,24 @@
  */
 package org.apache.directory.server.factory;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 
+import org.apache.directory.server.annotations.CreateKdcServer;
 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.kerberos.kdc.KdcServer;
 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.directory.server.protocol.shared.transport.UdpTransport;
 import org.apache.mina.util.AvailablePortFinder;
 import org.junit.runner.Description;
 
 /**
  * 
- * TODO ServerAnnotationProcessor.
+ * Annotation processor for creating LDAP and Kerberos servers.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
@@ -69,6 +73,10 @@
                     ldaps.setEnableSSL( true );
                     ldapServer.addTransports( ldaps );
                 }
+                else
+                {
+                    throw new IllegalArgumentException( "Unsupported protocol type " + protocol );
+                }
             }
         }
         else
@@ -121,17 +129,33 @@
     
     public static LdapServer getLdapServer( DirectoryService directoryService, int startPort ) throws Exception
     {
-        CreateLdapServer createLdapServer = null;
+        CreateLdapServer createLdapServer = ( CreateLdapServer ) getAnnotation( CreateLdapServer.class );
         
+        // Ok, we have found a CreateLdapServer annotation. Process it now.
+        return createLdapServer( createLdapServer, directoryService, startPort );
+    }
+
+
+    public static LdapServer getLdapServer( Description description, DirectoryService directoryService, int startPort ) throws Exception
+    {
+        CreateLdapServer createLdapServer = description.getAnnotation( CreateLdapServer.class );
+
+        // Ok, we have found a CreateLdapServer annotation. Process it now.
+        return createLdapServer( createLdapServer, directoryService, startPort );
+    }
+
+    @SuppressWarnings("unchecked")
+    private static Annotation getAnnotation( Class annotationClass ) throws Exception
+    {
         // Get the caller by inspecting the stackTrace
         StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
         
         // Get the enclosing class
-        Class<?> classCaller = Class.forName( stackTrace[2].getClassName() );
-        
+        Class<?> classCaller = Class.forName( stackTrace[3].getClassName() );
+
         // Get the current method
-        String methodCaller = stackTrace[2].getMethodName();
-        
+        String methodCaller = stackTrace[3].getMethodName();
+
         // Check if we have any annotation associated with the method
         Method[] methods = classCaller.getMethods();
         
@@ -139,31 +163,104 @@
         {
             if ( methodCaller.equals( method.getName() ) )
             {
-                createLdapServer = method.getAnnotation( CreateLdapServer.class );
+                Annotation annotation = method.getAnnotation( annotationClass );
                 
-                if ( createLdapServer != null )
+                if ( annotation != null )
                 {
-                    break;
+                    return annotation;
                 }
             }
         }
-
+        
         // No : look at the class level
-        if ( createLdapServer == null )
+        return classCaller.getAnnotation( annotationClass );
+    }
+    
+    
+    public static KdcServer getKdcServer( DirectoryService directoryService, int startPort ) throws Exception
+    {
+        CreateKdcServer createKdcServer = ( CreateKdcServer ) getAnnotation( CreateKdcServer.class );
+
+        return createKdcServer( createKdcServer, directoryService, startPort );
+    }
+
+    
+    private static KdcServer createKdcServer( CreateKdcServer createKdcServer, DirectoryService directoryService, int startPort )
+    {
+        if( createKdcServer == null )
         {
-            createLdapServer = classCaller.getAnnotation( CreateLdapServer.class );
+            return null;
         }
         
-        // Ok, we have found a CreateLdapServer annotation. Process it now.
-        return createLdapServer( createLdapServer, directoryService, startPort );
-    }
-
+        KdcServer kdcServer = new KdcServer();
+        kdcServer.setServiceName( createKdcServer.name() );
+        kdcServer.setKdcPrincipal( createKdcServer.kdcPrincipal() );
+        kdcServer.setPrimaryRealm( createKdcServer.primaryRealm() );
+        kdcServer.setMaximumTicketLifetime( createKdcServer.maxTicketLifetime() );
+        kdcServer.setMaximumRenewableLifetime( createKdcServer.maxRenewableLifetime() );
+        
+        CreateTransport[] transportBuilders = createKdcServer.transports();
+        
+        if( transportBuilders == null )
+        {
+            // create only UDP transport if none specified
+            UdpTransport defaultTransport = new UdpTransport( AvailablePortFinder.getNextAvailable( startPort ) );
+            kdcServer.addTransports( defaultTransport );
+        }
+        else if( transportBuilders.length > 0 )
+        {
+            for( CreateTransport transportBuilder : transportBuilders )
+            {
+                String protocol = transportBuilder.protocol();
+                int port = transportBuilder.port();
+                int nbThreads = transportBuilder.nbThreads();
+                int backlog = transportBuilder.backlog();
+                String address = transportBuilder.address();
 
-    public static LdapServer getLdapServer( Description description, DirectoryService directoryService, int startPort ) throws Exception
+                if ( port == -1 )
+                {
+                    port = AvailablePortFinder.getNextAvailable( startPort );
+                    startPort = port + 1;
+                }
+                
+                if ( protocol.equalsIgnoreCase( "TCP" ) )
+                {
+                    Transport tcp = new TcpTransport( address, port, nbThreads, backlog );
+                    kdcServer.addTransports( tcp );
+                }
+                else if ( protocol.equalsIgnoreCase( "UDP" ) )
+                {
+                    UdpTransport udp = new UdpTransport( address, port );
+                    kdcServer.addTransports( udp );
+                }
+                else
+                {
+                    throw new IllegalArgumentException( "Unsupported protocol type " + protocol );
+                }
+            }
+        }
+        
+        kdcServer.setDirectoryService( directoryService );
+        
+        // Launch the server
+        try
+        {
+            kdcServer.start();
+        }
+        catch ( Exception e )
+        {
+            e.printStackTrace();
+        }
+        
+        return kdcServer;
+    }
+    
+    
+    public static KdcServer getKdcServer( Description description, DirectoryService directoryService, int startPort ) throws Exception
     {
-        CreateLdapServer createLdapServer = description.getAnnotation( CreateLdapServer.class );
+        CreateKdcServer createLdapServer = description.getAnnotation( CreateKdcServer.class );
 
-        // Ok, we have found a CreateLdapServer annotation. Process it now.
-        return createLdapServer( createLdapServer, directoryService, startPort );
+        return createKdcServer( createLdapServer, directoryService, startPort );
     }
+
 }

Added: directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/CreateKdcServerAnnotationTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/CreateKdcServerAnnotationTest.java?rev=895027&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/CreateKdcServerAnnotationTest.java (added)
+++ directory/apacheds/branches/apacheds-schema/server-annotations/src/test/java/org/apache/directory/server/factory/CreateKdcServerAnnotationTest.java Fri Jan  1 15:40:43 2010
@@ -0,0 +1,69 @@
+/*
+ *   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 static org.junit.Assert.*;
+
+import org.apache.directory.server.annotations.CreateKdcServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.annotations.TransportType;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.factory.DSAnnotationProcessor;
+import org.apache.directory.server.kerberos.kdc.KdcServer;
+import org.junit.Test;
+
+/**
+ * TODO CreateKdcServerAnnotationTest.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+@CreateDS(name = "CreateKdcServerAnnotationTest-class")
+@CreateKdcServer(primaryRealm = "apache.org",
+                 kdcPrincipal = "krbtgt/apache.org@apache.org",
+                 maxTicketLifetime = 1000,
+                 maxRenewableLifetime = 2000,
+                 transports = 
+                     { @CreateTransport(protocol = "TCP"),
+                       @CreateTransport(protocol = "UDP")
+                     })
+public class CreateKdcServerAnnotationTest
+{
+    @Test
+    public void testCreateKdcServer() throws Exception
+    {
+        DirectoryService directoryService = DSAnnotationProcessor.getDirectoryService();
+        
+        assertEquals( "CreateKdcServerAnnotationTest-class", directoryService.getInstanceId() );
+        
+        KdcServer server = ServerAnnotationProcessor.getKdcServer( directoryService, 1024 );
+
+        assertEquals( 2, server.getTransports().length );
+        
+        assertEquals( directoryService, server.getDirectoryService() );
+        assertEquals( "apache.org", server.getPrimaryRealm() );
+        assertEquals( "krbtgt/apache.org@apache.org", server.getServicePrincipal().getName() );
+        assertEquals( 1000, server.getMaximumTicketLifetime() );
+        assertEquals( 2000, server.getMaximumRenewableLifetime() );
+    }
+}