directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ersi...@apache.org
Subject svn commit: r570883 - in /directory: apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/ apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/ shared/trunk/ldap/src/main/java/org/apache/director...
Date Wed, 29 Aug 2007 18:12:33 GMT
Author: ersiner
Date: Wed Aug 29 11:12:32 2007
New Revision: 570883

URL: http://svn.apache.org/viewvc?rev=570883&view=rev
Log:
Refactored external stored procedure execution framework to use new schema elements and corresponding
codes.


Removed:
    directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/JavaStoredProcedureExtendedOperationHandler.java
    directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/LanguageSpecificStoredProceureExtendedOperationHandler.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcedureUtils.java
Modified:
    directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/StoredProcedureExtendedOperationHandler.java
    directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/BaseJavaStoredProcUtils.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/LdapContextParameter.java

Modified: directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/StoredProcedureExtendedOperationHandler.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/StoredProcedureExtendedOperationHandler.java?rev=570883&r1=570882&r2=570883&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/StoredProcedureExtendedOperationHandler.java
(original)
+++ directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/support/extended/StoredProcedureExtendedOperationHandler.java
Wed Aug 29 11:12:32 2007
@@ -22,15 +22,25 @@
 package org.apache.directory.server.ldap.support.extended;
 
 
+import java.io.Serializable;
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
+import javax.naming.directory.Attributes;
 import javax.naming.ldap.Control;
 import javax.naming.ldap.LdapContext;
 
+import org.apache.commons.lang.SerializationUtils;
 import org.apache.directory.server.core.jndi.ServerLdapContext;
+import org.apache.directory.server.core.sp.StoredProcEngine;
+import org.apache.directory.server.core.sp.StoredProcEngineConfig;
+import org.apache.directory.server.core.sp.StoredProcExecutionManager;
+import org.apache.directory.server.core.sp.java.JavaStoredProcEngineConfig;
 import org.apache.directory.server.ldap.ExtendedOperationHandler;
 import org.apache.directory.server.ldap.LdapProtocolProvider;
 import org.apache.directory.server.ldap.SessionRegistry;
@@ -39,10 +49,13 @@
 import org.apache.directory.shared.ldap.codec.extended.operations.StoredProcedure;
 import org.apache.directory.shared.ldap.codec.extended.operations.StoredProcedureContainer;
 import org.apache.directory.shared.ldap.codec.extended.operations.StoredProcedureDecoder;
+import org.apache.directory.shared.ldap.codec.extended.operations.StoredProcedure.StoredProcedureParameter;
 import org.apache.directory.shared.ldap.message.ExtendedRequest;
 import org.apache.directory.shared.ldap.message.ExtendedResponse;
 import org.apache.directory.shared.ldap.message.extended.StoredProcedureRequest;
 import org.apache.directory.shared.ldap.message.extended.StoredProcedureResponse;
+import org.apache.directory.shared.ldap.sp.LdapContextParameter;
+import org.apache.directory.shared.ldap.util.StringTools;
 import org.apache.mina.common.IoSession;
 
 
