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 2014/01/06 16:11:15 UTC

svn commit: r1555852 - in /directory/shared/trunk/ldap: client/api/src/main/java/org/apache/directory/ldap/client/api/ codec/standalone/src/main/java/org/apache/directory/api/ldap/codec/standalone/ net/mina/src/main/java/org/apache/directory/api/ldap/c...

Author: kayyagari
Date: Mon Jan  6 15:11:15 2014
New Revision: 1555852

URL: http://svn.apache.org/r1555852
Log:
allow standalone codec service to be configured without depending on system properties (DIRAPI-170)
committed patch submitted by Lucas Theisen

Modified:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/AbstractLdapConnection.java
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
    directory/shared/trunk/ldap/codec/standalone/src/main/java/org/apache/directory/api/ldap/codec/standalone/StandaloneLdapApiService.java
    directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolCodecFactory.java
    directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolEncoder.java

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/AbstractLdapConnection.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/AbstractLdapConnection.java?rev=1555852&r1=1555851&r2=1555852&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/AbstractLdapConnection.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/AbstractLdapConnection.java Mon Jan  6 15:11:15 2014
@@ -58,7 +58,7 @@ public abstract class AbstractLdapConnec
     protected AtomicInteger messageId;
 
     /** the ldap codec service */
-    protected LdapApiService codec = LdapApiServiceFactory.getSingleton();
+    protected LdapApiService codec;
 
 
     /**
@@ -66,7 +66,13 @@ public abstract class AbstractLdapConnec
      */
     protected AbstractLdapConnection()
     {
+        this( LdapApiServiceFactory.getSingleton() );
+    }
+
+    protected AbstractLdapConnection( LdapApiService codec )
+    {
         messageId = new AtomicInteger( 0 );
+        this.codec = codec;
     }
 
 

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java?rev=1555852&r1=1555851&r2=1555852&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java (original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapNetworkConnection.java Mon Jan  6 15:11:15 2014
@@ -199,9 +199,6 @@ public class LdapNetworkConnection exten
      */
     private IoSession ldapSession;
 
-    /** The LDAP Api Service instance */
-    LdapApiService ldapApiService;
-
     /** a map to hold the ResponseFutures for all operations */
     private Map<Integer, ResponseFuture<? extends Response>> futureMap = new ConcurrentHashMap<Integer, ResponseFuture<? extends Response>>();
 
@@ -357,26 +354,20 @@ public class LdapNetworkConnection exten
      */
     public LdapNetworkConnection( LdapConnectionConfig config )
     {
-        super();
+        this( config, LdapApiServiceFactory.getSingleton() );
+    }
+
+    public LdapNetworkConnection( LdapConnectionConfig config, LdapApiService ldapApiService )
+    {
+        super( ldapApiService );
         this.config = config;
 
         if ( config.getBinaryAttributeDetector() == null )
         {
             config.setBinaryAttributeDetector( new DefaultConfigurableBinaryAttributeDetector() );
         }
-
-        // Load the LdapApiService now
-        try
-        {
-            ldapApiService = LdapApiServiceFactory.getSingleton();
-        }
-        catch ( Exception e )
-        {
-            e.printStackTrace();
-        }
     }
 
-
     /**
      * Create a new instance of a LdapConnection on localhost,
      * port 389 if the SSL flag is off, or 636 otherwise.
@@ -389,6 +380,12 @@ public class LdapNetworkConnection exten
     }
 
 
+    public LdapNetworkConnection( boolean useSsl, LdapApiService ldapApiService )
+    {
+        this( null, -1, useSsl, ldapApiService );
+    }
+
+
     /**
      * Create a new instance of a LdapConnection on a given
      * server, using the default port (389).
@@ -402,6 +399,12 @@ public class LdapNetworkConnection exten
     }
 
 
+    public LdapNetworkConnection( String server, LdapApiService ldapApiService )
+    {
+        this( server, -1, false, ldapApiService );
+    }
+
+
     /**
      * Create a new instance of a LdapConnection on a given
      * server, using the default port (389) if the SSL flag
@@ -417,6 +420,12 @@ public class LdapNetworkConnection exten
     }
 
 
+    public LdapNetworkConnection( String server, boolean useSsl, LdapApiService ldapApiService )
+    {
+        this( server, -1, useSsl, ldapApiService );
+    }
+
+
     /**
      * Create a new instance of a LdapConnection on a
      * given server and a given port. We don't use ssl.
@@ -430,6 +439,12 @@ public class LdapNetworkConnection exten
     }
 
 
+    public LdapNetworkConnection( String server, int port, LdapApiService ldapApiService )
+    {
+        this( server, port, false, ldapApiService );
+    }
+
+
     /**
      * Create a new instance of a LdapConnection on a given
      * server, and a give port. We set the SSL flag accordingly
@@ -442,8 +457,19 @@ public class LdapNetworkConnection exten
      */
     public LdapNetworkConnection( String server, int port, boolean useSsl )
     {
-        super();
-        config = new LdapConnectionConfig();
+        this( buildConfig( server, port, useSsl ) );
+    }
+    
+    
+    public LdapNetworkConnection( String server, int port, boolean useSsl, LdapApiService ldapApiService )
+    {
+        this( buildConfig( server, port, useSsl ), ldapApiService );
+    }
+
+    
+    private static LdapConnectionConfig buildConfig( String server, int port, boolean useSsl )
+    {
+        LdapConnectionConfig config = new LdapConnectionConfig();
         config.setUseSsl( useSsl );
 
         if ( port != -1 )
@@ -473,16 +499,8 @@ public class LdapNetworkConnection exten
         }
 
         config.setBinaryAttributeDetector( new SchemaBinaryAttributeDetector( null ) );
-
-        // Load the LdapApiService now
-        try
-        {
-            ldapApiService = LdapApiServiceFactory.getSingleton();
-        }
-        catch ( Exception e )
-        {
-            e.printStackTrace();
-        }
+        
+        return config;
     }
 
 
@@ -3119,7 +3137,7 @@ public class LdapNetworkConnection exten
             }
 
             // Decode the payload now
-            ExtendedResponseDecorator<?> decoratedResponse = ldapApiService.decorate( response );
+            ExtendedResponseDecorator<?> decoratedResponse = codec.decorate( response );
 
             return decoratedResponse;
         }

Modified: directory/shared/trunk/ldap/codec/standalone/src/main/java/org/apache/directory/api/ldap/codec/standalone/StandaloneLdapApiService.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/codec/standalone/src/main/java/org/apache/directory/api/ldap/codec/standalone/StandaloneLdapApiService.java?rev=1555852&r1=1555851&r2=1555852&view=diff
==============================================================================
--- directory/shared/trunk/ldap/codec/standalone/src/main/java/org/apache/directory/api/ldap/codec/standalone/StandaloneLdapApiService.java (original)
+++ directory/shared/trunk/ldap/codec/standalone/src/main/java/org/apache/directory/api/ldap/codec/standalone/StandaloneLdapApiService.java Mon Jan  6 15:11:15 2014
@@ -29,9 +29,11 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+
 import javax.naming.NamingException;
 import javax.naming.ldap.BasicControl;
 
+
 import org.apache.directory.api.asn1.DecoderException;
 import org.apache.directory.api.asn1.EncoderException;
 import org.apache.directory.api.asn1.ber.Asn1Container;
@@ -102,8 +104,10 @@ public class StandaloneLdapApiService im
 
     /**
      * Creates a new instance of StandaloneLdapCodecService. Optionally checks for
-     * system property {@link #PLUGIN_DIRECTORY_PROPERTY}. Intended for use by 
-     * unit test running tools like Maven's surefire:
+     * system property {@link #PLUGIN_DIRECTORY_PROPERTY}. 
+     * <br /><br />
+     * The following pom configuration is intended for use by unit test running 
+     * tools like Maven's surefire:
      * <pre>
      *   &lt;properties&gt;
      *     &lt;codec.plugin.directory&gt;${project.build.directory}/pluginDirectory&lt;/codec.plugin.directory&gt;
@@ -165,11 +169,16 @@ public class StandaloneLdapApiService im
      */
     public StandaloneLdapApiService() throws Exception
     {
-        // Load the controls
-        loadControls();
+        this( getControlsFromSystemProperties(), getExtendedOperationsFromSystemProperties() );
+    }
 
