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/07/07 23:17:47 UTC

svn commit: r419992 - in /directory/sandbox/ersiner/sptriggerdemo: ./ src/ src/main/ src/main/java/ src/main/java/sptriggerdemo/ src/main/resources/

Author: ersiner
Date: Fri Jul  7 14:17:46 2006
New Revision: 419992

URL: http://svn.apache.org/viewvc?rev=419992&view=rev
Log:
Adding a demo project for Stored Procedures and Triggers.

Added:
    directory/sandbox/ersiner/sptriggerdemo/
    directory/sandbox/ersiner/sptriggerdemo/pom.xml
    directory/sandbox/ersiner/sptriggerdemo/src/
    directory/sandbox/ersiner/sptriggerdemo/src/main/
    directory/sandbox/ersiner/sptriggerdemo/src/main/java/
    directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/
    directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/DemoTool.java
    directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/Greeter.java
    directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/MailingListManager.java
    directory/sandbox/ersiner/sptriggerdemo/src/main/resources/
    directory/sandbox/ersiner/sptriggerdemo/src/main/resources/sptriggerdemo.ldif

Added: directory/sandbox/ersiner/sptriggerdemo/pom.xml
URL: http://svn.apache.org/viewvc/directory/sandbox/ersiner/sptriggerdemo/pom.xml?rev=419992&view=auto
==============================================================================
--- directory/sandbox/ersiner/sptriggerdemo/pom.xml (added)
+++ directory/sandbox/ersiner/sptriggerdemo/pom.xml Fri Jul  7 14:17:46 2006
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>sp-trigger-demo</groupId>
+  <artifactId>sp-trigger-demo</artifactId>
+  <version>0.1-SNAPSHOT</version>
+  <name>LDAP Stored Procedures and Triggers Demo</name>
+  <packaging>jar</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.directory.shared</groupId>
+      <artifactId>shared-ldap</artifactId>
+      <version>0.9.6-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.directory.shared</groupId>
+      <artifactId>shared-asn1</artifactId>
+      <version>0.9.6-SNAPSHOT</version>  
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>nlog4j</artifactId>
+      <version>1.2.24</version>
+      <scope>provided</scope>
+    </dependency>
+
+  </dependencies>
+</project>
+

Added: directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/DemoTool.java
URL: http://svn.apache.org/viewvc/directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/DemoTool.java?rev=419992&view=auto
==============================================================================
--- directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/DemoTool.java (added)
+++ directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/DemoTool.java Fri Jul  7 14:17:46 2006
@@ -0,0 +1,98 @@
+/*
+ *   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 sptriggerdemo;
+
+
+import java.util.Hashtable;
+
+import javax.naming.NamingException;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+import org.apache.directory.shared.ldap.sp.StoredProcedureUtils;
+import org.apache.directory.shared.ldap.trigger.TriggerUtils;
+
+
+/**
+ * Demo utility for Stored Procedures and Triggers.
+ * 
+ * @author <a href="mailto:ersiner@apache.org">Ersin Er</a>
+ */
+public class DemoTool
+{
+    private static String host = "localhost";
+    private static int port = 10389;
+
+
+    private static LdapContext connectToServer() throws NamingException
+    {
+        Hashtable env = new Hashtable();
+        env.put( "java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory" );
+        env.put( "java.naming.provider.url", "ldap://" + host + ":" + port + "/ou=system" );
+        env.put( "java.naming.security.principal", "uid=admin,ou=system" );
+        env.put( "java.naming.security.credentials", "secret" );
+        env.put( "java.naming.security.authentication", "simple" );
+        return new InitialLdapContext( env, null );
+    }
+
+
+    public static void main( String[] args ) throws NamingException
+    {
+        LdapContext ctx = connectToServer();
+
+        // Get the entry where we will store Stored Procedures.
+        LdapContext spCtx = ( LdapContext ) ctx.lookup( "ou=Stored Procedures" );
+
+        // Load the first Stored Procedure with the loader utility
+        // which really is there for some abstraction only (no non-standard action inside). 
+        StoredProcedureUtils.loadStoredProcedureClass( spCtx, Greeter.class.getName(), DemoTool.class );
+
+        // Now, call the Stored Procedure with two arguments and get the response.
+        Object result = StoredProcedureUtils.callStoredProcedure( ctx, Greeter.class.getName() + ".sayHello",
+            new Object[]
+                { "Ersin", new Integer( 3 ) } );
+        System.out.println( "Stored Procedure response: " + result );
+
+        // If you see the expected output on your console,
+        // then the Stored Procedure test is OK with calling, sending parameters and getting response.
+
+        // Now, load the mailing list manager Stored Procedures for working with Triggers.
+        StoredProcedureUtils.loadStoredProcedureClass( spCtx, MailingListManager.class.getName(), DemoTool.class );
+
+        // Specify the subtree refinement for the set of entries
+        // which will be handled by the mailing list manager.
+        String subtreeSpec = "{ base \"ou=People\", minimum 2, specificationFilter item:person }";
+
+        // Form the Trigger Specification to be called after each person entry add.
+        // This trigger will provide automatic subscription to Everybody mailing list.
+        String triggerSpec1 = "AFTER add CALL \"" + MailingListManager.class.getName()
+            + ".subscribeAddedPersonToEverybodyList\" ( $entry )";
+
+        // Form the Trigger Specification to be called after each person entry delete.
+        // This trigger will provide automatic unsubscription from all mailing lists.
+        String triggerSpec2 = "AFTER delete CALL \"" + MailingListManager.class.getName()
+            + ".unsubscribeDeletedPersonFromAllLists\" ( $name )";
+
+        // Load both Trigger Specifications within Trigger Subentries.
+        TriggerUtils.loadTriggerSpecification( ctx, "triggerSubentry1", subtreeSpec, triggerSpec1 );
+        TriggerUtils.loadTriggerSpecification( ctx, "triggerSubentry2", subtreeSpec, triggerSpec2 );
+
+        System.out.println( "Now, it's time to play with your LDAP client ;-)" );
+    }
+
+}

