You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by jb...@apache.org on 2010/05/18 16:14:47 UTC

svn commit: r945668 - in /servicemix/components/bindings/servicemix-ldap/trunk: ./ src/main/java/org/apache/servicemix/ldap/ src/main/java/org/apache/servicemix/ldap/marshaler/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/t...

Author: jbonofre
Date: Tue May 18 14:14:47 2010
New Revision: 945668

URL: http://svn.apache.org/viewvc?rev=945668&view=rev
Log:
Add unit test on the default marshaler.
Add LdapConnection util class.

Added:
    servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapConnection.java   (with props)
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/
    servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshalerTest.java   (with props)
Modified:
    servicemix/components/bindings/servicemix-ldap/trunk/pom.xml
    servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapPollerEndpoint.java
    servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshaler.java
    servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/LdapMarshalerSupport.java

Modified: servicemix/components/bindings/servicemix-ldap/trunk/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-ldap/trunk/pom.xml?rev=945668&r1=945667&r2=945668&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-ldap/trunk/pom.xml (original)
+++ servicemix/components/bindings/servicemix-ldap/trunk/pom.xml Tue May 18 14:14:47 2010
@@ -1,7 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4_0_0.xsd">
          
 <!--
 

Added: servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapConnection.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapConnection.java?rev=945668&view=auto
==============================================================================
--- servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapConnection.java (added)
+++ servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapConnection.java Tue May 18 14:14:47 2010
@@ -0,0 +1,121 @@
+/*
+ * 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.servicemix.ldap;
+
+import java.net.URL;
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * <p>
+ * LDAP connection container.
+ * </p>
+ * 
+ * @author jbonofre
+ */
+public class LdapConnection {
+    
+    private final static transient Log LOG = LogFactory.getLog(LdapConnection.class);
+    
+    private Hashtable<String, String> env = new Hashtable<String, String>();
+    private DirContext context;
+    private SearchControls searchControls;
+    
+    /**
+     * <p>
+     * Create a new LDAP connection.
+     * </p>
+     * 
+     * @param url the LDAP URL.
+     * @param contextFactory the LDAP context factory.
+     * @param bindDn the LDAP bind DN (can be null).
+     * @param bindPassword the LDAP bind password.
+     */
+    public LdapConnection(URL url, String contextFactory, String bindDn, String bindPassword) {
+        LOG.debug("LDAP URL " + url);
+        env.put(Context.PROVIDER_URL, url.toString());
+        LOG.debug("Use LDAP initial context factory " + contextFactory);
+        env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
+        if (contextFactory.equals("")) {
+            LOG.debug("IBM WebSphere initial context factory detected, set required properties.");
+            env.put("com.ibm.websphere.naming.namespaceroot", "bootstraphostroot");
+            env.put("com.ibm.ws.naming.ldap.config", "local");
+            env.put("com.ibm.ws.naming.implementation", "WsnLdap");
+            env.put("com.ibm.ws.naming.ldap.masterurl", url.toString());
+        }
+        if (bindDn != null) {
+            LOG.debug("Use security authentication with bind DN " + bindDn);
+            env.put(Context.SECURITY_AUTHENTICATION, "simple");
+            env.put(Context.SECURITY_PRINCIPAL, bindDn);
+            env.put(Context.SECURITY_CREDENTIALS, bindPassword);
+        }
+        
+        LOG.debug("Create search controls with subtree scope.");
+        searchControls = new SearchControls();
+        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+    }
+    
+    /**
+     * <p>
+     * Connect to the LDAP directory.
+     * </p>
+     * 
+     * @throws Exception in case of connection failure.
+     */
+    public void connect() throws Exception {
+        if (context == null) {
+            context = new InitialDirContext(env);
+        }
+    }
+    
+    /**
+     * <p>
+     * Disconnect from the LDAP directory.
+     * </p>
+     * 
+     * @throws Exception in case of disconnect failure.
+     */
+    public void disconnect() throws Exception {
+        if (context != null) {
+            context.close();
+            context = null;
+        }
+    }
+    
+    /**
+     * <p>
+     * Search entries on the LDAP directory.
+     * </p>
+     * 
+     * @param searchBase the search base.
+     * @param filter the search filter.
+     * @return the NamingEnumeration containing entries.
+     * @throws Exception in case of search failure.
+     */
+    public NamingEnumeration search(String searchBase, String filter) throws Exception {
+        return context.search(searchBase, filter, searchControls);
+    }
+
+}