@@ -52,42 +65,61 @@
  */
 public class StoredProcedureExtendedOperationHandler implements ExtendedOperationHandler
 {
+    private StoredProcExecutionManager manager;
+    private static final Object[] EMPTY_CLASS_ARRAY = new Object[0];
+    public StoredProcedureExtendedOperationHandler()
+    {
+        super();
+        //StoredProcEngineConfig javaxScriptSPEngineConfig = new JavaxStoredProcEngineConfig();
+        StoredProcEngineConfig javaSPEngineConfig = new JavaStoredProcEngineConfig();
+        List<StoredProcEngineConfig> spEngineConfigs = new ArrayList<StoredProcEngineConfig>();
+        //spEngineConfigs.add( javaxScriptSPEngineConfig );
+        spEngineConfigs.add( javaSPEngineConfig );
+        String spContainer = "ou=Stored Procedures,ou=system";
+        this.manager = new StoredProcExecutionManager( spContainer, spEngineConfigs );
+    }
+
     public void handleExtendedOperation( IoSession session, SessionRegistry registry, ExtendedRequest
req ) throws Exception
     {
         Control[] connCtls = req.getControls().values().toArray( new Control[ req.getControls().size()
] );
         LdapContext ldapContext = registry.getLdapContext( session, connCtls, false);
-        ServerLdapContext serverLdapContext;
+        ServerLdapContext ctx;
         
         if ( ldapContext instanceof ServerLdapContext )
         {
-            serverLdapContext = ( ServerLdapContext ) ldapContext;
+            ctx = ( ServerLdapContext ) ldapContext;
         }
         else
         {
-            serverLdapContext = ( ServerLdapContext ) ldapContext.lookup( "" );
+            ctx = ( ServerLdapContext ) ldapContext.lookup( "" );
         }
         
         StoredProcedure spBean = decodeBean( req.getPayload() );
         
-        LanguageSpecificStoredProceureExtendedOperationHandler handler = null;
-        
-        byte[] responseStream = null;
-        
-        /**
-         * TODO This part may be replaced by a better handler determiner.
-         */
-        if ( spBean.getLanguage().equalsIgnoreCase( "Java" ) )
+        String procedure = StringTools.utf8ToString( spBean.getProcedure() );
+        Attributes spUnit = manager.findStoredProcUnit( ctx, procedure );
+        StoredProcEngine engine = manager.getStoredProcEngineInstance( spUnit );
+        
+        List valueList = new ArrayList( spBean.getParameters().size() );
+        Iterator<StoredProcedureParameter> it = spBean.getParameters().iterator();
+        while ( it.hasNext() )
         {
-            handler = new JavaStoredProcedureExtendedOperationHandler();
-            responseStream = handler.handleStoredProcedureExtendedOperation( serverLdapContext,
spBean );
+            StoredProcedureParameter pPojo = it.next();
+            byte[] serializedValue = pPojo.getValue();
+            Object value = SerializationUtils.deserialize( serializedValue );
+            if ( value.getClass().equals( LdapContextParameter.class ) )
+            {
+                String paramCtx = ( ( LdapContextParameter ) value ).getValue();
+                value = ctx.lookup( paramCtx );
+            }
+            valueList.add( value );
         }
+        Object[] values = valueList.toArray( EMPTY_CLASS_ARRAY );
         
-        /**
-         * FIXME: We may have issues sending SP result back to the client.
-         */
-        
-        ( ( ExtendedResponse )( req.getResultResponse() ) ).setResponse( responseStream );
+        Object response = engine.invokeProcedure( ctx, procedure, values );
         
+        byte[] serializedResponse = SerializationUtils.serialize( ( Serializable ) response
);
+        ( ( ExtendedResponse )( req.getResultResponse() ) ).setResponse( serializedResponse
);
         session.write( req.getResultResponse() );
         
     }

Modified: directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java?rev=570883&r1=570882&r2=570883&view=diff
==============================================================================
--- directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java
(original)
+++ directory/apacheds/trunk/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java
Wed Aug 29 11:12:32 2007
@@ -27,6 +27,7 @@
 
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
@@ -34,8 +35,9 @@
 import org.apache.directory.server.ldap.LdapConfiguration;
 import org.apache.directory.server.ldap.support.extended.StoredProcedureExtendedOperationHandler;
 import org.apache.directory.server.unit.AbstractServerTest;
+import org.apache.directory.shared.ldap.message.AttributesImpl;
 import org.apache.directory.shared.ldap.name.LdapDN;
-import org.apache.directory.shared.ldap.sp.JavaStoredProcedureUtils;
+import org.apache.directory.shared.ldap.sp.BaseJavaStoredProcUtils;
 import org.apache.directory.shared.ldap.sp.LdapContextParameter;
 
 
@@ -46,6 +48,7 @@
 public class StoredProcedureTest extends AbstractServerTest
 {
     private LdapContext ctx = null;
+    private LdapContext spCtx = null;
 
     
     public void setUp() throws Exception
@@ -67,6 +70,11 @@
         env.put( "java.naming.security.credentials", "secret" );
         env.put( "java.naming.security.authentication", "simple" );
         ctx = new InitialLdapContext( env, null );
+        
+        Attributes spContainer = new AttributesImpl( "objectClass", "top", true );
+        spContainer.get( "objectClass" ).add( "organizationalUnit" );
+        spContainer.put( "ou", "Stored Procedures" );
+        spCtx = ( LdapContext ) ctx.createSubcontext( "ou=Stored Procedures", spContainer
);
     }
 
 
@@ -81,11 +89,11 @@
 
     public void testExecuteProcedureWithReturnValue() throws NamingException
     {
-        String procedureName = HelloWorldProcedure.class.getName() + ".sayHello";
+        String procedureName = HelloWorldProcedure.class.getName() + ":sayHello";
         
-        JavaStoredProcedureUtils.loadStoredProcedureClass( ctx, HelloWorldProcedure.class
);
+        BaseJavaStoredProcUtils.loadStoredProcedureClass( spCtx, HelloWorldProcedure.class
);
         
-        Object response = JavaStoredProcedureUtils.callStoredProcedure( ctx, procedureName,
new Object[] { } );
+        Object response = BaseJavaStoredProcUtils.callStoredProcedure( ctx, procedureName,
new Object[] { } );
         
         assertEquals( "Hello World!", response );
     }
@@ -93,11 +101,11 @@
 
     public void testExecuteProcedureWithParametersAndReturnValue() throws NamingException,
IOException
     {
-        String procedureName = HelloWorldProcedure.class.getName() + ".sayHelloTo";
+        String procedureName = HelloWorldProcedure.class.getName() + ":sayHelloTo";
         
-        JavaStoredProcedureUtils.loadStoredProcedureClass( ctx, HelloWorldProcedure.class
);
+        BaseJavaStoredProcUtils.loadStoredProcedureClass( spCtx, HelloWorldProcedure.class
);
         
-        Object response = JavaStoredProcedureUtils.callStoredProcedure( ctx, procedureName,
new Object[] { "Ersin" } );
+        Object response = BaseJavaStoredProcUtils.callStoredProcedure( ctx, procedureName,
new Object[] { "Ersin" } );
         
         assertEquals( "Hello Ersin!", response );
     }
@@ -127,13 +135,13 @@
         
         injectEntries( ldif );
         
-        JavaStoredProcedureUtils.loadStoredProcedureClass( ctx, DITUtilitiesSP.class );
+        BaseJavaStoredProcUtils.loadStoredProcedureClass( spCtx, DITUtilitiesSP.class );
         
-        String spName = DITUtilitiesSP.class.getName() + ".deleteSubtree";
+        String spName = DITUtilitiesSP.class.getName() + ":deleteSubtree";
         Object[] params = new Object[] { new LdapContextParameter( "ou=system" ),
                                          new LdapDN( "ou=People" ) };
         
-        JavaStoredProcedureUtils.callStoredProcedure( ctx, spName, params );
+        BaseJavaStoredProcUtils.callStoredProcedure( ctx, spName, params );
         
         try
         {

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/BaseJavaStoredProcUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/BaseJavaStoredProcUtils.java?rev=570883&r1=570882&r2=570883&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/BaseJavaStoredProcUtils.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/BaseJavaStoredProcUtils.java
Wed Aug 29 11:12:32 2007
@@ -24,14 +24,18 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Serializable;
 import java.net.URL;
 
 import javax.naming.NamingException;
 import javax.naming.directory.Attributes;
 import javax.naming.ldap.LdapContext;
 
+import org.apache.commons.lang.SerializationUtils;
 import org.apache.directory.shared.ldap.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.message.AttributesImpl;
+import org.apache.directory.shared.ldap.message.extended.StoredProcedureRequest;
+import org.apache.directory.shared.ldap.message.extended.StoredProcedureResponse;
 
 /**
  * A utility class for working with Java Stored Procedures at the base level.
@@ -101,6 +105,54 @@
         attributes.put( "javaByteCode", buf );
         
         ctx.createSubcontext( "storedProcUnitName=" + fullClassName , attributes );
+    }
+    
+    public static Object callStoredProcedure( LdapContext ctx, String procedureName, Object[]
arguments ) throws NamingException
+    {
+        String language = "Java";
+        
+        Object responseObject;
+        try
+        {
+            /**
+             * Create a new stored procedure execution request.
+             */
+            StoredProcedureRequest req = new StoredProcedureRequest( 0, procedureName, language
);
+            
+            /**
+             * For each argument UTF-8-encode the type name
+             * and Java-serialize the value
+             * and add them to the request as a parameter object.
+             */
+            for ( int i = 0; i < arguments.length; i++ )
+            {
+                byte[] type;
+                byte[] value;
+                type = arguments[i].getClass().getName().getBytes( "UTF-8" );
+                value = SerializationUtils.serialize( ( Serializable ) arguments[i] );
+                req.addParameter( type, value );
+            }
+            
+            /**
+             * Call the stored procedure via the extended operation
+             * and get back its return value.
+             */
+            StoredProcedureResponse resp = ( StoredProcedureResponse ) ctx.extendedOperation(
req );
+            
+            /**
+             * Restore a Java object from the return value.
+             */
+            byte[] responseStream = resp.getEncodedValue();
+            responseObject = SerializationUtils.deserialize( responseStream );
+        }
+        catch ( Exception e )
+        {
+            NamingException ne = new NamingException();
+            ne.setRootCause( e );
+            throw ne;
+        }
+        
+        return responseObject;
     }
     
 }

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/LdapContextParameter.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/LdapContextParameter.java?rev=570883&r1=570882&r2=570883&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/LdapContextParameter.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/sp/LdapContextParameter.java
Wed Aug 29 11:12:32 2007
@@ -20,6 +20,8 @@
 
 package org.apache.directory.shared.ldap.sp;
 
+import java.io.Serializable;
+
 
 
 /**
@@ -28,8 +30,10 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev:$
  */
-public class LdapContextParameter
+public class LdapContextParameter implements Serializable
 {
+    private static final long serialVersionUID = -8703671542595407603L;
+    
     private String name;
 
     public LdapContextParameter( String name )
@@ -37,12 +41,7 @@
         this.name = name;
     }
     
-    public String getType()
-    {
-        return "$ldapContext";
-    }
-    
-    public Object getValue()
+    public String getValue()
     {
         return name;
     }



Mime
View raw message