Added: directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/Greeter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/Greeter.java?rev=419992&view=auto
==============================================================================
--- directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/Greeter.java (added)
+++ directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/Greeter.java Fri Jul  7 14:17:46 2006
@@ -0,0 +1,27 @@
+package sptriggerdemo;
+
+import javax.naming.NamingException;
+
+/**
+ * LDAP Stored Procedure Unit that does not really make much sense.
+ * Just has a "Hello World" type procedure.
+ * 
+ * @author <a href="mailto:ersiner@apache.org">Ersin Er</a>
+ */
+public class Greeter
+{
+    public static String sayHello( String who, Integer times ) throws NamingException
+    {
+        StringBuffer buffer = new StringBuffer();
+        
+        for ( int i = 0; i < times.intValue(); i++ )
+        {
+            buffer.append( "Hello " );
+        }
+        
+        buffer.append( who );
+        buffer.append( '!' );
+        
+        return buffer.toString();
+    }
+}

Added: directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/MailingListManager.java
URL: http://svn.apache.org/viewvc/directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/MailingListManager.java?rev=419992&view=auto
==============================================================================
--- directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/MailingListManager.java (added)
+++ directory/sandbox/ersiner/sptriggerdemo/src/main/java/sptriggerdemo/MailingListManager.java Fri Jul  7 14:17:46 2006
@@ -0,0 +1,88 @@
+/*
+ *   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 sptriggerdemo;
+
+
+import javax.naming.Name;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import javax.naming.ldap.LdapContext;
+
+
+/**
+ * LDAP Stored Procedure Unit for handling mailing lists
+ * modeled as static groups.
+ * 
+ * @author <a href="mailto:ersiner@apache.org">Ersin Er</a>
+ */
+public class MailingListManager
+{
+    /** the name of the context holding mailing lists */
+    private static String mlCtxName = "ou=Mailing Lists,ou=system";
+
+
+    public static void subscribeAddedPersonToEverybodyList( LdapContext ctx, Name addedEntryName )
+        throws NamingException
+    {
+        System.out.println( "User \"" + addedEntryName + "\" has been added." );
+
+        String everyBodyMlCtxName = "cn=Everybody," + mlCtxName;
+
+        Attributes newMember = new BasicAttributes( "member", addedEntryName.toString(), true );
+        ctx.modifyAttributes( everyBodyMlCtxName, DirContext.ADD_ATTRIBUTE, newMember );
+
+        System.out.println( "User \"" + addedEntryName + "\" has been subscribed to Everybody mail list." );
+    }
+
+
+    public static void unsubscribeDeletedPersonFromAllLists( LdapContext ctx, Name deletedEntryName )
+        throws NamingException
+    {
+        System.out.println( "User \"" + deletedEntryName + "\" has been deleted." );
+
+        String filterExpr = "(member={0})";
+        Object[] filterArgs = new Object[]
+            { deletedEntryName.toString() };
+        SearchControls ctrl = new SearchControls();
+        ctrl.setSearchScope( SearchControls.ONELEVEL_SCOPE );
+        NamingEnumeration results = ctx.search( mlCtxName, filterExpr, filterArgs, ctrl );
+        while ( results.hasMoreElements() )
+        {
+            SearchResult result = ( SearchResult ) results.nextElement();
+            String name = result.getName();
+            Attributes member = new BasicAttributes( "member", deletedEntryName.toString(), true );
+            try
+            {
+                ctx.modifyAttributes( name, DirContext.REMOVE_ATTRIBUTE, member );
+            }
+            catch ( NamingException e )
+            {
+                e.printStackTrace();
+                throw e;
+            }
+        }
+
+        System.out.println( "User \"" + deletedEntryName
+            + "\" has been unsubscribed from all mail lists which it was subscribed to." );
+    }
+}

Added: directory/sandbox/ersiner/sptriggerdemo/src/main/resources/sptriggerdemo.ldif
URL: http://svn.apache.org/viewvc/directory/sandbox/ersiner/sptriggerdemo/src/main/resources/sptriggerdemo.ldif?rev=419992&view=auto
==============================================================================
--- directory/sandbox/ersiner/sptriggerdemo/src/main/resources/sptriggerdemo.ldif (added)
+++ directory/sandbox/ersiner/sptriggerdemo/src/main/resources/sptriggerdemo.ldif Fri Jul  7 14:17:46 2006
@@ -0,0 +1,39 @@
+dn: ou=People, ou=system
+ou: People
+objectclass: organizationalUnit
+objectclass: top
+
+dn: ou=Managers, ou=People, ou=system
+ou: Managers
+objectclass: organizationalUnit
+objectclass: top
+
+dn: ou=Engineers, ou=People, ou=system
+ou: Engineers
+objectclass: organizationalUnit
+objectclass: top
+
+dn: ou=Mailing Lists, ou=system
+ou: Mailing Lists
+objectclass: organizationalUnit
+objectclass: top
+
+dn: cn=Everybody, ou=Mailing Lists, ou=system
+objectclass: extensibleObject
+objectclass: groupOfNames
+objectclass: top
+cn: Everybody
+member: uid=admin,ou=system
+
+dn: cn=Quality Project, ou=Mailing Lists, ou=system
+objectclass: extensibleObject
+objectclass: groupOfNames
+objectclass: top
+cn: Quality Project
+member: uid=admin,ou=system
+
+dn: ou=Stored Procedures,ou=system
+ou: Stored Procedures
+objectclass: organizationalUnit
+objectclass: top
+