Propchange: servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapConnection.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapPollerEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapPollerEndpoint.java?rev=945668&r1=945667&r2=945668&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapPollerEndpoint.java (original)
+++ servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/LdapPollerEndpoint.java Tue May 18 14:14:47 2010
@@ -17,18 +17,15 @@
 package org.apache.servicemix.ldap;
 
 import java.net.URL;
-import java.util.Hashtable;
+import java.util.ArrayList;
 
+import javax.jbi.management.DeploymentException;
 import javax.jbi.messaging.ExchangeStatus;
 import javax.jbi.messaging.InOnly;
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
-import javax.naming.Context;
 import javax.naming.NamingEnumeration;
-import javax.naming.directory.DirContext;
-import javax.naming.directory.InitialDirContext;
-import javax.naming.directory.SearchControls;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -49,21 +46,20 @@ import org.apache.servicemix.ldap.marsha
  */
 public class LdapPollerEndpoint extends PollingEndpoint implements LdapEndpointType {
     
-    // logging facility
     private final static transient Log LOG = LogFactory.getLog(LdapPollerEndpoint.class);
     
-    // attributes
     private URL url; // the LDAP server URL looks like ldap://hostname:port
-    private String bindDn; // use the Distinguish Name bindDn to bind to the LDAP directory
-    private String bindPassword; // bind password
+    private String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory"; // the LDAP context factory to use, the default is the Sun one
+    private String bindDn = null; // use the Distinguish Name bindDn to bind to the LDAP directory
+    private String bindPassword = null; // bind password
     private String searchBase; // use searchBase as the starting point for the search instead of the default
     private String filter = "(objectclass=*)"; // the search filter
     private boolean newOnly = false; // fetch only new entries if true, fetch all entries if false
+    private ArrayList cache = new ArrayList(); // contains the cache of latest LDAP entries
     private boolean persistent = false; // keep the LDAP connection open
     private LdapMarshalerSupport marshaler = new DefaultLdapMarshaler();
     
-    private DirContext context;
-    private SearchControls controls;
+    private LdapConnection ldapConnection;
     
     public LdapPollerEndpoint() { }
     
@@ -90,55 +86,55 @@ public class LdapPollerEndpoint extends 
      */
     @Override
     public void poll() throws Exception {
-        if(!persistent) {
-            this.connect();
+        if (!persistent) {
+            ldapConnection.connect();
         }
 
         LOG.debug("Define the search filter to " + filter + " in " + searchBase);
-        NamingEnumeration namingEnumeration = context.search(searchBase, filter, controls);
+        NamingEnumeration namingEnumeration = ldapConnection.search(searchBase, filter);
         
-        LOG.debug("Create the InOnly MessageExchangePattern");
+        // TODO compare with the cache to send the new entries
+        
+        // create an InOnly exchange
+        LOG.debug("Create the InOnly exchange.");
         InOnly exchange = getExchangeFactory().createInOnlyExchange();
+        // create the "in" normalized message
+        LOG.debug("Create the in message.");
         NormalizedMessage message = exchange.createMessage();
         exchange.setInMessage(message);
         
-        marshaler.toNMR(message, namingEnumeration);
+        // marshal the LDAP naming enumeration into the in message
+        marshaler.marshal(message, namingEnumeration);
         
+        // send the exchange
+        LOG.debug("Send the exchange.");
         send(exchange);
         
-        if(!persistent) {
-            this.disconnect();
+        if (!persistent) {
+            ldapConnection.disconnect();
         }
     }
     
-    /**
-     * Connect to the LDAP directory server context and define the search controls
-     * @throws Exception in case of exception during the LDAP connect
+    /*
+     * (non-Javadoc)
+     * @see org.apache.servicemix.common.endpoints.ConsumerEndpoint#validate()
      */
-    private void connect() throws Exception {
-        LOG.debug("Connect to the LDAP directory server " + url.toString());
-        Hashtable env = new Hashtable();
-        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
-        env.put(Context.PROVIDER_URL, url.toString());
-        LOG.debug("Init the JNDI LDAP directory context");
-        // TODO bind using the bind DN
-        context = new InitialDirContext(env);
+    @Override
+    public void validate() throws DeploymentException {
+        super.validate();
         
-        LOG.debug("Define the subtree scope search control");
-        controls = new SearchControls();
-        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
-    }
-    
-    /**
-     * Disconnect from the LDAP directory server
-     * @throws Exception in case of exception during the LDAP disconnect
-     */
-    private void disconnect() throws Exception {
-        LOG.debug("Close the LDAP directory server connection");
-        if (context != null) {
-            context.close();
-            context = null;
+        // TODO add an abstract WSDL
+        
+        // validate properties
+        if (url == null) {
+            throw new DeploymentException("LDAP URL is mandatory.");
+        }
+        if (contextFactory == null) {
+            throw new DeploymentException("LDAP Context Factory is mandatory.");
         }
+        
+        // create the LDAP connection
+        ldapConnection = new LdapConnection(url, contextFactory, bindDn, bindPassword);
     }
     
     /*
@@ -150,7 +146,7 @@ public class LdapPollerEndpoint extends 
         super.start();
         if (persistent) {
             LOG.debug("The LDAP connection is persistent, connect to the LDAP server now");
-            this.connect();
+            ldapConnection.connect();
         }
     }
     
@@ -162,7 +158,7 @@ public class LdapPollerEndpoint extends 
     public void stop() throws Exception {
         if (persistent) {
             LOG.debug("The LDAP connection is persistent, disconnect to the LDAP server now");
-            this.disconnect();
+            ldapConnection.disconnect();
         }
         super.stop();
     }

Modified: servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshaler.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshaler.java?rev=945668&r1=945667&r2=945668&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshaler.java (original)
+++ servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshaler.java Tue May 18 14:14:47 2010
@@ -68,13 +68,16 @@ public class DefaultLdapMarshaler implem
 	 * (non-Javadoc)
 	 * @see org.apache.servicemix.ldap.marshaler.LdapMarshalerSupport#readNamingEnumeration(javax.jbi.messaging.NormalizedMessage, javax.naming.NamingEnumeration)  
 	 */
-	public void toNMR(NormalizedMessage message, NamingEnumeration namingEnumeration) throws MessagingException {
-		if(message == null) {
-		    throw new MessagingException("The NormalizedMessage is null");
+	public void marshal(NormalizedMessage message, NamingEnumeration namingEnumeration) throws MessagingException {
+		if (message == null) {
+		    throw new MessagingException("The NormalizedMessage is null.");
 		}
-		if(namingEnumeration == null) {
-		    throw new MessagingException("The NamingEnumeration is null");
+		if (namingEnumeration == null) {
+		    throw new MessagingException("The NamingEnumeration is null.");
 		}
+		
+		// TODO use JAXB to marshal
+		
 		StringBuffer data = new StringBuffer();
 		
 		data.append(TAG_ENTRIES_OPEN);

Modified: servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/LdapMarshalerSupport.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/LdapMarshalerSupport.java?rev=945668&r1=945667&r2=945668&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/LdapMarshalerSupport.java (original)
+++ servicemix/components/bindings/servicemix-ldap/trunk/src/main/java/org/apache/servicemix/ldap/marshaler/LdapMarshalerSupport.java Tue May 18 14:14:47 2010
@@ -35,6 +35,6 @@ public interface LdapMarshalerSupport {
 	 * @param namingEnumeration the <code>NamingEnumeration</code> LDAP JNDI search result
 	 * @throws MessagingException in case of error during normalized message manipulation
 	 */
-    void toNMR(NormalizedMessage message, NamingEnumeration namingEnumeration) throws MessagingException;
+    void marshal(NormalizedMessage message, NamingEnumeration namingEnumeration) throws MessagingException;
 
 }

Added: servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshalerTest.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshalerTest.java?rev=945668&view=auto
==============================================================================
--- servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshalerTest.java (added)
+++ servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshalerTest.java Tue May 18 14:14:47 2010
@@ -0,0 +1,126 @@
+/*
+ * 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.servicemix.ldap.marshaler;
+
+import java.util.ArrayList;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.MessageExchangeFactory;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.naming.NamingEnumeration;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.SearchResult;
+
+import org.apache.servicemix.id.IdGenerator;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.messaging.MessageExchangeFactoryImpl;
+
+import junit.framework.TestCase;
+
+/**
+ * <p>
+ * Unit tests on the default LDAP marshaler.
+ * </p>
+ * 
+ * @author jbonofre
+ */
+public class DefaultLdapMarshalerTest extends TestCase {
+    
+    private LdapMarshalerSupport marshaler;
+    private MessageExchangeFactory messageExchangeFactory;
+    
+    /*
+     * (non-Javadoc)
+     * @see junit.framework.TestCase#setUp()
+     */
+    public void setUp() throws Exception {
+        this.marshaler = new DefaultLdapMarshaler();
+        this.messageExchangeFactory = new MessageExchangeFactoryImpl(new IdGenerator(), new AtomicBoolean(false));
+    }
+    
+    /**
+     * <p>
+     * Test the marshaling of a naming enumeration.
+     * </p>
+     * 
+     * @throws Exception in case of marshaling failure.
+     */
+    public void testMarshaling() throws Exception {
+        // create a testing naming enumeration
+        TestNamingEnumeration namingEnumeration = new TestNamingEnumeration();
+        
+        // create a InOnly exchange
+        InOnly exchange = messageExchangeFactory.createInOnlyExchange();
+        // create the in message
+        NormalizedMessage in = exchange.createMessage();
+        
+        // marshal the naming enumeration into the message
+        marshaler.marshal(in, namingEnumeration);
+        
+        // check the message content
+        assertEquals("<entries><entry><dn>test</dn><attributes><attribute><name>second</name><values></values></attribute><attribute><name>first</name><values></values></attribute></attributes></entry></entries>",
+                new SourceTransformer().toString(in.getContent()));
+    }
+
+}
+
+class TestNamingEnumeration implements NamingEnumeration {
+    
+    private SearchResult searchResult;
+    private boolean iterate = true;
+    
+    public TestNamingEnumeration() {
+        Attributes attributes = new BasicAttributes();
+        Attribute first = new BasicAttribute("first");
+        attributes.put(first);
+        Attribute second = new BasicAttribute("second");
+        attributes.put(second);
+        this.searchResult = new SearchResult("test", null, attributes);
+    }
+    
+    public void close() { }
+    
+    public boolean hasMore() { 
+        return iterate;
+    }
+    
+    public SearchResult next() {
+        if (iterate) {
+            iterate = false;
+            return searchResult;
+        }
+        return null;
+    }
+    
+    public boolean hasMoreElements() { 
+        return iterate; 
+    }
+    
+    public SearchResult nextElement() { 
+        if (iterate) {
+            iterate = false;
+            return searchResult;
+        }
+        return null;
+    }
+    
+    
+}

Propchange: servicemix/components/bindings/servicemix-ldap/trunk/src/test/java/org/apache/servicemix/ldap/marshaler/DefaultLdapMarshalerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain