You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2006/08/25 22:36:40 UTC

svn commit: r436934 - in /directory/trunks: apacheds/core-unit/src/test/java/org/apache/directory/server/core/trigger/ apacheds/server-unit/src/test/java/org/apache/directory/server/ shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/

Author: ersiner
Date: Fri Aug 25 13:36:39 2006
New Revision: 436934

URL: http://svn.apache.org/viewvc?rev=436934&view=rev
Log:
Made some cosmetic changes and added some javadoc to Java stored procedure utils.

Added:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcedureUtils.java
Removed:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/StoredProcedureUtils.java
Modified:
    directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/trigger/DefaulTriggerServiceTest.java
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java
    directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java

Modified: directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/trigger/DefaulTriggerServiceTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/trigger/DefaulTriggerServiceTest.java?rev=436934&r1=436933&r2=436934&view=diff
==============================================================================
--- directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/trigger/DefaulTriggerServiceTest.java (original)
+++ directory/trunks/apacheds/core-unit/src/test/java/org/apache/directory/server/core/trigger/DefaulTriggerServiceTest.java Fri Aug 25 13:36:39 2006
@@ -24,7 +24,7 @@
 import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.BasicAttributes;
 
-import org.apache.directory.shared.ldap.sp.StoredProcedureUtils;
+import org.apache.directory.shared.ldap.sp.JavaStoredProcedureUtils;
 
 
 /**
@@ -39,7 +39,7 @@
     public void testAfterDeleteBackupDeletedEntry() throws NamingException
     {
         // Load the stored procedure unit which has the stored procedure to be triggered.
-        StoredProcedureUtils.loadStoredProcedureClass( sysRoot, BackupUtilities.class.getName(), getClass() );
+        JavaStoredProcedureUtils.loadStoredProcedureClass( sysRoot, BackupUtilities.class.getName(), getClass() );
         
         // Create a container for backing up deleted entries.
         Attributes backupContext = new BasicAttributes( "ou", "backupContext", true );
@@ -75,7 +75,7 @@
     public void testBeforeDeleteLogWarning() throws NamingException
     {
         // Load the stored procedure unit which has the stored procedure to be triggered.
-        StoredProcedureUtils.loadStoredProcedureClass( sysRoot, LoggingUtilities.class.getName(), getClass() );
+        JavaStoredProcedureUtils.loadStoredProcedureClass( sysRoot, LoggingUtilities.class.getName(), getClass() );
         
         // Create the Triger Specification within a Trigger Subentry.
         createTriggerSubentry( "triggerSubentry1",

Modified: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java?rev=436934&r1=436933&r2=436934&view=diff
==============================================================================
--- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java (original)
+++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/DefaultServerTriggerServiceTest.java Fri Aug 25 13:36:39 2006
@@ -31,7 +31,7 @@
 import javax.naming.ldap.LdapContext;
 
 import org.apache.directory.server.ldap.support.extended.StoredProcedureExtendedOperationHandler;
-import org.apache.directory.shared.ldap.sp.StoredProcedureUtils;
+import org.apache.directory.shared.ldap.sp.JavaStoredProcedureUtils;
 
 
 /**
@@ -72,7 +72,7 @@
     public void testAfterDeleteBackupDeletedEntry() throws NamingException
     {
         // Load the stored procedure unit which has the stored procedure to be triggered.
-        StoredProcedureUtils.loadStoredProcedureClass( ctx, BackupUtilities.class.getName(), this.getClass() );
+        JavaStoredProcedureUtils.loadStoredProcedureClass( ctx, BackupUtilities.class.getName(), this.getClass() );
         
         // Create a container for backing up deleted entries.
         Attributes backupContext = new BasicAttributes( "ou", "backupContext", true );

Modified: directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java?rev=436934&r1=436933&r2=436934&view=diff
==============================================================================
--- directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java (original)
+++ directory/trunks/apacheds/server-unit/src/test/java/org/apache/directory/server/StoredProcedureTest.java Fri Aug 25 13:36:39 2006
@@ -28,7 +28,7 @@
 
 import org.apache.directory.server.ldap.support.extended.StoredProcedureExtendedOperationHandler;
 import org.apache.directory.server.unit.AbstractServerTest;
-import org.apache.directory.shared.ldap.sp.StoredProcedureUtils;
+import org.apache.directory.shared.ldap.sp.JavaStoredProcedureUtils;
 
 
 /**
@@ -56,7 +56,7 @@
         env.put( "java.naming.security.authentication", "simple" );
         ctx = new InitialLdapContext( env, null );
 
-        StoredProcedureUtils.loadStoredProcedureClass( ctx, HelloWorldProcedure.class.getName(), getClass() );
+        JavaStoredProcedureUtils.loadStoredProcedureClass( ctx, HelloWorldProcedure.class.getName(), getClass() );
     }
 
 
@@ -73,7 +73,7 @@
     {
         String procedureName = HelloWorldProcedure.class.getName() + ".sayHello";
         
-        Object response = StoredProcedureUtils.callStoredProcedure( ctx, procedureName, new Object[] { } );
+        Object response = JavaStoredProcedureUtils.callStoredProcedure( ctx, procedureName, new Object[] { } );
         
         assertEquals( "Hello World!", response );
     }
@@ -83,7 +83,7 @@
     {
         String procedureName = HelloWorldProcedure.class.getName() + ".sayHelloTo";
         
-        Object response = StoredProcedureUtils.callStoredProcedure( ctx, procedureName, new Object[] { "Ersin" } );
+        Object response = JavaStoredProcedureUtils.callStoredProcedure( ctx, procedureName, new Object[] { "Ersin" } );
         
         assertEquals( "Hello Ersin!", response );
     }

Added: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcedureUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcedureUtils.java?rev=436934&view=auto
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcedureUtils.java (added)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/sp/JavaStoredProcedureUtils.java Fri Aug 25 13:36:39 2006
@@ -0,0 +1,164 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.shared.ldap.sp;
+
+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.directory.BasicAttributes;
+import javax.naming.ldap.LdapContext;
+
+import org.apache.commons.lang.SerializationUtils;
+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.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev:$
+ */
+public class JavaStoredProcedureUtils
+{
+    
+    /**
+     * Returns the stream data of a Java class file
+     * whose fully qualified name is provided.
+     * 
+     * @param fullClassName
+     *           Fully qualified name of the class
+     *           with package name included and ".class" extension excluded.
+     * @param resourceLoader
+     *           The resource loader for the Stored Procedure Class. 
+     * @return
+     *           Stream data of the class file as a byte array.
+     * @throws NamingException
+     *           If an IO error occurs during reading the class file.
+     */
+    public static byte[] getClassFileAsStream( String fullClassName, Class resourceLoader ) throws NamingException
+    {
+        int lastDot = fullClassName.lastIndexOf( '.' );
+        String classFileName = fullClassName.substring( lastDot + 1 ) + ".class";
+        
+        URL url = resourceLoader.getResource( classFileName );
+        InputStream in = resourceLoader.getResourceAsStream( classFileName );
+        File file = new File( url.getFile() );
+        int size = ( int ) file.length();
+        byte[] buf = new byte[size];
+        
+        try
+        {
+            in.read( buf );
+            in.close();
+        }
+        catch ( IOException e )
+        {
+            NamingException ne = new NamingException();
+            ne.setRootCause( e );
+            throw ne;
+        }
+        
+        return buf;
+    }
+    
+    /**
+     * Loads a Java class's stream data as a subcontext of an LdapContext given.
+     * 
+     * @param ctx
+     *           The parent context of the Java class entry to be loaded.
+     * @param fullClassName
+     *           Fully qualified name of the class
+     *           with package name included and ".class" extension excluded.
+     * @param resourceLoader
+     *           The resource loader for the Stored Procedure Class. 
+     * @throws NamingException
+     *           If an error occurs during creating the subcontext.
+     */
+    public static void loadStoredProcedureClass( LdapContext ctx, String fullClassName, Class resourceLoader ) throws NamingException
+    {
+        byte[] buf = getClassFileAsStream( fullClassName, resourceLoader );
+        
+        Attributes attributes = new BasicAttributes( "objectClass", "top", true );
+        attributes.get( "objectClass" ).add( "javaClass" );
+        attributes.put( "fullyQualifiedClassName", fullClassName );
+        attributes.put( "byteCode", buf );
+        
+        ctx.createSubcontext( "fullyQualifiedClassName=" + fullClassName , attributes );
+    }
+    
+    /**
+     * Issues a Stored Procedure Call Extended Operation Request over an LdapContext.
+     * 
+     * @param ctx
+     * @param procedureName
+     * @param arguments
+     * @return
+     * @throws NamingException
+     */
+    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 = arguments[i].getClass().getName().getBytes( "UTF-8" );
+                byte[] value = SerializationUtils.serialize( ( Serializable ) arguments[i] );
+                req.addParameter( type, value );
+            }
+            
+            /**
+             * Call the stored prcedure 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;
+    }
+}