+    public StandaloneLdapApiService( List<String> controls, List<String> extendedOperations ) throws Exception
+    {
+        // Load the controls
+        loadControls( controls );
+            
         // Load the extended operations
-        loadExtendedOperations();
+        loadExtendedOperations( extendedOperations );
 
         if ( protocolCodecFactory == null )
         {
@@ -178,7 +187,16 @@ public class StandaloneLdapApiService im
                 @SuppressWarnings("unchecked")
                 Class<? extends ProtocolCodecFactory> clazz = ( Class<? extends ProtocolCodecFactory> )
                     Class.forName( DEFAULT_PROTOCOL_CODEC_FACTORY );
-                protocolCodecFactory = clazz.newInstance();
+                Constructor<? extends ProtocolCodecFactory> constructor =
+                        clazz.getConstructor( LdapApiService.class );
+                if ( constructor != null ) 
+                {
+                    protocolCodecFactory = constructor.newInstance( this );
+                }
+                else 
+                {
+                    protocolCodecFactory = clazz.newInstance();
+                }
             }
             catch ( Exception cause )
             {
@@ -189,11 +207,11 @@ public class StandaloneLdapApiService im
 
 
     /**
-     * Load the controls
+     * Parses the system properties to obtain the controls list.
      * 
      * @throws Exception
      */
-    private void loadControls() throws Exception
+    private static List<String> getControlsFromSystemProperties() throws Exception
     {
         List<String> controlsList = new ArrayList<String>();
 
@@ -231,46 +249,16 @@ public class StandaloneLdapApiService im
                 }
             }
         }
-
-        // Adding all controls
-        if ( controlsList.size() > 0 )
-        {
-            for ( String control : controlsList )
-            {
-                loadControl( control );
-            }
-        }
+        
+        return controlsList;
     }
 
-
     /**
-     * Loads a control from its FQCN.
-     *
-     * @param control the control FQCN
-     * @throws Exception
-     */
-    private void loadControl( String control ) throws Exception
-    {
-        Class<?>[] types = new Class<?>[]
-            { LdapApiService.class };
-        @SuppressWarnings("unchecked")
-        Class<? extends ControlFactory<?, ?>> clazz = ( Class<? extends ControlFactory<?, ?>> ) Class
-            .forName( control );
-        Constructor<?> constructor = clazz.getConstructor( types );
-
-        ControlFactory<?, ?> factory = ( ControlFactory<?, ?> ) constructor.newInstance( new Object[]
-            { this } );
-        controlFactories.put( factory.getOid(), factory );
-        LOG.info( "Registered control factory: {}", factory.getOid() );
-    }
-
-
-    /**
-     * Load the extended operations
+     * Parses the system properties to obtain the extended operations
      * 
      * @throws Exception
      */
-    private void loadExtendedOperations() throws Exception
+    private static List<String> getExtendedOperationsFromSystemProperties() throws Exception
     {
         List<String> extendedOperationsList = new ArrayList<String>();
 
@@ -321,7 +309,49 @@ public class StandaloneLdapApiService im
                 }
             }
         }
+        
+        return extendedOperationsList;
+    }
 
+    private void loadControls( List<String> controlsList ) throws Exception 
+    {
+        // Adding all controls
+        if ( controlsList.size() > 0 )
+        {
+            for ( String control : controlsList )
+            {
+                loadControl( control );
+            }
+        }
+    }
+
+
+    /**
+     * Loads a control from its FQCN.
+     *
+     * @param control the control FQCN
+     * @throws Exception
+     */
+    private void loadControl( String control ) throws Exception
+    {
+        Class<?>[] types = new Class<?>[]
+            { LdapApiService.class };
+        // note, trimming whitespace doesnt hurt as it is a class name and
+        // helps DI containers that use xml config as xml ignores whitespace
+        @SuppressWarnings("unchecked")
+        Class<? extends ControlFactory<?, ?>> clazz = ( Class<? extends ControlFactory<?, ?>> ) Class
+            .forName( control.trim() );
+        Constructor<?> constructor = clazz.getConstructor( types );
+
+        ControlFactory<?, ?> factory = ( ControlFactory<?, ?> ) constructor.newInstance( new Object[]
+            { this } );
+        controlFactories.put( factory.getOid(), factory );
+        LOG.info( "Registered control factory: {}", factory.getOid() );
+    }
+
+    
+    private void loadExtendedOperations( List<String> extendedOperationsList ) throws Exception
+    {
         // Adding all extended operations
         if ( extendedOperationsList.size() > 0 )
         {
@@ -337,9 +367,11 @@ public class StandaloneLdapApiService im
     {
         Class<?>[] types = new Class<?>[]
             { LdapApiService.class };
+        // note, trimming whitespace doesnt hurt as it is a class name and
+        // helps DI containers that use xml config as xml ignores whitespace
         @SuppressWarnings("unchecked")
         Class<? extends ExtendedOperationFactory<?, ?>> clazz = ( Class<? extends ExtendedOperationFactory<?, ?>> ) Class
-            .forName( extendedOperation );
+            .forName( extendedOperation.trim() );
         Constructor<?> constructor = clazz.getConstructor( types );
 
         @SuppressWarnings("unchecked")

Modified: directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolCodecFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolCodecFactory.java?rev=1555852&r1=1555851&r2=1555852&view=diff
==============================================================================
--- directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolCodecFactory.java (original)
+++ directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolCodecFactory.java Mon Jan  6 15:11:15 2014
@@ -20,6 +20,8 @@
 package org.apache.directory.api.ldap.codec.protocol.mina;
 
 
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
 import org.apache.mina.core.session.IoSession;
 import org.apache.mina.filter.codec.ProtocolCodecFactory;
 import org.apache.mina.filter.codec.ProtocolDecoder;
@@ -42,6 +44,17 @@ public class LdapProtocolCodecFactory im
     /** The LdapEncoder key */
     public static final String LDAP_ENCODER = "LDAP_ENCODER";
 
+    private LdapApiService ldapApiService;
+    
+    public LdapProtocolCodecFactory() 
+    {
+        this( LdapApiServiceFactory.getSingleton() );
+    }
+
+    public LdapProtocolCodecFactory( LdapApiService ldapApiService ) 
+    {
+        this.ldapApiService = ldapApiService;
+    }
 
     /**
      * Get the LDAP decoder.
@@ -63,6 +76,6 @@ public class LdapProtocolCodecFactory im
      */
     public ProtocolEncoder getEncoder( IoSession session )
     {
-        return new LdapProtocolEncoder();
+        return new LdapProtocolEncoder( ldapApiService );
     }
 }

Modified: directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolEncoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolEncoder.java?rev=1555852&r1=1555851&r2=1555852&view=diff
==============================================================================
--- directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolEncoder.java (original)
+++ directory/shared/trunk/ldap/net/mina/src/main/java/org/apache/directory/api/ldap/codec/protocol/mina/LdapProtocolEncoder.java Mon Jan  6 15:11:15 2014
@@ -22,6 +22,8 @@ package org.apache.directory.api.ldap.co
 
 import java.nio.ByteBuffer;
 
+
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
 import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
 import org.apache.directory.api.ldap.codec.api.LdapEncoder;
 import org.apache.directory.api.ldap.model.constants.Loggers;
@@ -59,7 +61,12 @@ public class LdapProtocolEncoder impleme
      */
     public LdapProtocolEncoder()
     {
-        this.encoder = new LdapEncoder( LdapApiServiceFactory.getSingleton() );
+        this( LdapApiServiceFactory.getSingleton() );
+    }
+
+    public LdapProtocolEncoder( LdapApiService ldapApiService )
+    {
+        this.encoder = new LdapEncoder( ldapApiService );
     }